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.
- Loading branch information
Showing
5 changed files
with
212 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# nuxt/no-env-in-mounted | ||
|
||
> disallow `process.server/process.client` in `mounted/beforeMount` | ||
- :gear: This rule is included in `"plugin:nuxt/base"`. | ||
|
||
## Rule Details | ||
|
||
This rule is for preventing using `process.server/process.client` in `mounted/beforeMount` since they're only executed in client side. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
|
||
export default { | ||
mounted() { | ||
const foo = 'bar' | ||
}, | ||
beforeMount() { | ||
const foo = 'bar' | ||
} | ||
} | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
|
||
export default { | ||
mounted() { | ||
if(process.server) { | ||
const foo = 'bar' | ||
} | ||
}, | ||
beforeMount() { | ||
if(process.client) { | ||
const foo = 'bar' | ||
} | ||
} | ||
} | ||
|
||
``` | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/no-env-in-mounted.js) | ||
- [Test source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/__test__/no-env-in-mounted.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,96 @@ | ||
/** | ||
* @fileoverview disallow `process.server/process.client` in `mounted/beforeMount` | ||
* @author Xin Du <clark.duxin@gmail.com> | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../no-env-in-mounted') | ||
|
||
var RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('no-env-in-mounted', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
mounted() { | ||
const foo = 'bar' | ||
}, | ||
beforeMount() { | ||
const foo = 'bar' | ||
} | ||
} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
mounted() { | ||
if(process.server) { | ||
const foo = 'bar' | ||
} | ||
}, | ||
beforeMount() { | ||
if(process.client) { | ||
const foo = 'bar' | ||
} | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected process.server in mounted.', | ||
type: 'MemberExpression' | ||
}, { | ||
message: 'Unexpected process.client in beforeMount.', | ||
type: 'MemberExpression' | ||
}], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
mounted() { | ||
if(process['client']) { | ||
const foo = 'bar' | ||
} | ||
}, | ||
beforeMount() { | ||
if(process['server']) { | ||
const foo = 'bar' | ||
} | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected process.client in mounted.', | ||
type: 'MemberExpression' | ||
}, { | ||
message: 'Unexpected process.server in beforeMount.', | ||
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,66 @@ | ||
/** | ||
* @fileoverview disallow `process.server/process.client` in `mounted/beforeMount` | ||
* @author Xin Du <clark.duxin@gmail.com> | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'disallow `process.server/process.client` in `mounted/beforeMount`', | ||
category: 'base' | ||
}, | ||
messages: { | ||
noEnv: 'Unexpected {{name}} in {{funcName}}.' | ||
} | ||
}, | ||
|
||
create: function (context) { | ||
// variables should be defined here | ||
const forbiddenNodes = [] | ||
const options = context.options[0] || {} | ||
|
||
const ENV = ['server', 'client'] | ||
const HOOKS = new Set(['mounted', 'beforeMount'].concat(options.methods || [])) | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return { | ||
MemberExpression (node) { | ||
const objectName = node.object.name | ||
if (objectName === 'process') { | ||
const propertyName = node.computed ? node.property.value : node.property.name | ||
if (propertyName && ENV.includes(propertyName)) { | ||
forbiddenNodes.push({ name: 'process.' + propertyName, node }) | ||
} | ||
} | ||
}, | ||
...utils.executeOnVue(context, obj => { | ||
for (const funcName of HOOKS) { | ||
const func = utils.getFunctionWithName(obj, funcName) | ||
for (const { name, node: child } of forbiddenNodes) { | ||
if (utils.isInFunction(func, child)) { | ||
context.report({ | ||
node: child, | ||
messageId: 'noEnv', | ||
data: { | ||
name, | ||
funcName | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} |