|
| 1 | +/** |
| 2 | + * @fileoverview Prop definitions should be detailed |
| 3 | + * @author Armano |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +function create (context) { |
| 10 | + // ---------------------------------------------------------------------- |
| 11 | + // Helpers |
| 12 | + // ---------------------------------------------------------------------- |
| 13 | + |
| 14 | + function objectHasType (node) { |
| 15 | + const typeProperty = node.properties |
| 16 | + .find(p => |
| 17 | + utils.getStaticPropertyName(p.key) === 'type' && |
| 18 | + ( |
| 19 | + p.value.type !== 'ArrayExpression' || |
| 20 | + p.value.elements.length > 0 |
| 21 | + ) |
| 22 | + ) |
| 23 | + return Boolean(typeProperty) |
| 24 | + } |
| 25 | + |
| 26 | + function checkProperties (items) { |
| 27 | + for (const cp of items) { |
| 28 | + if (cp.type !== 'Property') { |
| 29 | + return |
| 30 | + } |
| 31 | + let hasType = true |
| 32 | + if (cp.value.type === 'ObjectExpression') { // foo: { |
| 33 | + hasType = objectHasType(cp.value) |
| 34 | + } else if (cp.value.type === 'ArrayExpression') { // foo: [ |
| 35 | + hasType = cp.value.elements.length > 0 |
| 36 | + } else if (cp.value.type === 'FunctionExpression' || cp.value.type === 'ArrowFunctionExpression') { |
| 37 | + hasType = false |
| 38 | + } |
| 39 | + if (!hasType) { |
| 40 | + context.report({ |
| 41 | + node: cp, |
| 42 | + message: 'Prop "{{name}}" should define at least it\'s type.', |
| 43 | + data: { |
| 44 | + name: cp.key.name |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // ---------------------------------------------------------------------- |
| 52 | + // Public |
| 53 | + // ---------------------------------------------------------------------- |
| 54 | + |
| 55 | + return utils.executeOnVue(context, (obj) => { |
| 56 | + const node = obj.properties |
| 57 | + .find(p => |
| 58 | + p.type === 'Property' && |
| 59 | + p.key.type === 'Identifier' && |
| 60 | + p.key.name === 'props' |
| 61 | + ) |
| 62 | + |
| 63 | + if (!node) return |
| 64 | + |
| 65 | + if (node.value.type === 'ObjectExpression') { |
| 66 | + checkProperties(node.value.properties) |
| 67 | + } else { |
| 68 | + context.report({ |
| 69 | + node, |
| 70 | + message: 'Props should at least define their types.' |
| 71 | + }) |
| 72 | + } |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +// ------------------------------------------------------------------------------ |
| 77 | +// Rule Definition |
| 78 | +// ------------------------------------------------------------------------------ |
| 79 | + |
| 80 | +module.exports = { |
| 81 | + meta: { |
| 82 | + docs: { |
| 83 | + description: 'Prop definitions should be detailed', |
| 84 | + category: 'Best Practices', |
| 85 | + recommended: false |
| 86 | + }, |
| 87 | + fixable: null, // or "code" or "whitespace" |
| 88 | + schema: [ |
| 89 | + // fill in your schema |
| 90 | + ] |
| 91 | + }, |
| 92 | + |
| 93 | + create |
| 94 | +} |
0 commit comments