Skip to content

Commit

Permalink
Adds linting rule to avoid assignment to 'module' variable. Fixes #34859
Browse files Browse the repository at this point in the history
  • Loading branch information
mward-sudo committed Apr 15, 2022
1 parent c0f979b commit 1be5f36
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -649,6 +649,10 @@
{
"title": "invalid-getserversideprops-return-value",
"path": "/errors/invalid-getserversideprops-return-value.md"
},
{
"title": "no-assign-module-variable",
"path": "/errors/no-assign-module-variable.md"
}
]
}
Expand Down
13 changes: 13 additions & 0 deletions errors/no-assign-module-variable.md
@@ -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 = {...}
```
2 changes: 2 additions & 0 deletions packages/eslint-plugin-next/lib/index.js
Expand Up @@ -20,6 +20,7 @@ module.exports = {
'no-duplicate-head': require('./rules/no-duplicate-head'),
'inline-script-id': require('./rules/inline-script-id'),
'next-script-for-ga': require('./rules/next-script-for-ga'),
'no-assign-module-variable': require('./rules/no-assign-module-variable'),
},
configs: {
recommended: {
Expand All @@ -45,6 +46,7 @@ module.exports = {
'@next/next/no-typos': 1,
'@next/next/no-duplicate-head': 2,
'@next/next/inline-script-id': 2,
'@next/next/no-assign-module-variable': 2,
},
},
'core-web-vitals': {
Expand Down
30 changes: 30 additions & 0 deletions packages/eslint-plugin-next/lib/rules/no-assign-module-variable.js
@@ -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 test/unit/eslint-plugin-next/no-assign-module-variable.test.ts
@@ -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",
},
],
},
],
})

0 comments on commit 1be5f36

Please sign in to comment.