Skip to content
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

feat: provide AST node types with internal types stripped out #12967

Closed
wants to merge 3 commits 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/young-feet-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: provide AST node types with internal types stripped out
4 changes: 4 additions & 0 deletions packages/svelte/scripts/generate-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fs.writeFileSync(`${dir}/types/compiler/interfaces.d.ts`, "import '../index.js';

await createBundle({
output: `${dir}/types/index.d.ts`,
compilerOptions: {
// so that types/properties with `@internal` (and its dependencies) are removed from the output
stripInternal: true
},
modules: {
[pkg.name]: `${dir}/src/index.d.ts`,
[`${pkg.name}/action`]: `${dir}/src/action/public.d.ts`,
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function compileModule(source, options) {
* @overload
* @param {string} source
* @param {{ filename?: string; modern?: false }} [options]
* @returns {LegacyRoot}
* @returns {Record<string, any>}
*/

/**
Expand All @@ -102,7 +102,7 @@ export function compileModule(source, options) {
* https://svelte.dev/docs/svelte-compiler#svelte-parse
* @param {string} source
* @param {{ filename?: string; rootDir?: string; modern?: boolean }} [options]
* @returns {Root | LegacyRoot}
* @returns {Root | Record<string, any>}
*/
export function parse(source, { filename, rootDir, modern } = {}) {
state.reset_warning_filter(() => false);
Expand Down
40 changes: 40 additions & 0 deletions packages/svelte/src/compiler/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,43 @@ export type {
CompileResult,
Warning
} from './types/index';

export type {
AnimateDirective,
Attribute,
AwaitBlock,
BindDirective,
ClassDirective,
Comment,
Component,
ConstTag,
DebugTag,
EachBlock,
ExpressionTag,
Fragment,
HtmlTag,
IfBlock,
KeyBlock,
LetDirective,
OnDirective,
RegularElement,
RenderTag,
Root,
Script,
SlotElement,
StyleDirective,
SvelteBody,
SvelteComponent,
SvelteDocument,
SvelteElement,
SvelteFragment,
SvelteHead,
SvelteWindow,
SvelteSelf,
SnippetBlock,
SpreadAttribute,
Text,
TitleElement,
TransitionDirective,
UseDirective
} from './types/template';
34 changes: 25 additions & 9 deletions packages/svelte/src/compiler/types/template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ export interface BaseNode {
type: string;
start: number;
end: number;
/** This is set during parsing on elements/components/expressions/text (but not attributes etc) */
/** @internal This is set during parsing on elements/components/expressions/text (but not attributes etc) */
parent: SvelteNode | null;
}

export interface Fragment {
type: 'Fragment';
nodes: Array<Text | Tag | ElementLike | Block | Comment>;
/** @internal */
metadata: {
/**
* Fragments declare their own scopes. A transparent fragment is one whose scope
Expand Down Expand Up @@ -63,6 +64,7 @@ export interface Root extends BaseNode {
instance: Script | null;
/** The parsed `<script module>` element, if exists */
module: Script | null;
/** @internal */
metadata: {
/** Whether the component was parsed with typescript */
ts: boolean;
Expand Down Expand Up @@ -115,6 +117,7 @@ export interface Text extends BaseNode {
export interface ExpressionTag extends BaseNode {
type: 'ExpressionTag';
expression: Expression;
/** @internal */
metadata: {
expression: ExpressionMetadata;
};
Expand Down Expand Up @@ -152,6 +155,7 @@ export interface DebugTag extends BaseNode {
export interface RenderTag extends BaseNode {
type: 'RenderTag';
expression: SimpleCallExpression | (ChainExpression & { expression: SimpleCallExpression });
/** @internal */
metadata: {
dynamic: boolean;
args_with_call_expression: Set<number>;
Expand All @@ -161,7 +165,7 @@ export interface RenderTag extends BaseNode {
type Tag = ExpressionTag | HtmlTag | ConstTag | DebugTag | RenderTag;

/** An `animate:` directive */
interface AnimateDirective extends BaseNode {
export interface AnimateDirective extends BaseNode {
type: 'AnimateDirective';
/** The 'x' in `animate:x` */
name: string;
Expand All @@ -176,6 +180,7 @@ export interface BindDirective extends BaseNode {
name: string;
/** The y in `bind:x={y}` */
expression: Identifier | MemberExpression;
/** @internal */
metadata: {
binding_group_name: Identifier;
parent_each_blocks: EachBlock[];
Expand All @@ -189,6 +194,7 @@ export interface ClassDirective extends BaseNode {
name: 'class';
/** The 'y' in `class:x={y}`, or the `x` in `class:x` */
expression: Expression;
/** @internal */
metadata: {
expression: ExpressionMetadata;
};
Expand All @@ -211,6 +217,7 @@ export interface OnDirective extends BaseNode {
/** The 'y' in `on:x={y}` */
expression: null | Expression;
modifiers: string[]; // TODO specify
/** @internal */
metadata: {
expression: ExpressionMetadata;
};
Expand All @@ -231,14 +238,15 @@ export interface StyleDirective extends BaseNode {
/** The 'y' in `style:x={y}` */
value: true | ExpressionTag | Array<ExpressionTag | Text>;
modifiers: Array<'important'>;
/** @internal */
metadata: {
expression: ExpressionMetadata;
};
}

// TODO have separate in/out/transition directives
/** A `transition:`, `in:` or `out:` directive */
interface TransitionDirective extends BaseNode {
export interface TransitionDirective extends BaseNode {
type: 'TransitionDirective';
/** The 'x' in `transition:x` */
name: string;
Expand All @@ -252,7 +260,7 @@ interface TransitionDirective extends BaseNode {
}

/** A `use:` directive */
interface UseDirective extends BaseNode {
export interface UseDirective extends BaseNode {
type: 'UseDirective';
/** The 'x' in `use:x` */
name: string;
Expand All @@ -278,13 +286,14 @@ interface BaseElement extends BaseNode {

export interface Component extends BaseElement {
type: 'Component';
/** @internal */
metadata: {
scopes: Record<string, Scope>;
dynamic: boolean;
};
}

interface TitleElement extends BaseElement {
export interface TitleElement extends BaseElement {
type: 'TitleElement';
name: 'title';
}
Expand All @@ -296,6 +305,7 @@ export interface SlotElement extends BaseElement {

export interface RegularElement extends BaseElement {
type: 'RegularElement';
/** @internal */
metadata: {
/** `true` if this is an svg element */
svg: boolean;
Expand All @@ -307,7 +317,7 @@ export interface RegularElement extends BaseElement {
};
}

interface SvelteBody extends BaseElement {
export interface SvelteBody extends BaseElement {
type: 'SvelteBody';
name: 'svelte:body';
}
Expand All @@ -316,12 +326,13 @@ export interface SvelteComponent extends BaseElement {
type: 'SvelteComponent';
name: 'svelte:component';
expression: Expression;
/** @internal */
metadata: {
scopes: Record<string, Scope>;
};
}

interface SvelteDocument extends BaseElement {
export interface SvelteDocument extends BaseElement {
type: 'SvelteDocument';
name: 'svelte:document';
}
Expand All @@ -330,6 +341,7 @@ export interface SvelteElement extends BaseElement {
type: 'SvelteElement';
name: 'svelte:element';
tag: Expression;
/** @internal */
metadata: {
/**
* `true` if this is an svg element. The boolean may not be accurate because
Expand All @@ -350,7 +362,7 @@ export interface SvelteFragment extends BaseElement {
name: 'svelte:fragment';
}

interface SvelteHead extends BaseElement {
export interface SvelteHead extends BaseElement {
type: 'SvelteHead';
name: 'svelte:head';
}
Expand All @@ -364,12 +376,13 @@ export interface SvelteOptionsRaw extends BaseElement {
export interface SvelteSelf extends BaseElement {
type: 'SvelteSelf';
name: 'svelte:self';
/** @internal */
metadata: {
scopes: Record<string, Scope>;
};
}

interface SvelteWindow extends BaseElement {
export interface SvelteWindow extends BaseElement {
type: 'SvelteWindow';
name: 'svelte:window';
}
Expand Down Expand Up @@ -398,6 +411,7 @@ export interface EachBlock extends BaseNode {
fallback?: Fragment;
index?: string;
key?: Expression;
/** @internal */
metadata: {
expression: ExpressionMetadata;
keyed: boolean;
Expand Down Expand Up @@ -458,6 +472,7 @@ export interface Attribute extends BaseNode {
type: 'Attribute';
name: string;
value: true | ExpressionTag | Array<Text | ExpressionTag>;
/** @internal */
metadata: {
expression: ExpressionMetadata;
/** May be set if this is an event attribute */
Expand All @@ -468,6 +483,7 @@ export interface Attribute extends BaseNode {
export interface SpreadAttribute extends BaseNode {
type: 'SpreadAttribute';
expression: Expression;
/** @internal */
metadata: {
expression: ExpressionMetadata;
};
Expand Down
Loading
Loading