Skip to content

Commit 9a88363

Browse files
webschikJamesHenry
authored andcommitted
fix(eslint-plugin): support BigInt in restrict-plus-operands rule (typescript-eslint#309) (typescript-eslint#310)
1 parent 0635183 commit 9a88363

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

packages/eslint-plugin/docs/rules/restrict-plus-operands.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Examples of **correct** code:
44

55
```ts
66
var foo = parseInt('5.5', 10) + 10;
7+
var foo = 1n + 1n;
78
```
89

910
Examples of **incorrect** code:
1011

1112
```ts
1213
var foo = '5.5' + 5;
14+
var foo = 1n + 1;
1315
```
1416

1517
## Options

packages/eslint-plugin/src/rules/restrict-plus-operands.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default util.createRule({
1818
"Operands of '+' operation must either be both strings or both numbers.",
1919
notStrings:
2020
"Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.",
21+
notBigInts: "Operands of '+' operation must be both bigints.",
2122
},
2223
schema: [],
2324
},
@@ -27,7 +28,7 @@ export default util.createRule({
2728

2829
const typeChecker = service.program.getTypeChecker();
2930

30-
type BaseLiteral = 'string' | 'number' | 'invalid';
31+
type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid';
3132

3233
/**
3334
* Helper function to get base type of node
@@ -41,6 +42,10 @@ export default util.createRule({
4142
if (type.isStringLiteral()) {
4243
return 'string';
4344
}
45+
// is BigIntLiteral
46+
if (type.flags & ts.TypeFlags.BigIntLiteral) {
47+
return 'bigint';
48+
}
4449
if (type.isUnion()) {
4550
const types = type.types.map(getBaseTypeOfLiteralType);
4651

@@ -81,6 +86,11 @@ export default util.createRule({
8186
node,
8287
messageId: 'notStrings',
8388
});
89+
} else if (leftType === 'bigint' || rightType === 'bigint') {
90+
context.report({
91+
node,
92+
messageId: 'notBigInts',
93+
});
8494
} else {
8595
context.report({
8696
node,

packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ruleTester.run('restrict-plus-operands', rule, {
2222
`var foo = "5.5" + "10";`,
2323
`var foo = parseInt("5.5", 10) + 10;`,
2424
`var foo = parseFloat("5.5", 10) + 10;`,
25+
`var foo = 1n + 1n;`,
2526
`
2627
function test () : number { return 2; }
2728
var foo = test("5.5", 10) + 10;
@@ -262,5 +263,25 @@ var foo = pair + pair;
262263
},
263264
],
264265
},
266+
{
267+
code: `var foo = 1n + 1`,
268+
errors: [
269+
{
270+
messageId: 'notBigInts',
271+
line: 1,
272+
column: 11,
273+
},
274+
],
275+
},
276+
{
277+
code: `var foo = 1 + 1n`,
278+
errors: [
279+
{
280+
messageId: 'notBigInts',
281+
line: 1,
282+
column: 11,
283+
},
284+
],
285+
},
265286
],
266287
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy