WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit b3bef99

Browse files
committed
refactor: apply suggest
1 parent f589ad7 commit b3bef99

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

docs/rules/no-undef-directives.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default {
6969
}
7070
```
7171

72-
- `"ignore"` (`string[]`) An array of tags or regular expression patterns (e.g. `"/^custom-/"`) that ignore these rules. This option will check both kebab-case and PascalCase versions of the given tag names. Default is empty.
72+
- `"ignore"` (`string[]`) An array of directive names or regular expression patterns (e.g. `"/^custom-/"`) that ignore these rules. This option will check both kebab-case and PascalCase versions of the given directive names. Default is empty.
7373

7474
### `"ignore": ["foo"]`
7575

lib/rules/no-undef-directives.js

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ const utils = require('../utils')
88
const casing = require('../utils/casing')
99
const regexp = require('../utils/regexp')
1010

11-
/**
12-
* @param {string} str
13-
* @returns {string}
14-
*/
15-
function camelize(str) {
16-
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
17-
}
18-
1911
/**
2012
* @param {ObjectExpression} componentObject
2113
* @returns { { node: Property, name: string }[] } Array of ASTNodes
@@ -36,21 +28,20 @@ function getRegisteredDirectives(componentObject) {
3628
return []
3729
}
3830

39-
return directivesNode.value.properties
40-
.filter((node) => node.type === 'Property')
41-
.map((node) => {
42-
const name = utils.getStaticPropertyName(node)
43-
return name ? { node, name } : null
44-
})
45-
.filter((res) => !!res)
31+
// @ts-ignore
32+
return directivesNode.value.properties.flatMap((node) => {
33+
const name =
34+
node.type === 'Property' ? utils.getStaticPropertyName(node) : null
35+
return name ? [{ node, name }] : []
36+
})
4637
}
4738

4839
/**
4940
* @param {string} rawName
5041
* @param {Set<string>} definedNames
5142
*/
5243
function isDefinedInSetup(rawName, definedNames) {
53-
const camelName = camelize(rawName)
44+
const camelName = casing.camelCase(rawName)
5445
const variableName = `v${casing.capitalize(camelName)}`
5546
return definedNames.has(variableName)
5647
}
@@ -60,8 +51,9 @@ function isDefinedInSetup(rawName, definedNames) {
6051
* @param {Set<string>} definedNames
6152
*/
6253
function isDefinedInOptions(rawName, definedNames) {
63-
const camelName = camelize(rawName)
64-
if (definedNames.has(rawName) || definedNames.has(camelName)) {
54+
const camelName = casing.camelCase(rawName)
55+
56+
if (definedNames.has(rawName)) {
6557
return true
6658
}
6759

@@ -153,12 +145,13 @@ module.exports = {
153145
}
154146
}
155147

148+
/** @type {Set<string>} */
149+
const definedInOptionDirectives = new Set()
150+
156151
if (utils.isScriptSetup(context)) {
157152
// For <script setup>
158153
/** @type {Set<string>} */
159154
const definedInSetupDirectives = new Set()
160-
/** @type {Set<string>} */
161-
const definedInOptionDirectives = new Set()
162155

163156
const globalScope = context.sourceCode.scopeManager.globalScope
164157
if (globalScope) {
@@ -168,7 +161,7 @@ module.exports = {
168161
const moduleScope = globalScope.childScopes.find(
169162
(scope) => scope.type === 'module'
170163
)
171-
for (const variable of (moduleScope && moduleScope.variables) || []) {
164+
for (const variable of moduleScope?.variables ?? []) {
172165
definedInSetupDirectives.add(variable.name)
173166
}
174167
}
@@ -192,26 +185,23 @@ module.exports = {
192185
templateBodyVisitor,
193186
scriptVisitor
194187
)
195-
} else {
196-
// For Options API
197-
/** @type {Set<string>} */
198-
const definedInOptionDirectives = new Set()
188+
}
199189

200-
const scriptVisitor = utils.executeOnVue(context, (obj) => {
201-
for (const directive of getRegisteredDirectives(obj)) {
202-
definedInOptionDirectives.add(directive.name)
203-
}
204-
})
190+
// For Options API
191+
const scriptVisitor = utils.executeOnVue(context, (obj) => {
192+
for (const directive of getRegisteredDirectives(obj)) {
193+
definedInOptionDirectives.add(directive.name)
194+
}
195+
})
205196

206-
const templateBodyVisitor = createTemplateBodyVisitor((rawName) =>
207-
isDefinedInOptions(rawName, definedInOptionDirectives)
208-
)
197+
const templateBodyVisitor = createTemplateBodyVisitor((rawName) =>
198+
isDefinedInOptions(rawName, definedInOptionDirectives)
199+
)
209200

210-
return utils.defineTemplateBodyVisitor(
211-
context,
212-
templateBodyVisitor,
213-
scriptVisitor
214-
)
215-
}
201+
return utils.defineTemplateBodyVisitor(
202+
context,
203+
templateBodyVisitor,
204+
scriptVisitor
205+
)
216206
}
217207
}

0 commit comments

Comments
 (0)