Skip to content

Commit f9f8edb

Browse files
authored
refactor(svelte-ast-print): Polish codebase, improve tests syntax & docs (#118)
1 parent 7911bcb commit f9f8edb

28 files changed

+259
-300
lines changed

.github/workflows/ci_lint.yml

+2-6
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,8 @@ jobs:
6161
pnpm install
6262
--frozen-lockfile
6363
64-
# https://github.com/Codex-/knip-reporter
65-
- name: Create Knip report
66-
uses: codex-/knip-reporter@v2
67-
with:
68-
command_script_name: pnpm run "lint:knip" --reporter=json
69-
verbose: true
64+
- name: Check the project with Knip
65+
run: pnpm run "lint:knip"
7066

7167
lint_tsc:
7268
name: TSC

.github/workflows/cr_pr-preview.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,4 @@ jobs:
4141
--frozen-lockfile
4242
4343
- name: Publish packages preview with pkg-pr-new CLI
44-
run: pnpx pkg-pr-new publish --pnpm './pkgs/*' --packageManager=pnpm
45-
if: ${{ steps.changed-files.outputs.all_changed_files_count > 0 }}
44+
run: pnpx pkg-pr-new publish --pnpm './packages/*' --packageManager=pnpm

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"lint:biome": "biome check . --verbose",
2525
"lint:biome:format": "biome format . --verbose",
2626
"lint:biome:js": "biome lint . --verbose --unsafe",
27-
"lint:knip": "knip --tags=-lintignore ",
27+
"lint:knip": "knip --tags=-lintignore",
2828
"lint:tsc": "tsc --noEmit",
2929
"lint:typos": "typos --verbose",
3030
"serve:doc": "serve \"./docs\" ",

packages/svelte-ast-print/src/_internal/shared.ts

-3
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,3 @@ export abstract class Wrapper<T extends WrapperType = WrapperType> {
301301
}
302302
}
303303
}
304-
305-
// @ts-expect-error NOTE: This to solve cyclic dependency issue
306-
export const hub: typeof import("../lib.ts") = {};

packages/svelte-ast-print/src/_internal/template/element.ts renamed to packages/svelte-ast-print/src/_internal/template/element-like.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { AST as SV } from "svelte/compiler";
2-
3-
import { printAttributeLike } from "../../attribute.ts";
42
import { printFragment } from "../../fragment.ts";
3+
import { printAttributeLike } from "../../template/attribute-like.ts";
54
import * as char from "../char.js";
5+
import { has_frag_text_or_exp_tag_only } from "../fragment.js";
66
import { HTMLClosingTag, HTMLOpeningTag, HTMLSelfClosingTag } from "../html.js";
77
import type { PrintOptions } from "../option.js";
88
import { type Result, State } from "../shared.js";
9-
import { has_frag_text_or_exp_tag_only } from "./fragment.js";
109

