Skip to content

fix: use single quotes in new script files #96

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

Merged
merged 9 commits into from
Oct 14, 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
5 changes: 5 additions & 0 deletions .changeset/five-shoes-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

fix: improve formatting on new script files
49 changes: 14 additions & 35 deletions packages/ast-tooling/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions packages/core/tests/js/arrays/object-array/output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const array = [{
test: true
}, {
test2: "string"
}];
test2: 'string'
}];
2 changes: 1 addition & 1 deletion packages/core/tests/js/arrays/string-array/output.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const array = ["test", "test2"];
const array = ['test', 'test2'];
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const object = {
test: "string"
test: 'string'
};

export default object;
2 changes: 1 addition & 1 deletion packages/core/tests/js/exports/default-export/output.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default {
test: "string"
test: 'string'
};
4 changes: 2 additions & 2 deletions packages/core/tests/js/exports/named-export/output.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const variable = {
test: "string"
test: 'string'
};

export const variable2 = {
test2: "string2"
test2: 'string2'
};
2 changes: 1 addition & 1 deletion packages/core/tests/js/imports/default-import/output.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import MyPackage from "package";
import MyPackage from 'package';
4 changes: 2 additions & 2 deletions packages/core/tests/js/imports/empty-import/output.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import "package/file.css";
import "./relativ/file.css";
import 'package/file.css';
import './relativ/file.css';
4 changes: 2 additions & 2 deletions packages/core/tests/js/imports/named-import/output.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { Handle } from "@sveltejs/kit";
import { namedOne } from "package";
import { Handle } from '@sveltejs/kit';
import { namedOne } from 'package';
6 changes: 3 additions & 3 deletions packages/core/tests/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/js/object/create/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ const empty = {};

const created = {
foo: 1,
bar: "string"
bar: 'string'
};
2 changes: 1 addition & 1 deletion packages/core/tests/js/variables/declaration/output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const testNumber = 2;

const testObject = {
foo: "bar"
foo: 'bar'
};
2 changes: 1 addition & 1 deletion packages/core/tooling/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down