执行过npm install命令的vue-element-admin源码
康凯
2022-05-20 aa4c235a8ca67ea8b731f90c951a465e92c0a865
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
91
92
93
94
95
96
/**
 * @fileoverview Don't introduce side effects in computed properties
 * @author Michał Sajnóg
 */
'use strict'
 
const utils = require('../utils')
 
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
 
module.exports = {
  meta: {
    type: 'problem',
    docs: {
      description: 'disallow side effects in computed properties',
      category: 'essential',
      url: 'https://eslint.vuejs.org/rules/no-side-effects-in-computed-properties.html'
    },
    fixable: null,
    schema: []
  },
 
  create (context) {
    const forbiddenNodes = []
    let scopeStack = { upper: null, body: null }
 
    function onFunctionEnter (node) {
      scopeStack = { upper: scopeStack, body: node.body }
    }
 
    function onFunctionExit () {
      scopeStack = scopeStack.upper
    }
 
    return Object.assign({},
      {
        ':function': onFunctionEnter,
        ':function:exit': onFunctionExit,
 
        // this.xxx <=|+=|-=>
        'AssignmentExpression' (node) {
          if (node.left.type !== 'MemberExpression') return
          if (utils.parseMemberExpression(node.left)[0] === 'this') {
            forbiddenNodes.push({
              node,
              targetBody: scopeStack.body
            })
          }
        },
        // this.xxx <++|-->
        'UpdateExpression > MemberExpression' (node) {
          if (utils.parseMemberExpression(node)[0] === 'this') {
            forbiddenNodes.push({
              node,
              targetBody: scopeStack.body
            })
          }
        },
        // this.xxx.func()
        'CallExpression' (node) {
          const code = utils.parseMemberOrCallExpression(node)
          const MUTATION_REGEX = /(this.)((?!(concat|slice|map|filter)\().)[^\)]*((push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)\()/g
 
          if (MUTATION_REGEX.test(code)) {
            forbiddenNodes.push({
              node,
              targetBody: scopeStack.body
            })
          }
        }
      },
      utils.executeOnVue(context, (obj) => {
        const computedProperties = utils.getComputedProperties(obj)
 
        computedProperties.forEach(cp => {
          forbiddenNodes.forEach(({ node, targetBody }) => {
            if (
              cp.value &&
              node.loc.start.line >= cp.value.loc.start.line &&
              node.loc.end.line <= cp.value.loc.end.line &&
              targetBody === cp.value
            ) {
              context.report({
                node: node,
                message: 'Unexpected side effect in "{{key}}" computed property.',
                data: { key: cp.key }
              })
            }
          })
        })
      })
    )
  }
}