Skip to content

Commit

Permalink
[EngSys] prepare for upgrading TypeScript to v4.6
Browse files Browse the repository at this point in the history
Before upgrading we want to address the breaking change of catch variable now
defaulting to `unkown` by explicitly specify `: any` for implicit `any` catch
variables in our code base.

This commit applies the result of running the following codemod (credit: Maor)

```ts
import { API, FileInfo } from "jscodeshift";
export default function transformer(file: FileInfo, api: API) {
  const j = api.jscodeshift;
  const code = j(file.source);
  code
    .find(j.CatchClause)
    .filter(({ node }) => {
      return node.param && node.param.type == "Identifier" && !node.param.typeAnnotation;
    })
    .forEach(({ node }) => {
      if (node.param.type == "Identifier") {
        node.param.typeAnnotation = j.tsTypeAnnotation(j.tsAnyKeyword());
      }
    });
  return code.toSource();
}
```
  • Loading branch information
jeremymeng authored and weshaggard committed Apr 19, 2022
1 parent af13826 commit 191e4ce
Show file tree
Hide file tree
Showing 540 changed files with 2,120 additions and 2,122 deletions.
2 changes: 1 addition & 1 deletion common/smoke-test/test-cases/KeyVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class KeyVaultSecrets {
try {
await KeyVaultSecrets.setSecret();
await KeyVaultSecrets.getSecret();
} catch (err) {
} catch (err: any) {
throw err;
} finally {
await KeyVaultSecrets.deleteSecret();
Expand Down
2 changes: 1 addition & 1 deletion common/tools/dev-tool/src/commands/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default leafCommand(commandInfo, async (options) => {
console.log(chalk.blueBright(` Name/Version:\t${packageInfo.name}@${packageInfo.version}`));
console.log(chalk.blueBright(` Location:\t${packageInfo.path}`));
console.log();
} catch (error) {
} catch (error: any) {
log.error("Could not locate dev-tool package.");
log.error("Unable to display dev-tool version information.");
}
Expand Down
2 changes: 1 addition & 1 deletion common/tools/dev-tool/src/commands/package/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default leafCommand(commandInfo, async (options) => {
log.info(`Version specifier: ${currentPackage.name}@${currentPackage.version}`);
log.info(`Location: ${path.resolve(dir, currentPackage.path)}`);
}
} catch (error) {
} catch (error: any) {
log.error("Could not find package starting from", dir);
log.error(error);
}
Expand Down
4 changes: 2 additions & 2 deletions common/tools/dev-tool/src/commands/run/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default leafCommand(commandInfo, async (options) => {
sourcemap: true,
exports: "named",
});
} catch (error) {
} catch (error: any) {
log.error(error);
return false;
}
Expand Down Expand Up @@ -135,7 +135,7 @@ export default leafCommand(commandInfo, async (options) => {
format: "umd",
sourcemap: true,
});
} catch (error) {
} catch (error: any) {
log.error(error);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function createDockerContextDirectory(
try {
new URL(s);
return true;
} catch (err) {
} catch (err: any) {
return false;
}
};
Expand Down
2 changes: 1 addition & 1 deletion common/tools/dev-tool/src/commands/samples/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default leafCommand(commandInfo, async (options) => {

// This is where the actual magic of creating the output from the template happens
await factory(basePath);
} catch (ex) {
} catch (ex: any) {
log.error((ex as Error).message);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion common/tools/dev-tool/src/commands/samples/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function runSingle(name: string, accumulatedErrors: Array<[string, string]
const { main: sampleMain } = await import(name);
await sampleMain();
}
} catch (err) {
} catch (err: any) {
const truncatedError: string = (err as Error).toString().split("\n")[0].slice(0, 100);
accumulatedErrors.push([path.basename(name), truncatedError]);
log.warn(`Error in ${name}:`);
Expand Down
24 changes: 11 additions & 13 deletions common/tools/dev-tool/src/util/samples/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export async function makeSampleGenerationInfo(

try {
contents = fs.readFileSync(path.resolve(projectInfo.path, file));
} catch (ex) {
} catch (ex: any) {
fail(`Failed to read custom snippet file '${file}'`, ex);
}
return {
Expand Down Expand Up @@ -321,18 +321,16 @@ export async function makeSamplesFactory(
*/
function postProcess(moduleText: string | Buffer): string {
const content = Buffer.isBuffer(moduleText) ? moduleText.toString("utf8") : moduleText;
return (
content
.replace(new RegExp(`^\\s*\\*\\s*@${AZSDK_META_TAG_PREFIX}.*\n`, "gm"), "")
// We also need to clean up extra blank lines that might be left behind by
// removing azsdk tags. These regular expressions are extremely frustrating
// because they deal almost exclusively in the literal "/" and "*" characters.
.replace(/(\s+\*)+\//s, "\n */")
// Clean up blank lines at the beginning
.replace(/\/\*\*(\s+\*)*/s, `/**\n *`)
// Finally remove empty doc comments.
.replace(/\s*\/\*\*(\s+\*)*\/\s*/s, "\n\n")
);
return content
.replace(new RegExp(`^\\s*\\*\\s*@${AZSDK_META_TAG_PREFIX}.*\n`, "gm"), "")
// We also need to clean up extra blank lines that might be left behind by
// removing azsdk tags. These regular expressions are extremely frustrating
// because they deal almost exclusively in the literal "/" and "*" characters.
.replace(/(\s+\*)+\//s, "\n */")
// Clean up blank lines at the beginning
.replace(/\/\*\*(\s+\*)*/s, `/**\n *`)
// Finally remove empty doc comments.
.replace(/\s*\/\*\*(\s+\*)*\/\s*/s, "\n\n");
}

// We use a tempdir at the outer layer to avoid creating dirty trees
Expand Down
4 changes: 2 additions & 2 deletions common/tools/dev-tool/src/util/testProxyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function isProxyToolActive(): Promise<boolean> {
await makeRequest("http://localhost:5000/info/available", {});
log.info(`Proxy tool seems to be active at http://localhost:5000\n`);
return true;
} catch (error) {
} catch (error: any) {
return false;
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ async function getImageTag() {

log.info(`Image tag obtained from the powershell script => ${tag}\n`);
return tag;
} catch (_) {
} catch (_: any) {
log.warn(
`Unable to get the image tag from the powershell script, trying "latest" tag instead\n`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ try {
);
});
}
} catch (err) {
} catch (err: any) {
exclude = [];
}

Expand Down
6 changes: 3 additions & 3 deletions gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function pack(): void {
const distTags: StringMap<string> | undefined = npmViewResult["dist-tags"];
npmPackageVersion = distTags && distTags["latest"];
}
catch (error) {
catch (error: any) {
// This happens if the package doesn't exist in NPM.
}

Expand All @@ -263,7 +263,7 @@ function pack(): void {
_logger.log(`Filename: ${packFileName}`);
packedPackages++;
}
catch (error) {
catch (error: any) {
errorPackages++;
}
} else {
Expand Down Expand Up @@ -309,7 +309,7 @@ gulp.task("find-missing-sdks", async () => {
_logger.log(`Found azure-rest-api-specs repository in ${azureRestApiSpecsRepositoryPath}`);

await findMissingSdks(azureRestApiSpecsRepositoryPath);
} catch (error) {
} catch (error: any) {
_logger.logError(error);
}
});
Expand Down
2 changes: 1 addition & 1 deletion samples/frameworks/electron/ts/src/blobHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class BlobHandler {
try {
const buffer = await blobClient.downloadToBuffer();
return buffer.toString();
} catch (error) {
} catch (error: any) {
// It's possible the blob doesn't exist, which is fine.
// but if it's a different error we should let it bubble up.
// Please refer to https://github.com/Azure/azure-sdk-for-js/blob/f558cff1ab6f862a74b668abef89c36e53b980f0/sdk/storage/storage-blob/samples/typescript/src/errorsAndResponses.ts#L78
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function main() {
// the 'catch' below will now incorporate the update
onlyIfUnchanged: true,
});
} catch (err) {
} catch (err: any) {
if (err.statusCode === 412) {
// precondition failed
console.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function main() {
// Read the secret we created
const secret = await secretClient.getSecret(secretName);
console.log(`Get the secret from keyvault key: ${secretName}, value: ${secret.value}`);
} catch (err) {
} catch (err: any) {
const error = err as { code: string; statusCode: number };
if (error.code === "SecretNotFound" && error.statusCode === 404) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function main() {
label: "a label",
value: "new value",
});
} catch (err) {
} catch (err: any) {
console.log(`Error gets thrown - can't modify a read-only setting`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function main() {
// the 'catch' below will now incorporate the update
onlyIfUnchanged: true
});
} catch (err) {
} catch (err: any) {
if (err.statusCode === 412) {
// precondition failed
console.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function main() {
// Read the secret we created
const secret = await secretClient.getSecret(secretName);
console.log(`Get the secret from keyvault key: ${secretName}, value: ${secret.value}`);
} catch (err) {
} catch (err: any) {
const error = err as { code: string; statusCode: number };
if (error.code === "SecretNotFound" && error.statusCode === 404) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function main() {
label: "a label",
value: "new value"
});
} catch (err) {
} catch (err: any) {
console.log(`Error gets thrown - can't modify a read-only setting`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export function serializeAsConfigurationSettingParam(
if (isConfigSettingWithSecretReferenceValue(setting)) {
return SecretReferenceHelper.toConfigurationSettingParam(setting);
}
} catch (error) {
} catch (error: any) {
return setting as ConfigurationSettingParam;
}
throw new TypeError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("packagejson related tests", () => {
packageJsonContents = JSON.parse(
fs.readFileSync(path.join(__dirname, "../../../../package.json"), { encoding: "utf-8" })
);
} catch (e) {
} catch (e: any) {
// For unit tests
packageJsonContents = JSON.parse(
fs.readFileSync(path.join(__dirname, "../../../package.json"), { encoding: "utf-8" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("Should not retry forever", () => {
);
}
await Promise.all(promises);
} catch (error) {
} catch (error: any) {
errorWasThrown = true;
chai.assert.equal((error as any).name, "AbortError", "Unexpected error thrown");
}
Expand All @@ -84,7 +84,7 @@ describe("Should not retry forever", () => {
key: key,
value: "added",
});
} catch (error) {
} catch (error: any) {
errorWasThrown = true;
const err = error as RestError;
chai.assert.equal(err.name, "RestError", "Unexpected error thrown");
Expand Down
Loading

0 comments on commit 191e4ce

Please sign in to comment.