|
| 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 | + * @typedef {import('../utils').ComponentArrayProp} ComponentArrayProp |
| 14 | + * @typedef {import('../utils').ComponentObjectProp} ComponentObjectProp |
| 15 | + * @typedef {import('../utils').ComponentUnknownProp} ComponentUnknownProp |
| 16 | + * @typedef {import('../utils').ComponentProp} ComponentProp |
| 17 | + */ |
| 18 | + |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | +// Rule Definition |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +module.exports = { |
| 24 | + meta: { |
| 25 | + hasSuggestions: true, |
| 26 | + type: 'problem', |
| 27 | + docs: { |
| 28 | + description: 'enforce props with default values to be optional', |
| 29 | + categories: undefined, |
| 30 | + url: 'https://eslint.vuejs.org/rules/no-required-prop-with-default.html' |
| 31 | + }, |
| 32 | + fixable: 'code', |
| 33 | + schema: [ |
| 34 | + { |
| 35 | + type: 'object', |
| 36 | + properties: { |
| 37 | + autofix: { |
| 38 | + type: 'boolean' |
| 39 | + } |
| 40 | + }, |
| 41 | + additionalProperties: false |
| 42 | + } |
| 43 | + ], |
| 44 | + messages: { |
| 45 | + requireOptional: `Prop "{{ key }}" should be optional.`, |
| 46 | + fixRequiredProp: `Change this prop to be optional.` |
| 47 | + } |
| 48 | + }, |
| 49 | + /** @param {RuleContext} context */ |
| 50 | + create(context) { |
| 51 | + let canAutoFix = false |
| 52 | + const option = context.options[0] |
| 53 | + if (option) { |
| 54 | + canAutoFix = option.autofix |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param {ComponentArrayProp | ComponentObjectProp | ComponentUnknownProp | ComponentProp} prop |
| 59 | + * */ |
| 60 | + const handleObjectProp = (prop) => { |
| 61 | + if ( |
| 62 | + prop.type === 'object' && |
| 63 | + prop.propName && |
| 64 | + prop.value.type === 'ObjectExpression' && |
| 65 | + utils.findProperty(prop.value, 'default') |
| 66 | + ) { |
| 67 | + const requiredProperty = utils.findProperty(prop.value, 'required') |
| 68 | + if (!requiredProperty) return |
| 69 | + const requiredNode = requiredProperty.value |
| 70 | + if ( |
| 71 | + requiredNode && |
| 72 | + requiredNode.type === 'Literal' && |
| 73 | + !!requiredNode.value |
| 74 | + ) { |
| 75 | + context.report({ |
| 76 | + node: prop.node, |
| 77 | + loc: prop.node.loc, |
| 78 | + data: { |
| 79 | + key: prop.propName |
| 80 | + }, |
| 81 | + messageId: 'requireOptional', |
| 82 | + fix: canAutoFix |
| 83 | + ? (fixer) => fixer.replaceText(requiredNode, 'false') |
| 84 | + : null, |
| 85 | + suggest: canAutoFix |
| 86 | + ? null |
| 87 | + : [ |
| 88 | + { |
| 89 | + messageId: 'fixRequiredProp', |
| 90 | + fix: (fixer) => fixer.replaceText(requiredNode, 'false') |
| 91 | + } |
| 92 | + ] |
| 93 | + }) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return utils.compositingVisitors( |
| 99 | + utils.defineVueVisitor(context, { |
| 100 | + onVueObjectEnter(node) { |
| 101 | + utils.getComponentPropsFromOptions(node).map(handleObjectProp) |
| 102 | + } |
| 103 | + }), |
| 104 | + utils.defineScriptSetupVisitor(context, { |
| 105 | + onDefinePropsEnter(node, props) { |
| 106 | + if (!utils.hasWithDefaults(node)) { |
| 107 | + props.map(handleObjectProp) |
| 108 | + return |
| 109 | + } |
| 110 | + const withDefaultsProps = Object.keys( |
| 111 | + utils.getWithDefaultsPropExpressions(node) |
| 112 | + ) |
| 113 | + const requiredProps = props.flatMap((item) => |
| 114 | + item.type === 'type' && item.required ? [item] : [] |
| 115 | + ) |
| 116 | + |
| 117 | + for (const prop of requiredProps) { |
| 118 | + if (withDefaultsProps.includes(prop.propName)) { |
| 119 | + // skip setter & getter case |
| 120 | + if ( |
| 121 | + prop.node.type === 'TSMethodSignature' && |
| 122 | + (prop.node.kind === 'get' || prop.node.kind === 'set') |
| 123 | + ) { |
| 124 | + return |
| 125 | + } |
| 126 | + // skip computed |
| 127 | + if (prop.node.computed) { |
| 128 | + return |
| 129 | + } |
| 130 | + context.report({ |
| 131 | + node: prop.node, |
| 132 | + loc: prop.node.loc, |
| 133 | + data: { |
| 134 | + key: prop.propName |
| 135 | + }, |
| 136 | + messageId: 'requireOptional', |
| 137 | + fix: canAutoFix |
| 138 | + ? (fixer) => fixer.insertTextAfter(prop.key, '?') |
| 139 | + : null, |
| 140 | + suggest: canAutoFix |
| 141 | + ? null |
| 142 | + : [ |
| 143 | + { |
| 144 | + messageId: 'fixRequiredProp', |
| 145 | + fix: (fixer) => fixer.insertTextAfter(prop.key, '?') |
| 146 | + } |
| 147 | + ] |
| 148 | + }) |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + }) |
| 153 | + ) |
| 154 | + } |
| 155 | +} |
0 commit comments