Skip to content

Commit

Permalink
Slight rule rework
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Aug 12, 2016
1 parent 6c350de commit e00f09c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
33 changes: 5 additions & 28 deletions no-unused-requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
19 changes: 19 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
};
5 changes: 2 additions & 3 deletions valid-requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down

0 comments on commit e00f09c

Please sign in to comment.