Skip to content

fix(eslint-plugin): support BigInt in restrict-plus-operands rule #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 23, 2019
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
2 changes: 2 additions & 0 deletions packages/eslint-plugin/docs/rules/restrict-plus-operands.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ Examples of **correct** code:

```ts
var foo = parseInt('5.5', 10) + 10;
var foo = 1n + 1n;
```

Examples of **incorrect** code:

```ts
var foo = '5.5' + 5;
var foo = 1n + 1;
```

## Options
Expand Down
12 changes: 11 additions & 1 deletion packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default util.createRule({
"Operands of '+' operation must either be both strings or both numbers.",
notStrings:
"Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.",
notBigInts: "Operands of '+' operation must be both bigints.",
},
schema: [],
},
Expand All @@ -27,7 +28,7 @@ export default util.createRule({

const typeChecker = service.program.getTypeChecker();

type BaseLiteral = 'string' | 'number' | 'invalid';
type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid';

/**
* Helper function to get base type of node
Expand All @@ -41,6 +42,10 @@ export default util.createRule({
if (type.isStringLiteral()) {
return 'string';
}
// is BigIntLiteral
if (type.flags & ts.TypeFlags.BigIntLiteral) {
return 'bigint';
}
if (type.isUnion()) {
const types = type.types.map(getBaseTypeOfLiteralType);

Expand Down Expand Up @@ -81,6 +86,11 @@ export default util.createRule({
node,
messageId: 'notStrings',
});
} else if (leftType === 'bigint' || rightType === 'bigint') {
context.report({
node,
messageId: 'notBigInts',
});
} else {
context.report({
node,
Expand Down
21 changes: 21 additions & 0 deletions packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ruleTester.run('restrict-plus-operands', rule, {
`var foo = "5.5" + "10";`,
`var foo = parseInt("5.5", 10) + 10;`,
`var foo = parseFloat("5.5", 10) + 10;`,
`var foo = 1n + 1n;`,
`
function test () : number { return 2; }
var foo = test("5.5", 10) + 10;
Expand Down Expand Up @@ -262,5 +263,25 @@ var foo = pair + pair;
},
],
},
{
code: `var foo = 1n + 1`,
errors: [
{
messageId: 'notBigInts',
line: 1,
column: 11,
},
],
},
{
code: `var foo = 1 + 1n`,
errors: [
{
messageId: 'notBigInts',
line: 1,
column: 11,
},
],
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests for var foo = 1n + "1" and var foo = "1" + 1n?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@j-f1 , what do we want to get in these cases, notBigInts or notStrings errors?
Because now they are covered by notStrings check, that's why I haven't added them.

],
});
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