diff --git a/lib/rules/no-unused-vars.js b/lib/rules/no-unused-vars.js index 4dc6dc2bab3..b4ba8361d44 100644 --- a/lib/rules/no-unused-vars.js +++ b/lib/rules/no-unused-vars.js @@ -308,6 +308,10 @@ module.exports = { function getRhsNode(ref, prevRhsNode) { const id = ref.identifier; const parent = id.parent; + + if (!parent) { + return null; + } const grandparent = parent.parent; const refScope = ref.from.variableScope; const varScope = ref.resolved.scope.variableScope; @@ -321,6 +325,9 @@ module.exports = { return prevRhsNode; } + if (!grandparent) { + return null; + } if (parent.type === "AssignmentExpression" && grandparent.type === "ExpressionStatement" && id === parent.left && @@ -415,8 +422,15 @@ module.exports = { function isReadForItself(ref, rhsNode) { const id = ref.identifier; const parent = id.parent; + + if (!parent) { + return false; + } const grandparent = parent.parent; + if (!grandparent) { + return false; + } return ref.isRead() && ( // self update. e.g. `a += 1`, `a++` @@ -444,6 +458,10 @@ module.exports = { function isForInRef(ref) { let target = ref.identifier.parent; + // For empty loop body + if (!target) { + return false; + } // "for (var ...) { return; }" if (target.type === "VariableDeclarator") { @@ -565,6 +583,10 @@ module.exports = { if (type === "Parameter") { + if (!def.node.parent) { + continue; + } + // skip any setter argument if ((def.node.parent.type === "Property" || def.node.parent.type === "MethodDefinition") && def.node.parent.kind === "set") { continue;