@roadmunk/eslint-plugin-roadmunk-custom
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -1,5 +0,5 @@ | ||
"use strict"; | ||
'use strict'; | ||
module.exports = { | ||
/** | ||
/** | ||
* node : The node on which lint rule has fired. (MemberExpression) | ||
@@ -10,4 +10,4 @@ * context : context object | ||
*/ | ||
lodashAutofix : function (node, context, type, fixer) { | ||
/** | ||
lodashAutofix : function(node, context, type, fixer) { | ||
/** | ||
* node is MemberExpression (_.isNull or _.isUndefined) | ||
@@ -17,51 +17,51 @@ * node.parent is CallExpression (_.isNull() or _.isUndefined()) | ||
//The scope to which auto fix will be applied. Most of the times, this will be the call expression | ||
//unless it's preceded by UnaryExpression(s) | ||
let scope = node.parent; | ||
// The scope to which auto fix will be applied. Most of the times, this will be the call expression | ||
// unless it's preceded by UnaryExpression(s) | ||
let scope = node.parent; | ||
// If initial scope isn't a call expression something isn't right. | ||
// Shouldn't happen but let's be defensive | ||
// Also, if more than one argument was passed to isNull then don't attempt to autofix | ||
if (scope.type !== 'CallExpression' || scope.arguments.length > 1) { | ||
return; | ||
} | ||
// If initial scope isn't a call expression something isn't right. | ||
// Shouldn't happen but let's be defensive | ||
// Also, if more than one argument was passed to isNull then don't attempt to autofix | ||
if (scope.type !== 'CallExpression' || scope.arguments.length > 1) { | ||
return; | ||
} | ||
// Grab the argument that was passed to the isNull function | ||
const arg = scope.arguments[0]; | ||
let negate = false; | ||
// Grab the argument that was passed to the isNull function | ||
const arg = scope.arguments[0]; | ||
let negate = false; | ||
// Call expression's parent influences the way autofixing works. | ||
// If it is a UnaryExpression with operator ! then our fix has to check for inequality | ||
// Unless it is preceded by another ! operator | ||
if (isUnaryExpression(scope, '!')) { | ||
negate = !negate; | ||
scope = scope.parent; | ||
} | ||
// Call expression's parent influences the way autofixing works. | ||
// If it is a UnaryExpression with operator ! then our fix has to check for inequality | ||
// Unless it is preceded by another ! operator | ||
if (isUnaryExpression(scope, '!')) { | ||
negate = !negate; | ||
scope = scope.parent; | ||
} | ||
if (isUnaryExpression(scope, '!')) { | ||
negate = !negate; | ||
scope = scope.parent; | ||
} | ||
if (isUnaryExpression(scope, '!')) { | ||
negate = !negate; | ||
scope = scope.parent; | ||
} | ||
const fixToAppend = negate ? ` !== ${type}` : ` === ${type}`; | ||
const fixToAppend = negate ? ` !== ${type}` : ` === ${type}`; | ||
// Get an instance of `SourceCode` so we can convert the argument to source code | ||
// & append the fix to it | ||
const sourceCode = context.getSourceCode(); | ||
let fixedCode = sourceCode.getText(arg) + fixToAppend; | ||
// Get an instance of `SourceCode` so we can convert the argument to source code | ||
// & append the fix to it | ||
const sourceCode = context.getSourceCode(); | ||
let fixedCode = sourceCode.getText(arg) + fixToAppend; | ||
// If the scope's parent is a BinaryExpression (eg ===) | ||
// then wrap the fixedCode in brackets to avoid inadvertently changing the result | ||
// Eg: We don't want to turn `testVar === _.isNull(5)` to `testVar === 5 === null` | ||
// It should be turned into `testVar === (5 === null)` | ||
if (scope.parent.type === 'BinaryExpression') { | ||
fixedCode = `(${fixedCode})`; | ||
} | ||
// If the scope's parent is a BinaryExpression (eg ===) | ||
// then wrap the fixedCode in brackets to avoid inadvertently changing the result | ||
// Eg: We don't want to turn `testVar === _.isNull(5)` to `testVar === 5 === null` | ||
// It should be turned into `testVar === (5 === null)` | ||
if (scope.parent.type === 'BinaryExpression') { | ||
fixedCode = `(${fixedCode})`; | ||
} | ||
return fixer.replaceText(scope, fixedCode); | ||
} | ||
} | ||
return fixer.replaceText(scope, fixedCode); | ||
}, | ||
}; | ||
function isUnaryExpression(scope, operator) { | ||
return scope.parent.type === 'UnaryExpression' && scope.parent.operator === operator; | ||
} | ||
return scope.parent.type === 'UnaryExpression' && scope.parent.operator === operator; | ||
} |
@@ -5,19 +5,17 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
var requireIndex = require("requireindex"); | ||
const requireIndex = require('requireindex'); | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// Plugin Definition | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// import all rules in lib/rules | ||
module.exports.rules = requireIndex(__dirname + "/rules"); | ||
module.exports.rules = requireIndex(`${__dirname}/rules`); | ||
@@ -5,33 +5,32 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
const { lodashAutofix } = require('../helper.js'); | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
module.exports = { | ||
meta : { | ||
docs : { | ||
description : "Prevents the use of lodash's isNull method", | ||
category : "Best Practices", | ||
recommended : false | ||
}, | ||
fixable : 'code', | ||
schema : [], | ||
}, | ||
meta : { | ||
docs : { | ||
description : 'Prevents the use of lodash\'s isNull method', | ||
category : 'Best Practices', | ||
recommended : false, | ||
}, | ||
fixable : 'code', | ||
}, | ||
create : function(context) { | ||
return { | ||
MemberExpression(node) { | ||
if (node.object.name === "_" && node.property.name === "isNull") { | ||
context.report({ | ||
node, | ||
message : "Use native JavaScript to check for null", | ||
fix : lodashAutofix.bind(null, node, context, 'null'), | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
create : function(context) { | ||
return { | ||
MemberExpression(node) { | ||
if (node.object.name === '_' && node.property.name === 'isNull') { | ||
context.report({ | ||
node, | ||
message : 'Use native JavaScript to check for null', | ||
fix : lodashAutofix.bind(null, node, context, 'null'), | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
@@ -5,33 +5,32 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
const { lodashAutofix } = require('../helper.js'); | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
module.exports = { | ||
meta : { | ||
docs : { | ||
description : "Prevents the use of lodash's isUndefined method", | ||
category : "Best Practices", | ||
recommended : false | ||
}, | ||
fixable : 'code', | ||
schema : [], | ||
}, | ||
meta : { | ||
docs : { | ||
description : "Prevents the use of lodash's isUndefined method", | ||
category : 'Best Practices', | ||
recommended : false, | ||
}, | ||
fixable : 'code', | ||
}, | ||
create : function(context) { | ||
return { | ||
MemberExpression(node) { | ||
if (node.object.name === "_" && node.property.name === "isUndefined") { | ||
context.report({ | ||
node, | ||
message : "Use native JavaScript to check for undefined", | ||
fix : lodashAutofix.bind(null, node, context, 'undefined'), | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
create : function(context) { | ||
return { | ||
MemberExpression(node) { | ||
if (node.object.name === '_' && node.property.name === 'isUndefined') { | ||
context.report({ | ||
node, | ||
message : 'Use native JavaScript to check for undefined', | ||
fix : lodashAutofix.bind(null, node, context, 'undefined'), | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
{ | ||
"name": "@roadmunk/eslint-plugin-roadmunk-custom", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Plugin to hold custom ESLint rules for Roadmunk", | ||
@@ -13,2 +13,5 @@ "keywords": [ | ||
"scripts": { | ||
"lint": "eslint .", | ||
"lint:fix": "eslint . --fix", | ||
"pretest": "npm run lint", | ||
"test": "mocha tests --recursive" | ||
@@ -20,3 +23,4 @@ }, | ||
"devDependencies": { | ||
"eslint": "~3.9.1", | ||
"@roadmunk/eslint-config-roadmunk": "^3.0.2", | ||
"eslint": "^5.1.0", | ||
"mocha": "^5.2.0" | ||
@@ -23,0 +27,0 @@ }, |
@@ -50,5 +50,2 @@ # eslint-plugin-roadmunk-custom | ||
`no-align-assign` : Ensure that assignment statements are aligned |
@@ -5,395 +5,395 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
const rule = require("../../../lib/rules/no-lodash-isnull"); | ||
const RuleTester = require("eslint").RuleTester; | ||
const rule = require('../../../lib/rules/no-lodash-isnull'); | ||
const RuleTester = require('eslint').RuleTester; | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
const errors = [{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
}]; | ||
const errors = [ { | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
} ]; | ||
const ruleTester = new RuleTester(); | ||
ruleTester.run("no-lodash-isnull", rule, { | ||
ruleTester.run('no-lodash-isnull', rule, { | ||
valid: [ | ||
"valid = 5;", | ||
"valid = _.isNil(5);", | ||
"valid = 5 === null;", | ||
"test2 = function(opts) { return _.forEach([1, 2], function(v){ return v + 1}) }", | ||
valid : [ | ||
'valid = 5;', | ||
'valid = _.isNil(5);', | ||
'valid = 5 === null;', | ||
'test2 = function(opts) { return _.forEach([1, 2], function(v){ return v + 1}) }', | ||
"test = function() { if(test === 'something'){ return; } }", | ||
"obj = { method1 : function(){} }", | ||
"test = function() { return test ? 1 : 2 }", | ||
"test = true ? 1 : 2", | ||
"test = function(){ var test2 = {}; test2 === null ? 1 : 2 }", | ||
'obj = { method1 : function(){} }', | ||
'test = function() { return test ? 1 : 2 }', | ||
'test = true ? 1 : 2', | ||
'test = function(){ var test2 = {}; test2 === null ? 1 : 2 }', | ||
], | ||
invalid: [ | ||
//Simple tests with multiple data types | ||
invalid : [ | ||
// Simple tests with multiple data types | ||
{ | ||
code: "test = _.isNull(5)", | ||
code : 'test = _.isNull(5)', | ||
errors, | ||
output: "test = 5 === null", | ||
output : 'test = 5 === null', | ||
}, | ||
{ | ||
code: "test = !!_.isNull(5)", | ||
code : 'test = !!_.isNull(5)', | ||
errors, | ||
output: "test = 5 === null", | ||
output : 'test = 5 === null', | ||
}, | ||
{ | ||
code: "test = !_.isNull(5)", | ||
code : 'test = !_.isNull(5)', | ||
errors, | ||
output: "test = 5 !== null", | ||
output : 'test = 5 !== null', | ||
}, | ||
{ | ||
code: "test = _.isNull(5) ? 1 : 2", | ||
code : 'test = _.isNull(5) ? 1 : 2', | ||
errors, | ||
output: "test = 5 === null ? 1 : 2", | ||
output : 'test = 5 === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = !!_.isNull(5) ? 1 : 2", | ||
code : 'test = !!_.isNull(5) ? 1 : 2', | ||
errors, | ||
output: "test = 5 === null ? 1 : 2", | ||
output : 'test = 5 === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = !_.isNull(5) ? 1 : 2", | ||
code : 'test = !_.isNull(5) ? 1 : 2', | ||
errors, | ||
output: "test = 5 !== null ? 1 : 2", | ||
output : 'test = 5 !== null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = _.isNull('5')", | ||
code : "test = _.isNull('5')", | ||
errors, | ||
output: "test = '5' === null", | ||
output : "test = '5' === null", | ||
}, | ||
{ | ||
code: "test = _.isNull('') ? 1 : 2", | ||
code : "test = _.isNull('') ? 1 : 2", | ||
errors, | ||
output: "test = '' === null ? 1 : 2", | ||
output : "test = '' === null ? 1 : 2", | ||
}, | ||
{ | ||
code: "test = !_.isNull('') ? 1 : 2", | ||
code : "test = !_.isNull('') ? 1 : 2", | ||
errors, | ||
output: "test = '' !== null ? 1 : 2", | ||
output : "test = '' !== null ? 1 : 2", | ||
}, | ||
{ | ||
code: "test = _.isNull(true)", | ||
code : 'test = _.isNull(true)', | ||
errors, | ||
output: "test = true === null", | ||
output : 'test = true === null', | ||
}, | ||
{ | ||
code: "test = !_.isNull(true)", | ||
code : 'test = !_.isNull(true)', | ||
errors, | ||
output: "test = true !== null", | ||
output : 'test = true !== null', | ||
}, | ||
{ | ||
code: "test = _.isNull(false) ? 1 : 2", | ||
code : 'test = _.isNull(false) ? 1 : 2', | ||
errors, | ||
output: "test = false === null ? 1 : 2", | ||
output : 'test = false === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = !!_.isNull(false) ? 1 : 2", | ||
code : 'test = !!_.isNull(false) ? 1 : 2', | ||
errors, | ||
output: "test = false === null ? 1 : 2", | ||
output : 'test = false === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = _.isNull([])", | ||
code : 'test = _.isNull([])', | ||
errors, | ||
output: "test = [] === null", | ||
output : 'test = [] === null', | ||
}, | ||
{ | ||
code: "test = !_.isNull([])", | ||
code : 'test = !_.isNull([])', | ||
errors, | ||
output: "test = [] !== null", | ||
output : 'test = [] !== null', | ||
}, | ||
{ | ||
code: "test = _.isNull([1,2]) ? 1 : 2", | ||
code : 'test = _.isNull([1,2]) ? 1 : 2', | ||
errors, | ||
output: "test = [1,2] === null ? 1 : 2", | ||
output : 'test = [1,2] === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = !!_.isNull([1,2]) ? 1 : 2", | ||
code : 'test = !!_.isNull([1,2]) ? 1 : 2', | ||
errors, | ||
output: "test = [1,2] === null ? 1 : 2", | ||
output : 'test = [1,2] === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = _.isNull(function(){})", | ||
code : 'test = _.isNull(function(){})', | ||
errors, | ||
output: "test = function(){} === null", | ||
output : 'test = function(){} === null', | ||
}, | ||
{ | ||
code: "test = !_.isNull(function(){})", | ||
code : 'test = !_.isNull(function(){})', | ||
errors, | ||
output: "test = function(){} !== null", | ||
output : 'test = function(){} !== null', | ||
}, | ||
{ | ||
code: "test = _.isNull(function(){}) ? 1 : 2", | ||
code : 'test = _.isNull(function(){}) ? 1 : 2', | ||
errors, | ||
output: "test = function(){} === null ? 1 : 2", | ||
output : 'test = function(){} === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = !!_.isNull(function(){}) ? 1 : 2", | ||
code : 'test = !!_.isNull(function(){}) ? 1 : 2', | ||
errors, | ||
output: "test = function(){} === null ? 1 : 2", | ||
output : 'test = function(){} === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "test = _.isNull(null)", | ||
code : 'test = _.isNull(null)', | ||
errors, | ||
output: "test = null === null", | ||
output : 'test = null === null', | ||
}, | ||
{ | ||
code: "test = !_.isNull(null)", | ||
code : 'test = !_.isNull(null)', | ||
errors, | ||
output: "test = null !== null", | ||
output : 'test = null !== null', | ||
}, | ||
{ | ||
code: "var condition = null; test = _.isNull(condition) ? 1 : 2", | ||
code : 'var condition = null; test = _.isNull(condition) ? 1 : 2', | ||
errors, | ||
output: "var condition = null; test = condition === null ? 1 : 2", | ||
output : 'var condition = null; test = condition === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "var condition = null; test = !_.isNull(condition) ? 1 : 2", | ||
code : 'var condition = null; test = !_.isNull(condition) ? 1 : 2', | ||
errors, | ||
output: "var condition = null; test = condition !== null ? 1 : 2", | ||
output : 'var condition = null; test = condition !== null ? 1 : 2', | ||
}, | ||
{ | ||
code: "var condition = {}; test = _.isNull(condition) ? 1 : 2", | ||
code : 'var condition = {}; test = _.isNull(condition) ? 1 : 2', | ||
errors, | ||
output: "var condition = {}; test = condition === null ? 1 : 2", | ||
output : 'var condition = {}; test = condition === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "var condition = {}; test = !!_.isNull(condition) ? 1 : 2", | ||
code : 'var condition = {}; test = !!_.isNull(condition) ? 1 : 2', | ||
errors, | ||
output: "var condition = {}; test = condition === null ? 1 : 2", | ||
output : 'var condition = {}; test = condition === null ? 1 : 2', | ||
}, | ||
{ | ||
code: "var condition = function(){}; test = _.isNull(condition) ? 1 : 2", | ||
code : 'var condition = function(){}; test = _.isNull(condition) ? 1 : 2', | ||
errors, | ||
output: "var condition = function(){}; test = condition === null ? 1 : 2", | ||
output : 'var condition = function(){}; test = condition === null ? 1 : 2', | ||
}, | ||
//Testing object methods | ||
// Testing object methods | ||
{ | ||
code: "obj = { method1 : function(){ _.isNull(5) ? 1 : 2 } }", | ||
code : 'obj = { method1 : function(){ _.isNull(5) ? 1 : 2 } }', | ||
errors, | ||
output: "obj = { method1 : function(){ 5 === null ? 1 : 2 } }", | ||
output : 'obj = { method1 : function(){ 5 === null ? 1 : 2 } }', | ||
}, | ||
{ | ||
code: "obj = { method1 : function(){ !!_.isNull(5) ? 1 : 2 } }", | ||
code : 'obj = { method1 : function(){ !!_.isNull(5) ? 1 : 2 } }', | ||
errors, | ||
output: "obj = { method1 : function(){ 5 === null ? 1 : 2 } }", | ||
output : 'obj = { method1 : function(){ 5 === null ? 1 : 2 } }', | ||
}, | ||
{ | ||
code: "obj = { method1 : function(){ !_.isNull(5) ? 1 : 2 } }", | ||
code : 'obj = { method1 : function(){ !_.isNull(5) ? 1 : 2 } }', | ||
errors, | ||
output: "obj = { method1 : function(){ 5 !== null ? 1 : 2 } }", | ||
output : 'obj = { method1 : function(){ 5 !== null ? 1 : 2 } }', | ||
}, | ||
{ | ||
code: "obj = { method1 : function(){ _.isNull('5') ? 1 : 2 } }", | ||
code : "obj = { method1 : function(){ _.isNull('5') ? 1 : 2 } }", | ||
errors, | ||
output: "obj = { method1 : function(){ '5' === null ? 1 : 2 } }", | ||
output : "obj = { method1 : function(){ '5' === null ? 1 : 2 } }", | ||
}, | ||
{ | ||
code: "obj = { method1 : function(){ _.isNull([1,2]) ? 1 : 2 } }", | ||
code : 'obj = { method1 : function(){ _.isNull([1,2]) ? 1 : 2 } }', | ||
errors, | ||
output: "obj = { method1 : function(){ [1,2] === null ? 1 : 2 } }", | ||
output : 'obj = { method1 : function(){ [1,2] === null ? 1 : 2 } }', | ||
}, | ||
{ | ||
code: "obj = { method1: null, method2 : function(){ _.isNull(this.method1) ? 1 : 2 } }", | ||
code : 'obj = { method1: null, method2 : function(){ _.isNull(this.method1) ? 1 : 2 } }', | ||
errors, | ||
output: "obj = { method1: null, method2 : function(){ this.method1 === null ? 1 : 2 } }", | ||
output : 'obj = { method1: null, method2 : function(){ this.method1 === null ? 1 : 2 } }', | ||
}, | ||
{ | ||
code: "obj = { method1: null, method2 : function(){ !_.isNull(this.method1) ? 1 : 2 } }", | ||
code : 'obj = { method1: null, method2 : function(){ !_.isNull(this.method1) ? 1 : 2 } }', | ||
errors, | ||
output: "obj = { method1: null, method2 : function(){ this.method1 !== null ? 1 : 2 } }", | ||
output : 'obj = { method1: null, method2 : function(){ this.method1 !== null ? 1 : 2 } }', | ||
}, | ||
{ | ||
code: "obj = { method1: null, method2 : function(){ var val = _.isNull(this.method1) ? 1 : 2; var val2 = null; } }", | ||
code : 'obj = { method1: null, method2 : function(){ var val = _.isNull(this.method1) ? 1 : 2; var val2 = null; } }', | ||
errors, | ||
output: "obj = { method1: null, method2 : function(){ var val = this.method1 === null ? 1 : 2; var val2 = null; } }", | ||
output : 'obj = { method1: null, method2 : function(){ var val = this.method1 === null ? 1 : 2; var val2 = null; } }', | ||
}, | ||
{ | ||
code: "obj = { method1: null, method2 : function(){ var val = !!_.isNull(this.method1) ? 1 : 2; var val2 = null; } }", | ||
code : 'obj = { method1: null, method2 : function(){ var val = !!_.isNull(this.method1) ? 1 : 2; var val2 = null; } }', | ||
errors, | ||
output: "obj = { method1: null, method2 : function(){ var val = this.method1 === null ? 1 : 2; var val2 = null; } }", | ||
output : 'obj = { method1: null, method2 : function(){ var val = this.method1 === null ? 1 : 2; var val2 = null; } }', | ||
}, | ||
{ | ||
code: "obj = { outer : { inner : function(arg){ return _.isNull(arg); } } }", | ||
code : 'obj = { outer : { inner : function(arg){ return _.isNull(arg); } } }', | ||
errors, | ||
output: "obj = { outer : { inner : function(arg){ return arg === null; } } }", | ||
output : 'obj = { outer : { inner : function(arg){ return arg === null; } } }', | ||
}, | ||
//Testing functions | ||
// Testing functions | ||
{ | ||
code: "test2 = function(opt) { return _.isNull(opt) }", | ||
code : 'test2 = function(opt) { return _.isNull(opt) }', | ||
errors, | ||
output: "test2 = function(opt) { return opt === null }" | ||
output : 'test2 = function(opt) { return opt === null }', | ||
}, | ||
{ | ||
code: "test2 = function() { if(_.isNull(opt)) { return false; } }", | ||
code : 'test2 = function() { if(_.isNull(opt)) { return false; } }', | ||
errors, | ||
output: "test2 = function() { if(opt === null) { return false; } }", | ||
output : 'test2 = function() { if(opt === null) { return false; } }', | ||
}, | ||
{ | ||
code: "test2 = function() { return function(){ if(_.isNull(opt)) { return false; } } }", | ||
code : 'test2 = function() { return function(){ if(_.isNull(opt)) { return false; } } }', | ||
errors, | ||
output: "test2 = function() { return function(){ if(opt === null) { return false; } } }", | ||
output : 'test2 = function() { return function(){ if(opt === null) { return false; } } }', | ||
}, | ||
{ | ||
code: "test = function(){ var test2 = {}; _.isNull(test2) ? 1 : 2 }", | ||
code : 'test = function(){ var test2 = {}; _.isNull(test2) ? 1 : 2 }', | ||
errors, | ||
output: "test = function(){ var test2 = {}; test2 === null ? 1 : 2 }" | ||
output : 'test = function(){ var test2 = {}; test2 === null ? 1 : 2 }', | ||
}, | ||
{ | ||
code: "test = function(test2){ if(_.isNull(test2)) { return 1 } }", | ||
code : 'test = function(test2){ if(_.isNull(test2)) { return 1 } }', | ||
errors, | ||
output: "test = function(test2){ if(test2 === null) { return 1 } }", | ||
output : 'test = function(test2){ if(test2 === null) { return 1 } }', | ||
}, | ||
{ | ||
code: "test = function(){ if(_.isNull([])) { return 1 } }", | ||
code : 'test = function(){ if(_.isNull([])) { return 1 } }', | ||
errors, | ||
output: "test = function(){ if([] === null) { return 1 } }", | ||
output : 'test = function(){ if([] === null) { return 1 } }', | ||
}, | ||
// Mixing in logical & binary expressions | ||
{ | ||
code: "test = testVar === _.isNull(5)", | ||
code : 'test = testVar === _.isNull(5)', | ||
errors, | ||
output: "test = testVar === (5 === null)", | ||
output : 'test = testVar === (5 === null)', | ||
}, | ||
{ | ||
code: "test = testVar !== _.isNull(5)", | ||
code : 'test = testVar !== _.isNull(5)', | ||
errors, | ||
output: "test = testVar !== (5 === null)", | ||
output : 'test = testVar !== (5 === null)', | ||
}, | ||
{ | ||
code: "test = testVar !== !_.isNull(5)", | ||
code : 'test = testVar !== !_.isNull(5)', | ||
errors, | ||
output: "test = testVar !== (5 !== null)", | ||
output : 'test = testVar !== (5 !== null)', | ||
}, | ||
{ | ||
code: "test = testVar !== !!_.isNull(5)", | ||
code : 'test = testVar !== !!_.isNull(5)', | ||
errors, | ||
output: "test = testVar !== (5 === null)", | ||
output : 'test = testVar !== (5 === null)', | ||
}, | ||
{ | ||
code: "test = testVar === _.isNull(testVar2)", | ||
code : 'test = testVar === _.isNull(testVar2)', | ||
errors, | ||
output: "test = testVar === (testVar2 === null)", | ||
output : 'test = testVar === (testVar2 === null)', | ||
}, | ||
{ | ||
code: "test = _.isNull(5) === testVar", | ||
code : 'test = _.isNull(5) === testVar', | ||
errors, | ||
output: "test = (5 === null) === testVar", | ||
output : 'test = (5 === null) === testVar', | ||
}, | ||
{ | ||
code: "test = !_.isNull(5) === testVar", | ||
code : 'test = !_.isNull(5) === testVar', | ||
errors, | ||
output: "test = (5 !== null) === testVar", | ||
output : 'test = (5 !== null) === testVar', | ||
}, | ||
{ | ||
code: "test = !!_.isNull(5) === testVar", | ||
code : 'test = !!_.isNull(5) === testVar', | ||
errors, | ||
output: "test = (5 === null) === testVar", | ||
output : 'test = (5 === null) === testVar', | ||
}, | ||
{ | ||
code: "test4 = _.isNull(5) && 8 ? true : false;", | ||
code : 'test4 = _.isNull(5) && 8 ? true : false;', | ||
errors, | ||
output: "test4 = 5 === null && 8 ? true : false;" | ||
output : 'test4 = 5 === null && 8 ? true : false;', | ||
}, | ||
{ | ||
code: "test4 = 8 && _.isNull(5) ? true : false;", | ||
code : 'test4 = 8 && _.isNull(5) ? true : false;', | ||
errors, | ||
output: "test4 = 8 && 5 === null ? true : false;" | ||
output : 'test4 = 8 && 5 === null ? true : false;', | ||
}, | ||
//Multiple errors on same line & weird mixes of logical & binary expressions | ||
// Multiple errors on same line & weird mixes of logical & binary expressions | ||
{ | ||
code: "_.isNull(5) === _.isNull(10) && _.isNull(15)", | ||
errors: [ | ||
code : '_.isNull(5) === _.isNull(10) && _.isNull(15)', | ||
errors : [ | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
} | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
], | ||
output: "(5 === null) === (10 === null) && 15 === null" | ||
output : '(5 === null) === (10 === null) && 15 === null', | ||
}, | ||
{ | ||
code: "_.isNull(null) || _.isNull(10) && _.isNull(15)", | ||
errors: [ | ||
code : '_.isNull(null) || _.isNull(10) && _.isNull(15)', | ||
errors : [ | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
} | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
], | ||
output: "null === null || 10 === null && 15 === null" | ||
output : 'null === null || 10 === null && 15 === null', | ||
}, | ||
{ | ||
code: "_.isNull(undefined) || _.isNull(10) && _.isNull(null)", | ||
errors: [ | ||
code : '_.isNull(undefined) || _.isNull(10) && _.isNull(null)', | ||
errors : [ | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
} | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
], | ||
output: "undefined === null || 10 === null && null === null" | ||
output : 'undefined === null || 10 === null && null === null', | ||
}, | ||
{ | ||
code: "_.isNull(undefined) || _.isNull(null) && _.isNull(10)", | ||
errors: [ | ||
code : '_.isNull(undefined) || _.isNull(null) && _.isNull(10)', | ||
errors : [ | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for null", | ||
type: "MemberExpression" | ||
} | ||
message : 'Use native JavaScript to check for null', | ||
type : 'MemberExpression', | ||
}, | ||
], | ||
output: "undefined === null || null === null && 10 === null" | ||
output : 'undefined === null || null === null && 10 === null', | ||
}, | ||
//Confirm that if the user stores isNull in a variable, autofix will not be applied | ||
// Confirm that if the user stores isNull in a variable, autofix will not be applied | ||
{ | ||
code: "test = _.isNull", | ||
code : 'test = _.isNull', | ||
errors, | ||
output: "test = _.isNull" | ||
output : 'test = _.isNull', | ||
}, | ||
{ | ||
code: "test = !_.isNull", | ||
code : 'test = !_.isNull', | ||
errors, | ||
output: "test = !_.isNull" | ||
output : 'test = !_.isNull', | ||
}, | ||
] | ||
}); | ||
], | ||
}); |
@@ -5,330 +5,330 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
const rule = require("../../../lib/rules/no-lodash-isundefined"); | ||
const RuleTester = require("eslint").RuleTester; | ||
const rule = require('../../../lib/rules/no-lodash-isundefined'); | ||
const RuleTester = require('eslint').RuleTester; | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
// ------------------------------------------------------------------------------ | ||
var ruleTester = new RuleTester(); | ||
const ruleTester = new RuleTester(); | ||
const errors = [{ | ||
message : "Use native JavaScript to check for undefined", | ||
type : "MemberExpression" | ||
}]; | ||
const errors = [ { | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
} ]; | ||
ruleTester.run("no-lodash-undefined", rule, { | ||
valid: [ | ||
"valid = 5;", | ||
"test2 = function(opts) { return _.forEach([1, 2], function(v){ return v + 1}) }", | ||
"test = function() { if(test){ return; } }", | ||
"obj = { method1 : function(){} }", | ||
"test = function() { return test ? 1 : 2 }", | ||
"test = true ? 1 : 2", | ||
"test = test2 === undefined", | ||
"test = test2 !== undefined", | ||
"test = test2 == undefined", | ||
"test = test2 != undefined", | ||
"test2 = typeof test === 'undefined'", | ||
"test2 = typeof test == 'undefined'", | ||
"test2 = typeof test !== 'undefined'", | ||
"test2 = typeof test != 'undefined'", | ||
], | ||
ruleTester.run('no-lodash-undefined', rule, { | ||
valid : [ | ||
'valid = 5;', | ||
'test2 = function(opts) { return _.forEach([1, 2], function(v){ return v + 1}) }', | ||
'test = function() { if(test){ return; } }', | ||
'obj = { method1 : function(){} }', | ||
'test = function() { return test ? 1 : 2 }', | ||
'test = true ? 1 : 2', | ||
'test = test2 === undefined', | ||
'test = test2 !== undefined', | ||
'test = test2 == undefined', | ||
'test = test2 != undefined', | ||
"test2 = typeof test === 'undefined'", | ||
"test2 = typeof test == 'undefined'", | ||
"test2 = typeof test !== 'undefined'", | ||
"test2 = typeof test != 'undefined'", | ||
], | ||
invalid: [ | ||
{ | ||
code: "test = _.isUndefined(5)", | ||
errors, | ||
output: "test = 5 === undefined", | ||
}, | ||
{ | ||
code: "test = !!_.isUndefined(5)", | ||
errors, | ||
output: "test = 5 === undefined", | ||
}, | ||
{ | ||
code: "test = !_.isUndefined(5)", | ||
errors, | ||
output: "test = 5 !== undefined", | ||
}, | ||
{ | ||
code: "test = _.isUndefined(5) ? 1 : 2", | ||
errors, | ||
output: "test = 5 === undefined ? 1 : 2", | ||
}, | ||
{ | ||
code: "test = !!_.isUndefined(5) ? 1 : 2", | ||
errors, | ||
output: "test = 5 === undefined ? 1 : 2", | ||
}, | ||
{ | ||
code: "test = !_.isUndefined(5) ? 1 : 2", | ||
errors, | ||
output: "test = 5 !== undefined ? 1 : 2", | ||
}, | ||
{ | ||
code: "test = _.isUndefined(true)", | ||
errors, | ||
output : "test = true === undefined", | ||
}, | ||
{ | ||
code: "test = !_.isUndefined(true)", | ||
errors, | ||
output : "test = true !== undefined", | ||
}, | ||
{ | ||
code: "test = _.isUndefined(false) ? 1 : 2", | ||
errors, | ||
output: "test = false === undefined ? 1 : 2", | ||
}, | ||
{ | ||
code: "var condition = undefined; test = _.isUndefined(condition) ? 1 : 2", | ||
errors, | ||
output: "var condition = undefined; test = condition === undefined ? 1 : 2", | ||
}, | ||
{ | ||
code: "var condition = undefined; test = !_.isUndefined(condition) ? 1 : 2", | ||
errors, | ||
output: "var condition = undefined; test = condition !== undefined ? 1 : 2", | ||
}, | ||
{ | ||
code: "var condition = {}; test = _.isUndefined(condition) ? 1 : 2", | ||
errors, | ||
output: "var condition = {}; test = condition === undefined ? 1 : 2", | ||
}, | ||
{ | ||
code: "var condition = {}; test = !!_.isUndefined(condition) ? 1 : 2", | ||
errors, | ||
output: "var condition = {}; test = condition === undefined ? 1 : 2", | ||
}, | ||
{ | ||
code: "var condition = function(){}; test = _.isUndefined(condition) ? 1 : 2", | ||
errors, | ||
output: "var condition = function(){}; test = condition === undefined ? 1 : 2", | ||
}, | ||
//Testing object methods | ||
{ | ||
code: "obj = { method1 : function(){ _.isUndefined(5) ? 1 : 2 } }", | ||
errors, | ||
output: "obj = { method1 : function(){ 5 === undefined ? 1 : 2 } }", | ||
}, | ||
{ | ||
code: "obj = { method1 : function(){ !!_.isUndefined(5) ? 1 : 2 } }", | ||
errors, | ||
output: "obj = { method1 : function(){ 5 === undefined ? 1 : 2 } }", | ||
}, | ||
{ | ||
code: "obj = { method1 : function(){ !_.isUndefined(5) ? 1 : 2 } }", | ||
errors, | ||
output: "obj = { method1 : function(){ 5 !== undefined ? 1 : 2 } }", | ||
}, | ||
{ | ||
code: "obj = { method1 : function(){ _.isUndefined('5') ? 1 : 2 } }", | ||
errors, | ||
output: "obj = { method1 : function(){ '5' === undefined ? 1 : 2 } }", | ||
}, | ||
{ | ||
code: "obj = { method1 : function(){ _.isUndefined([1,2]) ? 1 : 2 } }", | ||
errors, | ||
output: "obj = { method1 : function(){ [1,2] === undefined ? 1 : 2 } }", | ||
}, | ||
{ | ||
code: "obj = { method1: undefined, method2 : function(){ _.isUndefined(this.method1) ? 1 : 2 } }", | ||
errors, | ||
output: "obj = { method1: undefined, method2 : function(){ this.method1 === undefined ? 1 : 2 } }", | ||
}, | ||
{ | ||
code: "obj = { method1: undefined, method2 : function(){ !_.isUndefined(this.method1) ? 1 : 2 } }", | ||
errors, | ||
output: "obj = { method1: undefined, method2 : function(){ this.method1 !== undefined ? 1 : 2 } }", | ||
}, | ||
{ | ||
code: "obj = { method1: undefined, method2 : function(){ var val = _.isUndefined(this.method1) ? 1 : 2; var val2 = undefined; } }", | ||
errors, | ||
output: "obj = { method1: undefined, method2 : function(){ var val = this.method1 === undefined ? 1 : 2; var val2 = undefined; } }", | ||
}, | ||
{ | ||
code: "obj = { method1: undefined, method2 : function(){ var val = !!_.isUndefined(this.method1) ? 1 : 2; var val2 = undefined; } }", | ||
errors, | ||
output: "obj = { method1: undefined, method2 : function(){ var val = this.method1 === undefined ? 1 : 2; var val2 = undefined; } }", | ||
}, | ||
{ | ||
code: "obj = { outer : { inner : function(arg){ return _.isUndefined(arg); } } }", | ||
errors, | ||
output: "obj = { outer : { inner : function(arg){ return arg === undefined; } } }", | ||
}, | ||
//Testing functions | ||
{ | ||
code: "test2 = function(opt) { return _.isUndefined(opt) }", | ||
errors, | ||
output : "test2 = function(opt) { return opt === undefined }" | ||
}, | ||
{ | ||
code: "test2 = function() { if(_.isUndefined(opt)) { return false; } }", | ||
errors, | ||
output: "test2 = function() { if(opt === undefined) { return false; } }", | ||
}, | ||
{ | ||
code: "test2 = function() { return function(){ if(_.isUndefined(opt)) { return false; } } }", | ||
errors, | ||
output: "test2 = function() { return function(){ if(opt === undefined) { return false; } } }", | ||
}, | ||
{ | ||
code: "test = function(){ var test2 = {}; _.isUndefined(test2) ? 1 : 2 }", | ||
errors, | ||
output: "test = function(){ var test2 = {}; test2 === undefined ? 1 : 2 }" | ||
}, | ||
{ | ||
code: "test = function(test2){ if(_.isUndefined(test2)) { return 1 } }", | ||
errors, | ||
output : "test = function(test2){ if(test2 === undefined) { return 1 } }", | ||
}, | ||
{ | ||
code: "test = function(){ if(_.isUndefined([])) { return 1 } }", | ||
errors, | ||
output : "test = function(){ if([] === undefined) { return 1 } }", | ||
}, | ||
// Mixing in logical & binary expressions | ||
{ | ||
code: "test = testVar === _.isUndefined(5)", | ||
errors, | ||
output: "test = testVar === (5 === undefined)", | ||
}, | ||
{ | ||
code: "test = testVar !== _.isUndefined(5)", | ||
errors, | ||
output: "test = testVar !== (5 === undefined)", | ||
}, | ||
{ | ||
code: "test = testVar !== !_.isUndefined(5)", | ||
errors, | ||
output: "test = testVar !== (5 !== undefined)", | ||
}, | ||
{ | ||
code: "test = testVar !== !!_.isUndefined(5)", | ||
errors, | ||
output: "test = testVar !== (5 === undefined)", | ||
}, | ||
{ | ||
code: "test = testVar === _.isUndefined(testVar2)", | ||
errors, | ||
output: "test = testVar === (testVar2 === undefined)", | ||
}, | ||
{ | ||
code: "test = _.isUndefined(5) === testVar", | ||
errors, | ||
output: "test = (5 === undefined) === testVar", | ||
}, | ||
{ | ||
code: "test = !_.isUndefined(5) === testVar", | ||
errors, | ||
output: "test = (5 !== undefined) === testVar", | ||
}, | ||
{ | ||
code: "test = !!_.isUndefined(5) === testVar", | ||
errors, | ||
output: "test = (5 === undefined) === testVar", | ||
}, | ||
{ | ||
code : "test4 = _.isUndefined(5) && 8 ? true : false;", | ||
errors, | ||
output : "test4 = 5 === undefined && 8 ? true : false;" | ||
}, | ||
{ | ||
code : "test4 = 8 && _.isUndefined(5) ? true : false;", | ||
errors, | ||
output : "test4 = 8 && 5 === undefined ? true : false;" | ||
invalid : [ | ||
{ | ||
code : 'test = _.isUndefined(5)', | ||
errors, | ||
output : 'test = 5 === undefined', | ||
}, | ||
//Multiple errors on same line & weird mixes of logical & binary expressions | ||
{ | ||
code: "_.isUndefined(5) === _.isUndefined(10) && _.isUndefined(15)", | ||
errors: [ | ||
code : 'test = !!_.isUndefined(5)', | ||
errors, | ||
output : 'test = 5 === undefined', | ||
}, | ||
{ | ||
code : 'test = !_.isUndefined(5)', | ||
errors, | ||
output : 'test = 5 !== undefined', | ||
}, | ||
{ | ||
code : 'test = _.isUndefined(5) ? 1 : 2', | ||
errors, | ||
output : 'test = 5 === undefined ? 1 : 2', | ||
}, | ||
{ | ||
code : 'test = !!_.isUndefined(5) ? 1 : 2', | ||
errors, | ||
output : 'test = 5 === undefined ? 1 : 2', | ||
}, | ||
{ | ||
code : 'test = !_.isUndefined(5) ? 1 : 2', | ||
errors, | ||
output : 'test = 5 !== undefined ? 1 : 2', | ||
}, | ||
{ | ||
code : 'test = _.isUndefined(true)', | ||
errors, | ||
output : 'test = true === undefined', | ||
}, | ||
{ | ||
code : 'test = !_.isUndefined(true)', | ||
errors, | ||
output : 'test = true !== undefined', | ||
}, | ||
{ | ||
code : 'test = _.isUndefined(false) ? 1 : 2', | ||
errors, | ||
output : 'test = false === undefined ? 1 : 2', | ||
}, | ||
{ | ||
code : 'var condition = undefined; test = _.isUndefined(condition) ? 1 : 2', | ||
errors, | ||
output : 'var condition = undefined; test = condition === undefined ? 1 : 2', | ||
}, | ||
{ | ||
code : 'var condition = undefined; test = !_.isUndefined(condition) ? 1 : 2', | ||
errors, | ||
output : 'var condition = undefined; test = condition !== undefined ? 1 : 2', | ||
}, | ||
{ | ||
code : 'var condition = {}; test = _.isUndefined(condition) ? 1 : 2', | ||
errors, | ||
output : 'var condition = {}; test = condition === undefined ? 1 : 2', | ||
}, | ||
{ | ||
code : 'var condition = {}; test = !!_.isUndefined(condition) ? 1 : 2', | ||
errors, | ||
output : 'var condition = {}; test = condition === undefined ? 1 : 2', | ||
}, | ||
{ | ||
code : 'var condition = function(){}; test = _.isUndefined(condition) ? 1 : 2', | ||
errors, | ||
output : 'var condition = function(){}; test = condition === undefined ? 1 : 2', | ||
}, | ||
// Testing object methods | ||
{ | ||
code : 'obj = { method1 : function(){ _.isUndefined(5) ? 1 : 2 } }', | ||
errors, | ||
output : 'obj = { method1 : function(){ 5 === undefined ? 1 : 2 } }', | ||
}, | ||
{ | ||
code : 'obj = { method1 : function(){ !!_.isUndefined(5) ? 1 : 2 } }', | ||
errors, | ||
output : 'obj = { method1 : function(){ 5 === undefined ? 1 : 2 } }', | ||
}, | ||
{ | ||
code : 'obj = { method1 : function(){ !_.isUndefined(5) ? 1 : 2 } }', | ||
errors, | ||
output : 'obj = { method1 : function(){ 5 !== undefined ? 1 : 2 } }', | ||
}, | ||
{ | ||
code : "obj = { method1 : function(){ _.isUndefined('5') ? 1 : 2 } }", | ||
errors, | ||
output : "obj = { method1 : function(){ '5' === undefined ? 1 : 2 } }", | ||
}, | ||
{ | ||
code : 'obj = { method1 : function(){ _.isUndefined([1,2]) ? 1 : 2 } }', | ||
errors, | ||
output : 'obj = { method1 : function(){ [1,2] === undefined ? 1 : 2 } }', | ||
}, | ||
{ | ||
code : 'obj = { method1: undefined, method2 : function(){ _.isUndefined(this.method1) ? 1 : 2 } }', | ||
errors, | ||
output : 'obj = { method1: undefined, method2 : function(){ this.method1 === undefined ? 1 : 2 } }', | ||
}, | ||
{ | ||
code : 'obj = { method1: undefined, method2 : function(){ !_.isUndefined(this.method1) ? 1 : 2 } }', | ||
errors, | ||
output : 'obj = { method1: undefined, method2 : function(){ this.method1 !== undefined ? 1 : 2 } }', | ||
}, | ||
{ | ||
code : 'obj = { method1: undefined, method2 : function(){ var val = _.isUndefined(this.method1) ? 1 : 2; var val2 = undefined; } }', | ||
errors, | ||
output : 'obj = { method1: undefined, method2 : function(){ var val = this.method1 === undefined ? 1 : 2; var val2 = undefined; } }', | ||
}, | ||
{ | ||
code : 'obj = { method1: undefined, method2 : function(){ var val = !!_.isUndefined(this.method1) ? 1 : 2; var val2 = undefined; } }', | ||
errors, | ||
output : 'obj = { method1: undefined, method2 : function(){ var val = this.method1 === undefined ? 1 : 2; var val2 = undefined; } }', | ||
}, | ||
{ | ||
code : 'obj = { outer : { inner : function(arg){ return _.isUndefined(arg); } } }', | ||
errors, | ||
output : 'obj = { outer : { inner : function(arg){ return arg === undefined; } } }', | ||
}, | ||
// Testing functions | ||
{ | ||
code : 'test2 = function(opt) { return _.isUndefined(opt) }', | ||
errors, | ||
output : 'test2 = function(opt) { return opt === undefined }', | ||
}, | ||
{ | ||
code : 'test2 = function() { if(_.isUndefined(opt)) { return false; } }', | ||
errors, | ||
output : 'test2 = function() { if(opt === undefined) { return false; } }', | ||
}, | ||
{ | ||
code : 'test2 = function() { return function(){ if(_.isUndefined(opt)) { return false; } } }', | ||
errors, | ||
output : 'test2 = function() { return function(){ if(opt === undefined) { return false; } } }', | ||
}, | ||
{ | ||
code : 'test = function(){ var test2 = {}; _.isUndefined(test2) ? 1 : 2 }', | ||
errors, | ||
output : 'test = function(){ var test2 = {}; test2 === undefined ? 1 : 2 }', | ||
}, | ||
{ | ||
code : 'test = function(test2){ if(_.isUndefined(test2)) { return 1 } }', | ||
errors, | ||
output : 'test = function(test2){ if(test2 === undefined) { return 1 } }', | ||
}, | ||
{ | ||
code : 'test = function(){ if(_.isUndefined([])) { return 1 } }', | ||
errors, | ||
output : 'test = function(){ if([] === undefined) { return 1 } }', | ||
}, | ||
// Mixing in logical & binary expressions | ||
{ | ||
code : 'test = testVar === _.isUndefined(5)', | ||
errors, | ||
output : 'test = testVar === (5 === undefined)', | ||
}, | ||
{ | ||
code : 'test = testVar !== _.isUndefined(5)', | ||
errors, | ||
output : 'test = testVar !== (5 === undefined)', | ||
}, | ||
{ | ||
code : 'test = testVar !== !_.isUndefined(5)', | ||
errors, | ||
output : 'test = testVar !== (5 !== undefined)', | ||
}, | ||
{ | ||
code : 'test = testVar !== !!_.isUndefined(5)', | ||
errors, | ||
output : 'test = testVar !== (5 === undefined)', | ||
}, | ||
{ | ||
code : 'test = testVar === _.isUndefined(testVar2)', | ||
errors, | ||
output : 'test = testVar === (testVar2 === undefined)', | ||
}, | ||
{ | ||
code : 'test = _.isUndefined(5) === testVar', | ||
errors, | ||
output : 'test = (5 === undefined) === testVar', | ||
}, | ||
{ | ||
code : 'test = !_.isUndefined(5) === testVar', | ||
errors, | ||
output : 'test = (5 !== undefined) === testVar', | ||
}, | ||
{ | ||
code : 'test = !!_.isUndefined(5) === testVar', | ||
errors, | ||
output : 'test = (5 === undefined) === testVar', | ||
}, | ||
{ | ||
code : 'test4 = _.isUndefined(5) && 8 ? true : false;', | ||
errors, | ||
output : 'test4 = 5 === undefined && 8 ? true : false;', | ||
}, | ||
{ | ||
code : 'test4 = 8 && _.isUndefined(5) ? true : false;', | ||
errors, | ||
output : 'test4 = 8 && 5 === undefined ? true : false;', | ||
}, | ||
// Multiple errors on same line & weird mixes of logical & binary expressions | ||
{ | ||
code : '_.isUndefined(5) === _.isUndefined(10) && _.isUndefined(15)', | ||
errors : [ | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
} | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
], | ||
output: "(5 === undefined) === (10 === undefined) && 15 === undefined" | ||
output : '(5 === undefined) === (10 === undefined) && 15 === undefined', | ||
}, | ||
{ | ||
code: "_.isUndefined(undefined) || _.isUndefined(10) && _.isUndefined(15)", | ||
errors: [ | ||
code : '_.isUndefined(undefined) || _.isUndefined(10) && _.isUndefined(15)', | ||
errors : [ | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
} | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
], | ||
output: "undefined === undefined || 10 === undefined && 15 === undefined" | ||
output : 'undefined === undefined || 10 === undefined && 15 === undefined', | ||
}, | ||
{ | ||
code: "_.isUndefined(undefined) || _.isUndefined(10) && _.isUndefined(undefined)", | ||
errors: [ | ||
code : '_.isUndefined(undefined) || _.isUndefined(10) && _.isUndefined(undefined)', | ||
errors : [ | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
} | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
], | ||
output: "undefined === undefined || 10 === undefined && undefined === undefined" | ||
output : 'undefined === undefined || 10 === undefined && undefined === undefined', | ||
}, | ||
{ | ||
code: "_.isUndefined(undefined) || _.isUndefined(undefined) && _.isUndefined(10)", | ||
errors: [ | ||
code : '_.isUndefined(undefined) || _.isUndefined(undefined) && _.isUndefined(10)', | ||
errors : [ | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
{ | ||
message: "Use native JavaScript to check for undefined", | ||
type: "MemberExpression" | ||
} | ||
message : 'Use native JavaScript to check for undefined', | ||
type : 'MemberExpression', | ||
}, | ||
], | ||
output: "undefined === undefined || undefined === undefined && 10 === undefined" | ||
output : 'undefined === undefined || undefined === undefined && 10 === undefined', | ||
}, | ||
//Confirm that if the user stores isUndefined in a variable, autofix will not be applied | ||
{ | ||
code : "test = _.isUndefined", | ||
errors, | ||
output : "test = _.isUndefined" | ||
}, | ||
{ | ||
code : "test = !_.isUndefined", | ||
errors, | ||
output : "test = !_.isUndefined" | ||
}, | ||
] | ||
}); | ||
// Confirm that if the user stores isUndefined in a variable, autofix will not be applied | ||
{ | ||
code : 'test = _.isUndefined', | ||
errors, | ||
output : 'test = _.isUndefined', | ||
}, | ||
{ | ||
code : 'test = !_.isUndefined', | ||
errors, | ||
output : 'test = !_.isUndefined', | ||
}, | ||
], | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
37901
15
1187
3
51
2