|
| 1 | +/** |
| 2 | + * @author @neferqiqi |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | +// ------------------------------------------------------------------------------ |
| 7 | +// Requirements |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | + |
| 10 | +const utils = require('../utils') |
| 11 | +/** |
| 12 | + * @typedef {import('../utils').ComponentTypeProp} ComponentTypeProp |
| 13 | + */ |
| 14 | + |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | +// Helpers |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +// ... |
| 20 | + |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | +// Rule Definition |
| 23 | +// ------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +module.exports = { |
| 26 | + meta: { |
| 27 | + type: 'suggestion', |
| 28 | + docs: { |
| 29 | + description: 'enforce props with default values to be optional', |
| 30 | + categories: undefined, |
| 31 | + url: 'https://eslint.vuejs.org/rules/prefer-optional-props-using-with-defaults.html' |
| 32 | + }, |
| 33 | + fixable: 'code', |
| 34 | + schema: [], |
| 35 | + messages: { |
| 36 | + // ... |
| 37 | + } |
| 38 | + }, |
| 39 | + /** @param {RuleContext} context */ |
| 40 | + create(context) { |
| 41 | + /** |
| 42 | + * @param {ComponentTypeProp} prop |
| 43 | + * @param {Token[]} tokens |
| 44 | + * */ |
| 45 | + const findKeyToken = (prop, tokens) => |
| 46 | + tokens.find((token) => { |
| 47 | + const isKeyIdentifierEqual = |
| 48 | + prop.key.type === 'Identifier' && token.value === prop.key.name |
| 49 | + const isKeyLiteralEqual = |
| 50 | + prop.key.type === 'Literal' && token.value === prop.key.raw |
| 51 | + return isKeyIdentifierEqual || isKeyLiteralEqual |
| 52 | + }) |
| 53 | + |
| 54 | + return utils.defineScriptSetupVisitor(context, { |
| 55 | + onDefinePropsEnter(node, props) { |
| 56 | + if (!utils.hasWithDefaults(node)) { |
| 57 | + return |
| 58 | + } |
| 59 | + const withDefaultsProps = Object.keys( |
| 60 | + utils.getWithDefaultsPropExpressions(node) |
| 61 | + ) |
| 62 | + const requiredProps = props.flatMap((item) => |
| 63 | + item.type === 'type' && item.required ? [item] : [] |
| 64 | + ) |
| 65 | + |
| 66 | + for (const prop of requiredProps) { |
| 67 | + if (withDefaultsProps.includes(prop.propName)) { |
| 68 | + const firstToken = context.getSourceCode().getFirstToken(prop.node) |
| 69 | + // skip setter & getter case |
| 70 | + if (firstToken.value === 'get' || firstToken.value === 'set') { |
| 71 | + return |
| 72 | + } |
| 73 | + // skip computed |
| 74 | + if (prop.node.computed) { |
| 75 | + return |
| 76 | + } |
| 77 | + const keyToken = findKeyToken( |
| 78 | + prop, |
| 79 | + context.getSourceCode().getTokens(prop.node) |
| 80 | + ) |
| 81 | + if (!keyToken) return |
| 82 | + context.report({ |
| 83 | + node: prop.node, |
| 84 | + loc: prop.node.loc, |
| 85 | + data: { |
| 86 | + key: prop.propName |
| 87 | + }, |
| 88 | + message: `Prop "{{ key }}" should be optional.`, |
| 89 | + fix: (fixer) => fixer.insertTextAfter(keyToken, '?') |
| 90 | + }) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments