This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): no commonjs api in nuxt config
- Loading branch information
Showing
6 changed files
with
229 additions
and
2 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,40 @@ | ||
# nuxt/no-cjs-in-config | ||
|
||
> Disallow commonjs module api `require/modules.exports/exports` in `nuxt.config.js` | ||
- :gear: This rule is included in `"plugin:nuxt/base"`. | ||
|
||
## Rule Details | ||
|
||
This rule is for preventing using `require/modules.exports/exportst` in `nuxt.config.js` | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
|
||
const { name } = require('./package.json') | ||
|
||
module.exports = { | ||
mode: 'universal', | ||
name | ||
} | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
|
||
import { name } from './package.json' | ||
|
||
export default { | ||
mode: 'universal', | ||
name | ||
} | ||
|
||
``` | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](../../lib/rules/no-cjs-in-config.js) | ||
- [Test source](../../lib/rules/__test__/no-cjs-in-config.test.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
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,83 @@ | ||
/** | ||
* @fileoverview Disallow `require/modules.exports/exports` in `nuxt.config.js` | ||
* @author Xin Du <[email protected]> | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../no-cjs-in-config') | ||
|
||
var RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('no-cjs-in-config', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'nuxt.config.js', | ||
code: ` | ||
import { name } from './package.json' | ||
export default { | ||
mode: 'universal', | ||
name | ||
} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'nuxt.config.js', | ||
code: ` | ||
const { name } = require('./package.json') | ||
`, | ||
errors: [{ | ||
message: 'Unexpected require, please use import instead.', | ||
type: 'Identifier' | ||
}], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'nuxt.config.js', | ||
code: ` | ||
module.exports = { | ||
mode: 'universal', | ||
name | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected module.exports, please use export default instead.', | ||
type: 'MemberExpression' | ||
}], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'nuxt.config.js', | ||
code: ` | ||
exports.test = { | ||
mode: 'universal', | ||
name | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected exports, please use export default instead.', | ||
type: 'MemberExpression' | ||
}], | ||
parserOptions | ||
} | ||
] | ||
}) |
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,101 @@ | ||
/** | ||
* @fileoverview Disallow `require/modules.exports/exports` in `nuxt.config.js` | ||
* @author Xin Du <[email protected]> | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'disallow commonjs module api `require/modules.exports/exports` in `nuxt.config.js`', | ||
category: 'base' | ||
}, | ||
messages: { | ||
noCjs: 'Unexpected {{cjs}}, please use {{esm}} instead.' | ||
} | ||
}, | ||
|
||
create (context) { | ||
// variables should be defined here | ||
const options = context.options[0] || {} | ||
const configFile = options.file || 'nuxt.config.js' | ||
let isNuxtConfig = false | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return { | ||
Program (node) { | ||
const filename = context.getFilename() | ||
if (filename === configFile) { | ||
isNuxtConfig = true | ||
} | ||
}, | ||
MemberExpression: function (node) { | ||
if (!isNuxtConfig) { | ||
return | ||
} | ||
|
||
// module.exports | ||
if (node.object.name === 'module' && node.property.name === 'exports') { | ||
context.report({ | ||
node, | ||
messageId: 'noCjs', | ||
data: { | ||
cjs: 'module.exports', | ||
esm: 'export default' | ||
} | ||
}) | ||
} | ||
|
||
// exports. | ||
if (node.object.name === 'exports') { | ||
const isInScope = context.getScope() | ||
.variables | ||
.some(variable => variable.name === 'exports') | ||
if (!isInScope) { | ||
context.report({ | ||
node, | ||
messageId: 'noCjs', | ||
data: { | ||
cjs: 'exports', | ||
esm: 'export default' | ||
} | ||
}) | ||
} | ||
} | ||
}, | ||
CallExpression: function (call) { | ||
const module = call.arguments[0] | ||
|
||
if ( | ||
!isNuxtConfig || | ||
context.getScope().type !== 'module' || | ||
!['ExpressionStatement', 'VariableDeclarator'].includes(call.parent.type) || | ||
call.callee.type !== 'Identifier' || | ||
call.callee.name !== 'require' || | ||
call.arguments.length !== 1 || | ||
module.type !== 'Literal' || | ||
typeof module.value !== 'string' | ||
) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node: call.callee, | ||
messageId: 'noCjs', | ||
data: { | ||
cjs: 'require', | ||
esm: 'import' | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |