Slight rule rework
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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}`;
|
||||
};
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user