Skip to content

Commit

Permalink
Improve no-use-computed-property-like-method rule (#1659)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
ota-meshi committed Oct 18, 2021
1 parent 7bca4d3 commit 00c3b99
Show file tree
Hide file tree
Showing 6 changed files with 907 additions and 260 deletions.
15 changes: 1 addition & 14 deletions lib/rules/no-undef-properties.js
Expand Up @@ -235,7 +235,7 @@ module.exports = {
function getVueComponentContextForTemplate() {
const keys = [...vueComponentContextMap.keys()]
const exported =
keys.find(isScriptSetupProgram) || keys.find(isExportObject)
keys.find(isScriptSetupProgram) || keys.find(utils.isInExportDefault)
return exported && vueComponentContextMap.get(exported)

/**
Expand All @@ -244,19 +244,6 @@ module.exports = {
function isScriptSetupProgram(node) {
return node === programNode
}
/**
* @param {ASTNode} node
*/
function isExportObject(node) {
let parent = node.parent
while (parent) {
if (parent.type === 'ExportDefaultDeclaration') {
return true
}
parent = parent.parent
}
return false
}
}

/**
Expand Down
38 changes: 1 addition & 37 deletions lib/rules/no-unused-properties.js
Expand Up @@ -79,49 +79,13 @@ const PROPERTY_LABEL = {
// Helpers
// ------------------------------------------------------------------------------

/**
* Find the variable of a given name.
* @param {RuleContext} context The rule context
* @param {Identifier} node The variable name to find.
* @returns {Variable|null} The found variable or null.
*/
function findVariable(context, node) {
return eslintUtils.findVariable(getScope(context, node), node)
}
/**
* Gets the scope for the current node
* @param {RuleContext} context The rule context
* @param {ESNode} currentNode The node to get the scope of
* @returns { import('eslint').Scope.Scope } The scope information for this node
*/
function getScope(context, currentNode) {
// On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
const inner = currentNode.type !== 'Program'
const scopeManager = context.getSourceCode().scopeManager

/** @type {ESNode | null} */
let node = currentNode
for (; node; node = /** @type {ESNode | null} */ (node.parent)) {
const scope = scopeManager.acquire(node, inner)

if (scope) {
if (scope.type === 'function-expression-name') {
return scope.childScopes[0]
}
return scope
}
}

return scopeManager.scopes[0]
}

/**
* @param {RuleContext} context
* @param {Identifier} id
* @returns {Expression}
*/
function findExpression(context, id) {
const variable = findVariable(context, id)
const variable = utils.findVariableByIdentifier(context, id)
if (!variable) {
return id
}
Expand Down

0 comments on commit 00c3b99

Please sign in to comment.