Skip to content

Commit cdc5bb9

Browse files
Merge pull request #471 from Gillespie59/development
2.4.0
2 parents 1d1394e + 7db701b commit cdc5bb9

File tree

6 files changed

+56
-33
lines changed

6 files changed

+56
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-angular",
3-
"version": "2.3.0",
3+
"version": "2.4.0",
44
"description": "ESLint rules for AngularJS projects",
55
"main": "index.js",
66
"scripts": {

rules/definedundefined.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,30 @@
1010
*/
1111
'use strict';
1212

13-
var utils = require('./utils/utils');
14-
13+
const utils = require('./utils/utils');
14+
const SHOULD_USE_ISDEFINED_OR_ISUNDEFINED = 'You should not use directly the "undefined" keyword. Prefer ' +
15+
'angular.isUndefined or angular.isDefined';
16+
const SHOULD_NOT_USE_BANG_WITH_ISDEFINED = 'Instead of !angular.isDefined, you can use the out-of-box angular.isUndefined method';
17+
const SHOULD_NOT_USE_BANG_WITH_ISUNDEFINED = 'Instead of !angular.isUndefined, you can use the out-of-box angular.isDefined method';
1518

1619
module.exports = {
1720
meta: {
18-
schema: []
21+
schema: [],
22+
fixable: 'code'
1923
},
2024
create: function(context) {
2125
function isCompareOperator(operator) {
2226
return operator === '===' || operator === '!==' || operator === '==' || operator === '!=';
2327
}
24-
function reportError(node) {
25-
context.report(node, 'You should not use directly the "undefined" keyword. Prefer ' +
26-
'angular.isUndefined or angular.isDefined', {});
28+
function reportError(node, message, fix) {
29+
context.report({
30+
node,
31+
message,
32+
fix
33+
});
2734
}
35+
var sourceCode = context.getSourceCode();
36+
2837
/**
2938
* Rule that check if we use angular.is(Un)defined() instead of the undefined keyword
3039
*/
@@ -35,22 +44,24 @@ module.exports = {
3544
node.parent.parent !== undefined &&
3645
node.parent.parent.operator === '!') {
3746
if (node.property.name === 'isDefined') {
38-
context.report(node, 'Instead of !angular.isDefined, you can use the out-of-box angular.isUndefined method', {});
47+
reportError(node, SHOULD_NOT_USE_BANG_WITH_ISDEFINED, fixer => fixer.replaceText(node.parent.parent, `angular.isUndefined(${node.parent.arguments[0].name})`));
3948
} else if (node.property.name === 'isUndefined') {
40-
context.report(node, 'Instead of !angular.isUndefined, you can use the out-of-box angular.isDefined method', {});
49+
reportError(node, SHOULD_NOT_USE_BANG_WITH_ISUNDEFINED, fixer => fixer.replaceText(node.parent.parent, `angular.isDefined(${node.parent.arguments[0].name})`));
4150
}
4251
}
4352
},
4453
BinaryExpression: function(node) {
4554
if (isCompareOperator(node.operator)) {
55+
let method = (node.operator === '!=' || node.operator === '!==') ? 'isDefined' : 'isUndefined';
56+
4657
if (utils.isTypeOfStatement(node.left) && node.right.value === 'undefined') {
47-
reportError(node);
58+
reportError(node, SHOULD_USE_ISDEFINED_OR_ISUNDEFINED, fixer => fixer.replaceText(node, `angular.${method}(${sourceCode.getText(node.left)})`));
4859
} else if (utils.isTypeOfStatement(node.right) && node.left.value === 'undefined') {
49-
reportError(node);
60+
reportError(node, SHOULD_USE_ISDEFINED_OR_ISUNDEFINED, fixer => fixer.replaceText(node, `angular.${method}(${sourceCode.getText(node.right)})`));
5061
} else if (node.left.type === 'Identifier' && node.left.name === 'undefined') {
51-
reportError(node);
62+
reportError(node, SHOULD_USE_ISDEFINED_OR_ISUNDEFINED, fixer => fixer.replaceText(node, `angular.${method}(${node.right.name})`));
5263
} else if (node.right.type === 'Identifier' && node.right.name === 'undefined') {
53-
reportError(node);
64+
reportError(node, SHOULD_USE_ISDEFINED_OR_ISUNDEFINED, fixer => fixer.replaceText(node, `angular.${method}(${node.left.name})`));
5465
}
5566
}
5667
}

rules/no-run-logic.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ module.exports = {
4242
return report(statement);
4343
}
4444
var expression = statement.expression;
45+
46+
/**
47+
* issue #466
48+
*/
49+
if (expression.type === 'Literal' && expression.value.indexOf('use strict') >= 0) {
50+
return;
51+
}
52+
4553
if (expression.type !== 'CallExpression') {
4654
return report(statement);
4755
}
@@ -52,7 +60,7 @@ module.exports = {
5260
return context.report(expression, 'Run function call expressions may not take any arguments');
5361
}
5462
expression.arguments.forEach(function(argument) {
55-
if (argument.type !== 'Literal' && argument.type !== 'Identifier') {
63+
if (argument && argument.type !== 'Literal' && argument.type !== 'Identifier') {
5664
context.report(argument, 'Run function call expressions may only take simple arguments');
5765
}
5866
});

test/definedundefined.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
// Requirements
55
// ------------------------------------------------------------------------------
66

7-
var rule = require('../rules/definedundefined');
8-
var RuleTester = require('eslint').RuleTester;
9-
var commonFalsePositives = require('./utils/commonFalsePositives');
7+
const rule = require('../rules/definedundefined');
8+
const RuleTester = require('eslint').RuleTester;
9+
const commonFalsePositives = require('./utils/commonFalsePositives');
1010

1111
// ------------------------------------------------------------------------------
1212
// Tests
1313
// ------------------------------------------------------------------------------
1414

15-
var eslintTester = new RuleTester();
15+
const eslintTester = new RuleTester();
1616
eslintTester.run('definedundefined', rule, {
1717
valid: [
1818
'angular.isUndefined(toto)',
@@ -24,19 +24,19 @@ eslintTester.run('definedundefined', rule, {
2424
'angular.isString(null)'
2525
].concat(commonFalsePositives),
2626
invalid: [
27-
{code: 'variable === undefined', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
28-
{code: 'undefined === variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
29-
{code: 'undefined !== variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
30-
{code: 'variable !== undefined', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
31-
{code: 'variable == undefined', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
32-
{code: 'undefined == variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
33-
{code: 'undefined != variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
34-
{code: 'variable != undefined', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
35-
{code: 'typeof variable === "undefined"', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
36-
{code: 'typeof variable !== "undefined"', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
37-
{code: '"undefined" == typeof variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
38-
{code: '"undefined" != typeof variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
39-
{code: '!angular.isUndefined(variable)', errors: [{message: 'Instead of !angular.isUndefined, you can use the out-of-box angular.isDefined method'}]},
40-
{code: '!angular.isDefined(variable)', errors: [{message: 'Instead of !angular.isDefined, you can use the out-of-box angular.isUndefined method'}]}
27+
{code: 'variable === undefined', output: 'angular.isUndefined(variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
28+
{code: 'undefined === variable', output: 'angular.isUndefined(variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
29+
{code: 'undefined !== variable', output: 'angular.isDefined(variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
30+
{code: 'variable !== undefined', output: 'angular.isDefined(variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
31+
{code: 'variable == undefined', output: 'angular.isUndefined(variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
32+
{code: 'undefined == variable', output: 'angular.isUndefined(variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
33+
{code: 'undefined != variable', output: 'angular.isDefined(variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
34+
{code: 'variable != undefined', output: 'angular.isDefined(variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
35+
{code: 'typeof variable === "undefined"', output: 'angular.isUndefined(typeof variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
36+
{code: 'typeof variable !== "undefined"', output: 'angular.isDefined(typeof variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
37+
{code: '"undefined" == typeof variable', output: 'angular.isUndefined(typeof variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
38+
{code: '"undefined" != typeof variable', output: 'angular.isDefined(typeof variable)', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
39+
{code: '!angular.isUndefined(variable)', output: 'angular.isDefined(variable)', errors: [{message: 'Instead of !angular.isUndefined, you can use the out-of-box angular.isDefined method'}]},
40+
{code: '!angular.isDefined(variable)', output: 'angular.isUndefined(variable)', errors: [{message: 'Instead of !angular.isDefined, you can use the out-of-box angular.isUndefined method'}]}
4141
]
4242
});

test/factory-name.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ var invalid = [];
2424
valid.push({
2525
code: 'app.factory("eslintFactory", function() {});',
2626
options: ['eslint']
27+
}, {
28+
code: 'app.factory("eslintFactory", EslintFactory); class EslintFactory {}',
29+
options: ['eslint'],
30+
parserOptions: {ecmaVersion: 6}
2731
}, {
2832
code: 'app.factory("eslintFactory", function() {});',
2933
options: [/^eslint/]

test/no-run-logic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var RuleTester = require('eslint').RuleTester;
1414
var eslintTester = new RuleTester();
1515
eslintTester.run('no-run-logic', rule, {
1616
valid: [
17-
'\'use strict;\'',
17+
'angular.module("").run(function() { \'use strict;\' });',
1818
'angular.module("").run();',
1919
'angular.module("").run(function() {});',
2020
'angular.module("").run(function() {foo()});',

0 commit comments

Comments
 (0)