Skip to content

Commit c87affd

Browse files
committed
fix: return ast from compile
Was returned from Svelte 4's compile method, and language tools uses it in various places for perf reasons (not doing double parses unnecessarily if you need both)
1 parent a271878 commit c87affd

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

.changeset/silly-balloons-deny.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`

packages/svelte/src/compiler/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function compile(source, options) {
2828
});
2929

3030
const analysis = analyze_component(parsed, combined_options);
31-
const result = transform_component(analysis, source, combined_options);
31+
const result = transform_component(parsed, analysis, source, combined_options);
3232
return result;
3333
} catch (e) {
3434
if (e instanceof CompileError) {
@@ -45,7 +45,7 @@ export function compile(source, options) {
4545
* https://svelte.dev/docs/svelte-compiler#svelte-compile
4646
* @param {string} source The component source code
4747
* @param {import('#compiler').ModuleCompileOptions} options
48-
* @returns {import('#compiler').CompileResult}
48+
* @returns {import('#compiler').ModuleCompileResult}
4949
*/
5050
export function compileModule(source, options) {
5151
try {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ import { client_component, client_module } from './client/transform-client.js';
55
import { getLocator } from 'locate-character';
66

77
/**
8+
* @param {import('#compiler').Root} ast
89
* @param {import('../types').ComponentAnalysis} analysis
910
* @param {string} source
1011
* @param {import('#compiler').ValidatedCompileOptions} options
1112
* @returns {import('#compiler').CompileResult}
1213
*/
13-
export function transform_component(analysis, source, options) {
14+
export function transform_component(ast, analysis, source, options) {
1415
if (options.generate === false) {
1516
return {
1617
js: /** @type {any} */ (null),
1718
css: null,
1819
warnings: transform_warnings(source, options.filename, analysis.warnings),
1920
metadata: {
2021
runes: analysis.runes
21-
}
22+
},
23+
ast
2224
};
2325
}
2426

@@ -50,21 +52,21 @@ export function transform_component(analysis, source, options) {
5052
warnings: transform_warnings(source, options.filename, analysis.warnings),
5153
metadata: {
5254
runes: analysis.runes
53-
}
55+
},
56+
ast
5457
};
5558
}
5659

5760
/**
5861
* @param {import('../types').Analysis} analysis
5962
* @param {string} source
6063
* @param {import('#compiler').ValidatedModuleCompileOptions} options
61-
* @returns {import('#compiler').CompileResult}
64+
* @returns {import('#compiler').ModuleCompileResult}
6265
*/
6366
export function transform_module(analysis, source, options) {
6467
if (options.generate === false) {
6568
return {
6669
js: /** @type {any} */ (null),
67-
css: null,
6870
warnings: transform_warnings(source, analysis.name, analysis.warnings),
6971
metadata: {
7072
runes: true
@@ -89,7 +91,6 @@ export function transform_module(analysis, source, options) {
8991

9092
return {
9193
js: print(program, {}),
92-
css: null,
9394
warnings: transform_warnings(source, analysis.name, analysis.warnings),
9495
metadata: {
9596
runes: true

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,17 @@ import type { SourceMap } from 'magic-string';
1111
import type { Context } from 'zimmerframe';
1212
import type { Scope } from '../phases/scope.js';
1313
import * as Css from './css.js';
14-
import type { EachBlock, Namespace, SvelteNode } from './template.js';
14+
import type { EachBlock, Namespace, Root, SvelteNode } from './template.js';
1515

16-
/** The return value of `compile` from `svelte/compiler` */
17-
export interface CompileResult {
16+
/** The return value of `compileModule` from `svelte/compiler` */
17+
export interface ModuleCompileResult {
1818
/** The compiled JavaScript */
1919
js: {
2020
/** The generated code */
2121
code: string;
2222
/** A source map */
2323
map: SourceMap;
2424
};
25-
/** The compiled CSS */
26-
css: null | {
27-
/** The generated code */
28-
code: string;
29-
/** A source map */
30-
map: SourceMap;
31-
};
3225
/**
3326
* An array of warning objects that were generated during compilation. Each warning has several properties:
3427
* - `code` is a string identifying the category of warning
@@ -48,6 +41,19 @@ export interface CompileResult {
4841
};
4942
}
5043

44+
/** The return value of `compile` from `svelte/compiler` */
45+
export interface CompileResult extends ModuleCompileResult {
46+
/** The compiled CSS */
47+
css: null | {
48+
/** The generated code */
49+
code: string;
50+
/** A source map */
51+
map: SourceMap;
52+
};
53+
/** The AST */
54+
ast: Root;
55+
}
56+
5157
export interface Warning {
5258
start?: Location;
5359
end?: Location;

packages/svelte/types/index.d.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ declare module 'svelte/compiler' {
502502
* https://svelte.dev/docs/svelte-compiler#svelte-compile
503503
* @param source The component source code
504504
* */
505-
export function compileModule(source: string, options: ModuleCompileOptions): CompileResult;
505+
export function compileModule(source: string, options: ModuleCompileOptions): ModuleCompileResult;
506506
/**
507507
* The parse function parses a component, returning only its abstract syntax tree.
508508
*
@@ -519,22 +519,15 @@ declare module 'svelte/compiler' {
519519
* @deprecated Replace this with `import { walk } from 'estree-walker'`
520520
* */
521521
function walk(): never;
522-
/** The return value of `compile` from `svelte/compiler` */
523-
interface CompileResult {
522+
/** The return value of `compileModule` from `svelte/compiler` */
523+
interface ModuleCompileResult {
524524
/** The compiled JavaScript */
525525
js: {
526526
/** The generated code */
527527
code: string;
528528
/** A source map */
529529
map: SourceMap;
530530
};
531-
/** The compiled CSS */
532-
css: null | {
533-
/** The generated code */
534-
code: string;
535-
/** A source map */
536-
map: SourceMap;
537-
};
538531
/**
539532
* An array of warning objects that were generated during compilation. Each warning has several properties:
540533
* - `code` is a string identifying the category of warning
@@ -554,6 +547,19 @@ declare module 'svelte/compiler' {
554547
};
555548
}
556549

550+
/** The return value of `compile` from `svelte/compiler` */
551+
interface CompileResult extends ModuleCompileResult {
552+
/** The compiled CSS */
553+
css: null | {
554+
/** The generated code */
555+
code: string;
556+
/** A source map */
557+
map: SourceMap;
558+
};
559+
/** The AST */
560+
ast: Root;
561+
}
562+
557563
interface Warning {
558564
start?: Location;
559565
end?: Location;

0 commit comments

Comments
 (0)