Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds linting rule to avoid assignment to 'module' variable. Fixes #34859
(#35279)
- Loading branch information
1 parent
c0f979b
commit 1be5f36
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# No assign module variable | ||
|
||
#### Why This Error Occurred | ||
|
||
A value is being assigned to the `module` variable. The `module` variable is already used and it is highly likely that assigning to this variable will cause errors. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Use a different variable name: | ||
|
||
```js | ||
let myModule = {...} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/eslint-plugin-next/lib/rules/no-assign-module-variable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: `Prohibit assignment to the 'module' variable`, | ||
recommended: true, | ||
url: 'https://nextjs.org/docs/messages/no-assign-module-variable', | ||
}, | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
VariableDeclaration(node) { | ||
// Checks node.declarations array for variable with id.name of 'module' | ||
const moduleVariableFound = node.declarations.some( | ||
(declaration) => declaration.id.name === 'module' | ||
) | ||
|
||
// Return early if no 'module' variable is found | ||
if (!moduleVariableFound) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node, | ||
message: `Do not assign to the variable 'module'. See: https://nextjs.org/docs/messages/no-assign-module-variable`, | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
42 changes: 42 additions & 0 deletions
42
test/unit/eslint-plugin-next/no-assign-module-variable.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import rule from '@next/eslint-plugin-next/lib/rules/no-assign-module-variable' | ||
import { RuleTester } from 'eslint' | ||
;(RuleTester as any).setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
modules: true, | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
const ruleTester = new RuleTester() | ||
|
||
ruleTester.run('no-assign-module-variable', rule, { | ||
valid: [ | ||
` | ||
let myModule = {}; | ||
export default function MyComponent() { | ||
return <></> | ||
} | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
let module = {}; | ||
export default function MyComponent() { | ||
return <></> | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"Do not assign to the variable 'module'. See: https://nextjs.org/docs/messages/no-assign-module-variable", | ||
}, | ||
], | ||
}, | ||
], | ||
}) |