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

Fix error when parsing keys with hyphen in some JSON files #7316

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/bun-types/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ interface ImportMeta {
readonly file: string;
/**
* The environment variables of the process
*
*
* ```ts
* import.meta.env === process.env
* import.meta.env === process.env
* ```
*/
readonly env: import("bun").Env;
Expand Down
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);
});
Loading