Compare commits

...

2 Commits

Author SHA1 Message Date
Claude Bot
d4c4767ad3 test: simplify regression test to single entry file
Consolidate the multi-file test into a single entry.js that sets up
globalThis and validates defines in one file, removing the need for
require() or import ordering concerns.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-19 09:35:24 +00:00
Claude Bot
481c1ba7c8 fix(bundler): allow reserved words as property names in --define values
When using `--define "VAR=x.import"`, Bun incorrectly treated the value
as a string literal because `import` is a keyword. However, reserved
words are valid as property names in JavaScript (e.g. `x.import`).

Only check the keyword restriction for the first part of a dotted path,
not subsequent parts which are property accesses.

Closes #21200

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-19 09:21:09 +00:00
2 changed files with 35 additions and 1 deletions

View File

@@ -155,12 +155,14 @@ pub const DefineData = struct {
// check for nested identifiers
var valueSplitter = std.mem.splitScalar(u8, value_str, '.');
var isIdent = true;
var isFirstPart = true;
while (valueSplitter.next()) |part| {
if (!js_lexer.isIdentifier(part) or js_lexer.Keywords.has(part)) {
if (!js_lexer.isIdentifier(part) or (isFirstPart and js_lexer.Keywords.has(part))) {
isIdent = false;
break;
}
isFirstPart = false;
}
if (isIdent) {

View File

@@ -0,0 +1,32 @@
import { describe } from "bun:test";
import { itBundled } from "../../bundler/expectBundled";
describe("bundler", () => {
// https://github.com/oven-sh/bun/issues/21200
// --define with reserved word property names should produce property access, not string literals
itBundled("regression/21200/DefineReservedWordProperty", {
files: {
"entry.js": /* js */ `
globalThis.x = { "import": 1, "export": 2, "class": 3, "function": 4, "var": 5, "default": 6 };
globalThis.a = { b: { c: { "import": 7 } } };
if (VAR1 !== 1) throw 'fail: VAR1=' + VAR1;
if (VAR2 !== 2) throw 'fail: VAR2=' + VAR2;
if (VAR3 !== 3) throw 'fail: VAR3=' + VAR3;
if (VAR4 !== 4) throw 'fail: VAR4=' + VAR4;
if (VAR5 !== 5) throw 'fail: VAR5=' + VAR5;
if (VAR6 !== 6) throw 'fail: VAR6=' + VAR6;
if (VAR7 !== 7) throw 'fail: VAR7=' + VAR7;
`,
},
define: {
VAR1: "x.import",
VAR2: "x.export",
VAR3: "x.class",
VAR4: "x.function",
VAR5: "x.var",
VAR6: "x.default",
VAR7: "a.b.c.import",
},
run: true,
});
});