执行过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
97
98
99
100
101
102
/**
 * @fileoverview require prop type to be a constructor
 * @author Michał Sajnóg
 */
'use strict'
 
const utils = require('../utils')
 
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
 
const message = 'The "{{name}}" property should be a constructor.'
 
const forbiddenTypes = [
  'Literal',
  'TemplateLiteral',
  'BinaryExpression',
  'UpdateExpression'
]
 
const isForbiddenType = node => forbiddenTypes.indexOf(node.type) > -1 && node.raw !== 'null'
 
module.exports = {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'require prop type to be a constructor',
      category: 'essential',
      url: 'https://eslint.vuejs.org/rules/require-prop-type-constructor.html'
    },
    fixable: 'code',  // or "code" or "whitespace"
    schema: []
  },
 
  create (context) {
    const fix = node => fixer => {
      let newText
      if (node.type === 'Literal') {
        if (typeof node.value !== 'string') {
          return undefined
        }
        newText = node.value
      } else if (
        node.type === 'TemplateLiteral' &&
        node.expressions.length === 0 &&
        node.quasis.length === 1
      ) {
        newText = node.quasis[0].value.cooked
      } else {
        return undefined
      }
      if (newText) {
        return fixer.replaceText(node, newText)
      }
    }
 
    const checkPropertyNode = (key, node) => {
      if (isForbiddenType(node)) {
        context.report({
          node: node,
          message,
          data: {
            name: utils.getStaticPropertyName(key)
          },
          fix: fix(node)
        })
      } else if (node.type === 'ArrayExpression') {
        node.elements
          .filter(prop => prop && isForbiddenType(prop))
          .forEach(prop => context.report({
            node: prop,
            message,
            data: {
              name: utils.getStaticPropertyName(key)
            },
            fix: fix(prop)
          }))
      }
    }
 
    return utils.executeOnVueComponent(context, (obj) => {
      const props = utils.getComponentProps(obj)
        .filter(prop => prop.key && prop.value)
 
      for (const prop of props) {
        if (isForbiddenType(prop.value) || prop.value.type === 'ArrayExpression') {
          checkPropertyNode(prop.key, prop.value)
        } else if (prop.value.type === 'ObjectExpression') {
          const typeProperty = prop.value.properties.find(property =>
            property.type === 'Property' &&
            property.key.name === 'type'
          )
 
          if (!typeProperty) continue
 
          checkPropertyNode(prop.key, typeProperty.value)
        }
      }
    })
  }
}