Skip to content

Commit e7869fa

Browse files
authored
fix: return ast from compile (#11191)
Svelte 4 does it and language tools assumes it's there This also uncovered another case of how ridicoulus the old AST was
1 parent 7363f87 commit e7869fa

File tree

6 files changed

+70
-18
lines changed

6 files changed

+70
-18
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +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+
result.ast = to_public_ast(source, parsed, options.modernAst);
4748
return result;
4849
} catch (e) {
4950
if (e instanceof CompileError) {
@@ -121,7 +122,16 @@ export function parse(source, options = {}) {
121122
throw e;
122123
}
123124

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

packages/svelte/src/compiler/legacy.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,29 +141,37 @@ export function convert(source, ast) {
141141
};
142142

143143
if (node.pending) {
144-
const first = /** @type {import('#compiler').BaseNode} */ (node.pending.nodes.at(0));
145-
const last = /** @type {import('#compiler').BaseNode} */ (node.pending.nodes.at(-1));
144+
const first = node.pending.nodes.at(0);
145+
const last = node.pending.nodes.at(-1);
146146

147-
pendingblock.start = first.start;
148-
pendingblock.end = last.end;
147+
pendingblock.start = first?.start ?? source.indexOf('}', node.expression.end) + 1;
148+
pendingblock.end = last?.end ?? pendingblock.start;
149149
pendingblock.skip = false;
150150
}
151151

152152
if (node.then) {
153-
const first = /** @type {import('#compiler').BaseNode} */ (node.then.nodes.at(0));
154-
const last = /** @type {import('#compiler').BaseNode} */ (node.then.nodes.at(-1));
153+
const first = node.then.nodes.at(0);
154+
const last = node.then.nodes.at(-1);
155155

156-
thenblock.start = pendingblock.end ?? first.start;
157-
thenblock.end = last.end;
156+
thenblock.start =
157+
pendingblock.end ?? first?.start ?? source.indexOf('}', node.expression.end) + 1;
158+
thenblock.end =
159+
last?.end ?? source.lastIndexOf('}', pendingblock.end ?? node.expression.end) + 1;
158160
thenblock.skip = false;
159161
}
160162

161163
if (node.catch) {
162-
const first = /** @type {import('#compiler').BaseNode} */ (node.catch.nodes.at(0));
163-
const last = /** @type {import('#compiler').BaseNode} */ (node.catch.nodes.at(-1));
164-
165-
catchblock.start = thenblock.end ?? pendingblock.end ?? first.start;
166-
catchblock.end = last.end;
164+
const first = node.catch.nodes.at(0);
165+
const last = node.catch.nodes.at(-1);
166+
167+
catchblock.start =
168+
thenblock.end ??
169+
pendingblock.end ??
170+
first?.start ??
171+
source.indexOf('}', node.expression.end) + 1;
172+
catchblock.end =
173+
last?.end ??
174+
source.lastIndexOf('}', thenblock.end ?? pendingblock.end ?? node.expression.end) + 1;
167175
catchblock.skip = false;
168176
}
169177

packages/svelte/src/compiler/phases/3-transform/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export function transform_component(analysis, source, options) {
2020
warnings: transform_warnings(source, options.filename, analysis.warnings),
2121
metadata: {
2222
runes: analysis.runes
23-
}
23+
},
24+
ast: /** @type {any} */ (null) // set afterwards
2425
};
2526
}
2627

@@ -62,7 +63,8 @@ export function transform_component(analysis, source, options) {
6263
warnings: transform_warnings(source, options.filename, analysis.warnings), // TODO apply preprocessor sourcemap
6364
metadata: {
6465
runes: analysis.runes
65-
}
66+
},
67+
ast: /** @type {any} */ (null) // set afterwards
6668
};
6769
}
6870

@@ -80,7 +82,8 @@ export function transform_module(analysis, source, options) {
8082
warnings: transform_warnings(source, analysis.name, analysis.warnings),
8183
metadata: {
8284
runes: true
83-
}
85+
},
86+
ast: /** @type {any} */ (null) // set afterwards
8487
};
8588
}
8689

@@ -105,7 +108,8 @@ export function transform_module(analysis, source, options) {
105108
warnings: transform_warnings(source, analysis.name, analysis.warnings),
106109
metadata: {
107110
runes: true
108-
}
111+
},
112+
ast: /** @type {any} */ (null) // set afterwards
109113
};
110114
}
111115

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
@@ -541,6 +541,8 @@ declare module 'svelte/compiler' {
541541
*/
542542
runes: boolean;
543543
};
544+
/** The AST */
545+
ast: any;
544546
}
545547

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

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

24812497
interface ModuleCompileOptions {

0 commit comments

Comments
 (0)