This repository was 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
/
Copy pathno-this-in-fetch-data.js
90 lines (77 loc) · 2.39 KB
/
no-this-in-fetch-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @fileoverview disallow `this` in `asyncData/fetch`
* @author Xin Du <[email protected]>
*/
'use strict'
const satisfies = require('semver/ranges/intersects')
const utils = require('../utils')
const resolver = require('../utils/resolver')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'disallow `this` in `asyncData/fetch`',
category: 'base'
},
messages: {
noThis: 'Unexpected this in {{funcName}}.'
}
},
create (context) {
// variables should be defined here
const forbiddenNodes = new Map()
const options = context.options[0] || {}
const HOOKS = new Set(
['asyncData'].concat(options.methods || [])
)
const { version } = resolver
// new fetch API can use `this` since 2.12.0
if (version && satisfies(version, '<2.12.0')) {
HOOKS.add('fetch')
}
let nodeUsingThis = []
// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
function enterFunction () {
nodeUsingThis = []
}
function exitFunction (node) {
if (nodeUsingThis.length > 0) {
forbiddenNodes.set(node, nodeUsingThis)
}
}
function markThisUsed (node) {
nodeUsingThis.push(node)
}
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return {
FunctionExpression: enterFunction,
'FunctionExpression:exit': exitFunction,
ArrowFunctionExpression: enterFunction,
'ArrowFunctionExpression:exit': exitFunction,
ThisExpression: markThisUsed,
Super: markThisUsed,
...utils.executeOnVue(context, obj => {
for (const funcName of HOOKS) {
const prop = utils.getFunctionWithName(obj, funcName)
if (prop && forbiddenNodes.has(prop.value)) {
for (const node of forbiddenNodes.get(prop.value)) {
context.report({
node,
messageId: 'noThis',
data: {
funcName
}
})
}
}
}
})
}
}
}