Skip to content

Commit c6a0985

Browse files
committed
add eslint rule
1 parent b140d90 commit c6a0985

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ module.exports = {
145145

146146
// We want to prevent async await usage in our files to prevent uncessary bundle size. Turned off in tests.
147147
'@sentry-internal/sdk/no-async-await': 'error',
148+
'@sentry-internal/sdk/no-nullish-coalescing': 'error',
148149

149150
// JSDOC comments are required for classes and methods. As we have a public facing codebase, documentation,
150151
// even if it may seems excessive at times, is important to emphasize. Turned off in tests.
@@ -178,6 +179,7 @@ module.exports = {
178179
'@typescript-eslint/no-non-null-assertion': 'off',
179180
'@typescript-eslint/no-empty-function': 'off',
180181
'@sentry-internal/sdk/no-async-await': 'off',
182+
'@sentry-internal/sdk/no-nullish-coalescing': 'off',
181183
},
182184
},
183185
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
module.exports = {
1212
rules: {
1313
'no-async-await': require('./rules/no-async-await'),
14+
'no-nullish-coalescing': require('./rules/no-nullish-coalescing'),
1415
'no-eq-empty': require('./rules/no-eq-empty'),
1516
},
1617
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @fileoverview disallow nullish coalescing operators as they were introduced only in ES2020 and hence require
3+
* us to add a polyfill. This increases bundle size more than avoiding nullish coalescing operators all together.
4+
*
5+
* @author Lukas Stracke
6+
*
7+
* Based on: https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/lib/rules/no-nullish-coalescing-operators.js
8+
*/
9+
'use strict';
10+
11+
// ------------------------------------------------------------------------------
12+
// Rule Definition
13+
// ------------------------------------------------------------------------------
14+
15+
module.exports = {
16+
meta: {
17+
type: 'problem',
18+
docs: {
19+
description: 'disallow nullish coalescing operators.',
20+
category: 'Best Practices',
21+
recommended: true,
22+
},
23+
messages: {
24+
forbidden: 'Avoid using nullish coalescing operators.',
25+
},
26+
fixable: null,
27+
schema: [],
28+
},
29+
create(context) {
30+
return {
31+
"LogicalExpression[operator='??']"(node) {
32+
context.report({
33+
node: context.getSourceCode().getTokenAfter(node.left, isNullishCoalescingOperator),
34+
messageId: 'forbidden',
35+
});
36+
},
37+
};
38+
},
39+
};
40+
41+
/**
42+
* Checks if the given token is a nullish coalescing operator or not.
43+
* @param {Token} token - The token to check.
44+
* @returns {boolean} `true` if the token is a nullish coalescing operator.
45+
*/
46+
function isNullishCoalescingOperator(token) {
47+
return token.value === '??' && token.type === 'Punctuator';
48+
}

packages/node/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
extends: ['../../.eslintrc.js'],
66
rules: {
77
'@sentry-internal/sdk/no-async-await': 'off',
8+
'@sentry-internal/sdk/no-nullish-coalescing': 'off',
89
},
910
};

0 commit comments

Comments
 (0)