Skip to content

fix: return ast from compile #10160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/silly-balloons-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: return ast from `compile`
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function compile(source, options) {
});

const analysis = analyze_component(parsed, combined_options);
const result = transform_component(analysis, source, combined_options);
const result = transform_component(parsed, analysis, source, combined_options);
return result;
} catch (e) {
if (e instanceof CompileError) {
Expand All @@ -45,7 +45,7 @@ export function compile(source, options) {
* https://svelte.dev/docs/svelte-compiler#svelte-compile
* @param {string} source The component source code
* @param {import('#compiler').ModuleCompileOptions} options
* @returns {import('#compiler').CompileResult}
* @returns {import('#compiler').ModuleCompileResult}
*/
export function compileModule(source, options) {
try {
Expand Down
13 changes: 7 additions & 6 deletions packages/svelte/src/compiler/phases/3-transform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import { client_component, client_module } from './client/transform-client.js';
import { getLocator } from 'locate-character';

/**
* @param {import('#compiler').Root} ast
* @param {import('../types').ComponentAnalysis} analysis
* @param {string} source
* @param {import('#compiler').ValidatedCompileOptions} options
* @returns {import('#compiler').CompileResult}
*/
export function transform_component(analysis, source, options) {
export function transform_component(ast, analysis, source, options) {
if (options.generate === false) {
return {
js: /** @type {any} */ (null),
css: null,
warnings: transform_warnings(source, options.filename, analysis.warnings),
metadata: {
runes: analysis.runes
}
},
ast
};
}

Expand Down Expand Up @@ -50,21 +52,21 @@ export function transform_component(analysis, source, options) {
warnings: transform_warnings(source, options.filename, analysis.warnings),
metadata: {
runes: analysis.runes
}
},
ast
};
}

/**
* @param {import('../types').Analysis} analysis
* @param {string} source
* @param {import('#compiler').ValidatedModuleCompileOptions} options
* @returns {import('#compiler').CompileResult}
* @returns {import('#compiler').ModuleCompileResult}
*/
export function transform_module(analysis, source, options) {
if (options.generate === false) {
return {
js: /** @type {any} */ (null),
css: null,
warnings: transform_warnings(source, analysis.name, analysis.warnings),
metadata: {
runes: true
Expand All @@ -89,7 +91,6 @@ export function transform_module(analysis, source, options) {

return {
js: print(program, {}),
css: null,
warnings: transform_warnings(source, analysis.name, analysis.warnings),
metadata: {
runes: true
Expand Down
26 changes: 16 additions & 10 deletions packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,17 @@ import type { SourceMap } from 'magic-string';
import type { Context } from 'zimmerframe';
import type { Scope } from '../phases/scope.js';
import * as Css from './css.js';
import type { EachBlock, Namespace, SvelteNode } from './template.js';
import type { EachBlock, Namespace, Root, SvelteNode } from './template.js';

/** The return value of `compile` from `svelte/compiler` */
export interface CompileResult {
/** The return value of `compileModule` from `svelte/compiler` */
export interface ModuleCompileResult {
/** The compiled JavaScript */
js: {
/** The generated code */
code: string;
/** A source map */
map: SourceMap;
};
/** The compiled CSS */
css: null | {
/** The generated code */
code: string;
/** A source map */
map: SourceMap;
};
/**
* An array of warning objects that were generated during compilation. Each warning has several properties:
* - `code` is a string identifying the category of warning
Expand All @@ -48,6 +41,19 @@ export interface CompileResult {
};
}

/** The return value of `compile` from `svelte/compiler` */
export interface CompileResult extends ModuleCompileResult {
/** The compiled CSS */
css: null | {
/** The generated code */
code: string;
/** A source map */
map: SourceMap;
};
/** The AST */
ast: Root;
}

export interface Warning {
start?: Location;
end?: Location;
Expand Down
26 changes: 16 additions & 10 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ declare module 'svelte/compiler' {
* https://svelte.dev/docs/svelte-compiler#svelte-compile
* @param source The component source code
* */
export function compileModule(source: string, options: ModuleCompileOptions): CompileResult;
export function compileModule(source: string, options: ModuleCompileOptions): ModuleCompileResult;
/**
* The parse function parses a component, returning only its abstract syntax tree.
*
Expand All @@ -519,22 +519,15 @@ declare module 'svelte/compiler' {
* @deprecated Replace this with `import { walk } from 'estree-walker'`
* */
function walk(): never;
/** The return value of `compile` from `svelte/compiler` */
interface CompileResult {
/** The return value of `compileModule` from `svelte/compiler` */
interface ModuleCompileResult {
/** The compiled JavaScript */
js: {
/** The generated code */
code: string;
/** A source map */
map: SourceMap;
};
/** The compiled CSS */
css: null | {
/** The generated code */
code: string;
/** A source map */
map: SourceMap;
};
/**
* An array of warning objects that were generated during compilation. Each warning has several properties:
* - `code` is a string identifying the category of warning
Expand All @@ -554,6 +547,19 @@ declare module 'svelte/compiler' {
};
}

/** The return value of `compile` from `svelte/compiler` */
interface CompileResult extends ModuleCompileResult {
/** The compiled CSS */
css: null | {
/** The generated code */
code: string;
/** A source map */
map: SourceMap;
};
/** The AST */
ast: Root;
}

interface Warning {
start?: Location;
end?: Location;
Expand Down