|
| 1 | +/** |
| 2 | + * @author Marton Csordas |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const casing = require('../utils/casing') |
| 12 | +const utils = require('../utils') |
| 13 | + |
| 14 | +const RESERVED_NAMES_IN_VUE3 = new Set( |
| 15 | + require('../utils/vue3-builtin-components') |
| 16 | +) |
| 17 | + |
| 18 | +// ------------------------------------------------------------------------------ |
| 19 | +// Helpers |
| 20 | +// ------------------------------------------------------------------------------ |
| 21 | + |
| 22 | +/** |
| 23 | + * Returns true if the given component name is valid, otherwise false. |
| 24 | + * @param {string} name |
| 25 | + * */ |
| 26 | +function isValidComponentName(name) { |
| 27 | + if (name.toLowerCase() === 'app' || RESERVED_NAMES_IN_VUE3.has(name)) { |
| 28 | + return true |
| 29 | + } else { |
| 30 | + const elements = casing.kebabCase(name).split('-') |
| 31 | + return elements.length > 1 |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// ------------------------------------------------------------------------------ |
| 36 | +// Rule Definition |
| 37 | +// ------------------------------------------------------------------------------ |
| 38 | + |
| 39 | +module.exports = { |
| 40 | + meta: { |
| 41 | + type: 'suggestion', |
| 42 | + docs: { |
| 43 | + description: 'require component names to be always multi-word', |
| 44 | + categories: undefined, |
| 45 | + url: 'https://eslint.vuejs.org/rules/multi-word-component-names.html' |
| 46 | + }, |
| 47 | + schema: [], |
| 48 | + messages: { |
| 49 | + unexpected: 'Component name "{{value}}" should always be multi-word.' |
| 50 | + } |
| 51 | + }, |
| 52 | + /** @param {RuleContext} context */ |
| 53 | + create(context) { |
| 54 | + const fileName = context.getFilename() |
| 55 | + let componentName = fileName.replace(/\.[^/.]+$/, '') |
| 56 | + |
| 57 | + return utils.compositingVisitors( |
| 58 | + { |
| 59 | + /** @param {Program} node */ |
| 60 | + Program(node) { |
| 61 | + if ( |
| 62 | + !node.body.length && |
| 63 | + utils.isVueFile(fileName) && |
| 64 | + !isValidComponentName(componentName) |
| 65 | + ) { |
| 66 | + context.report({ |
| 67 | + messageId: 'unexpected', |
| 68 | + data: { |
| 69 | + value: componentName |
| 70 | + }, |
| 71 | + loc: { line: 1, column: 0 } |
| 72 | + }) |
| 73 | + } |
| 74 | + } |
| 75 | + }, |
| 76 | + |
| 77 | + utils.executeOnVue(context, (obj) => { |
| 78 | + const node = utils.findProperty(obj, 'name') |
| 79 | + |
| 80 | + /** @type {SourceLocation | null} */ |
| 81 | + let loc = null |
| 82 | + |
| 83 | + // Check if the component has a name property. |
| 84 | + if (node) { |
| 85 | + const valueNode = node.value |
| 86 | + if (valueNode.type !== 'Literal') return |
| 87 | + |
| 88 | + componentName = `${valueNode.value}` |
| 89 | + loc = node.loc |
| 90 | + } else if ( |
| 91 | + obj.parent.type === 'CallExpression' && |
| 92 | + obj.parent.arguments.length === 2 |
| 93 | + ) { |
| 94 | + // The component is registered globally with 'Vue.component', where |
| 95 | + // the first paremter is the component name. |
| 96 | + const argument = obj.parent.arguments[0] |
| 97 | + if (argument.type !== 'Literal') return |
| 98 | + |
| 99 | + componentName = `${argument.value}` |
| 100 | + loc = argument.loc |
| 101 | + } |
| 102 | + |
| 103 | + if (!isValidComponentName(componentName)) { |
| 104 | + context.report({ |
| 105 | + messageId: 'unexpected', |
| 106 | + data: { |
| 107 | + value: componentName |
| 108 | + }, |
| 109 | + loc: loc || { line: 1, column: 0 } |
| 110 | + }) |
| 111 | + } |
| 112 | + }) |
| 113 | + ) |
| 114 | + } |
| 115 | +} |
0 commit comments