1110
/**
1211
* @internal

packages/svelte-ast-print/src/css.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ import {
2323
printCSSSelectorList,
2424
printCSSTypeSelector,
2525
} from "./css/selector.ts";
26-
import { printCSSStyleSheet } from "./root.ts";
27-
28-
export * from "./css/rule.ts";
29-
export * from "./css/selector.ts";
26+
import { printCSSStyleSheet } from "./template/root.ts";
3027

3128
/**
3229
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors}

packages/svelte-ast-print/src/css/rule.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, it } from "vitest";
44

55
import { printCSSAtrule, printCSSBlock, printCSSDeclaration, printCSSRule } from "./rule.ts";
66

7-
describe(printCSSBlock.name, () => {
7+
describe(printCSSBlock, () => {
88
it("prints correctly", ({ expect }) => {
99
const code = `
1010
<style>
@@ -24,7 +24,7 @@ describe(printCSSBlock.name, () => {
2424
});
2525
});
2626

27-
describe(printCSSDeclaration.name, () => {
27+
describe(printCSSDeclaration, () => {
2828
it("prints correctly", ({ expect }) => {
2929
const code = `
3030
<style>
@@ -38,7 +38,7 @@ describe(printCSSDeclaration.name, () => {
3838
});
3939
});
4040

41-
describe(printCSSAtrule.name, () => {
41+
describe(printCSSAtrule, () => {
4242
it("prints correctly", ({ expect }) => {
4343
const code = `
4444
<style>
@@ -60,7 +60,7 @@ describe(printCSSAtrule.name, () => {
6060
});
6161
});
6262

63-
describe(printCSSRule.name, () => {
63+
describe(printCSSRule, () => {
6464
it("prints correctly", ({ expect }) => {
6565
const code = `
6666
<style>

packages/svelte-ast-print/src/css/selector.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import {
1818
printCSSTypeSelector,
1919
} from "./selector.ts";
2020

21-
describe(printCSSSimpleSelector.name, () => {
22-
describe(printCSSAttributeSelector.name, () => {
21+
describe(printCSSSimpleSelector, () => {
22+
describe(printCSSAttributeSelector, () => {
2323
it("prints correctly without value", ({ expect }) => {
2424
const code = `
2525
<style>
@@ -57,7 +57,7 @@ describe(printCSSSimpleSelector.name, () => {
5757
});
5858
});
5959

60-
describe(printCSSClassSelector.name, () => {
60+
describe(printCSSClassSelector, () => {
6161
it("prints correctly", ({ expect }) => {
6262
const code = `
6363
<style>
@@ -71,7 +71,7 @@ describe(printCSSSimpleSelector.name, () => {
7171
});
7272
});
7373

74-
describe(printCSSIdSelector.name, () => {
74+
describe(printCSSIdSelector, () => {
7575
it("prints correctly", ({ expect }) => {
7676
const code = `
7777
<style>
@@ -85,7 +85,7 @@ describe(printCSSSimpleSelector.name, () => {
8585
});
8686
});
8787

88-
describe(printCSSNestingSelector.name, () => {
88+
describe(printCSSNestingSelector, () => {
8989
it("prints correctly", ({ expect }) => {
9090
const code = `
9191
<style>
@@ -101,7 +101,7 @@ describe(printCSSSimpleSelector.name, () => {
101101
});
102102
});
103103

104-
describe(printCSSPseudoClassSelector.name, () => {
104+
describe(printCSSPseudoClassSelector, () => {
105105
it("prints correctly without args", ({ expect }) => {
106106
const code = `
107107
<style>
@@ -127,7 +127,7 @@ describe(printCSSSimpleSelector.name, () => {
127127
});
128128
});
129129

130-
describe(printCSSPseudoElementSelector.name, () => {
130+
describe(printCSSPseudoElementSelector, () => {
131131
it("prints correctly", ({ expect }) => {
132132
const code = `
133133
<style>
@@ -141,7 +141,7 @@ describe(printCSSSimpleSelector.name, () => {
141141
});
142142
});
143143

144-
describe(printCSSTypeSelector.name, () => {
144+
describe(printCSSTypeSelector, () => {
145145
it("prints correctly", ({ expect }) => {
146146
const code = `
147147
<style>
@@ -155,7 +155,7 @@ describe(printCSSSimpleSelector.name, () => {
155155
});
156156
});
157157

158-
describe(printCSSNth.name, () => {
158+
describe(printCSSNth, () => {
159159
it("prints correctly simple number", ({ expect }) => {
160160
const code = `
161161
<style>
@@ -206,7 +206,7 @@ describe(printCSSSimpleSelector.name, () => {
206206
});
207207
});
208208

209-
describe(printCSSPercentage.name, () => {
209+
describe(printCSSPercentage, () => {
210210
it("prints correctly", ({ expect }) => {
211211
const code = `
212212
<style>
@@ -223,7 +223,7 @@ describe(printCSSSimpleSelector.name, () => {
223223
});
224224
});
225225

226-
describe(printCSSRelativeSelector.name, () => {
226+
describe(printCSSRelativeSelector, () => {
227227
it("prints correctly without combinator", ({ expect }) => {
228228
const code = `
229229
<style>
@@ -249,7 +249,7 @@ describe(printCSSRelativeSelector.name, () => {
249249
});
250250
});
251251

252-
describe(printCSSComplexSelector.name, () => {
252+
describe(printCSSComplexSelector, () => {
253253
it("prints correctly", ({ expect }) => {
254254
const code = `
255255
<style>
@@ -263,7 +263,7 @@ describe(printCSSComplexSelector.name, () => {
263263
});
264264
});
265265

266-
describe(printCSSSelectorList.name, () => {
266+
describe(printCSSSelectorList, () => {
267267
it("prints correctly", ({ expect }) => {
268268
const code = `
269269
<style>

packages/svelte-ast-print/src/css/selector.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Printers related to CSS **selector** related AST nodes only.
3-
* @module svelte-ast-print/css/seletor
3+
* @module svelte-ast-print/css/selector
44
*/
55

66
import type { AST as SV } from "svelte/compiler";

packages/svelte-ast-print/src/fragment.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, it } from "vitest";
44

55
import { printFragment } from "./fragment.ts";
66

