Skip to content

fix: return ast from compile #11191

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

Merged
merged 5 commits into from
Apr 16, 2024
Merged
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/poor-hats-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: return ast from `compile` (like Svelte 4 does)
12 changes: 11 additions & 1 deletion packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function compile(source, options) {
const analysis = analyze_component(parsed, source, combined_options);

const result = transform_component(analysis, source, combined_options);
result.ast = to_public_ast(source, parsed, options.modernAst);
return result;
} catch (e) {
if (e instanceof CompileError) {
Expand Down Expand Up @@ -121,7 +122,16 @@ export function parse(source, options = {}) {
throw e;
}

if (options.modern) {
return to_public_ast(source, ast, options.modern);
}

/**
* @param {string} source
* @param {import('#compiler').Root} ast
* @param {boolean | undefined} modern
*/
function to_public_ast(source, ast, modern) {
if (modern) {
// remove things that we don't want to treat as public API
return walk(ast, null, {
_(node, { next }) {
Expand Down
34 changes: 21 additions & 13 deletions packages/svelte/src/compiler/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,37 @@ export function convert(source, ast) {
};

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

pendingblock.start = first.start;
pendingblock.end = last.end;
pendingblock.start = first?.start ?? source.indexOf('}', node.expression.end) + 1;
pendingblock.end = last?.end ?? pendingblock.start;
pendingblock.skip = false;
}

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

thenblock.start = pendingblock.end ?? first.start;
thenblock.end = last.end;
thenblock.start =
pendingblock.end ?? first?.start ?? source.indexOf('}', node.expression.end) + 1;
thenblock.end =
last?.end ?? source.lastIndexOf('}', pendingblock.end ?? node.expression.end) + 1;
thenblock.skip = false;
}

if (node.catch) {
const first = /** @type {import('#compiler').BaseNode} */ (node.catch.nodes.at(0));
const last = /** @type {import('#compiler').BaseNode} */ (node.catch.nodes.at(-1));

catchblock.start = thenblock.end ?? pendingblock.end ?? first.start;
catchblock.end = last.end;
const first = node.catch.nodes.at(0);
const last = node.catch.nodes.at(-1);

catchblock.start =
thenblock.end ??
pendingblock.end ??
first?.start ??
source.indexOf('}', node.expression.end) + 1;
catchblock.end =
last?.end ??
source.lastIndexOf('}', thenblock.end ?? pendingblock.end ?? node.expression.end) + 1;
catchblock.skip = false;
}

Expand Down
12 changes: 8 additions & 4 deletions packages/svelte/src/compiler/phases/3-transform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export function transform_component(analysis, source, options) {
warnings: transform_warnings(source, options.filename, analysis.warnings),
metadata: {
runes: analysis.runes
}
},
ast: /** @type {any} */ (null) // set afterwards
};
}

Expand Down Expand Up @@ -62,7 +63,8 @@ export function transform_component(analysis, source, options) {
warnings: transform_warnings(source, options.filename, analysis.warnings), // TODO apply preprocessor sourcemap
metadata: {
runes: analysis.runes
}
},
ast: /** @type {any} */ (null) // set afterwards
};
}

Expand All @@ -80,7 +82,8 @@ export function transform_module(analysis, source, options) {
warnings: transform_warnings(source, analysis.name, analysis.warnings),
metadata: {
runes: true
}
},
ast: /** @type {any} */ (null) // set afterwards
};
}

Expand All @@ -105,7 +108,8 @@ export function transform_module(analysis, source, options) {
warnings: transform_warnings(source, analysis.name, analysis.warnings),
metadata: {
runes: true
}
},
ast: /** @type {any} */ (null) // set afterwards
};
}

Expand Down
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface CompileResult {
*/
runes: boolean;
};
/** The AST */
ast: any;
}

export interface Warning {
Expand Down Expand Up @@ -184,6 +186,13 @@ export interface CompileOptions extends ModuleCompileOptions {
* @default false
*/
hmr?: boolean;
/**
* If `true`, returns the modern version of the AST.
* Will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
*
* @default false
*/
modernAst?: boolean;
}

export interface ModuleCompileOptions {
Expand Down
16 changes: 16 additions & 0 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ declare module 'svelte/compiler' {
*/
runes: boolean;
};
/** The AST */
ast: any;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any because I couldn't be bothered to type this out as "if modernAst is true, it's this shape, else that shape", and a union of both would make everyone go type cast

}

interface Warning {
Expand Down Expand Up @@ -675,6 +677,13 @@ declare module 'svelte/compiler' {
* @default false
*/
hmr?: boolean;
/**
* If `true`, returns the modern version of the AST.
* Will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
*
* @default false
*/
modernAst?: boolean;
}

interface ModuleCompileOptions {
Expand Down Expand Up @@ -2476,6 +2485,13 @@ declare module 'svelte/types/compiler/interfaces' {
* @default false
*/
hmr?: boolean;
/**
* If `true`, returns the modern version of the AST.
* Will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
*
* @default false
*/
modernAst?: boolean;
}

interface ModuleCompileOptions {
Expand Down