执行过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
/**
 * @fileoverview Prevents boolean defaults from being set
 * @author Hiroki Osame
 */
'use strict'
 
const utils = require('../utils')
 
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
 
function isBooleanProp (prop) {
  return (
    prop.type === 'Property' &&
    prop.key.type === 'Identifier' &&
    prop.key.name === 'type' &&
    prop.value.type === 'Identifier' &&
    prop.value.name === 'Boolean'
  )
}
 
function getBooleanProps (props) {
  return props
    .filter(prop => (
      prop.value &&
      prop.value.properties &&
      prop.value.properties.find(isBooleanProp)
    ))
}
 
function getDefaultNode (propDef) {
  return propDef.value.properties.find(p => {
    return (
      p.type === 'Property' &&
      p.key.type === 'Identifier' &&
      p.key.name === 'default'
    )
  })
}
 
module.exports = {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'disallow boolean defaults',
      category: undefined,
      url: 'https://eslint.vuejs.org/rules/no-boolean-default.html'
    },
    fixable: 'code',
    schema: [
      {
        enum: ['default-false', 'no-default']
      }
    ]
  },
 
  create (context) {
    return utils.executeOnVueComponent(context, (obj) => {
      const props = utils.getComponentProps(obj)
      const booleanProps = getBooleanProps(props)
 
      if (!booleanProps.length) return
 
      const booleanType = context.options[0] || 'no-default'
 
      booleanProps.forEach((propDef) => {
        const defaultNode = getDefaultNode(propDef)
 
        switch (booleanType) {
          case 'no-default':
            if (defaultNode) {
              context.report({
                node: defaultNode,
                message: 'Boolean prop should not set a default (Vue defaults it to false).'
              })
            }
            break
 
          case 'default-false':
            if (
              defaultNode &&
              defaultNode.value.value !== false
            ) {
              context.report({
                node: defaultNode,
                message: 'Boolean prop should only be defaulted to false.'
              })
            }
            break
        }
      })
    })
  }
}