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
218 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# nuxt/no-timing-in-fetch-data | ||
|
||
> disallow `setTimeout/setInterval` in `asyncData/fetch` | ||
- :gear: This rule is included in `"plugin:nuxt/recommended"`. | ||
|
||
## Rule Details | ||
|
||
This rule is for preventing using `setTimeout/setInterval` in `asyncData/fetch` since it may lead to memory leak | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
|
||
export default { | ||
async asyncData() { | ||
let foo = 'baz' | ||
}, | ||
fetch() { | ||
let foo = 'baz' | ||
} | ||
} | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
|
||
export default { | ||
asyncData() { | ||
let foo = 'bar' | ||
setTimeout(() => { | ||
foo = 'baz' | ||
}, 0) | ||
}, | ||
fetch() { | ||
let foo = 'bar' | ||
setInterval(() => { | ||
foo = 'baz' | ||
}, 0) | ||
} | ||
} | ||
|
||
``` | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/no-timing-in-fetch-data.js) | ||
- [Test source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/__test__/no-timing-in-fetch-data.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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module.exports = { | ||
extends: require.resolve('./base.js'), | ||
rules: { | ||
'nuxt/no-globals-in-created': 'warn' | ||
'nuxt/no-globals-in-created': 'warn', | ||
'nuxt/no-timing-in-fetch-data': '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
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,94 @@ | ||
/** | ||
* @fileoverview disallow `setTimeout/setInterval` in `asyncData/fetch` | ||
* @author Xin Du <[email protected]> | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../no-timing-in-fetch-data') | ||
|
||
var RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('no-timing-in-fetch-data', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
async asyncData() { | ||
let foo = 'baz' | ||
}, | ||
fetch() { | ||
let foo = 'baz' | ||
} | ||
} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
asyncData() { | ||
let foo = 'bar' | ||
setTimeout(() => { | ||
foo = 'baz' | ||
}, 0) | ||
}, | ||
fetch() { | ||
let foo = 'bar' | ||
setInterval(() => { | ||
foo = 'baz' | ||
}, 0) | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected setTimeout in asyncData.', | ||
type: 'CallExpression' | ||
}, { | ||
message: 'Unexpected setInterval in fetch.', | ||
type: 'CallExpression' | ||
}], | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
asyncData() { | ||
let timer = setInterval | ||
}, | ||
fetch() { | ||
let timer = setTimeout | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'Unexpected setInterval in asyncData.', | ||
type: 'VariableDeclarator' | ||
}, { | ||
message: 'Unexpected setTimeout in fetch.', | ||
type: 'VariableDeclarator' | ||
}], | ||
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,70 @@ | ||
/** | ||
* @fileoverview disallow `setTimeout/setInterval` in `asyncData/fetch` | ||
* @author Xin Du <[email protected]> | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow `setTimeout/setInterval` in `asyncData/fetch`', | ||
category: 'ssr' | ||
}, | ||
messages: { | ||
noTiming: 'Unexpected {{name}} in {{funcName}}.' | ||
} | ||
}, | ||
|
||
create: function (context) { | ||
const forbiddenNodes = [] | ||
const options = context.options[0] || {} | ||
|
||
const HOOKS = new Set( | ||
['fetch', 'asyncData'].concat(options.methods || []) | ||
) | ||
const TIMING = ['setTimeout', 'setInterval'] | ||
|
||
function isTiming (name) { | ||
return TIMING.includes(name) | ||
} | ||
|
||
return { | ||
CallExpression: function (node) { | ||
if (!node.callee) return | ||
|
||
const name = node.callee.name | ||
|
||
if (isTiming(name)) { | ||
forbiddenNodes.push({ name, node }) | ||
} | ||
}, | ||
VariableDeclarator (node) { | ||
if (!node.init) return | ||
|
||
const name = node.init.name | ||
|
||
if (isTiming(name)) { | ||
forbiddenNodes.push({ name, node }) | ||
} | ||
}, | ||
...utils.executeOnVue(context, obj => { | ||
for (const { funcName, name, node } of utils.getFunctionWithChild(obj, HOOKS, forbiddenNodes)) { | ||
context.report({ | ||
node, | ||
messageId: 'noTiming', | ||
data: { | ||
name, | ||
funcName | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
} |