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

refactor: use primordials #37

Merged
merged 8 commits into from
Jan 17, 2022
Merged

refactor: use primordials #37

merged 8 commits into from
Jan 17, 2022

Conversation

bcoe
Copy link
Collaborator

@bcoe bcoe commented Jan 16, 2022

Modules in Node.js use primordial helpers, so that core APIs continue working if prototypes are mutated. By moving to using this same approach it will be easier to drop this module into Node.js once ready.

I believe we will want to do some similar work as part of the resolution to #6, to make the errors we generate follow Node's conventions -- my hope is we can keep this module a direct mirror of the code in utils/parseargs.js (in the Node codebase).

Modules in Node.js use primordial helpers, so that core APIs continue
working if prototypes are mutated. By moving to using this same approach
it will be easier to drop this module into Node.js once ready.
@@ -0,0 +1,432 @@
'use strict';

/* eslint-disable node-core/prefer-primordials */
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is identical to the primordials.js in the Node.js codebase.

const {
ArrayIsArray,
ArrayPrototypeConcat,
ArrayPrototypeIncludes,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This prevents monkey patched prototypes on types like Array from breaking our core functionality, and is an approach used widely in the Node codebase.

primordials.js Show resolved Hide resolved
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

You may want to use https://npmjs.com/call-bind, and call-bind/callBound, for something published - node's own patterns only work because node can guarantee first execution for its primordials file, which this package can not.

index.js Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
primordials.js Show resolved Hide resolved
bcoe and others added 3 commits January 16, 2022 17:26
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@bcoe
Copy link
Collaborator Author

bcoe commented Jan 16, 2022

You may want to use https://npmjs.com/call-bind, and call-bind/callBound, for something published - node's own patterns only work because node can guarantee first execution for its primordials file, which this package can not.

Ideally I would like to keep index.js able to be dropped in to the Node.js codebase, to prevent the two diverging significantly the second we open a PR on Node.js.

Could you submit a patch after this that modifies our primordials.js to use call-bind?

Now that I think of it, having a separately published shim with the same exports as primordials.js in the Node.js codebase would be very handy for future work where a polyfill is developed in parallel.

@ljharb
Copy link
Member

ljharb commented Jan 16, 2022

That’s a good idea. I’ll make such a package today or tomorrow, and then I’ll make a PR here to use it - that way index.js still can be drop-in (which was my thinking with the suggestion anyways; that call-bind would be used in primordials.js), but it’ll actually be robust when used outside of node core.

index.js Outdated Show resolved Hide resolved
package.json Outdated
@@ -6,8 +6,8 @@
"scripts": {
"coverage": "c8 --check-coverage node test/index.js",
"test": "c8 node test/index.js",
"posttest": "eslint index.js",
"fix": "eslint index.js --fix"
"posttest": "eslint *.js",
Copy link
Member

Choose a reason for hiding this comment

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

Why *.js and not just a dot?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because it flags a bunch of issues with unused variables in README.md, seems like a nuisance to apply the same linting rules to js code blocks, since you know they'll be partial code.

Copy link
Member

Choose a reason for hiding this comment

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

I’m confused; how could eslint possibly warn on md files if --ext=md isn’t passed?

Either way, we should disable vis eslintignore and/or overrides, not by limiting what gets linted (dot catches nested files, for example, *.js doesn’t)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I’m confused; how could eslint possibly warn on md files if --ext=md isn’t passed?

Quick comment. Might be layered uses of eslint, like editor extension. I opened a PR prompted by md warnings in my environment! #35

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just added README.md to ignores for now. I would have rather just added:

<!-- eslint-ignore no-unused-vars !-->

Tot he markdown file, which it seems should work based on markdown/markdown plugin information ... but none of the incantations I tried fixed it.

@bcoe bcoe requested a review from ljharb January 16, 2022 21:40
@@ -119,6 +121,7 @@ const { flags, values, positionals } = parseArgs(argv, options);
```

```js
const { parseArgs } = require('@pkgjs/parseargs');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This address one of the linting issues, might as well make it so samples are copy/pastable.

Copy link
Member

@darcyclarke darcyclarke left a comment

Choose a reason for hiding this comment

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

👍🏻 LGTM

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

LGTM! Sometime this week, I'll make a PR removing primordials.js and replacing the require from it with a package that does things properly for "not node core".

@bcoe bcoe merged commit a35c9b7 into main Jan 17, 2022
@bcoe bcoe deleted the primordials branch January 17, 2022 13:55
@bcoe
Copy link
Collaborator Author

bcoe commented Jan 17, 2022

Sometime this week, I'll make a PR removing primordials.js and replacing the require from it with a package that does things properly for "not node core".

@ljharb sounds great, I think this repo will give us a good pattern we can copy for other work.

This was referenced Jan 18, 2022
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