Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit eb02e0e

Browse files
committedFeb 21, 2019
feat(rule): no commonjs api in nuxt config
1 parent 649b58c commit eb02e0e

File tree

6 files changed

+229
-2
lines changed

6 files changed

+229
-2
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ This plugin provides four predefined configs:
8383
| | [nuxt/no-env-in-hooks](./docs/rules/no-env-in-hooks.md) | Disallow `process.server/process.client` in client only Vue lifecycle hooks like: `mounted, beforeMount, updated...` |
8484
| | [nuxt/no-globals-in-created](./docs/rules/no-globals-in-created.md) | Disallow `window/document` in `created/beforeCreate` |
8585
| | [nuxt/no-this-in-fetch-data](./docs/rules/no-this-in-fetch-data.md) | Disallow `this` in `asyncData/fetch` |
86+
| | [nuxt/no-cjs-in-config](./docs/rules/no-cjs-in-config.md) | Disallow `require/modules.exports/exports` in `nuxt.config.js` |
8687

8788
### Recommended Rules
8889

‎docs/rules/no-cjs-in-config.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# nuxt/no-cjs-in-config
2+
3+
> Disallow commonjs module api `require/modules.exports/exports` in `nuxt.config.js`
4+
5+
- :gear: This rule is included in `"plugin:nuxt/base"`.
6+
7+
## Rule Details
8+
9+
This rule is for preventing using `require/modules.exports/exportst` in `nuxt.config.js`
10+
11+
Examples of **incorrect** code for this rule:
12+
13+
```js
14+
15+
const { name } = require('./package.json')
16+
17+
module.exports = {
18+
mode: 'universal',
19+
name
20+
}
21+
22+
```
23+
24+
Examples of **correct** code for this rule:
25+
26+
```js
27+
28+
import { name } from './package.json'
29+
30+
export default {
31+
mode: 'universal',
32+
name
33+
}
34+
35+
```
36+
37+
## :mag: Implementation
38+
39+
- [Rule source](../../lib/rules/no-cjs-in-config.js)
40+
- [Test source](../../lib/rules/__test__/no-cjs-in-config.test.js)

‎lib/configs/base.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
'nuxt/no-env-in-context': 'error',
1919
'nuxt/no-env-in-hooks': 'error',
2020
'nuxt/no-globals-in-created': 'error',
21-
'nuxt/no-this-in-fetch-data': 'error'
21+
'nuxt/no-this-in-fetch-data': 'error',
22+
'nuxt/no-cjs-in-config': 'error'
2223
}
2324
}

