Skip to content

chore: get rid of @sveltejs/ast-tooling #534

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 4 commits into from
Apr 11, 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
5 changes: 0 additions & 5 deletions packages/ast-tooling/README.md

This file was deleted.

40 changes: 0 additions & 40 deletions packages/ast-tooling/package.json

This file was deleted.

9 changes: 0 additions & 9 deletions packages/ast-tooling/tsconfig.json

This file was deleted.

72 changes: 0 additions & 72 deletions packages/ast-tooling/utils.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/ast-tooling/vitest.config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export type * from './addon/options.ts';
export type * from './addon/config.ts';
export type * from './addon/workspace.ts';

export { Walker } from '@sveltejs/ast-tooling';
export { Walker } from './tooling/index.ts';
16 changes: 12 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@
"default": "./dist/parsers.js"
}
},
"dependencies": {
"@sveltejs/ast-tooling": "workspace:*"
},
"devDependencies": {
"@sveltejs/acorn-typescript": "^1.0.1",
"@sveltejs/clack-prompts": "workspace:*",
"@types/estree": "^1.0.6",
"acorn": "^8.14.0",
"decircular": "^1.0.0",
"dedent": "^1.5.3",
"dom-serializer": "^2.0.0",
"domhandler": "^5.0.3",
"domutils": "^3.1.0",
"esrap": "^1.4.5",
"htmlparser2": "^9.1.0",
"magic-string": "^0.30.15",
"picocolors": "^1.1.1"
"picocolors": "^1.1.1",
"postcss": "^8.4.49",
"silver-fleece": "^1.2.1",
"zimmerframe": "^1.1.2"
},
"keywords": [
"create",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import prettier from 'prettier';
import { describe, expect, test } from 'vitest';
import { parseCss, serializeCss } from '@sveltejs/ast-tooling';
import { parseCss, serializeCss } from '../../tooling/index.ts';

const baseDir = resolve(fileURLToPath(import.meta.url), '..');
const categoryDirectories = getDirectoryNames(baseDir);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import prettier from 'prettier';
import { describe, expect, test } from 'vitest';
import { parseHtml, serializeHtml } from '@sveltejs/ast-tooling';
import { parseHtml, serializeHtml } from '../../tooling/index.ts';

const baseDir = resolve(fileURLToPath(import.meta.url), '..');
const categoryDirectories = getDirectoryNames(baseDir);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import prettier from 'prettier';
import { describe, expect, test } from 'vitest';
import { parseScript, serializeScript } from '@sveltejs/ast-tooling';
import { parseScript, serializeScript } from '../../tooling/index.ts';

const baseDir = resolve(fileURLToPath(import.meta.url), '..');
const categoryDirectories = getDirectoryNames(baseDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { expect, test } from 'vitest';
import dedent from 'dedent';
import { guessIndentString, guessQuoteStyle } from '../utils.ts';
import { parseScript, serializeScript, type AstTypes } from '../index.ts';
import {
parseScript,
serializeScript,
guessIndentString,
guessQuoteStyle,
type AstTypes
} from '../tooling/index.ts';

test('guessIndentString - one tab', () => {
const code = dedent`
Expand Down
9 changes: 1 addition & 8 deletions packages/core/tooling/css/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Declaration,
Rule,
AtRule,
Comment,
type CssAst,
type CssChildNode
} from '@sveltejs/ast-tooling';
import { Declaration, Rule, AtRule, Comment, type CssAst, type CssChildNode } from '../index.ts';

export type { CssAst };

Expand Down
2 changes: 1 addition & 1 deletion packages/core/tooling/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
HtmlElement,
HtmlElementType,
parseHtml
} from '@sveltejs/ast-tooling';
} from '../index.ts';
import { addFromString } from '../js/common.ts';

export { HtmlElement, HtmlElementType };
Expand Down
83 changes: 72 additions & 11 deletions packages/ast-tooling/index.ts → packages/core/tooling/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as Walker from 'zimmerframe';
import type { TsEstree } from './js/ts-estree.ts';
import { Document, Element, type ChildNode } from 'domhandler';
import { ElementType, parseDocument } from 'htmlparser2';
import serializeDom from 'dom-serializer';
Expand All @@ -11,21 +13,10 @@ import {
type ChildNode as CssChildNode
} from 'postcss';
import * as fleece from 'silver-fleece';
import * as Walker from 'zimmerframe';
import type { TsEstree } from './ts-estree.ts';
import { guessIndentString, guessQuoteStyle } from './utils.ts';
import { print as esrapPrint } from 'esrap';
import * as acorn from 'acorn';
import { tsPlugin } from '@sveltejs/acorn-typescript';

/**
* Most of the AST tooling is pretty big in bundle size and bundling takes forever.
* Nevertheless bundling of these tools seems smart, as they add many dependencies to each install.
* In order to avoid long bundling during development, all of the AST tools have been extracted
* into this separate package and are bundled only here. This package has been marked as external
* and will not be bundled into all other projects / bundles.
*/

export {
// html
Document as HtmlDocument,
Expand Down Expand Up @@ -179,3 +170,73 @@ export function serializeJson(originalInput: string, data: unknown): string {

return fleece.stringify(data, { spaces });
}

// Sourced from `golden-fleece`
// https://github.com/Rich-Harris/golden-fleece/blob/f2446f331640f325e13609ed99b74b6a45e755c2/src/patch.ts#L302
export function guessIndentString(str: string | undefined): string {
if (!str) return '\t';

const lines = str.split('\n');

let tabs = 0;
let spaces = 0;
let minSpaces = 8;

lines.forEach((line) => {
const match = /^(?: +|\t+)/.exec(line);
if (!match) return;

const whitespace = match[0];
if (whitespace.length === line.length) return;

if (whitespace[0] === '\t') {
tabs += 1;
} else {
spaces += 1;
if (whitespace.length > 1 && whitespace.length < minSpaces) {
minSpaces = whitespace.length;
}
}
});

if (spaces > tabs) {
let result = '';
while (minSpaces--) result += ' ';
return result;
} else {
return '\t';
}
}

export function guessQuoteStyle(ast: TsEstree.Node): 'single' | 'double' | undefined {
let singleCount = 0;
let doubleCount = 0;

Walker.walk(ast, null, {
Literal(node) {
if (node.raw && node.raw.length >= 2) {
// we have at least two characters in the raw string that could represent both quotes
const quotes = [node.raw[0], node.raw[node.raw.length - 1]];
for (const quote of quotes) {
switch (quote) {
case "'":
singleCount++;
break;
case '"':
doubleCount++;
break;
default:
break;
}
}
}
}
});

if (singleCount === 0 && doubleCount === 0) {
// new file or file without any quotes
return undefined;
}

return singleCount > doubleCount ? 'single' : 'double';
}
2 changes: 1 addition & 1 deletion packages/core/tooling/js/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { areNodesEqual } from './common.ts';
import type { AstTypes } from '@sveltejs/ast-tooling';
import type { AstTypes } from '../index.ts';

export function createEmpty(): AstTypes.ArrayExpression {
const arrayExpression: AstTypes.ArrayExpression = {
Expand Down
8 changes: 1 addition & 7 deletions packages/core/tooling/js/common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
type AstTypes,
Walker,
parseScript,
serializeScript,
stripAst
} from '@sveltejs/ast-tooling';
import { type AstTypes, Walker, parseScript, serializeScript, stripAst } from '../index.ts';
import decircular from 'decircular';
import dedent from 'dedent';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/tooling/js/exports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AstTypes } from '@sveltejs/ast-tooling';
import type { AstTypes } from '../index.ts';

export type ExportDefaultReturn<T> = {
astNode: AstTypes.ExportDefaultDeclaration;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tooling/js/function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AstTypes } from '@sveltejs/ast-tooling';
import type { AstTypes } from '../index.ts';

export function call(name: string, args: string[]): AstTypes.CallExpression {
const callExpression: AstTypes.CallExpression = {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tooling/js/imports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Walker, type AstTypes } from '@sveltejs/ast-tooling';
import { Walker, type AstTypes } from '../index.ts';
import { areNodesEqual } from './common.ts';

export function addEmpty(ast: AstTypes.Program, importFrom: string): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tooling/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export * as imports from './imports.ts';
export * as variables from './variables.ts';
export * as exports from './exports.ts';
export * as kit from './kit.ts';
export type { AstTypes } from '@sveltejs/ast-tooling';
export type { AstTypes } from '../index.ts';
2 changes: 1 addition & 1 deletion packages/core/tooling/js/kit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Walker, type AstTypes } from '@sveltejs/ast-tooling';
import { Walker, type AstTypes } from '../index.ts';
import { common, functions, imports, variables, exports } from '../js/index.ts';

export function addGlobalAppInterface(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tooling/js/object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AstTypes } from '@sveltejs/ast-tooling';
import type { AstTypes } from '../index.ts';

export function property<T extends AstTypes.Expression | AstTypes.Identifier>(
ast: AstTypes.ObjectExpression,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tooling/js/variables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AstTypes } from '@sveltejs/ast-tooling';
import type { AstTypes } from '../index.ts';

export function declaration(
ast: AstTypes.Program | AstTypes.Declaration,
Expand Down
Loading