|
| 1 | +/** |
| 2 | + * @fileoverview Requires specific casing for the Prop name in Vue components |
| 3 | + * @author Yu Kimura |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const casing = require('../utils/casing') |
| 9 | +const allowedCaseOptions = ['camelCase', 'snake_case'] |
| 10 | + |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | +// Rule Definition |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | + |
| 15 | +function create (context) { |
| 16 | + const options = context.options[0] |
| 17 | + const caseType = allowedCaseOptions.indexOf(options) !== -1 ? options : 'camelCase' |
| 18 | + const converter = casing.getConverter(caseType) |
| 19 | + |
| 20 | + // ---------------------------------------------------------------------- |
| 21 | + // Public |
| 22 | + // ---------------------------------------------------------------------- |
| 23 | + |
| 24 | + return utils.executeOnVue(context, (obj) => { |
| 25 | + const node = obj.properties.find(p => |
| 26 | + p.type === 'Property' && |
| 27 | + p.key.type === 'Identifier' && |
| 28 | + p.key.name === 'props' && |
| 29 | + (p.value.type === 'ObjectExpression' || p.value.type === 'ArrayExpression') |
| 30 | + ) |
| 31 | + |
| 32 | + if (!node) return |
| 33 | + |
| 34 | + const items = node.value.type === 'ObjectExpression' ? node.value.properties : node.value.elements |
| 35 | + for (const item of items) { |
| 36 | + if (item.type !== 'Property') { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + const propName = item.key.type === 'Literal' ? item.key.value : item.key.name |
| 41 | + const convertedName = converter(propName) |
| 42 | + if (convertedName !== propName) { |
| 43 | + context.report({ |
| 44 | + node: item, |
| 45 | + message: 'Prop "{{name}}" is not in {{caseType}}.', |
| 46 | + data: { |
| 47 | + name: propName, |
| 48 | + caseType: caseType |
| 49 | + } |
| 50 | + }) |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +// ------------------------------------------------------------------------------ |
| 57 | +// Rule Definition |
| 58 | +// ------------------------------------------------------------------------------ |
| 59 | + |
| 60 | +module.exports = { |
| 61 | + meta: { |
| 62 | + docs: { |
| 63 | + description: 'enforce specific casing for the Prop name in Vue components', |
| 64 | + category: undefined // 'strongly-recommended' |
| 65 | + }, |
| 66 | + fixable: null, // or "code" or "whitespace" |
| 67 | + schema: [ |
| 68 | + { |
| 69 | + enum: allowedCaseOptions |
| 70 | + } |
| 71 | + ] |
| 72 | + }, |
| 73 | + |
| 74 | + create |
| 75 | +} |
0 commit comments