8000 feat(eslint): Create no-eq-empty rule (#3783) · jcomo/sentry-javascript@e814408 · GitHub
[go: up one dir, main page]

Skip to content

Commit e814408

Browse files
authored
feat(eslint): Create no-eq-empty rule (getsentry#3783)
Make sure we guard against using equality operator with an empty array or object.
1 parent f398340 commit e814408

File tree

5 files changed

+170
-4
lines changed

5 files changed

+170
-4
lines changed

packages/eslint-config-sdk/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ module.exports = {
2020
plugins: ['@typescript-eslint', 'jsdoc', 'deprecation'],
2121
parser: '@typescript-eslint/parser',
2222
rules: {
23+
// We want to guard against using the equality operator with empty arrays
24+
'@sentry-internal/sdk/no-eq-empty': 'error',
25+
2326
// Unused variables should be removed unless they are marked with and underscore (ex. _varName).
2427
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
2528

packages/eslint-plugin-sdk/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
module.exports = {
1212
rules: {
1313
'no-async-await': require('./rules/no-async-await'),
14+
'no-eq-empty': require('./rules/no-eq-empty'),
1415
},
1516
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @fileoverview Rule to disallow using the equality operator with empty arrays or objects
3+
* @author Abhijeet Prasad
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
module.exports = {
12+
meta: {
13+
type: 'problem',
14+
docs: {
15+
description: 'disallow using the equality operator with empty arrays or objects',
16+
category: 'Best Practices',
17+
recommended: true,
18+
},
19+
fixable: null,
20+
schema: [],
21+
messages: {
22+
equality: 'Do not apply the equality operator on an empty {{ name }}.{{ fix }}',
23+
},
24+
},
25+
create: function(context) {
26+
// variables should be defined here
27+
28+
//----------------------------------------------------------------------
29+
// Helpers
30+
//----------------------------------------------------------------------
31+
32+
// any helper functions should go here or else delete this section
33+
34+
//----------------------------------------------------------------------
35+
// Public
36+
//----------------------------------------------------------------------
37+
38+
return {
39+
BinaryExpression(node) {
40+
if (node.operator === '==' || node.operator === '===') {
41+
if (node.left.type !== 'ArrayExpression' && node.right.type === 'ArrayExpression') {
42+
context.report({
43+
node,
44+
messageId: 'equality',
45+
data: {
46+
name: 'array',
47+
fix: ' Use .length or Array.isArray instead.',
48+
},
49+
});
50+
} else if (node.left.type !== 'ObjectExpression' && node.right.type === 'ObjectExpression') {
51+
context.report({
52+
node,
53+
messageId: 'equality',
54+
data: {
55+
name: 'object',
56+
fix: '',
57+
},
58+
});
59+
}
60+
}
61+
},
62+
};
63+
},
64+
};

packages/eslint-plugin-sdk/test/lib/rules/no-async-await.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
// Requirements
99
//------------------------------------------------------------------------------
1010

11-
var rule = require('../../../src/rules/no-async-await'),
12-
path = require('path'),
13-
RuleTester = require('eslint').RuleTester;
11+
const rule = require('../../../src/rules/no-async-await');
12+
const RuleTester = require('eslint').RuleTester;
1413

1514
//------------------------------------------------------------------------------
1615
// Tests
@@ -21,7 +20,7 @@ RuleTester.setDefaultConfig({
2120
ecmaVersion: 8,
2221
},
2322
});
24-
var ruleTester = new RuleTester();
23+
const ruleTester = new RuleTester();
2524

2625
ruleTester.run('no-async-await', rule, {
2726
valid: [],
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @fileoverview Rule to disallow using the equality operator with empty array
3+
* @author Abhijeet Prasad
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
const rule = require('../../../src/rules/no-eq-empty');
12+
const RuleTester = require('eslint').RuleTester;
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
RuleTester.setDefaultConfig({
19+
parserOptions: {
20+
ecmaVersion: 8,
21+
},
22+
});
23+
const ruleTester = new RuleTester();
24+
25+
const arrayMessage = 'Do not apply the equality operator on an empty array. Use .length or Array.isArray instead.';
26+
const objectMessage = 'Do not apply the equality operator on an empty object.';
27+
28+
ruleTester.run('no-eq-empty', rule, {
29+
valid: [
30+
{
31+
code: 'const hey = [] === []',
32+
},
33+
{
34+
code: 'const hey = [] == []',
35+
},
36+
{
37+
code: 'const hey = {} === {}',
38+
},
39+
{
40+
code: 'const hey = {} == {}',
41+
},
42+
],
43+
invalid: [
44+
{
45+
code: 'empty === []',
46+
errors: [
47+
{
48+
message: arrayMessage,
49+
type: 'BinaryExpression',
50+
},
51+
],
52+
},
53+
{
54+
code: 'empty == []',
55+
errors: [
56+
{
57+
message: arrayMessage,
58+
type: 'BinaryExpression',
59+
},
60+
],
61+
},
62+
{
63+
code: 'const hey = function() {}() === []',
64+
errors: [
65+
{
66+
message: arrayMessage,
67+
type: 'BinaryExpression',
68+
},
69+
],
70+
},
71+
{
72+
code: 'empty === {}',
73+
errors: [
74+
{
75+
message: objectMessage,
76+
type: 'BinaryExpression',
77+
},
78+
],
79+
},
80+
{
81+
code: 'empty == {}',
82+
errors: [
83+
{
84+
message: objectMessage,
85+
type: 'BinaryExpression',
86+
},
87+
],
88+
},
89+
{
90+
code: 'const hey = function(){}() === {}',
91+
errors: [
92+
{
93+
message: objectMessage,
94+
type: 'BinaryExpression',
95+
},
96+
],
97+
},
98+
],
99+
});

0 commit comments

Comments
 (0)
0