From 823371c91484e37fb2dae37fa32ecfe48aa2b118 Mon Sep 17 00:00:00 2001 From: Jeremy Meng Date: Wed, 7 Jun 2023 16:42:53 -0700 Subject: [PATCH] [eslint-plugin] update rule to allow .cjs main entry (#26120) currently only index.js is allowed. --- .../docs/rules/ts-package-json-main-is-cjs.md | 10 ++++++- .../src/rules/ts-package-json-main-is-cjs.ts | 4 +-- .../rules/ts-package-json-main-is-cjs.ts | 30 ++++++++++++++----- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/common/tools/eslint-plugin-azure-sdk/docs/rules/ts-package-json-main-is-cjs.md b/common/tools/eslint-plugin-azure-sdk/docs/rules/ts-package-json-main-is-cjs.md index 8e88d5856240..2d3bdcc47e96 100644 --- a/common/tools/eslint-plugin-azure-sdk/docs/rules/ts-package-json-main-is-cjs.md +++ b/common/tools/eslint-plugin-azure-sdk/docs/rules/ts-package-json-main-is-cjs.md @@ -1,6 +1,6 @@ # ts-package-json-main-is-cjs -Requires `main` in `package.json` to be point to a CommonJS or UMD module. In this case, its assumed that it points to `"dist/index.js"`. +Requires `main` in `package.json` to be point to a CommonJS or UMD module. In this case, its assumed that it points to `"dist/index.js"` or `"dist/index.cjs"`. This rule is fixable using the `--fix` option. @@ -14,6 +14,14 @@ This rule is fixable using the `--fix` option. } ``` +### Good + +```json +{ + "main": "dist/index.cjs" +} +``` + ### Bad ```json diff --git a/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-main-is-cjs.ts b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-main-is-cjs.ts index ece3560b9b95..13c697b39d83 100644 --- a/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-main-is-cjs.ts +++ b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-main-is-cjs.ts @@ -45,10 +45,10 @@ export = { const nodeValue = node.value as Literal; const main = nodeValue.value as string; - if (!/^(\.\/)?dist\/index\.js$/.test(main)) { + if (!/^(\.\/)?dist\/index\.(c)?js$/.test(main)) { context.report({ node: nodeValue, - message: `main is set to ${main} when it should be set to dist/index.js`, + message: `main is set to ${main} when it should be set to dist/index.js or dist/index.cjs`, fix: (fixer: Rule.RuleFixer): Rule.Fix => fixer.replaceText(nodeValue, `"dist/index.js"`), }); diff --git a/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-package-json-main-is-cjs.ts b/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-package-json-main-is-cjs.ts index 659d1b80c454..56ee9ca6fced 100644 --- a/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-package-json-main-is-cjs.ts +++ b/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-package-json-main-is-cjs.ts @@ -259,6 +259,16 @@ ruleTester.run("ts-package-json-main-is-cjs", rule, { code: '{"main": "./dist/index.js"}', filename: "package.json", }, + { + // correct format #3 + code: '{"main": "dist/index.cjs"}', + filename: "package.json", + }, + { + // correct format #4 + code: '{"main": "./dist/index.cjs"}', + filename: "package.json", + }, { // a full example package.json (taken from https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/eventhub/event-hubs/package.json with "scripts" removed for testing purposes) code: examplePackageGood, @@ -296,7 +306,8 @@ ruleTester.run("ts-package-json-main-is-cjs", rule, { filename: "package.json", errors: [ { - message: "main is set to dist//index.js when it should be set to dist/index.js", + message: + "main is set to dist//index.js when it should be set to dist/index.js or dist/index.cjs", }, ], output: '{"main": "dist/index.js"}', @@ -306,7 +317,8 @@ ruleTester.run("ts-package-json-main-is-cjs", rule, { filename: "package.json", errors: [ { - message: "main is set to .dist/index.js when it should be set to dist/index.js", + message: + "main is set to .dist/index.js when it should be set to dist/index.js or dist/index.cjs", }, ], output: '{"main": "dist/index.js"}', @@ -316,7 +328,8 @@ ruleTester.run("ts-package-json-main-is-cjs", rule, { filename: "package.json", errors: [ { - message: "main is set to /dist/index.js when it should be set to dist/index.js", + message: + "main is set to /dist/index.js when it should be set to dist/index.js or dist/index.cjs", }, ], output: '{"main": "dist/index.js"}', @@ -327,7 +340,7 @@ ruleTester.run("ts-package-json-main-is-cjs", rule, { filename: "package.json", errors: [ { - message: "main is set to dist when it should be set to dist/index.js", + message: "main is set to dist when it should be set to dist/index.js or dist/index.cjs", }, ], output: '{"main": "dist/index.js"}', @@ -337,7 +350,8 @@ ruleTester.run("ts-package-json-main-is-cjs", rule, { filename: "package.json", errors: [ { - message: "main is set to index.js when it should be set to dist/index.js", + message: + "main is set to index.js when it should be set to dist/index.js or dist/index.cjs", }, ], output: '{"main": "dist/index.js"}', @@ -347,7 +361,8 @@ ruleTester.run("ts-package-json-main-is-cjs", rule, { filename: "package.json", errors: [ { - message: "main is set to dist/src/index.js when it should be set to dist/index.js", + message: + "main is set to dist/src/index.js when it should be set to dist/index.js or dist/index.cjs", }, ], output: '{"main": "dist/index.js"}', @@ -358,7 +373,8 @@ ruleTester.run("ts-package-json-main-is-cjs", rule, { filename: "package.json", errors: [ { - message: "main is set to index.js when it should be set to dist/index.js", + message: + "main is set to index.js when it should be set to dist/index.js or dist/index.cjs", }, ], output: examplePackageGood,