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

Error when parsing keys with hyphen in some JSON files #7261

Closed
Azarattum opened this issue Nov 22, 2023 · 3 comments · Fixed by #7316
Closed

Error when parsing keys with hyphen in some JSON files #7261

Azarattum opened this issue Nov 22, 2023 · 3 comments · Fixed by #7316
Labels
bug Something isn't working good first issue Something that would be good for new contributors

Comments

@Azarattum
Copy link

What version of Bun is running?

1.0.13+f5bf67bd1

What platform is your computer?

Linux 5.15.0-101.103.2.1.el8uek.x86_64 x86_64 x86_64

What steps can reproduce the bug?

When importing/requiring some JSON files that contain hyphens in the keys, Bun can throw a syntax error. Currently can reproduce with files named tsconfig.json. Maybe parsing of tsconfig is somehow different?

echo '{ "key-with-hyphen": true }' > tsconfig.json
bun -e 'require("./tsconfig.json")'

Results in:

SyntaxError: Unexpected token '-'. Expected '}' to end an export list.
      at /home/tsconfig.json:1:2

With files named other than tsconfig.json everything works as expected:

echo '{ "key-with-hyphen": true }' > demo.json
bun -e 'require("./demo.json")'

What is the expected behavior?

All JSON files should be treated and parsed the same.

What do you see instead?

tsconfig.json files throw when threre are hyphens in keys.

Additional information

The issue was discovered with tsconfig.json containing a key for ts-node configuration (which includes a hyphen).

@Azarattum Azarattum added the bug Something isn't working label Nov 22, 2023
@Jarred-Sumner
Copy link
Collaborator

Interesting

@Jarred-Sumner Jarred-Sumner added the good first issue Something that would be good for new contributors label Nov 23, 2023
@DontBreakAlex
Copy link
Contributor

So it turns out that JSON config files are handled differently

if (loader == .json and !path.isJSONCFile()) {
return ResolvedSource{
.allocator = null,
.source_code = bun.String.create(parse_result.source.contents),
.specifier = input_specifier,
.source_url = if (input_specifier.eqlUTF8(path.text)) input_specifier.dupeRef() else String.init(path.text),
.hash = 0,
.tag = ResolvedSource.Tag.json_for_object_loader,
};
}

bun/src/fs.zig

Lines 1608 to 1615 in 22818e6

pub fn isJSONCFile(this: *const Path) bool {
const str = this.name.filename;
if (!(strings.hasPrefixComptime(str, "tsconfig.") or strings.hasPrefixComptime(str, "jsconfig."))) {
return false;
}
return strings.hasSuffixComptime(str, ".json");
}

They go through are a special parser
pub fn ParseTSConfig(source: *const logger.Source, log: *logger.Log, allocator: std.mem.Allocator) !Expr {

and are rewritten as modules, thus producing a syntax error in this case:

var key_with_hyphen = !0

export {
    key_with_hyphen as key-with-hyphen
};

export default { "key-with-hyphen": key_with_hyphen };

@Jarred-Sumner why are these files rewritten like this ? Is it for JSON5 compatibility ?

@DontBreakAlex
Copy link
Contributor

DontBreakAlex commented Nov 25, 2023

According to MDN quoting the alias should work, but I get the same error rewriting it like this:

var key_with_hyphen = !0

export {
    key_with_hyphen as "key-with-hyphen"
};

export default { "key-with-hyphen": key_with_hyphen };

Edit: I'm not sure why this is not working, according WebKit's Bugzilla this has been supported for a while...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Something that would be good for new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants