Skip to content
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

fix: avoid using bigint literal syntax #542

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

fix: avoid using bigint literal syntax #542

wants to merge 5 commits into from

Conversation

mman
Copy link

@mman mman commented Oct 8, 2024

Attempts to address #541. After the discussion I have made a custom ESLint plugin that exposes no-big-int rule to catch usage of the bigint n literal as part of npm run lint. In order to actually make the local ESLint plugin / local ESLint rule work, I have to also migrate the ESLint configuration to the new flat file format.

Running the npm run lint against main reports this (note that there are unrelated linting errors that should probably be addressed in a separate PR).

€ npm run lint                              

> [email protected] lint
> eslint src


/Users/mman/Developer/victronenergy/readable-stream/src/errors.js
  339:19  error  ES2020 `bigint` syntax is forbidden  local/no-big-int
  339:25  error  ES2020 `bigint` syntax is forbidden  local/no-big-int
  339:42  error  ES2020 `bigint` syntax is forbidden  local/no-big-int
  339:48  error  ES2020 `bigint` syntax is forbidden  local/no-big-int

/Users/mman/Developer/victronenergy/readable-stream/src/primordials.js
  106:3  warning  Expected property shorthand  object-shorthand

/Users/mman/Developer/victronenergy/readable-stream/src/util.js
   27:15   error  'ERR_INVALID_ARG_TYPE' is not defined  no-undef
   27:64   error  Extra semicolon                        semi
   29:2    error  Extra semicolon                        semi
   32:5    error  Expected { after 'if' condition        curly
   32:15   error  'ERR_INVALID_ARG_TYPE' is not defined  no-undef
   32:60   error  Extra semicolon                        semi
   33:2    error  Extra semicolon                        semi
  158:17   error  'ERR_INVALID_ARG_TYPE' is not defined  no-undef
  158:70   error  Extra semicolon                        semi
  160:42   error  Extra semicolon                        semi
  161:43   error  Extra semicolon                        semi
  163:28   error  Extra semicolon                        semi
  165:39   error  Extra semicolon                        semi
  167:114  error  Extra semicolon                        semi
  169:54   error  Extra semicolon                        semi
  170:8    error  Extra semicolon                        semi
  175:32   error  Extra semicolon                        semi
  176:8    error  Unexpected trailing comma              comma-dangle
  177:6    error  Extra semicolon                        semi

Running it against my fork looks clean:

€ npm run lint

> [email protected] lint
> eslint src


/Users/mman/Developer/victronenergy/readable-stream-mman/src/primordials.js
  106:3  warning  Expected property shorthand  object-shorthand

/Users/mman/Developer/victronenergy/readable-stream-mman/src/util.js
   27:15   error  'ERR_INVALID_ARG_TYPE' is not defined  no-undef
   27:64   error  Extra semicolon                        semi
   29:2    error  Extra semicolon                        semi
   32:5    error  Expected { after 'if' condition        curly
   32:15   error  'ERR_INVALID_ARG_TYPE' is not defined  no-undef
   32:60   error  Extra semicolon                        semi
   33:2    error  Extra semicolon                        semi
  158:17   error  'ERR_INVALID_ARG_TYPE' is not defined  no-undef
  158:70   error  Extra semicolon                        semi
  160:42   error  Extra semicolon                        semi
  161:43   error  Extra semicolon                        semi
  163:28   error  Extra semicolon                        semi
  165:39   error  Extra semicolon                        semi
  167:114  error  Extra semicolon                        semi
  169:54   error  Extra semicolon                        semi
  170:8    error  Extra semicolon                        semi
  175:32   error  Extra semicolon                        semi
  176:8    error  Unexpected trailing comma              comma-dangle
  177:6    error  Extra semicolon                        semi

@mcollina
Copy link
Member

mcollina commented Oct 8, 2024

Can you please add a test?

@mman
Copy link
Author

mman commented Oct 8, 2024

@mcollina I'd love to but I'm not familiar with the code base that much and looking around I'm not sure what to test exactly, but if you give me some guidance or pointers I'll be happy to add one (or more). I see that tests in https://github.com/nodejs/readable-stream/blob/main/test/ours/test-errors.js basically only cover creating of specific instances of Error.

How would you like the test to work?

@mman
Copy link
Author

mman commented Nov 13, 2024

@mcollina ping. Could you please help me understand what tests would you like to see? I'd like to get this merged. Thx!

@mcollina
Copy link
Member

My understanding is that this error is creating problems for a certain set of bundlers and/or browsers. Therefore, we need a test that verify we would not regress this in the future.

Use said bundlers/browser and verify this. As you can see, we use playwright: https://github.com/nodejs/readable-stream/blob/main/test/browser/runner-browser.mjs.

@mman
Copy link
Author

mman commented Nov 13, 2024

My understanding is that it does not actually cause issues in bundlers. The original code uses new JS syntax that gets passed through "as is" using any known bundler and/or transpiler (like babel) and so the resulting code fails to parse correctly if the target environment JS runtime is not able to handle the new JS syntax.

So the point of the PR is to simply avoid the new JS syntax. That is all. I'm not sure how to test it 🤷‍♂️.

@kanongil
Copy link

Yeah, this issue concerns a runtime feature, which cannot go in a regular test case. Still, it is important to make the CI fail on this usage, so future code changes don't inadvertently use literals again.

Ideally the test suite would be extended to test under a runtime with no BigInt support, but the seems complicated to implement. Alternatively, BigInt literal usage could be catched the linting stage, with a new eslint rule. Unfortunately there doesn't seem to be any existing rules that handle this. It should be simple enough to implement a custom rule though, as the logic is just these lines.

@mman
Copy link
Author

mman commented Nov 18, 2024

Thanks @kanongil for your view. Agree completely. In my codebase I was actually able to workaround this problem by using rather simple babel plugin that replaces the bigint n literal syntax with regular JS BigInt constructor invocation.

What do you suggest for a proper fix though? I will be happy to contribute a PR to create this new rule and configure it where appropriate if you give me pointers...

@MattiasBuelens
Copy link

We would at least want an ESLint rule that disallows BigInt literals. So basically take the code that @kanongil mentioned, wrap it a small local ESLint rule inside this project and add that rule to our .eslintrc.js. 🙂

I don't think we have tests for our lint rules. Just make sure that ESLint actually errors if you add a BigInt literal to the code. 😉

@mman
Copy link
Author

mman commented Nov 29, 2024

@mcollina @kanongil @MattiasBuelens PR description updated. Created a custom ESLint rule, turned it into a custom local ESLint plugin, referencing in a new style ESLint flat config file. Seems to work. What do you think?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Member

If CI is happy, it works for me!

@cristian-atehortua
Copy link

@mcollina when can this be merged? CI failures are because there are no way to install some node versions in MacOS:

Error: Unable to find Node version '12.x' for platform darwin and architecture arm64.

@mcollina
Copy link
Member

Could you skip those platforms?

@cristian-atehortua
Copy link

I'm not the author. I'm just interested in this getting merged.

Maybe @mman can

@mman
Copy link
Author

mman commented Dec 18, 2024

@mcollina Updated the workflow files to exclude node 12.x and 14.x for macos-latest the same way it was already done for windows-latest. Let's see if the checks pass now...

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

still LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants