Skip to content

Commit c14d358

Browse files
authored
fix: avoid using bigint literal syntax (#542)
* fix: avoid using bigint literal syntax * simplify * fix: Add custom eslint plugin with no-big-int rule * fix: Migrate .eslintrc.js to flat format eslint.config.mjs * fix(gh): exclude node 12.x, 14.x on macos-latest
1 parent 837ec9e commit c14d358

File tree

7 files changed

+72
-25
lines changed

7 files changed

+72
-25
lines changed

.eslintrc.js

-23
This file was deleted.

.github/workflows/bundlers.yml

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ jobs:
2020
node-version: 12.x
2121
- os: windows-latest
2222
node-version: 14.x
23+
- os: macos-latest
24+
node-version: 12.x
25+
- os: macos-latest
26+
node-version: 14.x
2327
steps:
2428
- name: Checkout
2529
uses: actions/checkout@v3

.github/workflows/node.yml

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ jobs:
1919
node-version: 12.x
2020
- os: windows-latest
2121
node-version: 14.x
22+
- os: macos-latest
23+
node-version: 12.x
24+
- os: macos-latest
25+
node-version: 14.x
2226
steps:
2327
- name: Checkout
2428
uses: actions/checkout@v3

eslint-plugin-local/index.mjs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
import nbi from './no-big-int.mjs'
4+
5+
export default {
6+
rules: {
7+
'no-big-int': nbi,
8+
},
9+
}

eslint-plugin-local/no-big-int.mjs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
export default {
4+
meta: {
5+
docs: {
6+
description: 'disallow `bigint` syntax',
7+
category: 'ES2020',
8+
recommended: false,
9+
},
10+
fixable: null,
11+
messages: {
12+
forbidden: 'ES2020 `bigint` syntax is forbidden.',
13+
},
14+
schema: [],
15+
type: 'problem',
16+
},
17+
create(context) {
18+
return {
19+
Literal(node) {
20+
if (node.bigint != null) {
21+
context.report({ messageId: 'forbidden', node })
22+
}
23+
},
24+
}
25+
},
26+
}

eslint.config.mjs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { FlatCompat } from '@eslint/eslintrc'
2+
const compat = new FlatCompat()
3+
4+
import eslintPluginLocal from './eslint-plugin-local/index.mjs'
5+
6+
export default [
7+
// standard,
8+
...compat.extends('eslint-config-standard'),
9+
{
10+
files: ['**/**.js', '**/**.mjs'],
11+
languageOptions: {
12+
sourceType: 'module',
13+
ecmaVersion: 'latest',
14+
},
15+
plugins: { 'local': eslintPluginLocal },
16+
rules: {
17+
/*
18+
This is inserted to make this compatible with prettier.
19+
Once https://github.com/prettier/prettier/issues/3845 and https://github.com/prettier/prettier/issues/3847 are solved this might be not needed any more.
20+
*/
21+
'space-before-function-paren': 0,
22+
curly: [2, 'all'],
23+
'local/no-big-int': 'error',
24+
},
25+
},
26+
]
27+

src/errors.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ E(
335335
received = addNumericalSeparator(String(input))
336336
} else if (typeof input === 'bigint') {
337337
received = String(input)
338-
339-
if (input > 2n ** 32n || input < -(2n ** 32n)) {
338+
const limit = BigInt(2) ** BigInt(32)
339+
if (input > limit || input < -limit) {
340340
received = addNumericalSeparator(received)
341341
}
342342

0 commit comments

Comments
 (0)