Skip to content

Commit

Permalink
fixed typeof check and add possible positives (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
tilmanschweitzer committed Dec 6, 2015
1 parent 7e70866 commit 8e36405
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions rules/definedundefined.js
Expand Up @@ -38,9 +38,9 @@ module.exports = function(context) {
},
BinaryExpression: function(node) {
if (isCompareOperator(node.operator)) {
if (utils.isTypeOfStatement(node.left) || utils.isToStringStatement(node.left)) {
if (utils.isTypeOfStatement(node.left) && node.right.value === 'undefined') {
reportError(node);
} else if (utils.isTypeOfStatement(node.right) || utils.isToStringStatement(node.right)) {
} else if (utils.isTypeOfStatement(node.right) && node.left.value === 'undefined') {
reportError(node);
} else if (node.left.type === 'Identifier' && node.left.name === 'undefined') {
reportError(node);
Expand Down
15 changes: 13 additions & 2 deletions test/definedundefined.js
Expand Up @@ -16,15 +16,26 @@ var eslintTester = new RuleTester();
eslintTester.run('definedundefined', rule, {
valid: [
'angular.isUndefined(toto)',
'angular.isDefined(toto)'
'angular.isDefined(toto)',
// possible false positives
'variable === otherValue',
'variable === null',
'variable > undefined',
'angular.isString(null)'
].concat(commonFalsePositives),
invalid: [
{code: 'variable === undefined', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'undefined === variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'undefined !== variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'variable !== undefined', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'typeof variable == "undefined"', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'variable == undefined', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'undefined == variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'undefined != variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'variable != undefined', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'typeof variable === "undefined"', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: 'typeof variable !== "undefined"', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: '"undefined" == typeof variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: '"undefined" != typeof variable', errors: [{message: 'You should not use directly the "undefined" keyword. Prefer angular.isUndefined or angular.isDefined'}]},
{code: '!angular.isUndefined(variable)', errors: [{message: 'Instead of !angular.isUndefined, you can use the out-of-box angular.isDefined method'}]},
{code: '!angular.isDefined(variable)', errors: [{message: 'Instead of !angular.isDefined, you can use the out-of-box angular.isUndefined method'}]}
]
Expand Down

0 comments on commit 8e36405

Please sign in to comment.