Skip to content

Commit 2d03dc5

Browse files
authored
feat: provide AST node types with internal types stripped out (alternative) (#12968)
Uses new dts-buddy capabilities and the corresponding tsconfig option to strip away types/properties (and their dependencies) that are marked with `@internal` Also hides the legacy AST types from the output To not clutter the exports, the AST types are moved into a namespace named AST closes #12292
1 parent b70d12c commit 2d03dc5

File tree

133 files changed

+1273
-1753
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+1273
-1753
lines changed

.changeset/young-feet-double.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+
feat: provide AST node types with internal types stripped out

packages/svelte/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"@rollup/plugin-virtual": "^3.0.2",
124124
"@types/aria-query": "^5.0.4",
125125
"@types/node": "^20.11.5",
126-
"dts-buddy": "^0.5.1",
126+
"dts-buddy": "^0.5.3",
127127
"esbuild": "^0.21.5",
128128
"rollup": "^4.21.0",
129129
"source-map": "^0.7.4",

packages/svelte/scripts/generate-types.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ fs.writeFileSync(`${dir}/types/compiler/interfaces.d.ts`, "import '../index.js';
2121

2222
await createBundle({
2323
output: `${dir}/types/index.d.ts`,
24+
compilerOptions: {
25+
// so that types/properties with `@internal` (and its dependencies) are removed from the output
26+
stripInternal: true
27+
},
2428
modules: {
2529
[pkg.name]: `${dir}/src/index.d.ts`,
2630
[`${pkg.name}/action`]: `${dir}/src/action/public.d.ts`,

packages/svelte/src/compiler/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @import { LegacyRoot } from './types/legacy-nodes.js' */
2-
/** @import { CompileOptions, CompileResult, ValidatedCompileOptions, ModuleCompileOptions, Root } from '#compiler' */
2+
/** @import { CompileOptions, CompileResult, ValidatedCompileOptions, ModuleCompileOptions } from '#compiler' */
3+
/** @import { AST } from './public.js' */
34
import { walk as zimmerframe_walk } from 'zimmerframe';
45
import { convert } from './legacy.js';
56
import { parse as parse_acorn } from './phases/1-parse/acorn.js';
@@ -77,7 +78,7 @@ export function compileModule(source, options) {
7778
* @overload
7879
* @param {string} source
7980
* @param {{ filename?: string; modern: true }} options
80-
* @returns {Root}
81+
* @returns {AST.Root}
8182
*/
8283

8384
/**
@@ -90,7 +91,7 @@ export function compileModule(source, options) {
9091
* @overload
9192
* @param {string} source
9293
* @param {{ filename?: string; modern?: false }} [options]
93-
* @returns {LegacyRoot}
94+
* @returns {Record<string, any>}
9495
*/
9596

9697
/**
@@ -102,7 +103,7 @@ export function compileModule(source, options) {
102103
* https://svelte.dev/docs/svelte-compiler#svelte-parse
103104
* @param {string} source
104105
* @param {{ filename?: string; rootDir?: string; modern?: boolean }} [options]
105-
* @returns {Root | LegacyRoot}
106+
* @returns {AST.Root | LegacyRoot}
106107
*/
107108
export function parse(source, { filename, rootDir, modern } = {}) {
108109
state.reset_warning_filter(() => false);
@@ -114,7 +115,7 @@ export function parse(source, { filename, rootDir, modern } = {}) {
114115

115116
/**
116117
* @param {string} source
117-
* @param {Root} ast
118+
* @param {AST.Root} ast
118119
* @param {boolean | undefined} modern
119120
*/
120121
function to_public_ast(source, ast, modern) {

packages/svelte/src/compiler/legacy.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @import { Expression } from 'estree' */
2-
/** @import { BaseNode, ConstTag, Root, SvelteNode, TemplateNode, Text } from '#compiler' */
2+
/** @import { AST, SvelteNode, TemplateNode } from '#compiler' */
33
/** @import * as Legacy from './types/legacy-nodes.js' */
44
import { walk } from 'zimmerframe';
55
import {
@@ -36,7 +36,7 @@ function remove_surrounding_whitespace_nodes(nodes) {
3636
/**
3737
* Transform our nice modern AST into the monstrosity emitted by Svelte 4
3838
* @param {string} source
39-
* @param {Root} ast
39+
* @param {AST.Root} ast
4040
* @returns {Legacy.LegacyRoot}
4141
*/
4242
export function convert(source, ast) {
@@ -74,8 +74,8 @@ export function convert(source, ast) {
7474
let end = null;
7575

7676
if (node.fragment.nodes.length > 0) {
77-
const first = /** @type {BaseNode} */ (node.fragment.nodes.at(0));
78-
const last = /** @type {BaseNode} */ (node.fragment.nodes.at(-1));
77+
const first = /** @type {AST.BaseNode} */ (node.fragment.nodes.at(0));
78+
const last = /** @type {AST.BaseNode} */ (node.fragment.nodes.at(-1));
7979

8080
start = first.start;
8181
end = last.end;
@@ -242,7 +242,7 @@ export function convert(source, ast) {
242242
return node;
243243
}
244244

245-
const modern_node = /** @type {ConstTag} */ (node);
245+
const modern_node = /** @type {AST.ConstTag} */ (node);
246246
const { id: left } = { ...modern_node.declaration.declarations[0] };
247247
// @ts-ignore
248248
delete left.typeAnnotation;
@@ -584,7 +584,7 @@ export function convert(source, ast) {
584584
const parent = path.at(-1);
585585
if (parent?.type === 'RegularElement' && parent.name === 'style') {
586586
// these text nodes are missing `raw` for some dumb reason
587-
return /** @type {Text} */ ({
587+
return /** @type {AST.Text} */ ({
588588
type: 'Text',
589589
start: node.start,
590590
end: node.end,

packages/svelte/src/compiler/migrate/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** @import { Visitors } from 'zimmerframe' */
33
/** @import { ComponentAnalysis } from '../phases/types.js' */
44
/** @import { Scope } from '../phases/scope.js' */
5-
/** @import * as Compiler from '#compiler' */
5+
/** @import { AST, Binding, SvelteNode, ValidatedCompileOptions } from '#compiler' */
66
import MagicString from 'magic-string';
77
import { walk } from 'zimmerframe';
88
import { parse } from '../phases/1-parse/index.js';
@@ -30,7 +30,7 @@ export function migrate(source) {
3030

3131
const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};
3232

33-
/** @type {Compiler.ValidatedCompileOptions} */
33+
/** @type {ValidatedCompileOptions} */
3434
const combined_options = {
3535
...validate_component_options({}, ''),
3636
...parsed_options,
@@ -160,7 +160,7 @@ export function migrate(source) {
160160
let needs_reordering = false;
161161

162162
for (const [node, { dependencies }] of state.analysis.reactive_statements) {
163-
/** @type {Compiler.Binding[]} */
163+
/** @type {Binding[]} */
164164
let ids = [];
165165
if (
166166
node.body.type === 'ExpressionStatement' &&
@@ -229,7 +229,7 @@ export function migrate(source) {
229229
* }} State
230230
*/
231231

232-
/** @type {Visitors<Compiler.SvelteNode, State>} */
232+
/** @type {Visitors<SvelteNode, State>} */
233233
const instance_script = {
234234
_(node, { state, next }) {
235235
// @ts-expect-error
@@ -335,7 +335,7 @@ const instance_script = {
335335
// }
336336
}
337337

338-
const binding = /** @type {Compiler.Binding} */ (state.scope.get(declarator.id.name));
338+
const binding = /** @type {Binding} */ (state.scope.get(declarator.id.name));
339339

340340
if (state.analysis.uses_props && (declarator.init || binding.updated)) {
341341
throw new Error(
@@ -478,7 +478,7 @@ const instance_script = {
478478
}
479479
};
480480

481-
/** @type {Visitors<Compiler.SvelteNode, State>} */
481+
/** @type {Visitors<SvelteNode, State>} */
482482
const template = {
483483
Identifier(node, { state, path }) {
484484
handle_identifier(node, state, path);
@@ -596,7 +596,7 @@ const template = {
596596
/**
597597
* @param {VariableDeclarator} declarator
598598
* @param {MagicString} str
599-
* @param {Array<Compiler.SvelteNode>} path
599+
* @param {SvelteNode[]} path
600600
*/
601601
function extract_type_and_comment(declarator, str, path) {
602602
const parent = path.at(-1);
@@ -641,11 +641,11 @@ function extract_type_and_comment(declarator, str, path) {
641641
}
642642

643643
/**
644-
* @param {Compiler.RegularElement | Compiler.SvelteElement | Compiler.SvelteWindow | Compiler.SvelteDocument | Compiler.SvelteBody} element
644+
* @param {AST.RegularElement | AST.SvelteElement | AST.SvelteWindow | AST.SvelteDocument | AST.SvelteBody} element
645645
* @param {State} state
646646
*/
647647
function handle_events(element, state) {
648-
/** @type {Map<string, Compiler.OnDirective[]>} */
648+
/** @type {Map<string, AST.OnDirective[]>} */
649649
const handlers = new Map();
650650
for (const attribute of element.attributes) {
651651
if (attribute.type !== 'OnDirective') continue;
@@ -880,7 +880,7 @@ function get_node_range(source, node) {
880880
}
881881

882882
/**
883-
* @param {Compiler.OnDirective} last
883+
* @param {AST.OnDirective} last
884884
* @param {State} state
885885
*/
886886
function generate_event_name(last, state) {

packages/svelte/src/compiler/phases/1-parse/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { TemplateNode, Fragment, Root, SvelteOptionsRaw } from '#compiler' */
1+
/** @import { AST, TemplateNode } from '#compiler' */
22
// @ts-expect-error acorn type definitions are borked in the release we use
33
import { isIdentifierStart, isIdentifierChar } from 'acorn';
44
import fragment from './state/fragment.js';
@@ -31,10 +31,10 @@ export class Parser {
3131
/** @type {TemplateNode[]} */
3232
stack = [];
3333

34-
/** @type {Fragment[]} */
34+
/** @type {AST.Fragment[]} */
3535
fragments = [];
3636

37-
/** @type {Root} */
37+
/** @type {AST.Root} */
3838
root;
3939

4040
/** @type {Record<string, boolean>} */
@@ -122,7 +122,7 @@ export class Parser {
122122
(thing) => thing.type === 'SvelteOptions'
123123
);
124124
if (options_index !== -1) {
125-
const options = /** @type {SvelteOptionsRaw} */ (this.root.fragment.nodes[options_index]);
125+
const options = /** @type {AST.SvelteOptionsRaw} */ (this.root.fragment.nodes[options_index]);
126126
this.root.fragment.nodes.splice(options_index, 1);
127127
this.root.options = read_options(options);
128128

@@ -292,7 +292,7 @@ export class Parser {
292292

293293
/**
294294
* @param {string} template
295-
* @returns {Root}
295+
* @returns {AST.Root}
296296
*/
297297
export function parse(template) {
298298
const parser = new Parser(template);

packages/svelte/src/compiler/phases/1-parse/read/options.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/** @import { ObjectExpression } from 'estree' */
2-
/** @import { SvelteOptionsRaw, Root, SvelteOptions } from '#compiler' */
2+
/** @import { AST } from '#compiler' */
33
import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js';
44
import * as e from '../../../errors.js';
55

66
const regex_valid_tag_name = /^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/;
77

88
/**
9-
* @param {SvelteOptionsRaw} node
10-
* @returns {Root['options']}
9+
* @param {AST.SvelteOptionsRaw} node
10+
* @returns {AST.Root['options']}
1111
*/
1212
export default function read_options(node) {
13-
/** @type {SvelteOptions} */
13+
/** @type {AST.SvelteOptions} */
1414
const component_options = {
1515
start: node.start,
1616
end: node.end,
@@ -39,7 +39,7 @@ export default function read_options(node) {
3939
break; // eslint doesn't know this is unnecessary
4040
}
4141
case 'customElement': {
42-
/** @type {SvelteOptions['customElement']} */
42+
/** @type {AST.SvelteOptions['customElement']} */
4343
const ce = {};
4444
const { value: v } = attribute;
4545
const value = v === true || Array.isArray(v) ? v : [v];

packages/svelte/src/compiler/phases/1-parse/read/script.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @import { Program } from 'estree' */
2-
/** @import { Attribute, SpreadAttribute, Directive, Script } from '#compiler' */
2+
/** @import { AST, Directive } from '#compiler' */
33
/** @import { Parser } from '../index.js' */
44
import * as acorn from '../acorn.js';
55
import { regex_not_newline_characters } from '../../patterns.js';
@@ -16,8 +16,8 @@ const ALLOWED_ATTRIBUTES = ['context', 'generics', 'lang', 'module'];
1616
/**
1717
* @param {Parser} parser
1818
* @param {number} start
19-
* @param {Array<Attribute | SpreadAttribute | Directive>} attributes
20-
* @returns {Script}
19+
* @param {Array<AST.Attribute | AST.SpreadAttribute | Directive>} attributes
20+
* @returns {AST.Script}
2121
*/
2222
export function read_script(parser, start, attributes) {
2323
const script_start = parser.index;
@@ -45,7 +45,7 @@ export function read_script(parser, start, attributes) {
4545
/** @type {'default' | 'module'} */
4646
let context = 'default';
4747

48-
for (const attribute of /** @type {Attribute[]} */ (attributes)) {
48+
for (const attribute of /** @type {AST.Attribute[]} */ (attributes)) {
4949
if (RESERVED_ATTRIBUTES.includes(attribute.name)) {
5050
e.script_reserved_attribute(attribute, attribute.name);
5151
}

packages/svelte/src/compiler/phases/1-parse/read/style.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { Attribute, SpreadAttribute, Directive, Css } from '#compiler' */
1+
/** @import { AST, Css, Directive } from '#compiler' */
22
/** @import { Parser } from '../index.js' */
33
import * as e from '../../../errors.js';
44

@@ -18,7 +18,7 @@ const REGEX_HTML_COMMENT_CLOSE = /-->/;
1818
/**
1919
* @param {Parser} parser
2020
* @param {number} start
21-
* @param {Array<Attribute | SpreadAttribute | Directive>} attributes
21+
* @param {Array<AST.Attribute | AST.SpreadAttribute | Directive>} attributes
2222
* @returns {Css.StyleSheet}
2323
*/
2424
export default function read_style(parser, start, attributes) {

0 commit comments

Comments
 (0)