Skip to content

Commit

Permalink
fix(transpiler): error when parsing keys with hyphen in some JSON fil…
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
2 people authored and ryoppippi committed Feb 1, 2024
1 parent 915bb08 commit e5206f8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/js_printer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ fn NewPrinter(
pub fn printClauseAlias(p: *Printer, alias: string) void {
std.debug.assert(alias.len > 0);

if (!strings.containsNonBmpCodePoint(alias)) {
if (!strings.containsNonBmpCodePointOrIsInvalidIdentifier(alias)) {
p.printSpaceBeforeIdentifier();
p.printIdentifier(alias);
} else {
Expand Down
20 changes: 19 additions & 1 deletion src/string_immutable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const CodePoint = bun.CodePoint;
const bun = @import("root").bun;
pub const joiner = @import("./string_joiner.zig");
const log = bun.Output.scoped(.STR, true);
const js_lexer = @import("./js_lexer.zig");

pub const Encoding = enum {
ascii,
Expand Down Expand Up @@ -216,7 +217,6 @@ pub fn fmtIdentifier(name: string) FormatValidIdentifier {
/// This will always allocate
pub const FormatValidIdentifier = struct {
name: string,
const js_lexer = @import("./js_lexer.zig");
pub fn format(self: FormatValidIdentifier, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
var iterator = strings.CodepointIterator.init(self.name);
var cursor = strings.CodepointIterator.Cursor{};
Expand Down Expand Up @@ -4367,6 +4367,24 @@ pub fn containsNonBmpCodePoint(text: string) bool {
return false;
}

pub fn containsNonBmpCodePointOrIsInvalidIdentifier(text: string) bool {
var iter = CodepointIterator.init(text);
var curs = CodepointIterator.Cursor{};

if (!iter.next(&curs)) return true;

if (curs.c > 0xFFFF or !js_lexer.isIdentifierStart(curs.c))
return true;

while (iter.next(&curs)) {
if (curs.c > 0xFFFF or !js_lexer.isIdentifierContinue(curs.c)) {
return true;
}
}

return false;
}

// this is std.mem.trim except it doesn't forcibly change the slice to be const
pub fn trim(slice: anytype, comptime values_to_strip: []const u8) @TypeOf(slice) {
var begin: usize = 0;
Expand Down
23 changes: 23 additions & 0 deletions test/regression/issue/07261.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { bunEnv, bunExe } from "harness";
import { mkdirSync, rmSync, writeFileSync, mkdtempSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";

it("imports tsconfig.json with abritary keys", async () => {
const testDir = mkdtempSync(join(tmpdir(), "issue7261-"));

// Clean up from prior runs if necessary
rmSync(testDir, { recursive: true, force: true });

// Create a directory with our test tsconfig.json
mkdirSync(testDir, { recursive: true });
writeFileSync(join(testDir, "tsconfig.json"), '{ "key-with-hyphen": true }');

const { exitCode } = Bun.spawnSync({
cmd: [bunExe(), "-e", `require('${join(testDir, "tsconfig.json")}')`],
env: bunEnv,
stderr: "inherit",
});

expect(exitCode).toBe(0);
});

0 comments on commit e5206f8

Please sign in to comment.