diff --git a/no-unused-requires.js b/no-unused-requires.js index 2436bb4..13ba97c 100644 --- a/no-unused-requires.js +++ b/no-unused-requires.js @@ -2,25 +2,6 @@ const util = require('./util'); -function getName(node) { - if (node.type !== 'MemberExpression') { - return; - } - if (node.property.type !== 'Identifier' || node.property.computed) { - return; - } - let objectName; - if (node.object.type === 'Identifier' && !node.object.computed) { - objectName = node.object.name; - } else if (node.object.type === 'MemberExpression' && !node.object.computed) { - objectName = getName(node.object); - } - if (!objectName) { - return; - } - return `${objectName}.${node.property.name}`; -} - exports.rule = { meta: { docs: { @@ -48,20 +29,15 @@ exports.rule = { } const name = arg.value; - const ancestors = context.getAncestors(); - const parent = ancestors[ancestors.length - 1]; - if (!parent) { - return; - } - requireStatements[name] = statement; } }, MemberExpression: function(node) { - const name = getName(node); + const name = util.getName(node); if (name in requireStatements) { - const requiredAncestor = context.getAncestors().some(ancestorNode => !!requireStatements[getName(ancestorNode)]); + const requiredAncestor = context.getAncestors().some( + ancestorNode => !!requireStatements[util.getName(ancestorNode)]); if (!requiredAncestor) { usedNames[name] = true; } @@ -73,7 +49,8 @@ exports.rule = { if (name in requireStatements) { const ancestors = context.getAncestors(); if (ancestors.length && ancestors[0].type === 'MemberExpression') { - const requiredAncestor = context.getAncestors().some(ancestorNode => !!requireStatements[getName(ancestorNode)]); + const requiredAncestor = ancestors.some( + ancestorNode => !!requireStatements[util.getName(ancestorNode)]); if (!requiredAncestor) { usedNames[name] = true; } diff --git a/util.js b/util.js index 605a5d7..a7c163b 100644 --- a/util.js +++ b/util.js @@ -28,3 +28,22 @@ exports.isRequireExpression = function(node) { exports.isRequireStatement = function(node) { return isGoogStatement(node, 'require'); }; + +var getName = exports.getName = function(node) { + if (node.type !== 'MemberExpression') { + return; + } + if (node.property.type !== 'Identifier' || node.property.computed) { + return; + } + let objectName; + if (node.object.type === 'Identifier' && !node.object.computed) { + objectName = node.object.name; + } else if (node.object.type === 'MemberExpression' && !node.object.computed) { + objectName = getName(node.object); + } + if (!objectName) { + return; + } + return `${objectName}.${node.property.name}`; +}; diff --git a/valid-requires.js b/valid-requires.js index e98a4e0..3643abc 100644 --- a/valid-requires.js +++ b/valid-requires.js @@ -13,13 +13,12 @@ exports.rule = { return { CallExpression: function(expression) { if (util.isRequireExpression(expression)) { - const ancestors = context.getAncestors(); - const parent = ancestors[ancestors.length - 1]; + const parent = expression.parent; if (parent.type !== 'ExpressionStatement') { return context.report(expression, 'Expected goog.require() to in an expression statement'); } - if (ancestors.length !== 2) { + if (parent.parent.type !== 'Program') { return context.report(expression, 'Expected goog.require() to be at the top level'); }