|
| 1 | +/** |
| 2 | + * @author Doug Wade <[email protected]> |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const casing = require('../utils/casing') |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + type: 'problem', |
| 13 | + schema: [], |
| 14 | + docs: { |
| 15 | + description: |
| 16 | + 'require the registered component name to match the imported component name', |
| 17 | + categories: undefined, |
| 18 | + url: 'https://eslint.vuejs.org/rules/match-component-import-name.html' |
| 19 | + }, |
| 20 | + fixable: null, |
| 21 | + messages: { |
| 22 | + unexpected: |
| 23 | + 'Component alias {{importedName}} should be one of: {{expectedName}}.' |
| 24 | + } |
| 25 | + }, |
| 26 | + /** |
| 27 | + * @param {RuleContext} context |
| 28 | + * @returns {RuleListener} |
| 29 | + */ |
| 30 | + create(context) { |
| 31 | + /** |
| 32 | + * @param {Identifier} identifier |
| 33 | + * @return {Array<String>} |
| 34 | + */ |
| 35 | + function getExpectedNames(identifier) { |
| 36 | + return [ |
| 37 | + casing.pascalCase(identifier.name), |
| 38 | + casing.kebabCase(identifier.name) |
| 39 | + ] |
| 40 | + } |
| 41 | + |
| 42 | + return utils.executeOnVueComponent(context, (obj) => { |
| 43 | + const components = utils.findProperty(obj, 'components') |
| 44 | + if ( |
| 45 | + !components || |
| 46 | + !components.value || |
| 47 | + components.value.type !== 'ObjectExpression' |
| 48 | + ) { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + components.value.properties.forEach( |
| 53 | + /** @param {Property | SpreadElement} property */ |
| 54 | + (property) => { |
| 55 | + if ( |
| 56 | + property.type === 'SpreadElement' || |
| 57 | + property.value.type !== 'Identifier' || |
| 58 | + property.computed === true |
| 59 | + ) { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + const importedName = utils.getStaticPropertyName(property) || '' |
| 64 | + const expectedNames = getExpectedNames(property.value) |
| 65 | + if (!expectedNames.includes(importedName)) { |
| 66 | + context.report({ |
| 67 | + node: property, |
| 68 | + messageId: 'unexpected', |
| 69 | + data: { |
| 70 | + importedName, |
| 71 | + expectedName: expectedNames.join(', ') |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | + } |
| 76 | + ) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
0 commit comments