-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-this-in-before-route-enter.js
83 lines (79 loc) · 2.29 KB
/
no-this-in-before-route-enter.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
/**
* @fileoverview Don't use this in a beforeRouteEnter method
* @author Przemyslaw Jan Beigert
*/
'use strict'
const utils = require('../utils')
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow `this` usage in a `beforeRouteEnter` method',
categories: null,
url: 'https://eslint.vuejs.org/rules/no-this-in-before-route-enter.html'
},
fixable: null,
schema: [],
messages: {
disallow:
"'beforeRouteEnter' does NOT have access to `this` component instance. https://router.vuejs.org/guide/advanced/navigation-guards.html#in-component-guards."
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {FunctionExpression | FunctionDeclaration} node
* @property {boolean} beforeRouteEnter
*/
/** @type {Set<FunctionExpression>} */
const beforeRouteEnterFunctions = new Set()
/** @type {ScopeStack | null} */
let scopeStack = null
/**
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node
*/
function onFunctionEnter(node) {
if (node.type === 'ArrowFunctionExpression') {
return
}
scopeStack = {
upper: scopeStack,
node,
beforeRouteEnter: beforeRouteEnterFunctions.has(
/** @type {never} */ (node)
)
}
}
/**
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node
*/
function onFunctionExit(node) {
if (scopeStack && scopeStack.node === node) {
scopeStack = scopeStack.upper
}
}
return utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
const beforeRouteEnter = utils.findProperty(node, 'beforeRouteEnter')
if (
beforeRouteEnter &&
beforeRouteEnter.value.type === 'FunctionExpression'
) {
beforeRouteEnterFunctions.add(beforeRouteEnter.value)
}
},
':function': onFunctionEnter,
':function:exit': onFunctionExit,
ThisExpression(node) {
if (scopeStack && scopeStack.beforeRouteEnter) {
context.report({
node,
messageId: 'disallow'
})
}
}
})
}
}