Skip to content

Commit b52fd77

Browse files
committed
fix: return ast from compile
Svelte 4 does it and language tools assumes it's there
1 parent d061f2f commit b52fd77

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

.changeset/poor-hats-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: return ast from `compile` (like Svelte 4 does)

packages/svelte/src/compiler/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function compile(source, options) {
4444
const analysis = analyze_component(parsed, source, combined_options);
4545

4646
const result = transform_component(analysis, source, combined_options);
47-
return result;
47+
return { ...result, ast: to_public_ast(source, parsed, options.modernAst) };
4848
} catch (e) {
4949
if (e instanceof CompileError) {
5050
handle_compile_error(e, options.filename, source);
@@ -121,7 +121,16 @@ export function parse(source, options = {}) {
121121
throw e;
122122
}
123123

124-
if (options.modern) {
124+
return to_public_ast(source, ast, options.modern);
125+
}
126+
127+
/**
128+
* @param {string} source
129+
* @param {import('#compiler').Root} ast
130+
* @param {boolean | undefined} modern
131+
*/
132+
function to_public_ast(source, ast, modern) {
133+
if (modern) {
125134
// remove things that we don't want to treat as public API
126135
return walk(ast, null, {
127136
_(node, { next }) {

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export interface CompileResult {
4646
*/
4747
runes: boolean;
4848
};
49+
/** The AST */
50+
ast: any;
4951
}
5052

5153
export interface Warning {
@@ -184,6 +186,13 @@ export interface CompileOptions extends ModuleCompileOptions {
184186
* @default false
185187
*/
186188
hmr?: boolean;
189+
/**
190+
* If `true`, returns the modern version of the AST.
191+
* Will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
192+
*
193+
* @default false
194+
*/
195+
modernAst?: boolean;
187196
}
188197

189198
export interface ModuleCompileOptions {

packages/svelte/types/index.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ declare module 'svelte/compiler' {
542542
*/
543543
runes: boolean;
544544
};
545+
/** The AST */
546+
ast: any;
545547
}
546548

547549
interface Warning {
@@ -676,6 +678,13 @@ declare module 'svelte/compiler' {
676678
* @default false
677679
*/
678680
hmr?: boolean;
681+
/**
682+
* If `true`, returns the modern version of the AST.
683+
* Will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
684+
*
685+
* @default false
686+
*/
687+
modernAst?: boolean;
679688
}
680689

681690
interface ModuleCompileOptions {
@@ -2477,6 +2486,13 @@ declare module 'svelte/types/compiler/interfaces' {
24772486
* @default false
24782487
*/
24792488
hmr?: boolean;
2489+
/**
2490+
* If `true`, returns the modern version of the AST.
2491+
* Will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
2492+
*
2493+
* @default false
2494+
*/
2495+
modernAst?: boolean;
24802496
}
24812497

24822498
interface ModuleCompileOptions {

0 commit comments

Comments
 (0)