-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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(resolve): add readPackage(Sync)
#51182
Conversation
@SimenB Thank you for submitting this PR! This is a live comment which I will keep updated. 2 packages in this PRCode ReviewsBecause this is a widely-used package, a DT maintainer will need to review it before it can be merged. Status
All of the items on the list are green. To merge, you need to post a comment including the string "Ready to merge" to bring in your changes. Diagnostic Information: What the bot saw about this PR{
"type": "info",
"now": "-",
"pr_number": 51182,
"author": "SimenB",
"headCommitOid": "faf0de9d9a3b626d478c1c29329a6323f22ba405",
"lastPushDate": "2021-02-13T08:23:53.000Z",
"lastActivityDate": "2021-02-18T16:49:46.000Z",
"maintainerBlessed": false,
"mergeOfferDate": "2021-02-18T16:40:55.000Z",
"mergeRequestDate": "2021-02-18T16:49:46.000Z",
"mergeRequestUser": "SimenB",
"hasMergeConflict": false,
"isFirstContribution": false,
"popularityLevel": "Critical",
"pkgInfo": [
{
"name": "browser-resolve",
"kind": "edit",
"files": [
{
"path": "types/browser-resolve/browser-resolve-tests.ts",
"kind": "test"
}
],
"owners": [
"marionebl",
"peterblazejewicz"
],
"addedOwners": [],
"deletedOwners": [],
"popularityLevel": "Popular"
},
{
"name": "resolve",
"kind": "edit",
"files": [
{
"path": "types/resolve/index.d.ts",
"kind": "definition"
},
{
"path": "types/resolve/resolve-tests.ts",
"kind": "test"
}
],
"owners": [
"marionebl",
"ajafff"
],
"addedOwners": [
"ljharb"
],
"deletedOwners": [],
"popularityLevel": "Critical"
}
],
"reviews": [
{
"type": "approved",
"reviewer": "orta",
"date": "2021-02-18T16:40:19.000Z",
"isMaintainer": true
},
{
"type": "approved",
"reviewer": "ljharb",
"date": "2021-02-13T14:39:45.000Z",
"isMaintainer": false
},
{
"type": "stale",
"reviewer": "rbuckton",
"date": "2021-02-13T03:06:35.000Z",
"abbrOid": "6b7be4a"
}
],
"ciResult": "pass"
} |
🔔 @marionebl @peterblazejewicz @ajafff — please review this PR in the next few days. Be sure to explicitly select |
} | ||
|
||
export interface SyncOpts extends Opts { | ||
/** how to read files synchronously (defaults to fs.readFileSync) */ | ||
readFileSync?: (file: string, encoding: BufferEncoding) => string | Buffer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SimenB The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
👋 Hi there! I’ve run some quick measurements against master and your PR. These metrics should help the humans reviewing this PR gauge whether it might negatively affect compile times or editor responsiveness for users who install these typings. Let’s review the numbers, shall we? These typings are for a version of resolve that doesn’t yet exist on master, so I’ve compared them with v1.19. Comparison details 📊
It looks like nothing changed too much. I won’t post performance data again unless it gets worse. |
@SimenB The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
types/resolve/resolve-tests.ts
Outdated
}); | ||
resolved = resolve.sync('typescript', { | ||
readPackageSync(readFileSync, file) { | ||
return JSON.parse(readFileSync(file)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON.parse
in node can take a Buffer
, so the type error is wrong. Should I just cast it?
$ node -p 'JSON.parse(Buffer.from(JSON.stringify({ works: true})))'
{ works: true }
/cc @orta
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON.parse
only takes in a Buffer
because it does .toString()
on the input (https://tc39.es/ecma262/#sec-json.parse, step 1). JSON.parse(1)
and JSON.parse(null)
also "work", but that doesn't mean we should widen the input to accept anything. I'd recommend being more explicit (i.e., readFileSync(file, "utf8")
or JSON.parse(readFileSync(file).toString("utf8"))
, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either way, the cast is fine in this case since we're testing the types, not the runtime behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it takes any toStringable, then the proper type is string | { toString: () => string }
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it takes any toStringable, then the proper type is
string | { toString: () => string }
, no?
I went for that, with the nice side effect of removing the dependency on @types/node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but it’d be nice to capture the mutual exclusivity of readPackage/readFile
Co-authored-by: Jordan Harband <ljharb@gmail.com>
can probably use a union type for the options. will fix at the same time when addressing #51182 (comment) |
@SimenB The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
@SimenB The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
@rbuckton Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
Ready to merge |
I just published |
…` by @SimenB * fix(resolve): add readPackage(Sync) * Update resolve-tests.ts * Update types/resolve/index.d.ts Co-authored-by: Jordan Harband <ljharb@gmail.com> * maybe fix ci * use never to force mutual exclusivity * use intersection types rather than extra interfaces * chore: use toString rather than Buffer * add node types to browser resolve Co-authored-by: Jordan Harband <ljharb@gmail.com>
…` by @SimenB * fix(resolve): add readPackage(Sync) * Update resolve-tests.ts * Update types/resolve/index.d.ts Co-authored-by: Jordan Harband <ljharb@gmail.com> * maybe fix ci * use never to force mutual exclusivity * use intersection types rather than extra interfaces * chore: use toString rather than Buffer * add node types to browser resolve Co-authored-by: Jordan Harband <ljharb@gmail.com>
Please fill in this template.
If changing an existing definition:
readPackage
andreadPackageSync
browserify/resolve#236