Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds linting rule to avoid assignment to 'module' variable #35279

Merged
merged 1 commit into from Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
},
mward-sudo marked this conversation as resolved.
Show resolved Hide resolved
},
'core-web-vitals': {
Expand Down
@@ -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",
},
],
},
],
})