Skip to content

Commit 00c3b99

Browse files
authored
Improve no-use-computed-property-like-method rule (#1659)
- Change to track variables to check types. - Change to track the conditional expression to check types. - Change condition to track branch and return to check types. - Change to check the expressions in template as well. - Fix known bugs.
1 parent 7bca4d3 commit 00c3b99

6 files changed

+907
-260
lines changed

lib/rules/no-undef-properties.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ module.exports = {
235235
function getVueComponentContextForTemplate() {
236236
const keys = [...vueComponentContextMap.keys()]
237237
const exported =
238-
keys.find(isScriptSetupProgram) || keys.find(isExportObject)
238+
keys.find(isScriptSetupProgram) || keys.find(utils.isInExportDefault)
239239
return exported && vueComponentContextMap.get(exported)
240240

241241
/**
@@ -244,19 +244,6 @@ module.exports = {
244244
function isScriptSetupProgram(node) {
245245
return node === programNode
246246
}
247-
/**
248-
* @param {ASTNode} node
249-
*/
250-
function isExportObject(node) {
251-
let parent = node.parent
252-
while (parent) {
253-
if (parent.type === 'ExportDefaultDeclaration') {
254-
return true
255-
}
256-
parent = parent.parent
257-
}
258-
return false
259-
}
260247
}
261248

262249
/**

lib/rules/no-unused-properties.js

+1-37
Original file line numberDiff line numberDiff line change
@@ -79,49 +79,13 @@ const PROPERTY_LABEL = {
7979
// Helpers
8080
// ------------------------------------------------------------------------------
8181

82-
/**
83-
* Find the variable of a given name.
84-
* @param {RuleContext} context The rule context
85-
* @param {Identifier} node The variable name to find.
86-
* @returns {Variable|null} The found variable or null.
87-
*/
88-
function findVariable(context, node) {
89-
return eslintUtils.findVariable(getScope(context, node), node)
90-
}
91-
/**
92-
* Gets the scope for the current node
93-
* @param {RuleContext} context The rule context
94-
* @param {ESNode} currentNode The node to get the scope of
95-
* @returns { import('eslint').Scope.Scope } The scope information for this node
96-
*/
97-
function getScope(context, currentNode) {
98-
// On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
99-
const inner = currentNode.type !== 'Program'
100-
const scopeManager = context.getSourceCode().scopeManager
101-
102-
/** @type {ESNode | null} */
103-
let node = currentNode
104-
for (; node; node = /** @type {ESNode | null} */ (node.parent)) {
105-
const scope = scopeManager.acquire(node, inner)
106-
107-
if (scope) {
108-
if (scope.type === 'function-expression-name') {
109-
return scope.childScopes[0]
110-
}
111-
return scope
112-
}
113-
}
114-
115-
return scopeManager.scopes[0]
116-
}
117-
11882
/**
11983
* @param {RuleContext} context
12084
* @param {Identifier} id
12185
* @returns {Expression}
12286
*/
12387
function findExpression(context, id) {
124-
const variable = findVariable(context, id)
88+
const variable = utils.findVariableByIdentifier(context, id)
12589
if (!variable) {
12690
return id
12791
}

0 commit comments

Comments
 (0)