-
Notifications
You must be signed in to change notification settings - Fork 2.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 error when parsing keys with hyphen in some JSON files #7316
Conversation
src/bundler.zig
Outdated
@@ -1435,12 +1435,18 @@ pub const Bundler = struct { | |||
}, prop.key.?.loc), | |||
.value = prop.value.?, | |||
}; | |||
var maybe_quoted = name; | |||
if (strings.containsChar(name, '-')) { |
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.
Is there already a function that checks if a string is a valid variable name or do I need to write one ?
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.
yeah, MutableString.ensureValidIdentifier
. It will check if it's a valid identifier, and create a new one with the non-valid characters replaced by _
if it is not valid
src/bundler.zig
Outdated
@@ -1435,12 +1435,18 @@ pub const Bundler = struct { | |||
}, prop.key.?.loc), | |||
.value = prop.value.?, | |||
}; | |||
var maybe_quoted = name; |
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.
var maybe_quoted = name; | |
var maybe_quoted = MutableString.ensureValidIdentifier(name, allocator) catch return null; |
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.
We already use this function to create the name of temporary variable, and it replaces the hyphens with underscores. With your suggestion the code becomes this:
var key_with_hyphen = !0;
export {
key_with_hyphen
};
export default { "key-with-hyphen": key_with_hyphen };
a711c7e
to
b939c8f
Compare
b939c8f
to
78377d5
Compare
So I figured It was not the bundler's job to handle quoting stuff. I instead modified to printer to quote aliases if they are not a valid identifier. This removes the need for allocation. I think this PR is ready to be merged. |
thank you |
…es (oven-sh#7316) * Quote export aliases with hyphens when converting jsons to modules * Add test * Handle quotes in printer and not in bundler * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
What does this PR do?
Fixes #7261 introduced by #4783.
The solution is to quote export aliases when required so that
becomes
Relevant: tc39/ecma262#2154 and https://bugs.webkit.org/show_bug.cgi?id=217576
How did you verify your code works?
I wrote automated tests
I did not add allocations
bun-debug test 07261.test.ts
)