Skip to content

Commit

Permalink
Only check references inside class definitions (fixes #1).
Browse files Browse the repository at this point in the history
  • Loading branch information
burnnat committed Jun 29, 2015
1 parent 97f61af commit 5b512ac
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 48 deletions.
101 changes: 53 additions & 48 deletions lib/rules/ext-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ var classdata = require("../classdata");
//------------------------------------------------------------------------------

module.exports = function(context) {

"use strict";

var classes = classdata.loadDefaults();
var calls = [];

var getPropertyKey = function(property) {
var key = property.key;

if (key.type === "Identifier") {
return key.name;
}
else if (key.type === "Literal") {
return "" + key.value;
}

return null;
};

var addDependency = function(typeName) {
calls[calls.length - 1].dependencies[classes.alternates[typeName] || typeName] = true;
};

var addReference = function(typeName) {
calls[calls.length - 1].references[classes.alternates[typeName] || typeName] = true;
};

var handleTypes = function(node, key, callback, options) {
if (options.string && node.type === "Literal" && typeof node.value === "string") {
callback(node.value);
Expand All @@ -49,7 +49,7 @@ module.exports = function(context) {
else if (options.objectkey && node.type === "ObjectExpression") {
node.properties.forEach(function(prop) {
var subkey = getPropertyKey(prop);

if (subkey === options.objectkey) {
handleTypes(prop.value, key, callback, options);
}
Expand All @@ -66,76 +66,81 @@ module.exports = function(context) {
);
}
};

var parseReference = function(node) {
if (node.type === "Identifier") {
return node.name;
}
else if (node.type === "MemberExpression") {
var obj = parseReference(node.object);
var prop = parseReference(node.property);

if (obj && prop) {
return obj + "." + prop;
}
}

return null;
};


return {

"Program": function() {
classes.loadScope(context.getScope());
},

"MemberExpression": function(node) {
if (calls.length < 1) {
// Don't include top-level references without a wrapping class.
return;
}

var ancestors = context.getAncestors();
var parent = ancestors[ancestors.length - 1];

if (parent.type !== "MemberExpression") {
var member = parseReference(node);

if (member) {
var cls = classes.getClassName(member);

if (cls) {
addReference(cls);
}
}
}
},

"Property": function(node) {
var key = getPropertyKey(node);

switch (key) {
case "xtype":
case "items":
key = "widget";
break;

case "defaultReaderType":
key = "reader";
break;

case "defaultWriterType":
key = "writer";
break;
}

var aliases = classes.aliases[key];

if (aliases) {
var addAliasReference = function(type) {
var cls = aliases[type];

if (cls) {
addReference(cls);
}
};

handleTypes(
node.value,
key,
Expand All @@ -153,52 +158,52 @@ module.exports = function(context) {
);
}
},

"CallExpression": function(node) {
var info = {
node: node,
references: {}
};

calls.push(info);

var callee = node.callee;
var method = callee.property
? callee.property.name
: null;

var args = node.arguments;
var arg;

if (callee.type === "MemberExpression" && callee.object.name === "Ext") {
if (method === "create" && args.length > 0) {
arg = node.arguments[0];

if (arg.type === "Literal" && typeof arg.value === "string") {
addReference(arg.value);
}
}
else if (method === "define" && args.length > 1) {
arg = node.arguments[0];

if (arg.type === "Literal" && typeof arg.value === "string") {
info.name = arg.value;
}

arg = node.arguments[1];

if (arg.type === "ObjectExpression") {
info.dependencies = {};

arg.properties.forEach(function(prop) {
var key = getPropertyKey(prop);

var handler;
var options;

if (key === "requires" || key === "uses") {
handler = addDependency;

options = {
string: true,
array: true
Expand All @@ -209,10 +214,10 @@ module.exports = function(context) {
addDependency(type);
addReference(type);
};

options = { string: true };
}

if (handler) {
options.strict = true;
handleTypes(prop.value, key, handler, options);
Expand All @@ -222,13 +227,13 @@ module.exports = function(context) {
}
}
},

"CallExpression:exit": function() {
var call = calls.pop();

var deps = call.dependencies;
var refs = call.references;

if (deps) {
Object.keys(refs).forEach(function(name) {
if (refs[name] && !deps[name] && !classes.foundation[name]) {
Expand All @@ -242,7 +247,7 @@ module.exports = function(context) {
);
}
});

Object.keys(deps).forEach(function(name) {
if (deps[name] && !refs[name]) {
context.report(
Expand All @@ -258,12 +263,12 @@ module.exports = function(context) {
}
else if (calls.length > 0) {
var parent = calls[calls.length - 1].references;

Object.keys(refs).forEach(function(name) {
parent[name] = parent[name] || refs[name];
});
}
}
};

};
14 changes: 14 additions & 0 deletions test/lib/rules/ext-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ var eslintTester = new ESLintTester(linter);

eslintTester.addRuleTest("lib/rules/ext-deps", {
valid: [
{
code: "Ext.require('App.Class');",
global: {
Ext: true,
App: true
}
},
{
code: "Ext.util.History.back();",
global: {
Ext: true,
App: true
}
},
{
code: "Ext.define('App.Class', { requires: ['Ext.panel.Panel'], constructor: function() { this.panel = new Ext.panel.Panel(); } });",
global: {
Expand Down

0 comments on commit 5b512ac

Please sign in to comment.