Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DI for angular?component #598

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 110 additions & 29 deletions rules/di.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* require a consistent DI syntax
*
* All your DI should use the same syntax : the Array, function, or $inject syntaxes ("di": [2, "array, function, or $inject"])
* All your DI should use the same syntax : the Array, function, or $inject syntaxes ('di': [2, 'array, function, or $inject'])
*
* @version 0.1.0
* @category conventions
Expand All @@ -13,29 +13,28 @@ var utils = require('./utils/utils');

var angularRule = require('./utils/angular-rule');


module.exports = {
meta: {
docs: {
url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/di.md'
url:
'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/di.md'
},
schema: [{
enum: [
'function',
'array',
'$inject'
]
}, {
type: 'object',
properties: {
matchNames: {
type: 'boolean'
},
stripUnderscores: {
type: 'boolean'
schema: [
{
enum: ['function', 'array', '$inject']
},
{
type: 'object',
properties: {
matchNames: {
type: 'boolean'
},
stripUnderscores: {
type: 'boolean'
}
}
}
}]
]
},
create: angularRule(function(context) {
var syntax = context.options[0] || 'function';
Expand All @@ -45,17 +44,27 @@ module.exports = {
var stripUnderscores = extra.stripUnderscores === true;

function report(node) {
context.report(node, 'You should use the {{syntax}} syntax for DI', {
syntax: syntax
});
context.report(
node,
'You should use the {{syntax}} syntax for DI',
{
syntax: syntax
}
);
}

var $injectProperties = {};

function maybeNoteInjection(node) {
if (syntax === '$inject' && node.left && node.left.property &&
((utils.isLiteralType(node.left.property) && node.left.property.value === '$inject') ||
(utils.isIdentifierType(node.left.property) && node.left.property.name === '$inject'))) {
if (
syntax === '$inject' &&
node.left &&
node.left.property &&
((utils.isLiteralType(node.left.property) &&
node.left.property.value === '$inject') ||
(utils.isIdentifierType(node.left.property) &&
node.left.property.name === '$inject'))
) {
$injectProperties[node.left.object.name] = node.right;
}
}
Expand All @@ -74,7 +83,11 @@ module.exports = {
if (syntax === 'array') {
if (utils.isArrayType(fn.parent)) {
if (fn.parent.elements.length - 1 !== fn.params.length) {
context.report(fn, 'The signature of the method is incorrect', {});
context.report(
fn,
'The signature of the method is incorrect',
{}
);
return;
}

Expand All @@ -89,7 +102,11 @@ module.exports = {
return name !== fn.parent.elements[i].value;
});
if (invalidArray.length > 0) {
context.report(fn, 'You have an error in your DI configuration. Each items of the array should match exactly one function parameter', {});
context.report(
fn,
'You have an error in your DI configuration. Each items of the array should match exactly one function parameter',
{}
);
return;
}
}
Expand All @@ -113,12 +130,19 @@ module.exports = {

if ($injectArray && utils.isArrayType($injectArray)) {
if ($injectArray.elements.length !== fn.params.length) {
context.report(fn, 'The signature of the method is incorrect', {});
context.report(
fn,
'The signature of the method is incorrect',
{}
);
return;
}

if (matchNames) {
var invalidInjectArray = fn.params.filter(function(e, i) {
var invalidInjectArray = fn.params.filter(function(
e,
i
) {
var name = e.name;

if (stripUnderscores) {
Expand All @@ -128,7 +152,11 @@ module.exports = {
return name !== $injectArray.elements[i].value;
});
if (invalidInjectArray.length > 0) {
context.report(fn, 'You have an error in your DI configuration. Each items of the array should match exactly one function parameter', {});
context.report(
fn,
'You have an error in your DI configuration. Each items of the array should match exactly one function parameter',
{}
);
return;
}
}
Expand All @@ -155,6 +183,59 @@ module.exports = {
checkDi(null, providerFn);
checkDi(null, $get);
},
'angular?component': function(callee) {
var routeObject = callee.parent.arguments[1];

var elementLength = 0;
var elements = null;
var parameters = null;

if (routeObject.properties) {
routeObject.properties.forEach(function(prop) {
if (prop.key.name === 'controller') {
elements = prop.value.elements;
if (elements.length) {
elementLength = elements.length - 1;
}
prop.value.elements.forEach(function(element) {
if (element.type === 'FunctionExpression') {
parameters = element.params;
}
});
}
});
}

if (elementLength !== parameters.length) {
context.report(
routeObject,
'The signature of the method is incorrect',
{}
);
return;
}

if (matchNames) {
var invalidArray = parameters.filter(function(e, i) {
var name = e.name;

if (stripUnderscores) {
name = normalizeParameter(name);
}

return name !== elements[i].value;
});

if (invalidArray.length > 0) {
context.report(
routeObject,
'You have an error in your DI configuration. Each items of the array should match exactly one function parameter',
{}
);
return;
}
}
},
AssignmentExpression: function(node) {
maybeNoteInjection(node);
}
Expand Down
27 changes: 0 additions & 27 deletions rules/service-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,6 @@ function getConfig(options) {
return config;
}

/**
* Used only by `ForDeprecatedBehavior()` for making sure it was run only one time
* @type {boolean}
*/
var didWarnForDeprecatedBehavior = false;

/**
* Warn if API is deprecated
* @param {Array.<*>} options
*/
function warnForDeprecatedBehavior(options) {
if (didWarnForDeprecatedBehavior) {
return;
}
didWarnForDeprecatedBehavior = true;

var config = getConfig(options);

/* istanbul ignore if */
if (config.oldBehavior) {
// eslint-disable-next-line
console.warn('The rule `angular/service-name` will be split up to different rules in the next version. Please read the docs for more information');
}
}

module.exports = {
meta: {
docs: {
Expand All @@ -85,8 +60,6 @@ module.exports = {
}]
},
create: function(context) {
warnForDeprecatedBehavior(context.options);

return {

CallExpression: function(node) {
Expand Down