-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint): Create no-eq-empty rule (#3783)
Make sure we guard against using equality operator with an empty array or object.
- Loading branch information
1 parent
f398340
commit e814408
Showing
5 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @fileoverview Rule to disallow using the equality operator with empty arrays or objects | ||
* @author Abhijeet Prasad | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow using the equality operator with empty arrays or objects', | ||
category: 'Best Practices', | ||
recommended: true, | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
equality: 'Do not apply the equality operator on an empty {{ name }}.{{ fix }}', | ||
}, | ||
}, | ||
create: function(context) { | ||
// variables should be defined here | ||
|
||
//---------------------------------------------------------------------- | ||
// Helpers | ||
//---------------------------------------------------------------------- | ||
|
||
// any helper functions should go here or else delete this section | ||
|
||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
|
||
return { | ||
BinaryExpression(node) { | ||
if (node.operator === '==' || node.operator === '===') { | ||
if (node.left.type !== 'ArrayExpression' && node.right.type === 'ArrayExpression') { | ||
context.report({ | ||
node, | ||
messageId: 'equality', | ||
data: { | ||
name: 'array', | ||
fix: ' Use .length or Array.isArray instead.', | ||
}, | ||
}); | ||
} else if (node.left.type !== 'ObjectExpression' && node.right.type === 'ObjectExpression') { | ||
context.report({ | ||
node, | ||
messageId: 'equality', | ||
data: { | ||
name: 'object', | ||
fix: '', | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* @fileoverview Rule to disallow using the equality operator with empty array | ||
* @author Abhijeet Prasad | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../src/rules/no-eq-empty'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
}, | ||
}); | ||
const ruleTester = new RuleTester(); | ||
|
||
const arrayMessage = 'Do not apply the equality operator on an empty array. Use .length or Array.isArray instead.'; | ||
const objectMessage = 'Do not apply the equality operator on an empty object.'; | ||
|
||
ruleTester.run('no-eq-empty', rule, { | ||
valid: [ | ||
{ | ||
code: 'const hey = [] === []', | ||
}, | ||
{ | ||
code: 'const hey = [] == []', | ||
}, | ||
{ | ||
code: 'const hey = {} === {}', | ||
}, | ||
{ | ||
code: 'const hey = {} == {}', | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'empty === []', | ||
errors: [ | ||
{ | ||
message: arrayMessage, | ||
type: 'BinaryExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'empty == []', | ||
errors: [ | ||
{ | ||
message: arrayMessage, | ||
type: 'BinaryExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'const hey = function() {}() === []', | ||
errors: [ | ||
{ | ||
message: arrayMessage, | ||
type: 'BinaryExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'empty === {}', | ||
errors: [ | ||
{ | ||
message: objectMessage, | ||
type: 'BinaryExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'empty == {}', | ||
errors: [ | ||
{ | ||
message: objectMessage, | ||
type: 'BinaryExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'const hey = function(){}() === {}', | ||
errors: [ | ||
{ | ||
message: objectMessage, | ||
type: 'BinaryExpression', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |