Slight rule rework

This commit is contained in:
Tim Schaub
2016-08-08 21:57:06 -06:00
parent 508750712a
commit 4778efb772
3 changed files with 26 additions and 31 deletions

View File

@@ -2,25 +2,6 @@
const util = require('./util'); 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 = { exports.rule = {
meta: { meta: {
docs: { docs: {
@@ -48,20 +29,15 @@ exports.rule = {
} }
const name = arg.value; const name = arg.value;
const ancestors = context.getAncestors();
const parent = ancestors[ancestors.length - 1];
if (!parent) {
return;
}
requireStatements[name] = statement; requireStatements[name] = statement;
} }
}, },
MemberExpression: function(node) { MemberExpression: function(node) {
const name = getName(node); const name = util.getName(node);
if (name in requireStatements) { 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) { if (!requiredAncestor) {
usedNames[name] = true; usedNames[name] = true;
} }
@@ -73,7 +49,8 @@ exports.rule = {
if (name in requireStatements) { if (name in requireStatements) {
const ancestors = context.getAncestors(); const ancestors = context.getAncestors();
if (ancestors.length && ancestors[0].type === 'MemberExpression') { 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) { if (!requiredAncestor) {
usedNames[name] = true; usedNames[name] = true;
} }

View File

@@ -28,3 +28,22 @@ exports.isRequireExpression = function(node) {
exports.isRequireStatement = function(node) { exports.isRequireStatement = function(node) {
return isGoogStatement(node, 'require'); 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}`;
};

View File

@@ -13,13 +13,12 @@ exports.rule = {
return { return {
CallExpression: function(expression) { CallExpression: function(expression) {
if (util.isRequireExpression(expression)) { if (util.isRequireExpression(expression)) {
const ancestors = context.getAncestors(); const parent = expression.parent;
const parent = ancestors[ancestors.length - 1];
if (parent.type !== 'ExpressionStatement') { if (parent.type !== 'ExpressionStatement') {
return context.report(expression, 'Expected goog.require() to in an expression statement'); 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'); return context.report(expression, 'Expected goog.require() to be at the top level');
} }