|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 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 | +const casing = require('../utils/casing') |
| 13 | + |
| 14 | +/** |
| 15 | + * @typedef {import('../utils').ComponentArrayProp} ComponentArrayProp |
| 16 | + * @typedef {import('../utils').ComponentObjectProp} ComponentObjectProp |
| 17 | + * @typedef {import('../utils').ComponentTypeProp} ComponentTypeProp |
| 18 | + */ |
| 19 | + |
| 20 | +// ------------------------------------------------------------------------------ |
| 21 | +// Helpers |
| 22 | +// ------------------------------------------------------------------------------ |
| 23 | + |
| 24 | +const RESERVED = { |
| 25 | + 3: ['key', 'ref'], |
| 26 | + 2: ['key', 'ref', 'is', 'slot', 'slot-scope', 'slotScope', 'class', 'style'] |
| 27 | +} |
| 28 | + |
| 29 | +// ------------------------------------------------------------------------------ |
| 30 | +// Rule Definition |
| 31 | +// ------------------------------------------------------------------------------ |
| 32 | + |
| 33 | +module.exports = { |
| 34 | + meta: { |
| 35 | + type: 'problem', |
| 36 | + docs: { |
| 37 | + description: 'disallow reserved names in props', |
| 38 | + categories: ['vue3-essential', 'essential'], |
| 39 | + url: 'https://eslint.vuejs.org/rules/no-reserved-props.html', |
| 40 | + defaultOptions: { |
| 41 | + vue2: [{ vueVersion: 2 }] |
| 42 | + } |
| 43 | + }, |
| 44 | + fixable: null, |
| 45 | + schema: [ |
| 46 | + { |
| 47 | + type: 'object', |
| 48 | + properties: { |
| 49 | + vueVersion: { |
| 50 | + enum: [2, 3] |
| 51 | + } |
| 52 | + }, |
| 53 | + additionalProperties: false |
| 54 | + } |
| 55 | + ], |
| 56 | + messages: { |
| 57 | + reserved: |
| 58 | + "'{{propName}}' is a reserved attribute and cannot be used as props." |
| 59 | + } |
| 60 | + }, |
| 61 | + /** @param {RuleContext} context */ |
| 62 | + create(context) { |
| 63 | + const options = context.options[0] || {} |
| 64 | + /** @type {2|3} */ |
| 65 | + const vueVersion = options.vueVersion || 3 |
| 66 | + |
| 67 | + const reserved = new Set(RESERVED[vueVersion]) |
| 68 | + |
| 69 | + /** |
| 70 | + * @param {(ComponentArrayProp | ComponentObjectProp | ComponentTypeProp)[]} props |
| 71 | + */ |
| 72 | + function processProps(props) { |
| 73 | + for (const prop of props) { |
| 74 | + if (prop.propName != null && reserved.has(prop.propName)) { |
| 75 | + context.report({ |
| 76 | + node: prop.node, |
| 77 | + messageId: `reserved`, |
| 78 | + data: { |
| 79 | + propName: casing.kebabCase(prop.propName) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return utils.compositingVisitors( |
| 87 | + utils.defineScriptSetupVisitor(context, { |
| 88 | + onDefinePropsEnter(_node, props) { |
| 89 | + processProps(props) |
| 90 | + } |
| 91 | + }), |
| 92 | + utils.executeOnVue(context, (obj) => { |
| 93 | + processProps(utils.getComponentProps(obj)) |
| 94 | + }) |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
0 commit comments