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

fix: using svelte public compiler types #9

Merged
merged 1 commit into from
Oct 28, 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
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
ecmaVersion: 2021
},
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'unused-imports/no-unused-imports-ts': 'error',
'@typescript-eslint/no-unused-vars': [
'warn',
Expand Down
2 changes: 1 addition & 1 deletion src/print-ast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AST } from 'svelte/src/compiler/types/template.js';
import type { AST } from 'svelte/compiler';
import { DefaultPrinterIdentOptions, PrinterIdentOptions, printHtml, printScript } from './index.js';

export default function printAst(root: AST.Root, indent: PrinterIdentOptions = DefaultPrinterIdentOptions): string {
Expand Down
99 changes: 71 additions & 28 deletions src/print-html.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
import { generate } from 'astring';
import _, { isArray } from 'lodash';
import { walk } from 'estree-walker';
import type { TemplateNode, AST, SvelteNode, Directive, ElementLike } from 'svelte/src/compiler/types/template.js';
import { AST } from 'svelte/compiler';
import { DefaultPrinterIdentOptions, PrinterIdentOptions } from './index.js';
import { Node } from 'estree';
export type Write = (text: string) => void;

type ElementLike =
| AST.Component
| AST.TitleElement
| AST.SlotElement
| AST.RegularElement
| AST.SvelteBody
| AST.SvelteComponent
| AST.SvelteDocument
| AST.SvelteElement
| AST.SvelteFragment
| AST.SvelteHead
| AST.SvelteOptionsRaw
| AST.SvelteSelf
| AST.SvelteWindow;

type Directive =
| AST.AnimateDirective
| AST.BindDirective
| AST.ClassDirective
| AST.LetDirective
| AST.OnDirective
| AST.StyleDirective
| AST.TransitionDirective
| AST.UseDirective;

type Tag = AST.ExpressionTag | AST.HtmlTag | AST.ConstTag | AST.DebugTag | AST.RenderTag;
type Block = AST.EachBlock | AST.IfBlock | AST.AwaitBlock | AST.KeyBlock | AST.SnippetBlock;

type TemplateNode =
| AST.Root
| AST.Text
| Tag
| ElementLike
| AST.Attribute
| AST.SpreadAttribute
| Directive
| AST.Comment
| Block;

type SvelteNode = Node | TemplateNode | AST.Fragment | any;

const HTML_VOID_ELEMENTS = new Set([
'area',
'base',
Expand All @@ -23,6 +65,7 @@ const HTML_VOID_ELEMENTS = new Set([
]);

export interface PrinterContext {
/* eslint-disable unused-imports/no-unused-imports-ts */
_this: any;
write: Write;
indent: PrinterIdentOptions;
Expand All @@ -36,12 +79,12 @@ export abstract class BaseHtmlNodePrinter {
}

class FragmentPrinter extends BaseHtmlNodePrinter {
enter(_: any, __: any, ___: PrinterContext) {}
leave(_: any, __: any, ___: PrinterContext) {}
enter(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

class ExpressionTagPrinter extends BaseHtmlNodePrinter {
enter(node: AST.ExpressionTag, __: any, context: PrinterContext) {
enter(node: AST.ExpressionTag, __: SvelteNode, context: PrinterContext) {
const { write } = context;
write(`{${generate(node.expression, context.indent)}}`);

Expand All @@ -50,7 +93,7 @@ class ExpressionTagPrinter extends BaseHtmlNodePrinter {
expression: undefined
});
}
leave(_: any, __: any, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

class ElementPrinter extends BaseHtmlNodePrinter {
Expand Down Expand Up @@ -141,7 +184,7 @@ class ElementPrinter extends BaseHtmlNodePrinter {
}
}

enter(node: ElementLike, _: any, context: PrinterContext) {
enter(node: ElementLike, _: SvelteNode, context: PrinterContext) {
const { write } = context;

write(`<${node.name}`);
Expand Down Expand Up @@ -221,18 +264,18 @@ class TextPrinter extends BaseHtmlNodePrinter {
}
}

leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

class NoOpPrinter extends BaseHtmlNodePrinter {
enter(node: TemplateNode, parent: TemplateNode, context: PrinterContext) {
enter(node: SvelteNode, parent: SvelteNode, context: PrinterContext) {
context._this.skip();
}
leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

class CommentPrinter extends BaseHtmlNodePrinter {
enter(node: AST.Comment, __: any, context: PrinterContext) {
enter(node: AST.Comment, __: SvelteNode, context: PrinterContext) {
const { write } = context;

const comment = _.trim(node.data);
Expand All @@ -242,7 +285,7 @@ class CommentPrinter extends BaseHtmlNodePrinter {
write(context.indent.lineEnd);
}
}
leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

class IfBlockPrinter extends BaseHtmlNodePrinter {
Expand Down Expand Up @@ -285,7 +328,7 @@ class IfBlockPrinter extends BaseHtmlNodePrinter {
}

class EachBlockPrinter extends BaseHtmlNodePrinter {
enter(node: AST.EachBlock, parent: TemplateNode, context: PrinterContext) {
enter(node: AST.EachBlock, parent: SvelteNode, context: PrinterContext) {
const { write } = context;

write(`{#each ${generate(node.expression, context.indent)}`);
Expand All @@ -311,14 +354,14 @@ class EachBlockPrinter extends BaseHtmlNodePrinter {
key: undefined
});
}
leave(node: TemplateNode, parent: TemplateNode, context: PrinterContext) {
leave(node: SvelteNode, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write('{/each}');
}
}

class AwaitBlockPrinter extends BaseHtmlNodePrinter {
enter(node: AST.AwaitBlock, parent: TemplateNode, context: PrinterContext) {
enter(node: AST.AwaitBlock, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write(`{#await ${generate(node.expression, context.indent)}}`);

Expand Down Expand Up @@ -356,14 +399,14 @@ class AwaitBlockPrinter extends BaseHtmlNodePrinter {
error: undefined
});
}
leave(node: TemplateNode, parent: TemplateNode, context: PrinterContext) {
leave(node: SvelteNode, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write(`{/await}`);
}
}

class KeyBlockPrinter extends BaseHtmlNodePrinter {
enter(node: AST.KeyBlock, parent: TemplateNode, context: PrinterContext) {
enter(node: AST.KeyBlock, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write(`{#key ${generate(node.expression, context.indent)}}`);

Expand All @@ -372,51 +415,51 @@ class KeyBlockPrinter extends BaseHtmlNodePrinter {
expression: undefined
});
}
leave(node: TemplateNode, parent: TemplateNode, context: PrinterContext) {
leave(node: SvelteNode, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write('{/key}');
}
}

class HtmlTagPrinter extends BaseHtmlNodePrinter {
enter(node: AST.HtmlTag, parent: TemplateNode, context: PrinterContext) {
enter(node: AST.HtmlTag, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write(`{@html ${generate(node.expression, context.indent)}}`);
context._this.replace({
...node,
expression: undefined
});
}
leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

class DebugTagPrinter extends BaseHtmlNodePrinter {
enter(node: AST.DebugTag, parent: TemplateNode, context: PrinterContext) {
enter(node: AST.DebugTag, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write(`{@debug ${node.identifiers.map((id: any) => generate(id, context.indent)).join(', ')}}`);
write(`{@debug ${node.identifiers.map(id => generate(id, context.indent)).join(', ')}}`);

context._this.replace({
...node,
identifiers: undefined
});
}
leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

class ConstTagPrinter extends BaseHtmlNodePrinter {
enter(node: AST.ConstTag, parent: TemplateNode, context: PrinterContext) {
enter(node: AST.ConstTag, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write(`{@const${generate(node.declaration, context.indent).replace(/;|const/g, '')}}`);
context._this.replace({
...node,
declaration: undefined
});
}
leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

class SnippetBlockPrinter extends BaseHtmlNodePrinter {
enter(node: AST.SnippetBlock, parent: TemplateNode, context: PrinterContext) {
enter(node: AST.SnippetBlock, parent: SvelteNode, context: PrinterContext) {
const { write } = context;

write(
Expand All @@ -431,14 +474,14 @@ class SnippetBlockPrinter extends BaseHtmlNodePrinter {
parameters: undefined
});
}
leave(node: AST.SnippetBlock, parent: TemplateNode, context: PrinterContext) {
leave(node: AST.SnippetBlock, parent: SvelteNode, context: PrinterContext) {
const { write } = context;
write(`{/snippet}`);
}
}

class RenderTagPrinter extends BaseHtmlNodePrinter {
enter(node: AST.RenderTag, parent: TemplateNode, context: PrinterContext) {
enter(node: AST.RenderTag, parent: SvelteNode, context: PrinterContext) {
const { write } = context;

write(`{@render ${generate(node.expression, context.indent)}}`);
Expand All @@ -448,7 +491,7 @@ class RenderTagPrinter extends BaseHtmlNodePrinter {
expression: undefined
});
}
leave(_: TemplateNode, __: TemplateNode, ___: PrinterContext) {}
leave(_: SvelteNode, __: SvelteNode, ___: PrinterContext) {}
}

const NoOp = new NoOpPrinter();
Expand Down
2 changes: 1 addition & 1 deletion src/print-script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generate } from 'astring';
import type { AST } from 'svelte/src/compiler/types/index.js';
import type { AST } from 'svelte/compiler';
import { DefaultPrinterIdentOptions, PrinterIdentOptions } from './index.js';

export default function printScript(root: AST.Root, indent: PrinterIdentOptions = DefaultPrinterIdentOptions): string {
Expand Down
Loading