From f0dfd8b82ac77c792193d89ca590ad0121254ec5 Mon Sep 17 00:00:00 2001 From: Manuel Date: Sun, 9 Feb 2025 18:58:51 +0100 Subject: [PATCH] fix: support `TSModuleDeclaration` --- .changeset/fresh-nails-lay.md | 5 +++++ src/handlers.js | 16 ++++++++++++++++ test/samples/ts-module-declaration/expected.ts | 7 +++++++ .../ts-module-declaration/expected.ts.map | 11 +++++++++++ test/samples/ts-module-declaration/input.ts | 7 +++++++ 5 files changed, 46 insertions(+) create mode 100644 .changeset/fresh-nails-lay.md create mode 100644 test/samples/ts-module-declaration/expected.ts create mode 100644 test/samples/ts-module-declaration/expected.ts.map create mode 100644 test/samples/ts-module-declaration/input.ts diff --git a/.changeset/fresh-nails-lay.md b/.changeset/fresh-nails-lay.md new file mode 100644 index 0000000..4ea8807 --- /dev/null +++ b/.changeset/fresh-nails-lay.md @@ -0,0 +1,5 @@ +--- +'esrap': patch +--- + +fix: support `TSModuleDeclaration` diff --git a/src/handlers.js b/src/handlers.js index fe098ef..379f4c8 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -1506,6 +1506,22 @@ const handlers = { state.commands.push(dedent, newline, '}', newline); }, + TSModuleBlock(node, state) { + state.commands.push(' {', indent, newline); + sequence(node.body, state, false, handle); + state.commands.push(dedent, newline, '}'); + }, + + TSModuleDeclaration(node, state) { + if (node.declare) state.commands.push('declare '); + else state.commands.push('namespace '); + + handle(node.id, state); + + if (!node.body) return; + handle(node.body, state); + }, + TSNonNullExpression(node, state) { handle(node.expression, state); state.commands.push('!'); diff --git a/test/samples/ts-module-declaration/expected.ts b/test/samples/ts-module-declaration/expected.ts new file mode 100644 index 0000000..106d6b3 --- /dev/null +++ b/test/samples/ts-module-declaration/expected.ts @@ -0,0 +1,7 @@ +declare global { + namespace App { + interface Error {} + } +} + +export {}; \ No newline at end of file diff --git a/test/samples/ts-module-declaration/expected.ts.map b/test/samples/ts-module-declaration/expected.ts.map new file mode 100644 index 0000000..2b334e8 --- /dev/null +++ b/test/samples/ts-module-declaration/expected.ts.map @@ -0,0 +1,11 @@ +{ + "version": 3, + "names": [], + "sources": [ + "input.js" + ], + "sourcesContent": [ + "declare global {\n\tnamespace App {\n\t\tinterface Error {}\n\t}\n}\n\nexport {};\n" + ], + "mappings": "QAAQ,MAAM;WACH,GAAG;YACF,KAAK;;;;" +} \ No newline at end of file diff --git a/test/samples/ts-module-declaration/input.ts b/test/samples/ts-module-declaration/input.ts new file mode 100644 index 0000000..e05a84c --- /dev/null +++ b/test/samples/ts-module-declaration/input.ts @@ -0,0 +1,7 @@ +declare global { + namespace App { + interface Error {} + } +} + +export {};