Skip to content
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

chore(svelte-ast-print): Simplify package.json#exports & expand modules #117

Merged
merged 3 commits into from
Feb 21, 2025
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
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ This is a monorepo with packages to **boost the [DX]** of working with [AST] _(u

In order to work with AST, the following processes are recognized:

1. [Analyzing](#analyze) the [AST] object(s).
1. [Building](#build) programmatically the [AST] node(s), or an entire object.
1. [Parsing](#parse) _stringified_ code syntax into [AST] object.
1. [Traversing](#traverse) the [AST] object.
1. [Analyzing](#analyze) the [AST] object(s).
1. [Printing](#print) the [AST] object back into _stringified_ code syntax.

> [!IMPORTANT]
Expand All @@ -35,6 +35,16 @@ In order to work with AST, the following processes are recognized:
>
> Not all of these packages are part of this monorepo.

### Analyze

Analyze the received [AST] object(s). Contains type-guards and utilities.

| Name | Languages | In this repository? |
| ---------------------- | ---------------------------------- | ------------------- |
| [`js-ast-analyze`] | ![icon-js] | ✅ |
| [`ts-ast-analyze`] | ![icon-js]![icon-ts] | ✅ |
| [`svelte-ast-analyze`] | ![icon-js]![icon-ts]![icon-svelte] | ✅ |

### Build

Sometimes you need to do some code transformation...
Expand Down Expand Up @@ -62,16 +72,6 @@ In other words, _walk_ on the AST object.
| --------------- | ---------------------------------- | ------------------- |
| [`zimmerframe`] | ![icon-js]![icon-ts]![icon-svelte] | ❌ |

### Analyze

Analyze the received [AST] object(s). Contains type-guards and utilities.

| Name | Languages | In this repository? |
| ---------------------- | ---------------------------------- | ------------------- |
| [`js-ast-analyze`] | ![icon-js] | ✅ |
| [`ts-ast-analyze`] | ![icon-js]![icon-ts] | ✅ |
| [`svelte-ast-analyze`] | ![icon-js]![icon-ts]![icon-svelte] | ✅ |

### Print

Print the [AST] object or nodes into stringified code syntax.
Expand Down
14 changes: 7 additions & 7 deletions knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
"vite": false
},
"pkgs/js-ast-analyze": {
"entry": ["src/lib.ts"],
"entry": ["src/*.ts"],
"project": ["**/*.{json,js,ts}"]
},
"pkgs/js-ast-build": {
"entry": ["src/lib.ts"],
"entry": ["src/*.ts"],
"project": ["**/*.{json,js,ts}"]
},
"pkgs/ts-ast-analyze": {
"entry": ["src/lib.ts"],
"entry": ["src/*.ts"],
"project": ["**/*.{json,js,ts}"]
},
"pkgs/ts-ast-build": {
"entry": ["src/lib.ts"],
"entry": ["src/*.ts"],
"project": ["**/*.{json,js,ts}"]
},
"pkgs/svelte-ast-analyze": {
"entry": ["src/lib.ts"],
"entry": ["src/*.ts"],
"project": ["**/*.{json,js,ts}"]
},
"pkgs/svelte-ast-build": {
"entry": ["src/lib.ts"],
"entry": ["src/*.ts"],
"project": ["**/*.{json,js,ts}"]
},
"pkgs/svelte-ast-print": {
"entry": ["src/lib.ts"],
"entry": ["src/*.ts", "!src/_internal/"],
"project": ["**/*.{json,js,ts}"]
}
}
Expand Down
29 changes: 3 additions & 26 deletions pkgs/svelte-ast-print/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,9 @@
},
"files": ["dist/"],
"exports": {
".": {
"development": "./src/lib.ts",
"types": "./dist/lib.d.ts",
"default": "./dist/lib.js"
},
"./css": {
"development": "./src/css/mod.ts",
"types": "./dist/css/mod.d.ts",
"default": "./dist/css/mod.js"
},
"./html": {
"development": "./src/html/mod.ts",
"types": "./dist/html/mod.d.ts",
"default": "./dist/html/mod.js"
},
"./js": {
"development": "./src/js/mod.ts",
"types": "./dist/js/mod.d.ts",
"default": "./dist/js/mod.js"
},
"./template": {
"development": "./src/css/template.ts",
"types": "./dist/template/mod.d.ts",
"default": "./dist/template/mod.js"
},
"./package.json": "./package.json"
".": "./dist/lib.js",
"./*": "./dist/*.js",
"./_internal/*": null
},
"scripts": {
"build": "pnpm run \"/^build:.*/\" ",
Expand Down
6 changes: 4 additions & 2 deletions pkgs/svelte-ast-print/src/_internal/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Node = JS.Node | SvelteOnlyNode;

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

/**
* @internal
* @lintignore
*/
export class Result<N extends Node = Node> {
node: N;
Expand Down Expand Up @@ -285,3 +284,6 @@ export abstract class Wrapper<T extends WrapperType = WrapperType> {
}
}
}

// @ts-expect-error NOTE: This to solve cyclic dependency issue
export const hub: typeof import("../lib.ts") = {};
26 changes: 11 additions & 15 deletions pkgs/svelte-ast-print/src/_internal/template/element.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AST as SV } from "svelte/compiler";

import type { printAttributeLike, printFragment } from "../../template/mod.js";
import { printAttributeLike } from "../../attribute.ts";
import { printFragment } from "../../fragment.ts";
import * as char from "../char.js";
import { HTMLClosingTag, HTMLOpeningTag, HTMLSelfClosingTag } from "../html.js";
import type { PrintOptions } from "../option.js";
Expand Down Expand Up @@ -91,10 +92,8 @@ function is_el_self_closing(n: SV.ElementLike): boolean {
export function print_maybe_self_closing_el<N extends SV.ElementLike>(params: {
n: N;
opts: Partial<PrintOptions>;
attr_printer: typeof printAttributeLike;
frag_printer: typeof printFragment;
}): Result<N> {
const { n, opts, attr_printer, frag_printer } = params;
const { n, opts } = params;
const st = State.get(n, opts);
const self_closing = is_el_self_closing(n);
if (self_closing) {
Expand All @@ -104,22 +103,22 @@ export function print_maybe_self_closing_el<N extends SV.ElementLike>(params: {
n.name,
);
if (n.attributes.length > 0) {
for (const a of n.attributes) tag.insert(char.SPACE, attr_printer(a));
for (const a of n.attributes) tag.insert(char.SPACE, printAttributeLike(a));
}
tag.insert(char.SPACE);
st.add(tag);
return st.result;
}
const opening = new HTMLOpeningTag("inline", n.name);
if (n.attributes.length > 0) {
for (const a of n.attributes) opening.insert(char.SPACE, attr_printer(a));
for (const a of n.attributes) opening.insert(char.SPACE, printAttributeLike(a));
}
st.add(opening);
const should_break =
// @ts-expect-error `Set.prototype.has()` doesn't accept loose string
!NATIVE_INLINE_ELS.has(n.name) && !has_frag_text_or_exp_tag_only(n.fragment.nodes);
if (should_break) st.break(+1);
if (n.fragment) st.add(frag_printer(n.fragment, opts));
if (n.fragment) st.add(printFragment(n.fragment, opts));
if (should_break) st.break(-1);
st.add(new HTMLClosingTag("inline", n.name));
return st.result;
Expand All @@ -132,13 +131,12 @@ export function print_maybe_self_closing_el<N extends SV.ElementLike>(params: {
export function print_self_closing_el<N extends SV.ElementLike>(params: {
n: N;
opts: Partial<PrintOptions>;
attr_printer: typeof printAttributeLike;
}): Result<N> {
const { n, opts, attr_printer } = params;
const { n, opts } = params;
const st = State.get(params.n, opts);
const tag = new HTMLSelfClosingTag("inline", n.name);
if (n.attributes.length > 0) {
for (const a of n.attributes) tag.insert(char.SPACE, attr_printer(a, opts));
for (const a of n.attributes) tag.insert(char.SPACE, printAttributeLike(a, opts));
}
tag.insert(char.SPACE);
st.add(tag);
Expand All @@ -152,21 +150,19 @@ export function print_self_closing_el<N extends SV.ElementLike>(params: {
export function print_non_self_closing_el<N extends SV.ElementLike>(params: {
n: N;
opts: Partial<PrintOptions>;
attr_printer: typeof printAttributeLike;
frag_printer: typeof printFragment;
}): Result<N> {
const { n, opts, attr_printer, frag_printer } = params;
const { n, opts } = params;
const st = State.get(n, opts);
const opening = new HTMLOpeningTag("inline", n.name);
if (n.attributes.length > 0) {
for (const a of n.attributes) opening.insert(char.SPACE, attr_printer(a));
for (const a of n.attributes) opening.insert(char.SPACE, printAttributeLike(a));
}
st.add(opening);
const should_break =
// @ts-expect-error `Set.prototype.has()` doesn't accept loose string
!NATIVE_INLINE_ELS.has(n.name) && !has_frag_text_or_exp_tag_only(n.fragment.nodes);
if (should_break) st.break(+1);
st.add(frag_printer(n.fragment, opts));
st.add(printFragment(n.fragment, opts));
if (should_break) st.break(-1);
st.add(new HTMLClosingTag("inline", n.name));
return st.result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
printStyleDirective,
printTransitionDirective,
printUseDirective,
} from "./mod.ts";
} from "./attribute.ts";

vitest.describe(printAttributeLike.name, () => {
vitest.describe(printAttribute.name, () => {
Expand Down
Loading
Loading