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

Commit 4b32052

Browse files
committed
feat: add no-env-in-mounted
1 parent 291d915 commit 4b32052

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

docs/rules/no-env-in-mounted.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# nuxt/no-env-in-mounted
2+
3+
> disallow `process.server/process.client` in `mounted/beforeMount`
4+
5+
- :gear: This rule is included in `"plugin:nuxt/base"`.
6+
7+
## Rule Details
8+
9+
This rule is for preventing using `process.server/process.client` in `mounted/beforeMount` since they're only executed in client side.
10+
11+
Examples of **incorrect** code for this rule:
12+
13+
```js
14+
15+
export default {
16+
mounted() {
17+
const foo = 'bar'
18+
},
19+
beforeMount() {
20+
const foo = 'bar'
21+
}
22+
}
23+
24+
```
25+
26+
Examples of **correct** code for this rule:
27+
28+
```js
29+
30+
export default {
31+
mounted() {
32+
if(process.server) {
33+
const foo = 'bar'
34+
}
35+
},
36+
beforeMount() {
37+
if(process.client) {
38+
const foo = 'bar'
39+
}
40+
}
41+
}
42+
43+
```
44+
45+
## :mag: Implementation
46+
47+
- [Rule source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/no-env-in-mounted.js)
48+
- [Test source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/__test__/no-env-in-mounted.test.js)

lib/configs/base.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
],
1717
rules: {
1818
'nuxt/no-env-in-context': 'error',
19+
'nuxt/no-env-in-mounted': 'error',
1920
'nuxt/no-this-in-fetch-data': 'error'
2021
}
2122
}

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22
rules: {
33
'no-env-in-context': require('./rules/no-env-in-context'),
4+
'no-env-in-mounted': require('./rules/no-env-in-mounted'),
45
'no-globals-in-created': require('./rules/no-globals-in-created'),
56
'no-this-in-fetch-data': require('./rules/no-this-in-fetch-data'),
67
'no-timing-in-fetch-data': require('./rules/no-timing-in-fetch-data')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @fileoverview disallow `process.server/process.client` in `mounted/beforeMount`
3+
* @author Xin Du <[email protected]>
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
var rule = require('../no-env-in-mounted')
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-env-in-mounted', rule, {
26+
27+
valid: [
28+
{
29+
filename: 'test.vue',
30+
code: `
31+
export default {
32+
mounted() {
33+
const foo = 'bar'
34+
},
35+
beforeMount() {
36+
const foo = 'bar'
37+
}
38+
}
39+
`,
40+
parserOptions
41+
}
42+
],
43+
44+
invalid: [
45+
{
46+
filename: 'test.vue',
47+
code: `
48+
export default {
49+
mounted() {
50+
if(process.server) {
51+
const foo = 'bar'
52+
}
53+
},
54+
beforeMount() {
55+
if(process.client) {
56+
const foo = 'bar'
57+
}
58+
}
59+
}
60+
`,
61+
errors: [{
62+
message: 'Unexpected process.server in mounted.',
63+
type: 'MemberExpression'
64+
}, {
65+
message: 'Unexpected process.client in beforeMount.',
66+
type: 'MemberExpression'
67+
}],
68+
parserOptions
69+
},
70+
{
71+
filename: 'test.vue',
72+
code: `
73+
export default {
74+
mounted() {
75+
if(process['client']) {
76+
const foo = 'bar'
77+
}
78+
},
79+
beforeMount() {
80+
if(process['server']) {
81+
const foo = 'bar'
82+
}
83+
}
84+
}
85+
`,
86+
errors: [{
87+
message: 'Unexpected process.client in mounted.',
88+
type: 'MemberExpression'
89+
}, {
90+
message: 'Unexpected process.server in beforeMount.',
91+
type: 'MemberExpression'
92+
}],
93+
parserOptions
94+
}
95+
]
96+
})

lib/rules/no-env-in-mounted.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @fileoverview disallow `process.server/process.client` in `mounted/beforeMount`
3+
* @author Xin Du <[email protected]>
4+
*/
5+
'use strict'
6+
7+
const utils = require('../utils')
8+
9+
// ------------------------------------------------------------------------------
10+
// Rule Definition
11+
// ------------------------------------------------------------------------------
12+
13+
module.exports = {
14+
meta: {
15+
docs: {
16+
description:
17+
'disallow `process.server/process.client` in `mounted/beforeMount`',
18+
category: 'base'
19+
},
20+
messages: {
21+
noEnv: 'Unexpected {{name}} in {{funcName}}.'
22+
}
23+
},
24+
25+
create: function (context) {
26+
// variables should be defined here
27+
const forbiddenNodes = []
28+
const options = context.options[0] || {}
29+
30+
const ENV = ['server', 'client']
31+
const HOOKS = new Set(['mounted', 'beforeMount'].concat(options.methods || []))
32+
33+
// ----------------------------------------------------------------------
34+
// Public
35+
// ----------------------------------------------------------------------
36+
37+
return {
38+
MemberExpression (node) {
39+
const objectName = node.object.name
40+
if (objectName === 'process') {
41+
const propertyName = node.computed ? node.property.value : node.property.name
42+
if (propertyName && ENV.includes(propertyName)) {
43+
forbiddenNodes.push({ name: 'process.' + propertyName, node })
44+
}
45+
}
46+
},
47+
...utils.executeOnVue(context, obj => {
48+
for (const funcName of HOOKS) {
49+
const func = utils.getFunctionWithName(obj, funcName)
50+
for (const { name, node: child } of forbiddenNodes) {
51+
if (utils.isInFunction(func, child)) {
52+
context.report({
53+
node: child,
54+
messageId: 'noEnv',
55+
data: {
56+
name,
57+
funcName
58+
}
59+
})
60+
}
61+
}
62+
}
63+
})
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)