diff --git a/.changeset/little-ties-jam.md b/.changeset/little-ties-jam.md new file mode 100644 index 0000000000..1e730a9fc2 --- /dev/null +++ b/.changeset/little-ties-jam.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: update return type of `parse` function diff --git a/packages/svelte/src/compiler/index.js b/packages/svelte/src/compiler/index.js index 5eb22bcd0a..e19955a523 100644 --- a/packages/svelte/src/compiler/index.js +++ b/packages/svelte/src/compiler/index.js @@ -87,7 +87,7 @@ export function compileModule(source, options) { * @overload * @param {string} source * @param {{ filename?: string; modern?: false }} [options] - * @returns {Record} + * @returns {LegacyRoot} */ /** diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index 22b47d35ec..819d4db4c6 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -570,7 +570,7 @@ declare module 'svelte/animate' { } declare module 'svelte/compiler' { - import type { Expression, Identifier, ArrayExpression, ArrowFunctionExpression, VariableDeclaration, VariableDeclarator, MemberExpression, ObjectExpression, Pattern, Program, ChainExpression, SimpleCallExpression } from 'estree'; + import type { Expression, Identifier, ArrayExpression, ArrowFunctionExpression, VariableDeclaration, VariableDeclarator, MemberExpression, ObjectExpression, Pattern, Program, ChainExpression, SimpleCallExpression, AssignmentExpression } from 'estree'; import type { SourceMap } from 'magic-string'; import type { Location } from 'locate-character'; /** @@ -607,7 +607,7 @@ declare module 'svelte/compiler' { export function parse(source: string, options?: { filename?: string; modern?: false; - } | undefined): Record; + } | undefined): LegacyRoot; /** * @deprecated Replace this with `import { walk } from 'estree-walker'` * */ @@ -1267,6 +1267,255 @@ declare module 'svelte/compiler' { export function migrate(source: string): { code: string; }; + interface BaseNode { + type: string; + start: number; + end: number; + } + + interface BaseElement extends BaseNode { + name: string; + attributes: Array; + children: Array; + } + + interface LegacyRoot extends BaseNode { + html: LegacySvelteNode; + css?: any; + instance?: any; + module?: any; + } + + interface LegacyAction extends BaseNode { + type: 'Action'; + /** The 'x' in `use:x` */ + name: string; + /** The 'y' in `use:x={y}` */ + expression: null | Expression; + } + + interface LegacyAnimation extends BaseNode { + type: 'Animation'; + /** The 'x' in `animate:x` */ + name: string; + /** The y in `animate:x={y}` */ + expression: null | Expression; + } + + interface LegacyBinding extends BaseNode { + type: 'Binding'; + /** The 'x' in `bind:x` */ + name: string; + /** The y in `bind:x={y}` */ + expression: Identifier | MemberExpression; + } + + interface LegacyBody extends BaseElement { + type: 'Body'; + name: 'svelte:body'; + } + + interface LegacyAttribute extends BaseNode { + type: 'Attribute'; + name: string; + value: true | Array; + } + + interface LegacyAttributeShorthand extends BaseNode { + type: 'AttributeShorthand'; + expression: Expression; + } + + interface LegacyLet extends BaseNode { + type: 'Let'; + /** The 'x' in `let:x` */ + name: string; + /** The 'y' in `let:x={y}` */ + expression: null | Identifier | ArrayExpression | ObjectExpression; + } + + interface LegacyCatchBlock extends BaseNode { + type: 'CatchBlock'; + children: LegacySvelteNode[]; + skip: boolean; + } + + interface LegacyClass extends BaseNode { + type: 'Class'; + /** The 'x' in `class:x` */ + name: 'class'; + /** The 'y' in `class:x={y}`, or the `x` in `class:x` */ + expression: Expression; + } + + interface LegacyDocument extends BaseElement { + type: 'Document'; + } + + interface LegacyElement { + type: 'Element'; + } + + interface LegacyEventHandler extends BaseNode { + type: 'EventHandler'; + /** The 'x' in `on:x` */ + name: string; + /** The 'y' in `on:x={y}` */ + expression: null | Expression; + modifiers: string[]; + } + + interface LegacyHead extends BaseElement { + type: 'Head'; + } + + interface LegacyInlineComponent extends BaseElement { + type: 'InlineComponent'; + /** Set if this is a `` */ + expression?: Expression; + } + + interface LegacyMustacheTag extends BaseNode { + type: 'MustacheTag'; + expression: Expression; + } + + interface LegacyOptions { + type: 'Options'; + name: 'svelte:options'; + attributes: Array; + } + + interface LegacyPendingBlock extends BaseNode { + type: 'PendingBlock'; + children: LegacySvelteNode[]; + skip: boolean; + } + + interface LegacyRawMustacheTag extends BaseNode { + type: 'RawMustacheTag'; + expression: Expression; + } + + interface LegacySpread extends BaseNode { + type: 'Spread'; + expression: Expression; + } + + interface LegacySlot extends BaseElement { + type: 'Slot'; + } + + interface LegacySlotTemplate extends BaseElement { + type: 'SlotTemplate'; + } + + interface LegacyThenBlock extends BaseNode { + type: 'ThenBlock'; + children: LegacySvelteNode[]; + skip: boolean; + } + + interface LegacyTitle extends BaseElement { + type: 'Title'; + name: 'title'; + } + + interface LegacyConstTag extends BaseNode { + type: 'ConstTag'; + expression: AssignmentExpression; + } + + interface LegacyTransition extends BaseNode { + type: 'Transition'; + /** The 'x' in `transition:x` */ + name: string; + /** The 'y' in `transition:x={y}` */ + expression: null | Expression; + modifiers: Array<'local' | 'global'>; + /** True if this is a `transition:` or `in:` directive */ + intro: boolean; + /** True if this is a `transition:` or `out:` directive */ + outro: boolean; + } + + /** A `style:` directive */ + interface LegacyStyleDirective extends BaseNode { + type: 'StyleDirective'; + /** The 'x' in `style:x` */ + name: string; + /** The 'y' in `style:x={y}` */ + value: true | Array; + modifiers: Array<'important'>; + } + + interface LegacyWindow extends BaseElement { + type: 'Window'; + } + + interface LegacyComment extends BaseNode { + type: 'Comment'; + /** the contents of the comment */ + data: string; + /** any svelte-ignore directives — would result in ['a', 'b', 'c'] */ + ignores: string[]; + } + + type LegacyDirective = + | LegacyAnimation + | LegacyBinding + | LegacyClass + | LegacyLet + | LegacyEventHandler + | LegacyStyleDirective + | LegacyTransition + | LegacyAction; + + type LegacyAttributeLike = LegacyAttribute | LegacySpread | LegacyDirective; + + type LegacyElementLike = + | LegacyBody + | LegacyCatchBlock + | LegacyComment + | LegacyDocument + | LegacyElement + | LegacyHead + | LegacyInlineComponent + | LegacyMustacheTag + | LegacyOptions + | LegacyPendingBlock + | LegacyRawMustacheTag + | LegacySlot + | LegacySlotTemplate + | LegacyThenBlock + | LegacyTitle + | LegacyWindow; + + interface LegacyStyle extends BaseNode { + type: 'Style'; + attributes: any[]; + content: { + start: number; + end: number; + styles: string; + }; + children: any[]; + } + + interface LegacySelector extends BaseNode { + type: 'Selector'; + children: Array; + } + + type LegacyCssNode = LegacyStyle | LegacySelector; + + type LegacySvelteNode = + | LegacyConstTag + | LegacyElementLike + | LegacyAttributeLike + | LegacyAttributeShorthand + | LegacyCssNode + | AST.Text; namespace Css { export interface BaseNode { start: number;