7-
describe(printFragment.name, () => {
7+
describe(printFragment, () => {
88
it("it prints correctly fragment code", ({ expect }) => {
99
const code = `
1010
<h1>Shopping list</h1>

packages/svelte-ast-print/src/fragment.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
*/
55

66
import type { AST as SV } from "svelte/compiler";
7-
8-
import { isBlock } from "./_internal/template/block.ts";
9-
import { isElementLike } from "./_internal/template/element.ts";
107
import type { PrintOptions } from "./_internal/option.ts";
11-
import { hub, type Result, State } from "./_internal/shared.ts";
12-
import { printBlock } from "./block.ts";
13-
import { printElementLike } from "./element.ts";
14-
import { printHTMLNode } from "./html.ts";
15-
import { printTag } from "./tag.ts";
8+
import { type Result, State } from "./_internal/shared.ts";
9+
import { isBlock } from "./_internal/template/block.ts";
10+
import { isElementLike } from "./_internal/template/element-like.ts";
11+
import { printBlock } from "./template/block.ts";
12+
import { printElementLike } from "./template/element-like.ts";
13+
import { printHTMLNode } from "./template/html.ts";
14+
import { printTag } from "./template/tag.ts";
1615

1716
/**
1817
* @since 1.0.0
@@ -78,7 +77,3 @@ export function printFragment(n: SV.Fragment, opts: Partial<PrintOptions> = {}):
7877
}
7978
return st.result;
8079
}
81-
82-
Object.assign(hub, {
83-
printFragment,
84-
});

packages/svelte-ast-print/src/lib.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
*
1818
* ```ts
1919
* import type { AST } from "svelte/compiler";
20-
* import { printSvelteSnippet } from "svelte-ast-print/template";
20+
* import { printSnippetBlock } from "svelte-ast-print/template/block";
2121
*
2222
* // How you obtain the node is up to you.
2323
* // Either by building programmatically or from parsing
2424
* let node: AST.SnippetBlock;
2525
*
26-
* const stringified = printSvelteSnippet(node);
26+
* const stringified = printSnippetBlock(node);
2727
* ```
2828
*
2929
* @example General usage
@@ -70,12 +70,17 @@
7070
* @module svelte-ast-print
7171
*/
7272

73+
import type esrap from "esrap";
7374
import type * as JS from "estree";
7475
import type { AST as SV } from "svelte/compiler";
7576

7677
import { print_js } from "./_internal/js.ts";
7778
import type { PrintOptions } from "./_internal/option.js";
78-
import { hub, isSvelteOnlyNode, type Result, State, type SvelteOnlyNode } from "./_internal/shared.ts";
79+
import { isSvelteOnlyNode, type Result, State, type SvelteOnlyNode } from "./_internal/shared.ts";
80+
import { printCSSNode } from "./css.ts";
81+
import { printFragment } from "./fragment.ts";
82+
import { printRoot, printScript } from "./template/root.ts";
83+
import { printTemplateNode } from "./template.ts";
7984

8085
/**
8186
* @param n Svelte or JavaScript/TypeScript ESTree specification complaint AST node
@@ -104,7 +109,7 @@ export function printSvelte<N extends SvelteOnlyNode>(n: N, opts: Partial<PrintO
104109
const st = State.get(n, opts);
105110
switch (n.type) {
106111
case "Root": {
107-
st.add(hub.printRoot(n, opts));
112+
st.add(printRoot(n, opts));
108113
break;
109114
}
110115
// CSS
@@ -126,15 +131,15 @@ export function printSvelte<N extends SvelteOnlyNode>(n: N, opts: Partial<PrintO
126131
case "Atrule":
127132
case "Rule":
128133
case "StyleSheet": {
129-
st.add(hub.printCSSNode(n, opts));
134+
st.add(printCSSNode(n, opts));
130135
break;
131136
}
132137
case "Fragment": {
133-
st.add(hub.printFragment(n, opts));
138+
st.add(printFragment(n, opts));
134139
break;
135140
}
136141
case "Script": {
137-
st.add(hub.printScript(n, opts));
142+
st.add(printScript(n, opts));
138143
break;
139144
}
140145
// attribute-like
@@ -178,14 +183,9 @@ export function printSvelte<N extends SvelteOnlyNode>(n: N, opts: Partial<PrintO
178183
// HTML
179184
case "Comment":
180185
case "Text": {
181-
st.add(hub.printTemplateNode(n, opts));
186+
st.add(printTemplateNode(n, opts));
182187
break;
183188
}
184189
}
185190
return st.result;
186191
}
187-
188-
export * from "./css.ts";
189-
export * from "./fragment.ts";
190-
export * from "./root.ts";
191-
export * from "./template.ts";

packages/svelte-ast-print/src/template.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import type { AST as SV } from "svelte/compiler";
77

88
import type { PrintOptions } from "./_internal/option.ts";
99
import type { Result } from "./_internal/shared.ts";
10-
import { printAttributeLike } from "./attribute.ts";
11-
import { printBlock } from "./block.ts";
12-
import { printElementLike } from "./element.ts";
13-
import { printHTMLNode } from "./html.ts";
14-
import { printRoot } from "./root.ts";
15-
import { printTag } from "./tag.ts";
10+
import { printAttributeLike } from "./template/attribute-like.ts";
11+
import { printBlock } from "./template/block.ts";
12+
import { printElementLike } from "./template/element-like.ts";
13+
import { printHTMLNode } from "./template/html.ts";
14+
import { printRoot } from "./template/root.ts";
15+
import { printTag } from "./template/tag.ts";
1616

1717
/**
1818
* @since 1.0.0
@@ -61,9 +61,3 @@ export function printTemplateNode(n: SV.TemplateNode, opts: Partial<PrintOptions
6161
case "Root": return printRoot(n, opts);
6262
}
6363
}
64-
65-
export * from "./attribute.ts";
66-
export * from "./block.ts";
67-
export * from "./element.ts";
68-
export * from "./html.ts";
69-
export * from "./tag.ts";

0 commit comments

Comments
 (0)