|
| 1 | +/** |
| 2 | + * @fileoverview Define a style for the props casing in templates. |
| 3 | + * @author Armano |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const casing = require('../utils/casing') |
| 9 | + |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | +// Rule Definition |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +function create (context) { |
| 15 | + const sourceCode = context.getSourceCode() |
| 16 | + const options = context.options[0] |
| 17 | + const useHyphenated = options !== 'never' |
| 18 | + |
| 19 | + const kebabCase = casing.getConverter('kebab-case') |
| 20 | + const caseConverter = casing.getConverter(useHyphenated ? 'kebab-case' : 'camelCase') |
| 21 | + |
| 22 | + function reportIssue (node, name) { |
| 23 | + const text = sourceCode.getText(node.key) |
| 24 | + |
| 25 | + context.report({ |
| 26 | + node: node.key, |
| 27 | + loc: node.loc, |
| 28 | + message: useHyphenated ? "Attribute '{{text}}' must be hyphenated." : "Attribute '{{text}}' cann't be hyphenated.", |
| 29 | + data: { |
| 30 | + text |
| 31 | + }, |
| 32 | + fix: fixer => fixer.replaceText(node.key, text.replace(name, caseConverter(name))) |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + function isIgnoredAttribute (value) { |
| 37 | + if (value.indexOf('data-') !== -1 || value.indexOf('aria-') !== -1) { |
| 38 | + return true |
| 39 | + } |
| 40 | + return useHyphenated ? kebabCase(value) === value : kebabCase(value) !== value |
| 41 | + } |
| 42 | + |
| 43 | + // ---------------------------------------------------------------------- |
| 44 | + // Public |
| 45 | + // ---------------------------------------------------------------------- |
| 46 | + |
| 47 | + utils.registerTemplateBodyVisitor(context, { |
| 48 | + VAttribute (node) { |
| 49 | + if (!utils.isCustomComponent(node.parent.parent)) return |
| 50 | + |
| 51 | + const name = !node.directive ? node.key.name : node.key.name === 'bind' ? node.key.argument : false |
| 52 | + if (!name) return |
| 53 | + |
| 54 | + if (isIgnoredAttribute(name)) { |
| 55 | + return |
| 56 | + } |
| 57 | + reportIssue(node, name) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + return {} |
| 62 | +} |
| 63 | + |
| 64 | +module.exports = { |
| 65 | + meta: { |
| 66 | + docs: { |
| 67 | + description: 'Define a style for the props casing in templates.', |
| 68 | + category: 'Stylistic Issues', |
| 69 | + recommended: false |
| 70 | + }, |
| 71 | + fixable: 'code', |
| 72 | + schema: [ |
| 73 | + { |
| 74 | + enum: ['always', 'never'] |
| 75 | + } |
| 76 | + ] |
| 77 | + }, |
| 78 | + |
| 79 | + create |
| 80 | +} |
0 commit comments