Skip to content

Commit c1618bc

Browse files
manuel3108benmccannAdrianGonz97
authored
fix: use single quotes in new script files (#96)
* fix: provide defaults while serializing new script files * remove unused function * update tests * improve * use tabs * update snapshots * Create five-shoes-invent.md * fix changeset --------- Co-authored-by: Ben McCann <[email protected]> Co-authored-by: AdrianGonz97 <[email protected]>
1 parent a5fa54b commit c1618bc

File tree

14 files changed

+37
-53
lines changed

14 files changed

+37
-53
lines changed

.changeset/five-shoes-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
fix: improve formatting on new script files

packages/ast-tooling/index.ts

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { parse as tsParse } from 'recast/parsers/typescript.js';
2-
import { parse as recastParse, print as recastPrint } from 'recast';
3-
import { Document, Element, Text, type ChildNode } from 'domhandler';
2+
import { parse as recastParse, print as recastPrint, type Options as RecastOptions } from 'recast';
3+
import { Document, Element, type ChildNode } from 'domhandler';
44
import { ElementType, parseDocument } from 'htmlparser2';
5-
import { appendChild, prependChild, removeElement, textContent } from 'domutils';
5+
import { removeElement, textContent } from 'domutils';
66
import serializeDom from 'dom-serializer';
77
import {
88
Root as CssAst,
@@ -65,8 +65,17 @@ export function parseScript(content: string): AstTypes.Program {
6565
return recastOutput.program;
6666
}
6767

68-
export function serializeScript(ast: AstTypes.ASTNode): string {
69-
return recastPrint(ast).code;
68+
export function serializeScript(ast: AstTypes.ASTNode, previousContent?: string): string {
69+
let options: RecastOptions | undefined;
70+
if (!previousContent) {
71+
// provide sensible defaults if we generate a new file
72+
options = {
73+
quote: 'single',
74+
useTabs: true
75+
};
76+
}
77+
78+
return recastPrint(ast, options).code;
7079
}
7180

7281
export function parseCss(content: string): CssAst {
@@ -143,36 +152,6 @@ export function parseSvelte(content: string): SvelteAst {
143152
return { jsAst, htmlAst, cssAst };
144153
}
145154

146-
export function serializeSvelte(asts: SvelteAst): string {
147-
const { jsAst, htmlAst, cssAst } = asts;
148-
149-
const css = serializeCss(cssAst);
150-
const newScriptValue = serializeScript(jsAst);
151-
152-
if (newScriptValue.length > 0) {
153-
const scriptTag = new Element('script', {}, undefined, ElementType.ElementType.Script);
154-
for (const child of scriptTag.children) {
155-
removeElement(child);
156-
}
157-
158-
appendChild(scriptTag, new Text(newScriptValue));
159-
prependChild(htmlAst, scriptTag);
160-
}
161-
162-
if (css.length > 0) {
163-
const styleTag = new Element('style', {}, undefined, ElementType.ElementType.Style);
164-
for (const child of styleTag.children) {
165-
removeElement(child);
166-
}
167-
168-
appendChild(styleTag, new Text(css));
169-
appendChild(htmlAst, styleTag);
170-
}
171-
172-
const content = serializeHtml(htmlAst);
173-
return content;
174-
}
175-
176155
export function parseJson(content: string): any {
177156
// some of the files we need to process contain comments. The default
178157
// node JSON.parse fails parsing those comments.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const array = [{
22
test: true
33
}, {
4-
test2: "string"
5-
}];
4+
test2: 'string'
5+
}];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const array = ["test", "test2"];
1+
const array = ['test', 'test2'];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const object = {
2-
test: "string"
2+
test: 'string'
33
};
44

55
export default object;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default {
2-
test: "string"
2+
test: 'string'
33
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const variable = {
2-
test: "string"
2+
test: 'string'
33
};
44

55
export const variable2 = {
6-
test2: "string2"
6+
test2: 'string2'
77
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import MyPackage from "package";
1+
import MyPackage from 'package';
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import "package/file.css";
2-
import "./relativ/file.css";
1+
import 'package/file.css';
2+
import './relativ/file.css';
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import { Handle } from "@sveltejs/kit";
2-
import { namedOne } from "package";
1+
import { Handle } from '@sveltejs/kit';
2+
import { namedOne } from 'package';

0 commit comments

Comments
 (0)