Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar committed Oct 28, 2024
1 parent 5ea501b commit 9c8dbb0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
32 changes: 5 additions & 27 deletions src/print-ast.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import type { AST } from 'svelte/src/compiler/types/template.js';
import type { AST } from 'svelte/src/compiler/types/template.js';
import { DefaultPrinterIdentOptions, PrinterIdentOptions, printHtml, printScript } from './index.js';

export interface PrintAstParams {
ast: AST.Root;
indent?: PrinterIdentOptions;
}

export default function printAst(params: PrintAstParams): string {
const { ast } = params;
const ident = {
...DefaultPrinterIdentOptions,
...params.indent
};

const markup = printHtml({
rootNode: ast.html,
ident
});

const scriptInstance = ast.instance ? printScript({ script: ast.instance, ident }) : '';
const scriptModule = ast.module ? printScript({ script: ast.module, ident }) : '';

let result = '';

result += scriptModule + ident.lineEnd;
result += scriptInstance + ident.lineEnd;
result += markup;
export default function printAst(root: AST.Root, indent: PrinterIdentOptions = DefaultPrinterIdentOptions): string {
const markup = printHtml(root, indent);
const script = printScript(root, indent);

return result;
return script + indent.lineEnd + markup;
}
13 changes: 5 additions & 8 deletions src/tests/print-ast.test.ts_ → src/tests/print-ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { describe, expect, test } from 'vitest';
import printAst from '../print-ast';

function testPrintAst(code: string, expectedResult?: string) {
const ast = parse(code, { modern: false });
const result = printAst({
ast,
indent: {
indent: '',
lineEnd: ''
}
const root = parse(code, { modern: true });
const result = printAst(root, {
indent: '',
lineEnd: ''
});
expect(result).toBe(expectedResult ?? code);
parse(result);
Expand All @@ -21,6 +18,6 @@ describe('print-ast', () => {
});

test('with module', () => {
testPrintAst('<script context="module">let b;</script><script>let a;</script><main>Hello,World</main>');
testPrintAst('<script>let a;</script><script context="module">let b;</script><main>Hello,World</main>');
});
});

0 comments on commit 9c8dbb0

Please sign in to comment.