-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
module: warn on invalid package.json type field #60180
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -212,6 +212,15 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON( | |||||
| if (value.get_string().get(field_value)) { | ||||||
| return throw_invalid_package_config(); | ||||||
| } | ||||||
|
|
||||||
| if (field_value != "commonjs" && field_value != "module") { | ||||||
| fprintf(stderr, | ||||||
| "node: [MODULE_INVALID_TYPE] Invalid \"type\" field in package.json: \"%.*s\". " | ||||||
| "Expected \"commonjs\" or \"module\".\n", | ||||||
| static_cast<int>(field_value.size()), | ||||||
| field_value.data()); | ||||||
| } | ||||||
|
|
||||||
| // Only update type if it is "commonjs" or "module" | ||||||
| // The default value is "none" for backward compatibility. | ||||||
| if (field_value == "commonjs" || field_value == "module") { | ||||||
|
|
@@ -275,7 +284,7 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) { | |||||
|
|
||||||
| if (package_json == nullptr) { | ||||||
| return; | ||||||
| } | ||||||
| } | ||||||
|
||||||
| } | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -204,4 +204,68 @@ describe('findPackageJSON', () => { // Throws when no arguments are provided | |||||||
| assert.ok(stdout.includes(foundPjsonPath), stdout); | ||||||||
| assert.strictEqual(code, 0); | ||||||||
| }); | ||||||||
|
|
||||||||
|
|
||||||||
| }); | ||||||||
|
|
||||||||
| describe('package.json type field validation', () => { | ||||||||
|
||||||||
| In principle, when adding a new test, it should be placed in a new file. | |
| Unless there is strong motivation to do so, refrain from appending | |
| new test cases to an existing file. Similar to the reproductions we ask |
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.
Done! I've added a new test 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.
This should be using the usual machinery (
process.emitWarning()in JS, orProcessEmitWarning()in C++) to emit warning so that it can be suppressed withNODE_NO_WARNINGS=1etc. or captured through event listeners by users.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.
Done! Changed to use ProcessEmitWarning() instead of fprintf(). Let me know if the implementation looks correct.