Skip to content
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
12 changes: 12 additions & 0 deletions src/js_parser/parse/parse_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1795,15 +1795,27 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
if p.lexer.is_contextual_keyword(b"global") {
p.lexer.next()?;
p.lexer.expect(T::TOpenBrace)?;
let scope_index = p.scopes_in_order.len();
let _ = p.parse_stmts_up_to(T::TCloseBrace, opts)?;
p.lexer.next()?;
// The statements inside are dropped, so discard any scopes they
// recorded or the visit pass will hit a scope order mismatch.
p.discard_scopes_up_to(scope_index);
return Ok(Some(p.s(S::TypeScript {}, loc)));
}

// "declare const x: any"
let scope_index = p.scopes_in_order.len();
let stmt = p.parse_stmt(opts)?;
if let Some(decs) = &opts.ts_decorators {
p.discard_scopes_up_to(decs.scope_index);
} else {
// The statement is dropped below (or reduced to just its bindings
// for "export declare var" inside a namespace), so discard any
// scopes it recorded or the visit pass will hit a scope order
// mismatch (e.g. "declare foo: bar" parses a labeled statement
// that records a Label scope).
p.discard_scopes_up_to(scope_index);
}

// Unlike almost all uses of "declare", statements that use
Expand Down
50 changes: 50 additions & 0 deletions test/bundler/transpiler/scope-mismatch-panic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,53 @@ const fn = () => {
expect(exitCode).not.toBe(0);
});
});

describe("TypeScript 'declare' statements discard scopes of dropped statements", () => {
// Each of these parses a statement after "declare" that records scopes during the
// parse pass and is then dropped. The recorded scopes used to be left behind, so
// visiting the following class statement hit "Scope mismatch while visiting".
const cases: [name: string, source: string, expected: string[]][] = [
[
"declare module with an invalid name followed by a class",
"declare module : es2015\nclass Foo {}\n",
["class Foo"],
],
["declare followed by a labeled statement and a class", "declare foo: bar\nclass Foo {}\n", ["class Foo"]],
[
"declare const with an arrow function initializer followed by a class",
"declare const x = () => {};\nclass Foo {}\n",
["class Foo"],
],
[
"declare global containing nested blocks followed by a class",
"declare global { if (1) { let x = 1 } }\nclass Foo {}\n",
["class Foo"],
],
[
"export declare with an initializer inside a namespace",
"namespace ns { export declare const x = () => {}; export function y() { return x } }\nclass Foo {}\n",
["function y", "class Foo"],
],
];

test.concurrent.each(cases)("%s", async (_name, source, expected) => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`process.stdout.write(new Bun.Transpiler({ loader: "tsx" }).transformSync(${JSON.stringify(source)}))`,
],
env: bunEnv,
stderr: "pipe",
stdout: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stderr).toBe("");
for (const substring of expected) {
expect(stdout).toContain(substring);
}
expect(exitCode).toBe(0);
});
});
Loading