Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/eslint-config-sdk/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module.exports = {
plugins: ['@typescript-eslint', 'jsdoc', 'deprecation'],
parser: '@typescript-eslint/parser',
rules: {
// We want to guard against using the equality operator with empty arrays
'@sentry-internal/sdk/no-eq-empty': 'error',

// Unused variables should be removed unless they are marked with and underscore (ex. _varName).
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],

Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin-sdk/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
module.exports = {
rules: {
'no-async-await': require('./rules/no-async-await'),
'no-eq-empty': require('./rules/no-eq-empty'),
},
};
64 changes: 64 additions & 0 deletions packages/eslint-plugin-sdk/src/rules/no-eq-empty.js
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: '',
},
});
}
}
},
};
},
};
7 changes: 3 additions & 4 deletions packages/eslint-plugin-sdk/test/lib/rules/no-async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
// Requirements
//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------
// Tests
Expand All @@ -21,7 +20,7 @@ RuleTester.setDefaultConfig({
ecmaVersion: 8,
},
});
var ruleTester = new RuleTester();
const ruleTester = new RuleTester();

ruleTester.run('no-async-await', rule, {
valid: [],
Expand Down
99 changes: 99 additions & 0 deletions packages/eslint-plugin-sdk/test/lib/rules/no-eq-empty.js
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',
},
],
},
],
});