|
| 1 | +/** |
| 2 | + * @author Pig Fang |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | + |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | +// Helpers |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +/** |
| 18 | + * @param {ArrayExpression} node |
| 19 | + * @param {RuleContext} context |
| 20 | + */ |
| 21 | +function checkArrayExpression(node, context) { |
| 22 | + const booleanType = node.elements.find( |
| 23 | + (element) => |
| 24 | + element && element.type === 'Identifier' && element.name === 'Boolean' |
| 25 | + ) |
| 26 | + if (!booleanType) { |
| 27 | + return |
| 28 | + } |
| 29 | + const booleanTypeIndex = node.elements.indexOf(booleanType) |
| 30 | + if (booleanTypeIndex > 0) { |
| 31 | + context.report({ |
| 32 | + node: booleanType, |
| 33 | + messageId: 'shouldBeFirst', |
| 34 | + suggest: [ |
| 35 | + { |
| 36 | + messageId: 'moveToFirst', |
| 37 | + fix: (fixer) => { |
| 38 | + const sourceCode = context.getSourceCode() |
| 39 | + |
| 40 | + const elements = node.elements.slice() |
| 41 | + elements.splice(booleanTypeIndex, 1) |
| 42 | + const code = elements |
| 43 | + .filter(utils.isDef) |
| 44 | + .map((element) => sourceCode.getText(element)) |
| 45 | + code.unshift('Boolean') |
| 46 | + |
| 47 | + return fixer.replaceText(node, `[${code.join(', ')}]`) |
| 48 | + } |
| 49 | + } |
| 50 | + ] |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// ------------------------------------------------------------------------------ |
| 56 | +// Rule Definition |
| 57 | +// ------------------------------------------------------------------------------ |
| 58 | + |
| 59 | +module.exports = { |
| 60 | + meta: { |
| 61 | + type: 'problem', |
| 62 | + docs: { |
| 63 | + description: 'enforce `Boolean` comes first in component prop types', |
| 64 | + categories: undefined, |
| 65 | + url: 'https://eslint.vuejs.org/rules/prefer-prop-type-boolean-first.html' |
| 66 | + }, |
| 67 | + fixable: null, |
| 68 | + // eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- `context.report` with suggestion is not recognized in `checkArrayExpression` |
| 69 | + hasSuggestions: true, |
| 70 | + schema: [], |
| 71 | + messages: { |
| 72 | + shouldBeFirst: 'Type `Boolean` should be at first in prop types.', |
| 73 | + moveToFirst: 'Move `Boolean` to be first in prop types.' |
| 74 | + } |
| 75 | + }, |
| 76 | + /** @param {RuleContext} context */ |
| 77 | + create(context) { |
| 78 | + /** |
| 79 | + * @param {import('../utils').ComponentProp} prop |
| 80 | + */ |
| 81 | + function checkProperty(prop) { |
| 82 | + const { value } = prop |
| 83 | + if (!value) { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if (value.type === 'ArrayExpression') { |
| 88 | + checkArrayExpression(value, context) |
| 89 | + } else if (value.type === 'ObjectExpression') { |
| 90 | + const type = value.properties.find( |
| 91 | + /** @return {property is Property} */ |
| 92 | + (property) => |
| 93 | + property.type === 'Property' && |
| 94 | + utils.getStaticPropertyName(property) === 'type' |
| 95 | + ) |
| 96 | + if (!type || type.value.type !== 'ArrayExpression') { |
| 97 | + return |
| 98 | + } |
| 99 | + checkArrayExpression(type.value, context) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return utils.compositingVisitors( |
| 104 | + utils.defineScriptSetupVisitor(context, { |
| 105 | + onDefinePropsEnter(_, props) { |
| 106 | + props.forEach(checkProperty) |
| 107 | + } |
| 108 | + }), |
| 109 | + utils.executeOnVue(context, (obj) => { |
| 110 | + const props = utils.getComponentPropsFromOptions(obj) |
| 111 | + props.forEach(checkProperty) |
| 112 | + }) |
| 113 | + ) |
| 114 | + } |
| 115 | +} |
0 commit comments