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): add no-globals-in-created
- Loading branch information
Showing
7 changed files
with
257 additions
and
6 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,38 @@ | ||
# nuxt/no-globals-in-created | ||
|
||
> disallow `window/document` in `created/beforeCreate` | ||
- :gear: This rule is included in `"plugin:nuxt/ssr"`. | ||
|
||
## Rule Details | ||
|
||
This rule is for preventing using `window/document` in `created/beforeCreate`, since `created/beforeCreate` may be executed in server side in SSR. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
|
||
export default { | ||
created() { | ||
window.foo = 'bar' | ||
} | ||
} | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
|
||
export default { | ||
created() { | ||
const foo = 'bar' | ||
} | ||
} | ||
|
||
``` | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/no-globals-in-created.js) | ||
- [Test source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/__test__/no-globals-in-created.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
extends: require.resolve('./base.js'), | ||
rules: { | ||
'nuxt/no-globals-in-created': 'warn' | ||
} | ||
} |
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,109 @@ | ||
/** | ||
* @fileoverview disallow `window/document` in `created/beforeCreate` | ||
* @author Clark Du | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../no-globals-in-created') | ||
|
||
var RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('no-globals-in-created', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
created() { | ||
const path = this.$route.path | ||
}, | ||
beforeCreate() { | ||
const path = this.$route.params.foo | ||
} | ||
} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
created() { | ||
const path = window.location.pathname | ||
}, | ||
beforeCreate() { | ||
const foo = document.foo | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected window in created.', | ||
type: 'MemberExpression' | ||
}, { | ||
message: 'Unexpected document in beforeCreate.', | ||
type: 'MemberExpression' | ||
}], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
created() { | ||
document.foo = 'bar' | ||
}, | ||
beforeCreate() { | ||
window.foo = 'bar' | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected document in created.', | ||
type: 'MemberExpression' | ||
}, { | ||
message: 'Unexpected window in beforeCreate.', | ||
type: 'MemberExpression' | ||
}], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
created() { | ||
return window.foo | ||
}, | ||
beforeCreate() { | ||
return document.foo | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected window in created.', | ||
type: 'MemberExpression' | ||
}, { | ||
message: 'Unexpected document in beforeCreate.', | ||
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
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,74 @@ | ||
/** | ||
* @fileoverview disallow `window/document` in `created/beforeCreate` | ||
* @author Clark Du | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow `window/document` in `created/beforeCreate`', | ||
category: 'ssr' | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [ | ||
// fill in your schema | ||
], | ||
messages: { | ||
noGlobals: 'Unexpected {{name}} in {{funcName}}.' | ||
} | ||
}, | ||
|
||
create: function (context) { | ||
const forbiddenNodes = [] | ||
const options = context.options[0] || {} | ||
|
||
const HOOKS = new Set( | ||
['created', 'beforeCreate'].concat(options.methods || []) | ||
) | ||
const GLOBALS = ['window', 'document'] | ||
|
||
function isGlobals (name) { | ||
return GLOBALS.includes(name) | ||
} | ||
|
||
return { | ||
MemberExpression (node) { | ||
if (!node.object) return | ||
|
||
const name = node.object.name | ||
|
||
if (isGlobals(name)) { | ||
forbiddenNodes.push({ name, node }) | ||
} | ||
}, | ||
VariableDeclarator (node) { | ||
if (!node.init) return | ||
|
||
const name = node.init.name | ||
|
||
if (isGlobals(name)) { | ||
forbiddenNodes.push({ name, node }) | ||
} | ||
}, | ||
...utils.executeOnVue(context, obj => { | ||
for (const { funcName, name, node } of utils.getParentFuncs(obj, HOOKS, forbiddenNodes)) { | ||
context.report({ | ||
node, | ||
messageId: 'noGlobals', | ||
data: { | ||
name, | ||
funcName | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
} |
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