From a0934dc015ecd2bc3696cabe3013fc883431aab6 Mon Sep 17 00:00:00 2001 From: Jeremy Meng Date: Wed, 15 Jan 2025 16:15:07 -0800 Subject: [PATCH] [eslint-plugin] update regex of matching TypeScript file extentions Some of our custom rules perform operations on TypeScript files only and they use a regex of `/\.ts$/.test(fileName)` to check whether a file is TypeScript code file or not. With the migration to ESM we now can have .mts and .cts files as well. This PR updates the pattern to also match those files. --- .../src/rules/github-source-headers.ts | 2 +- .../src/rules/ts-doc-internal.ts | 2 +- .../tests/fixture/file-browser.mts | 0 .../tests/fixture/src/test-browser.mts | 0 .../tests/fixture/tsconfig.json | 2 ++ .../tests/rules/github-source-headers.ts | 16 +++++++++++++ .../tests/rules/ts-doc-internal.ts | 24 +++++++++++++++++++ 7 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 common/tools/eslint-plugin-azure-sdk/tests/fixture/file-browser.mts create mode 100644 common/tools/eslint-plugin-azure-sdk/tests/fixture/src/test-browser.mts diff --git a/common/tools/eslint-plugin-azure-sdk/src/rules/github-source-headers.ts b/common/tools/eslint-plugin-azure-sdk/src/rules/github-source-headers.ts index 67bf7d5e4ec9..5ba5e5790e27 100644 --- a/common/tools/eslint-plugin-azure-sdk/src/rules/github-source-headers.ts +++ b/common/tools/eslint-plugin-azure-sdk/src/rules/github-source-headers.ts @@ -33,7 +33,7 @@ export default createRule({ }, defaultOptions: [], create(context) { - if (!/\.ts$/.test(context.filename)) { + if (!/\.ts|\.mts|\.cts$/.test(context.filename)) { return {}; } return { diff --git a/common/tools/eslint-plugin-azure-sdk/src/rules/ts-doc-internal.ts b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-doc-internal.ts index d2546ab439d2..61c3acfa961d 100644 --- a/common/tools/eslint-plugin-azure-sdk/src/rules/ts-doc-internal.ts +++ b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-doc-internal.ts @@ -113,7 +113,7 @@ export default createRule({ const fileName = context.filename; // on the first run, if on a .ts file (program.getSourceFile is file-type dependent) - if (context.settings.exported === undefined && /\.ts$/.test(fileName)) { + if (context.settings.exported === undefined && /\.ts|\.mts|\.cts$/.test(fileName)) { const packageExports = getLocalExports(context); if (packageExports !== undefined) { context.settings.exported = packageExports; diff --git a/common/tools/eslint-plugin-azure-sdk/tests/fixture/file-browser.mts b/common/tools/eslint-plugin-azure-sdk/tests/fixture/file-browser.mts new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/common/tools/eslint-plugin-azure-sdk/tests/fixture/src/test-browser.mts b/common/tools/eslint-plugin-azure-sdk/tests/fixture/src/test-browser.mts new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/common/tools/eslint-plugin-azure-sdk/tests/fixture/tsconfig.json b/common/tools/eslint-plugin-azure-sdk/tests/fixture/tsconfig.json index 2b2ee13a5ff3..ff26c2a5c994 100644 --- a/common/tools/eslint-plugin-azure-sdk/tests/fixture/tsconfig.json +++ b/common/tools/eslint-plugin-azure-sdk/tests/fixture/tsconfig.json @@ -4,9 +4,11 @@ }, "include": [ "file.ts", + "file-browser.mts", "package.json", "not_package.json", "src/test.ts", + "src/test-browser.mts", "tests/test.ts", "service-bus/package.json", "invalid/package.json", diff --git a/common/tools/eslint-plugin-azure-sdk/tests/rules/github-source-headers.ts b/common/tools/eslint-plugin-azure-sdk/tests/rules/github-source-headers.ts index 97b3c11ab4e8..ee11c760d6b1 100644 --- a/common/tools/eslint-plugin-azure-sdk/tests/rules/github-source-headers.ts +++ b/common/tools/eslint-plugin-azure-sdk/tests/rules/github-source-headers.ts @@ -39,6 +39,11 @@ ruleTester.run("github-source-headers", rule, { code: valid, filename: "file.ts", }, + { + // only the fields we care about + code: valid, + filename: "file-browser.mts", + }, { // incorrect format but in a file we don't care about code: 'console.log("hello")', @@ -57,6 +62,17 @@ ruleTester.run("github-source-headers", rule, { ], output: valid, }, + { + // no comments .mts + code: 'console.log("hello")', + filename: "file-browser.mts", + errors: [ + { + message: configError, + }, + ], + output: valid, + }, // wrong headers { code: invalid1, diff --git a/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-doc-internal.ts b/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-doc-internal.ts index c5995b85af29..8dc316a101ce 100644 --- a/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-doc-internal.ts +++ b/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-doc-internal.ts @@ -74,6 +74,15 @@ ruleTester.run("ts-doc-internal", rule, { function ExampleFunction() {}`, filename: "src/test.ts", }, + { + code: ` + /** + * Other documentation + * @hidden + */ + function ExampleFunction() {}`, + filename: "src/test-browser.mts", + }, ], invalid: [ // class @@ -119,5 +128,20 @@ ruleTester.run("ts-doc-internal", rule, { }, ], }, + // .mts file + { + code: ` + /** + * Other documentation + * @ignore + */ + function ExampleFunction() {}`, + filename: "src/test-browser.mts", + errors: [ + { + message: "internal items with TSDoc comments should include an @internal or @hidden tag", + }, + ], + }, ], });