@@ -19,19 +19,14 @@ const { toRegExp } = require('eslint-plugin-vue/lib/utils/regexp')
19
19
const allowedCaseOptions = [ 'PascalCase' , 'kebab-case' ]
20
20
const defaultCase = 'PascalCase'
21
21
22
- // ------------------------------------------------------------------------------
23
- // Rule Definition
24
- // ------------------------------------------------------------------------------
25
-
26
22
module . exports = {
27
23
meta : {
28
24
type : 'suggestion' ,
29
25
docs : {
30
26
description :
31
27
'enforce specific casing for the component naming style in template' ,
32
28
categories : undefined ,
33
- url : 'https://eslint.vuejs.org/rules/component-name-in-template-casing.html' ,
34
- dropIn : true
29
+ url : 'https://eslint-plugin-vue-pug.rash.codes/rules/component-name-in-template-casing.html'
35
30
} ,
36
31
fixable : 'code' ,
37
32
schema : [
@@ -41,6 +36,11 @@ module.exports = {
41
36
{
42
37
type : 'object' ,
43
38
properties : {
39
+ globals : {
40
+ type : 'array' ,
41
+ items : { type : 'string' } ,
42
+ uniqueItems : true
43
+ } ,
44
44
ignores : {
45
45
type : 'array' ,
46
46
items : { type : 'string' } ,
@@ -59,18 +59,34 @@ module.exports = {
59
59
create ( context ) {
60
60
const caseOption = context . options [ 0 ]
61
61
const options = context . options [ 1 ] || { }
62
- const caseType =
63
- allowedCaseOptions . indexOf ( caseOption ) !== - 1 ? caseOption : defaultCase
62
+ const caseType = allowedCaseOptions . includes ( caseOption )
63
+ ? caseOption
64
+ : defaultCase
64
65
/** @type {RegExp[] } */
65
66
const ignores = ( options . ignores || [ ] ) . map ( toRegExp )
67
+ /** @type {string[] } */
68
+ const globals = ( options . globals || [ ] ) . map ( casing . pascalCase )
66
69
const registeredComponentsOnly = options . registeredComponentsOnly !== false
67
70
const tokens =
68
71
context . parserServices . getTemplateBodyTokenStore &&
69
72
context . parserServices . getTemplateBodyTokenStore ( )
70
73
71
- /** @type { string[] } */
72
- const registeredComponents = [ ]
74
+ /** @type { Set< string> } */
75
+ const registeredComponents = new Set ( globals )
73
76
77
+ if ( utils . isScriptSetup ( context ) ) {
78
+ // For <script setup>
79
+ const globalScope = context . getSourceCode ( ) . scopeManager . globalScope
80
+ if ( globalScope ) {
81
+ // Only check find the import module
82
+ const moduleScope = globalScope . childScopes . find (
83
+ ( scope ) => scope . type === 'module'
84
+ )
85
+ for ( const variable of ( moduleScope && moduleScope . variables ) || [ ] ) {
86
+ registeredComponents . add ( variable . name )
87
+ }
88
+ }
89
+ }
74
90
/**
75
91
* Checks whether the given node is the verification target node.
76
92
* @param {VElement } node element node
@@ -82,30 +98,21 @@ module.exports = {
82
98
return false
83
99
}
84
100
85
- if ( ! registeredComponentsOnly ) {
86
- // If the user specifies registeredComponentsOnly as false, it checks all component tags.
87
- if (
88
- ( ! utils . isHtmlElementNode ( node ) && ! utils . isSvgElementNode ( node ) ) ||
89
- utils . isHtmlWellKnownElementName ( node . rawName ) ||
90
- utils . isSvgWellKnownElementName ( node . rawName )
91
- ) {
92
- return false
93
- }
94
- return true
95
- }
96
- // We only verify the components registered in the component.
97
101
if (
98
- registeredComponents
99
- . filter ( ( name ) => casing . isPascalCase ( name ) ) // When defining a component with PascalCase, you can use either case
100
- . some (
101
- ( name ) =>
102
- node . rawName === name || casing . pascalCase ( node . rawName ) === name
103
- )
102
+ ( ! utils . isHtmlElementNode ( node ) && ! utils . isSvgElementNode ( node ) ) ||
103
+ utils . isHtmlWellKnownElementName ( node . rawName ) ||
104
+ utils . isSvgWellKnownElementName ( node . rawName )
104
105
) {
106
+ return false
107
+ }
108
+
109
+ if ( ! registeredComponentsOnly ) {
110
+ // If the user specifies registeredComponentsOnly as false, it checks all component tags.
105
111
return true
106
112
}
107
113
108
- return false
114
+ // We only verify the registered components.
115
+ return registeredComponents . has ( casing . pascalCase ( node . rawName ) )
109
116
}
110
117
111
118
let hasInvalidEOF = false
@@ -148,9 +155,9 @@ module.exports = {
148
155
} ,
149
156
...( registeredComponentsOnly
150
157
? utils . executeOnVue ( context , ( obj ) => {
151
- registeredComponents . push (
152
- ... utils . getRegisteredComponents ( obj ) . map ( ( n ) => n . name )
153
- )
158
+ for ( const n of utils . getRegisteredComponents ( obj ) ) {
159
+ registeredComponents . add ( n . name )
160
+ }
154
161
} )
155
162
: { } )
156
163
}
0 commit comments