diff --git a/package-lock.json b/package-lock.json index 4c4ebf7..172f5ad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-angular", - "version": "4.0.1", + "version": "4.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/rules/di.js b/rules/di.js index 1ccd6c8..283edb9 100644 --- a/rules/di.js +++ b/rules/di.js @@ -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 @@ -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'; @@ -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; } } @@ -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; } @@ -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; } } @@ -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) { @@ -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; } } @@ -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); } diff --git a/rules/service-name.js b/rules/service-name.js index df132e6..e7cedcc 100644 --- a/rules/service-name.js +++ b/rules/service-name.js @@ -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: { @@ -85,8 +60,6 @@ module.exports = { }] }, create: function(context) { - warnForDeprecatedBehavior(context.options); - return { CallExpression: function(node) {