|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * issue https://github.com/vuejs/eslint-plugin-vue/issues/250 |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | +const casing = require('../utils/casing') |
| 13 | + |
| 14 | +const allowedCaseOptions = ['PascalCase', 'kebab-case'] |
| 15 | +const defaultCase = 'PascalCase' |
| 16 | + |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | +// Rule Definition |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +module.exports = { |
| 22 | + meta: { |
| 23 | + docs: { |
| 24 | + description: 'enforce specific casing for the component naming style in template', |
| 25 | + category: undefined, // strongly-recommended |
| 26 | + url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.1/docs/rules/component-name-in-template-casing.md' |
| 27 | + }, |
| 28 | + fixable: 'code', |
| 29 | + schema: [ |
| 30 | + { |
| 31 | + enum: allowedCaseOptions |
| 32 | + }, |
| 33 | + { |
| 34 | + type: 'object', |
| 35 | + properties: { |
| 36 | + ignores: { |
| 37 | + type: 'array', |
| 38 | + items: { type: 'string' }, |
| 39 | + uniqueItems: true, |
| 40 | + additionalItems: false |
| 41 | + } |
| 42 | + }, |
| 43 | + additionalProperties: false |
| 44 | + } |
| 45 | + ] |
| 46 | + }, |
| 47 | + |
| 48 | + create (context) { |
| 49 | + const caseOption = context.options[0] |
| 50 | + const options = context.options[1] || {} |
| 51 | + const caseType = allowedCaseOptions.indexOf(caseOption) !== -1 ? caseOption : defaultCase |
| 52 | + const ignores = options.ignores || [] |
| 53 | + const tokens = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore() |
| 54 | + const sourceCode = context.getSourceCode() |
| 55 | + |
| 56 | + let hasInvalidEOF = false |
| 57 | + |
| 58 | + return utils.defineTemplateBodyVisitor(context, { |
| 59 | + 'VElement' (node) { |
| 60 | + if (hasInvalidEOF) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if (!utils.isCustomComponent(node)) { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + const name = node.rawName |
| 69 | + if (ignores.indexOf(name) >= 0) { |
| 70 | + return |
| 71 | + } |
| 72 | + const casingName = casing.getConverter(caseType)(name) |
| 73 | + if (casingName !== name) { |
| 74 | + const startTag = node.startTag |
| 75 | + const open = tokens.getFirstToken(startTag) |
| 76 | + |
| 77 | + context.report({ |
| 78 | + node: open, |
| 79 | + loc: open.loc, |
| 80 | + message: 'Component name "{{name}}" is not {{caseType}}.', |
| 81 | + data: { |
| 82 | + name, |
| 83 | + caseType |
| 84 | + }, |
| 85 | + fix: fixer => { |
| 86 | + const endTag = node.endTag |
| 87 | + if (!endTag) { |
| 88 | + return fixer.replaceText(open, `<${casingName}`) |
| 89 | + } |
| 90 | + const endTagOpen = tokens.getFirstToken(endTag) |
| 91 | + // If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by: |
| 92 | + // return [ |
| 93 | + // fixer.replaceText(open, `<${casingName}`), |
| 94 | + // fixer.replaceText(endTagOpen, `</${casingName}`) |
| 95 | + // ] |
| 96 | + const code = `<${casingName}${sourceCode.text.slice(open.range[1], endTagOpen.range[0])}</${casingName}` |
| 97 | + return fixer.replaceTextRange([open.range[0], endTagOpen.range[1]], code) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | + } |
| 102 | + }, { |
| 103 | + Program (node) { |
| 104 | + hasInvalidEOF = utils.hasInvalidEOF(node) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments