Skip to content

Commit

Permalink
[eslint-plugin] update rule to allow .cjs main entry (#26120)
Browse files Browse the repository at this point in the history
currently only index.js is allowed.
  • Loading branch information
jeremymeng authored Jun 7, 2023
1 parent e04622b commit 823371c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -14,6 +14,14 @@ This rule is fixable using the `--fix` option.
}
```

### Good

```json
{
"main": "dist/index.cjs"
}
```

### Bad

```json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"`),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"}',
Expand All @@ -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"}',
Expand All @@ -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"}',
Expand All @@ -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"}',
Expand All @@ -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"}',
Expand All @@ -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"}',
Expand All @@ -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,
Expand Down

0 comments on commit 823371c

Please sign in to comment.