|
| 1 | +/** |
| 2 | + * @fileoverview require prop type to be a constructor |
| 3 | + * @author Michał Sajnóg |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +const message = 'The "{{name}}" property should be a constructor.' |
| 14 | + |
| 15 | +const forbiddenTypes = [ |
| 16 | + 'Literal', |
| 17 | + 'TemplateLiteral', |
| 18 | + 'BinaryExpression', |
| 19 | + 'UpdateExpression' |
| 20 | +] |
| 21 | + |
| 22 | +const isForbiddenType = nodeType => forbiddenTypes.indexOf(nodeType) > -1 |
| 23 | + |
| 24 | +module.exports = { |
| 25 | + meta: { |
| 26 | + docs: { |
| 27 | + description: 'require prop type to be a constructor', |
| 28 | + category: undefined, // essential |
| 29 | + url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.1/docs/rules/require-prop-type-constructor.md' |
| 30 | + }, |
| 31 | + fixable: 'code', // or "code" or "whitespace" |
| 32 | + schema: [] |
| 33 | + }, |
| 34 | + |
| 35 | + create (context) { |
| 36 | + const fix = node => fixer => { |
| 37 | + if (node.type === 'Literal') { |
| 38 | + return fixer.replaceText(node, node.value) |
| 39 | + } else if ( |
| 40 | + node.type === 'TemplateLiteral' && |
| 41 | + node.expressions.length === 0 && |
| 42 | + node.quasis.length === 1 |
| 43 | + ) { |
| 44 | + return fixer.replaceText(node, node.quasis[0].value.cooked) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const checkPropertyNode = (p) => { |
| 49 | + if (isForbiddenType(p.value.type)) { |
| 50 | + context.report({ |
| 51 | + node: p.value, |
| 52 | + message, |
| 53 | + data: { |
| 54 | + name: utils.getStaticPropertyName(p.key) |
| 55 | + }, |
| 56 | + fix: fix(p.value) |
| 57 | + }) |
| 58 | + } else if (p.value.type === 'ArrayExpression') { |
| 59 | + p.value.elements |
| 60 | + .filter(prop => isForbiddenType(prop.type)) |
| 61 | + .forEach(prop => context.report({ |
| 62 | + node: prop, |
| 63 | + message, |
| 64 | + data: { |
| 65 | + name: utils.getStaticPropertyName(p.key) |
| 66 | + }, |
| 67 | + fix: fix(prop) |
| 68 | + })) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return utils.executeOnVueComponent(context, (obj) => { |
| 73 | + const node = obj.properties.find(p => |
| 74 | + p.type === 'Property' && |
| 75 | + p.key.type === 'Identifier' && |
| 76 | + p.key.name === 'props' && |
| 77 | + p.value.type === 'ObjectExpression' |
| 78 | + ) |
| 79 | + |
| 80 | + if (!node) return |
| 81 | + |
| 82 | + node.value.properties.forEach(p => { |
| 83 | + if (isForbiddenType(p.value.type) || p.value.type === 'ArrayExpression') { |
| 84 | + checkPropertyNode(p) |
| 85 | + } else if (p.value.type === 'ObjectExpression') { |
| 86 | + const typeProperty = p.value.properties.find(prop => |
| 87 | + prop.type === 'Property' && |
| 88 | + prop.key.name === 'type' |
| 89 | + ) |
| 90 | + |
| 91 | + if (!typeProperty) return |
| 92 | + |
| 93 | + checkPropertyNode(typeProperty) |
| 94 | + } |
| 95 | + }) |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments