Skip to content

Commit 331ee33

Browse files
authored
chore(svelte-ast-print): Simplify package.json#exports & expand modules (#117)
1 parent f633293 commit 331ee33

29 files changed

+1647
-1577
lines changed

knip.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@
99
"vite": false
1010
},
1111
"pkgs/js-ast-analyze": {
12-
"entry": ["src/lib.ts"],
12+
"entry": ["src/*.ts"],
1313
"project": ["**/*.{json,js,ts}"]
1414
},
1515
"pkgs/js-ast-build": {
16-
"entry": ["src/lib.ts"],
16+
"entry": ["src/*.ts"],
1717
"project": ["**/*.{json,js,ts}"]
1818
},
1919
"pkgs/ts-ast-analyze": {
20-
"entry": ["src/lib.ts"],
20+
"entry": ["src/*.ts"],
2121
"project": ["**/*.{json,js,ts}"]
2222
},
2323
"pkgs/ts-ast-build": {
24-
"entry": ["src/lib.ts"],
24+
"entry": ["src/*.ts"],
2525
"project": ["**/*.{json,js,ts}"]
2626
},
2727
"pkgs/svelte-ast-analyze": {
28-
"entry": ["src/lib.ts"],
28+
"entry": ["src/*.ts"],
2929
"project": ["**/*.{json,js,ts}"]
3030
},
3131
"pkgs/svelte-ast-build": {
32-
"entry": ["src/lib.ts"],
32+
"entry": ["src/*.ts"],
3333
"project": ["**/*.{json,js,ts}"]
3434
},
3535
"pkgs/svelte-ast-print": {
36-
"entry": ["src/lib.ts"],
36+
"entry": ["src/*.ts", "!src/_internal/"],
3737
"project": ["**/*.{json,js,ts}"]
3838
}
3939
}

pkgs/svelte-ast-print/package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@
3838
"files": ["dist/"],
3939
"exports": {
4040
".": "./dist/lib.js",
41-
"./css": "./dist/css/mod.js",
42-
"./html": "./dist/html/mod.js",
43-
"./js": "./dist/js/mod.js",
44-
"./template": "./dist/template/mod.js",
45-
"./package.json": "./package.json"
41+
"./*": "./dist/*.js",
42+
"./_internal/*": null
4643
},
4744
"scripts": {
4845
"build": "pnpm run \"/^build:.*/\" ",

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Node = JS.Node | SvelteOnlyNode;
88

99
/**
1010
* @internal
11-
* State to store our garbage collectible state for node that is being processed for printing.
11+
* Storage to store our garbage collectible state for node that is being processed for printing.
1212
* Each function call should generate unique identifier to the passed user options - by default is `{}`.
1313
*/
1414
const STORE = new WeakMap<Node, State>();
@@ -128,7 +128,6 @@ class Collector {
128128

129129
/**
130130
* @internal
131-
* @lintignore
132131
*/
133132
export class Result<N extends Node = Node> {
134133
node: N;
@@ -285,3 +284,6 @@ export abstract class Wrapper<T extends WrapperType = WrapperType> {
285284
}
286285
}
287286
}
287+
288+
// @ts-expect-error NOTE: This to solve cyclic dependency issue
289+
export const hub: typeof import("../lib.ts") = {};

pkgs/svelte-ast-print/src/_internal/template/element.ts

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

3-
import type { printAttributeLike, printFragment } from "../../template/mod.js";
3+
import { printAttributeLike } from "../../attribute.ts";
4+
import { printFragment } from "../../fragment.ts";
45
import * as char from "../char.js";
56
import { HTMLClosingTag, HTMLOpeningTag, HTMLSelfClosingTag } from "../html.js";
67
import type { PrintOptions } from "../option.js";
@@ -91,10 +92,8 @@ function is_el_self_closing(n: SV.ElementLike): boolean {
9192
export function print_maybe_self_closing_el<N extends SV.ElementLike>(params: {
9293
n: N;
9394
opts: Partial<PrintOptions>;
94-
attr_printer: typeof printAttributeLike;
95-
frag_printer: typeof printFragment;
9695
}): Result<N> {
97-
const { n, opts, attr_printer, frag_printer } = params;
96+
const { n, opts } = params;
9897
const st = State.get(n, opts);
9998
const self_closing = is_el_self_closing(n);
10099
if (self_closing) {
@@ -104,22 +103,22 @@ export function print_maybe_self_closing_el<N extends SV.ElementLike>(params: {
104103
n.name,
105104
);
106105
if (n.attributes.length > 0) {
107-
for (const a of n.attributes) tag.insert(char.SPACE, attr_printer(a));
106+
for (const a of n.attributes) tag.insert(char.SPACE, printAttributeLike(a));
108107
}
109108
tag.insert(char.SPACE);
110109
st.add(tag);
111110
return st.result;
112111
}
113112
const opening = new HTMLOpeningTag("inline", n.name);
114113
if (n.attributes.length > 0) {
115-
for (const a of n.attributes) opening.insert(char.SPACE, attr_printer(a));
114+
for (const a of n.attributes) opening.insert(char.SPACE, printAttributeLike(a));
116115
}
117116
st.add(opening);
118117
const should_break =
119118
// @ts-expect-error `Set.prototype.has()` doesn't accept loose string
120119
!NATIVE_INLINE_ELS.has(n.name) && !has_frag_text_or_exp_tag_only(n.fragment.nodes);
121120
if (should_break) st.break(+1);
122-
if (n.fragment) st.add(frag_printer(n.fragment, opts));
121+
if (n.fragment) st.add(printFragment(n.fragment, opts));
123122
if (should_break) st.break(-1);
124123
st.add(new HTMLClosingTag("inline", n.name));
125124
return st.result;
@@ -132,13 +131,12 @@ export function print_maybe_self_closing_el<N extends SV.ElementLike>(params: {
132131
export function print_self_closing_el<N extends SV.ElementLike>(params: {
133132
n: N;
134133
opts: Partial<PrintOptions>;
135-
attr_printer: typeof printAttributeLike;
136134
}): Result<N> {
137-
const { n, opts, attr_printer } = params;
135+
const { n, opts } = params;
138136
const st = State.get(params.n, opts);
139137
const tag = new HTMLSelfClosingTag("inline", n.name);
140138
if (n.attributes.length > 0) {
141-
for (const a of n.attributes) tag.insert(char.SPACE, attr_printer(a, opts));
139+
for (const a of n.attributes) tag.insert(char.SPACE, printAttributeLike(a, opts));
142140
}
143141
tag.insert(char.SPACE);
144142
st.add(tag);
@@ -152,21 +150,19 @@ export function print_self_closing_el<N extends SV.ElementLike>(params: {
152150
export function print_non_self_closing_el<N extends SV.ElementLike>(params: {
153151
n: N;
154152
opts: Partial<PrintOptions>;
155-
attr_printer: typeof printAttributeLike;
156-
frag_printer: typeof printFragment;
157153
}): Result<N> {
158-
const { n, opts, attr_printer, frag_printer } = params;
154+
const { n, opts } = params;
159155
const st = State.get(n, opts);
160156
const opening = new HTMLOpeningTag("inline", n.name);
161157
if (n.attributes.length > 0) {
162-
for (const a of n.attributes) opening.insert(char.SPACE, attr_printer(a));
158+
for (const a of n.attributes) opening.insert(char.SPACE, printAttributeLike(a));
163159
}
164160
st.add(opening);
165161
const should_break =
166162
// @ts-expect-error `Set.prototype.has()` doesn't accept loose string
167163
!NATIVE_INLINE_ELS.has(n.name) && !has_frag_text_or_exp_tag_only(n.fragment.nodes);
168164
if (should_break) st.break(+1);
169-
st.add(frag_printer(n.fragment, opts));
165+
st.add(printFragment(n.fragment, opts));
170166
if (should_break) st.break(-1);
171167
st.add(new HTMLClosingTag("inline", n.name));
172168
return st.result;

pkgs/svelte-ast-print/src/template/attribute.test.ts renamed to pkgs/svelte-ast-print/src/attribute.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
printStyleDirective,
1515
printTransitionDirective,
1616
printUseDirective,
17-
} from "./mod.ts";
17+
} from "./attribute.ts";
1818

1919
vitest.describe(printAttributeLike.name, () => {
2020
vitest.describe(printAttribute.name, () => {

0 commit comments

Comments
 (0)