Skip to content

Commit 012fbe8

Browse files
committed
update component-name-in-template-casing from upstream
1 parent 80a114c commit 012fbe8

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

lib/rules/component-name-in-template-casing.js

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,37 @@
44
*/
55
'use strict'
66

7-
// ------------------------------------------------------------------------------
8-
// Requirements
9-
// ------------------------------------------------------------------------------
10-
117
const utils = require('eslint-plugin-vue/lib/utils')
128
const casing = require('eslint-plugin-vue/lib/utils/casing')
139
const { toRegExp } = require('eslint-plugin-vue/lib/utils/regexp')
1410

15-
// -----------------------------------------------------------------------------
16-
// Helpers
17-
// -----------------------------------------------------------------------------
18-
1911
const allowedCaseOptions = ['PascalCase', 'kebab-case']
2012
const defaultCase = 'PascalCase'
2113

14+
/**
15+
* Checks whether the given variable is the type-only import object.
16+
* @param {Variable} variable
17+
* @returns {boolean} `true` if the given variable is the type-only import.
18+
*/
19+
function isTypeOnlyImport(variable) {
20+
if (variable.defs.length === 0) return false
21+
22+
return variable.defs.every((def) => {
23+
if (def.type !== 'ImportBinding') {
24+
return false
25+
}
26+
if (def.parent.importKind === 'type') {
27+
// check for `import type Foo from './xxx'`
28+
return true
29+
}
30+
if (def.node.type === 'ImportSpecifier' && def.node.importKind === 'type') {
31+
// check for `import { type Foo } from './xxx'`
32+
return true
33+
}
34+
return false
35+
})
36+
}
37+
2238
module.exports = {
2339
meta: {
2440
type: 'suggestion',
@@ -53,7 +69,10 @@ module.exports = {
5369
},
5470
additionalProperties: false
5571
}
56-
]
72+
],
73+
messages: {
74+
incorrectCase: 'Component name "{{name}}" is not {{caseType}}.'
75+
}
5776
},
5877
/** @param {RuleContext} context */
5978
create(context) {
@@ -67,9 +86,10 @@ module.exports = {
6786
/** @type {string[]} */
6887
const globals = (options.globals || []).map(casing.pascalCase)
6988
const registeredComponentsOnly = options.registeredComponentsOnly !== false
89+
const sourceCode = context.getSourceCode()
7090
const tokens =
71-
context.parserServices.getTemplateBodyTokenStore &&
72-
context.parserServices.getTemplateBodyTokenStore()
91+
sourceCode.parserServices.getTemplateBodyTokenStore &&
92+
sourceCode.parserServices.getTemplateBodyTokenStore()
7393

7494
/** @type { Set<string> } */
7595
const registeredComponents = new Set(globals)
@@ -83,7 +103,9 @@ module.exports = {
83103
(scope) => scope.type === 'module'
84104
)
85105
for (const variable of (moduleScope && moduleScope.variables) || []) {
86-
registeredComponents.add(variable.name)
106+
if (!isTypeOnlyImport(variable)) {
107+
registeredComponents.add(variable.name)
108+
}
87109
}
88110
}
89111
}
@@ -99,9 +121,13 @@ module.exports = {
99121
}
100122

101123
if (
102-
(!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
124+
(!utils.isHtmlElementNode(node) &&
125+
!utils.isSvgElementNode(node) &&
126+
!utils.isMathElementNode(node)) ||
103127
utils.isHtmlWellKnownElementName(node.rawName) ||
104-
utils.isSvgWellKnownElementName(node.rawName)
128+
utils.isSvgWellKnownElementName(node.rawName) ||
129+
utils.isMathWellKnownElementName(node.rawName) ||
130+
utils.isVueBuiltInElementName(node.rawName)
105131
) {
106132
return false
107133
}
@@ -137,7 +163,7 @@ module.exports = {
137163
context.report({
138164
node: open,
139165
loc: open.loc,
140-
message: 'Component name "{{name}}" is not {{caseType}}.',
166+
messageId: 'incorrectCase',
141167
data: {
142168
name,
143169
caseType

0 commit comments

Comments
 (0)