diff --git a/.changeset/five-shoes-invent.md b/.changeset/five-shoes-invent.md new file mode 100644 index 00000000..a512724d --- /dev/null +++ b/.changeset/five-shoes-invent.md @@ -0,0 +1,5 @@ +--- +'sv': patch +--- + +fix: improve formatting on new script files diff --git a/packages/ast-tooling/index.ts b/packages/ast-tooling/index.ts index 110ca956..374eff5a 100644 --- a/packages/ast-tooling/index.ts +++ b/packages/ast-tooling/index.ts @@ -1,8 +1,8 @@ import { parse as tsParse } from 'recast/parsers/typescript.js'; -import { parse as recastParse, print as recastPrint } from 'recast'; -import { Document, Element, Text, type ChildNode } from 'domhandler'; +import { parse as recastParse, print as recastPrint, type Options as RecastOptions } from 'recast'; +import { Document, Element, type ChildNode } from 'domhandler'; import { ElementType, parseDocument } from 'htmlparser2'; -import { appendChild, prependChild, removeElement, textContent } from 'domutils'; +import { removeElement, textContent } from 'domutils'; import serializeDom from 'dom-serializer'; import { Root as CssAst, @@ -65,8 +65,17 @@ export function parseScript(content: string): AstTypes.Program { return recastOutput.program; } -export function serializeScript(ast: AstTypes.ASTNode): string { - return recastPrint(ast).code; +export function serializeScript(ast: AstTypes.ASTNode, previousContent?: string): string { + let options: RecastOptions | undefined; + if (!previousContent) { + // provide sensible defaults if we generate a new file + options = { + quote: 'single', + useTabs: true + }; + } + + return recastPrint(ast, options).code; } export function parseCss(content: string): CssAst { @@ -143,36 +152,6 @@ export function parseSvelte(content: string): SvelteAst { return { jsAst, htmlAst, cssAst }; } -export function serializeSvelte(asts: SvelteAst): string { - const { jsAst, htmlAst, cssAst } = asts; - - const css = serializeCss(cssAst); - const newScriptValue = serializeScript(jsAst); - - if (newScriptValue.length > 0) { - const scriptTag = new Element('script', {}, undefined, ElementType.ElementType.Script); - for (const child of scriptTag.children) { - removeElement(child); - } - - appendChild(scriptTag, new Text(newScriptValue)); - prependChild(htmlAst, scriptTag); - } - - if (css.length > 0) { - const styleTag = new Element('style', {}, undefined, ElementType.ElementType.Style); - for (const child of styleTag.children) { - removeElement(child); - } - - appendChild(styleTag, new Text(css)); - appendChild(htmlAst, styleTag); - } - - const content = serializeHtml(htmlAst); - return content; -} - export function parseJson(content: string): any { // some of the files we need to process contain comments. The default // node JSON.parse fails parsing those comments. diff --git a/packages/core/tests/js/arrays/object-array/output.ts b/packages/core/tests/js/arrays/object-array/output.ts index ed083891..c47de750 100644 --- a/packages/core/tests/js/arrays/object-array/output.ts +++ b/packages/core/tests/js/arrays/object-array/output.ts @@ -1,5 +1,5 @@ const array = [{ test: true }, { - test2: "string" -}]; + test2: 'string' +}]; \ No newline at end of file diff --git a/packages/core/tests/js/arrays/string-array/output.ts b/packages/core/tests/js/arrays/string-array/output.ts index 6aca6896..7d00f941 100644 --- a/packages/core/tests/js/arrays/string-array/output.ts +++ b/packages/core/tests/js/arrays/string-array/output.ts @@ -1 +1 @@ -const array = ["test", "test2"]; \ No newline at end of file +const array = ['test', 'test2']; \ No newline at end of file diff --git a/packages/core/tests/js/exports/default-export-with-variable/output.ts b/packages/core/tests/js/exports/default-export-with-variable/output.ts index 14a059d1..e3eafc9d 100644 --- a/packages/core/tests/js/exports/default-export-with-variable/output.ts +++ b/packages/core/tests/js/exports/default-export-with-variable/output.ts @@ -1,5 +1,5 @@ const object = { - test: "string" + test: 'string' }; export default object; \ No newline at end of file diff --git a/packages/core/tests/js/exports/default-export/output.ts b/packages/core/tests/js/exports/default-export/output.ts index 3811fee1..76088cc4 100644 --- a/packages/core/tests/js/exports/default-export/output.ts +++ b/packages/core/tests/js/exports/default-export/output.ts @@ -1,3 +1,3 @@ export default { - test: "string" + test: 'string' }; \ No newline at end of file diff --git a/packages/core/tests/js/exports/named-export/output.ts b/packages/core/tests/js/exports/named-export/output.ts index 937841dc..c8a12a18 100644 --- a/packages/core/tests/js/exports/named-export/output.ts +++ b/packages/core/tests/js/exports/named-export/output.ts @@ -1,7 +1,7 @@ export const variable = { - test: "string" + test: 'string' }; export const variable2 = { - test2: "string2" + test2: 'string2' }; \ No newline at end of file diff --git a/packages/core/tests/js/imports/default-import/output.ts b/packages/core/tests/js/imports/default-import/output.ts index aa937cdd..9203d582 100644 --- a/packages/core/tests/js/imports/default-import/output.ts +++ b/packages/core/tests/js/imports/default-import/output.ts @@ -1 +1 @@ -import MyPackage from "package"; \ No newline at end of file +import MyPackage from 'package'; \ No newline at end of file diff --git a/packages/core/tests/js/imports/empty-import/output.ts b/packages/core/tests/js/imports/empty-import/output.ts index a4b33a71..47f21090 100644 --- a/packages/core/tests/js/imports/empty-import/output.ts +++ b/packages/core/tests/js/imports/empty-import/output.ts @@ -1,2 +1,2 @@ -import "package/file.css"; -import "./relativ/file.css"; \ No newline at end of file +import 'package/file.css'; +import './relativ/file.css'; \ No newline at end of file diff --git a/packages/core/tests/js/imports/named-import/output.ts b/packages/core/tests/js/imports/named-import/output.ts index 2ae493d7..1a3abd90 100644 --- a/packages/core/tests/js/imports/named-import/output.ts +++ b/packages/core/tests/js/imports/named-import/output.ts @@ -1,2 +1,2 @@ -import { Handle } from "@sveltejs/kit"; -import { namedOne } from "package"; \ No newline at end of file +import { Handle } from '@sveltejs/kit'; +import { namedOne } from 'package'; \ No newline at end of file diff --git a/packages/core/tests/js/index.ts b/packages/core/tests/js/index.ts index a1ab2ed5..fc93e163 100644 --- a/packages/core/tests/js/index.ts +++ b/packages/core/tests/js/index.ts @@ -15,14 +15,14 @@ for (const categoryDirectory of categoryDirectories) { const testDirectoryPath = join(baseDir, categoryDirectory, testName); const inputFilePath = join(testDirectoryPath, 'input.ts'); - const input = fs.existsSync(inputFilePath) ? fs.readFileSync(inputFilePath) : ''; - const ast = parseScript(input.toString()); + const input = fs.existsSync(inputFilePath) ? fs.readFileSync(inputFilePath, 'utf8') : ''; + const ast = parseScript(input); // dynamic imports always need to provide the path inline for static analysis const module = await import(`./${categoryDirectory}/${testName}/run.ts`); module.run({ ast }); - const output = serializeScript(ast); + const output = serializeScript(ast, input); await expect(output).toMatchFileSnapshot(`${testDirectoryPath}/output.ts`); }); } diff --git a/packages/core/tests/js/object/create/output.ts b/packages/core/tests/js/object/create/output.ts index 7ae0ab02..a31bc6cf 100644 --- a/packages/core/tests/js/object/create/output.ts +++ b/packages/core/tests/js/object/create/output.ts @@ -2,5 +2,5 @@ const empty = {}; const created = { foo: 1, - bar: "string" + bar: 'string' }; \ No newline at end of file diff --git a/packages/core/tests/js/variables/declaration/output.ts b/packages/core/tests/js/variables/declaration/output.ts index 63df3119..2863841c 100644 --- a/packages/core/tests/js/variables/declaration/output.ts +++ b/packages/core/tests/js/variables/declaration/output.ts @@ -1,5 +1,5 @@ const testNumber = 2; const testObject = { - foo: "bar" + foo: 'bar' }; \ No newline at end of file diff --git a/packages/core/tooling/parsers.ts b/packages/core/tooling/parsers.ts index 2bfe4b85..846cb8ea 100644 --- a/packages/core/tooling/parsers.ts +++ b/packages/core/tooling/parsers.ts @@ -8,7 +8,7 @@ type ParseBase = { export function parseScript(source: string): { ast: tools.AstTypes.Program } & ParseBase { const ast = tools.parseScript(source); - const generateCode = () => tools.serializeScript(ast); + const generateCode = () => tools.serializeScript(ast, source); return { ast, source, generateCode }; }