‎lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = {
44
'no-env-in-hooks': require('./rules/no-env-in-hooks'),
55
'no-globals-in-created': require('./rules/no-globals-in-created'),
66
'no-this-in-fetch-data': require('./rules/no-this-in-fetch-data'),
7-
'no-timing-in-fetch-data': require('./rules/no-timing-in-fetch-data')
7+
'no-timing-in-fetch-data': require('./rules/no-timing-in-fetch-data'),
8+
'no-cjs-in-config': require('./rules/no-cjs-in-config')
89
},
910
configs: {
1011
'base': require('./configs/base'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @fileoverview Disallow `require/modules.exports/exports` in `nuxt.config.js`
3+
* @author Xin Du <clark.duxin@gmail.com>
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
var rule = require('../no-cjs-in-config')
12+
13+
var RuleTester = require('eslint').RuleTester
14+
15+
const parserOptions = {
16+
ecmaVersion: 2018,
17+
sourceType: 'module'
18+
}
19+
20+
// ------------------------------------------------------------------------------
21+
// Tests
22+
// ------------------------------------------------------------------------------
23+
24+
var ruleTester = new RuleTester()
25+
ruleTester.run('no-cjs-in-config', rule, {
26+
27+
valid: [
28+
{
29+
filename: 'nuxt.config.js',
30+
code: `
31+
import { name } from './package.json'
32+
33+
export default {
34+
mode: 'universal',
35+
name
36+
}
37+
`,
38+
parserOptions
39+
}
40+
],
41+
42+
invalid: [
43+
{
44+
filename: 'nuxt.config.js',
45+
code: `
46+
const { name } = require('./package.json')
47+
`,
48+
errors: [{
49+
message: 'Unexpected require, please use import instead.',
50+
type: 'Identifier'
51+
}],
52+
parserOptions
53+
},
54+
{
55+
filename: 'nuxt.config.js',
56+
code: `
57+
module.exports = {
58+
mode: 'universal',
59+
name
60+
}
61+
`,
62+
errors: [{
63+
message: 'Unexpected module.exports, please use export default instead.',
64+
type: 'MemberExpression'
65+
}],
66+
parserOptions
67+
},
68+
{
69+
filename: 'nuxt.config.js',
70+
code: `
71+
exports.test = {
72+
mode: 'universal',
73+
name
74+
}
75+
`,
76+
errors: [{
77+
message: 'Unexpected exports, please use export default instead.',
78+
type: 'MemberExpression'
79+
}],
80+
parserOptions
81+
}
82+
]
83+
})

‎lib/rules/no-cjs-in-config.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @fileoverview Disallow `require/modules.exports/exports` in `nuxt.config.js`
3+
* @author Xin Du <clark.duxin@gmail.com>
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Rule Definition
9+
// ------------------------------------------------------------------------------
10+
11+
module.exports = {
12+
meta: {
13+
docs: {
14+
description:
15+
'disallow commonjs module api `require/modules.exports/exports` in `nuxt.config.js`',
16+
category: 'base'
17+
},
18+
messages: {
19+
noCjs: 'Unexpected {{cjs}}, please use {{esm}} instead.'
20+
}
21+
},
22+
23+
create (context) {
24+
// variables should be defined here
25+
const options = context.options[0] || {}
26+
const configFile = options.file || 'nuxt.config.js'
27+
let isNuxtConfig = false
28+
29+
// ----------------------------------------------------------------------
30+
// Public
31+
// ----------------------------------------------------------------------
32+
33+
return {
34+
Program (node) {
35+
const filename = context.getFilename()
36+
if (filename === configFile) {
37+
isNuxtConfig = true
38+
}
39+
},
40+
MemberExpression: function (node) {
41+
if (!isNuxtConfig) {
42+
return
43+
}
44+
45+
// module.exports
46+
if (node.object.name === 'module' && node.property.name === 'exports') {
47+
context.report({
48+
node,
49+
messageId: 'noCjs',
50+
data: {
51+
cjs: 'module.exports',
52+
esm: 'export default'
53+
}
54+
})
55+
}
56+
57+
// exports.
58+
if (node.object.name === 'exports') {
59+
const isInScope = context.getScope()
60+
.variables
61+
.some(variable => variable.name === 'exports')
62+
if (!isInScope) {
63+
context.report({
64+
node,
65+
messageId: 'noCjs',
66+
data: {
67+
cjs: 'exports',
68+
esm: 'export default'
69+
}
70+
})
71+
}
72+
}
73+
},
74+
CallExpression: function (call) {
75+
const module = call.arguments[0]
76+
77+
if (
78+
!isNuxtConfig ||
79+
context.getScope().type !== 'module' ||
80+
!['ExpressionStatement', 'VariableDeclarator'].includes(call.parent.type) ||
81+
call.callee.type !== 'Identifier' ||
82+
call.callee.name !== 'require' ||
83+
call.arguments.length !== 1 ||
84+
module.type !== 'Literal' ||
85+
typeof module.value !== 'string'
86+
) {
87+
return
88+
}
89+
90+
context.report({
91+
node: call.callee,
92+
messageId: 'noCjs',
93+
data: {
94+
cjs: 'require',
95+
esm: 'import'
96+
}
97+
})
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)
This repository has been archived.