Skip to content

Commit e045a54

Browse files
chore: get rid of @sveltejs/ast-tooling (#534)
Co-authored-by: AdrianGonz97 <[email protected]>
1 parent c94db33 commit e045a54

28 files changed

+151
-235
lines changed

packages/ast-tooling/README.md

-5
This file was deleted.

packages/ast-tooling/package.json

-40
This file was deleted.

packages/ast-tooling/tsconfig.json

-9
This file was deleted.

packages/ast-tooling/utils.ts

-72
This file was deleted.

packages/ast-tooling/vitest.config.ts

-8
This file was deleted.

packages/core/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export type * from './addon/options.ts';
99
export type * from './addon/config.ts';
1010
export type * from './addon/workspace.ts';
1111

12-
export { Walker } from '@sveltejs/ast-tooling';
12+
export { Walker } from './tooling/index.ts';

packages/core/package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,23 @@
4141
"default": "./dist/parsers.js"
4242
}
4343
},
44-
"dependencies": {
45-
"@sveltejs/ast-tooling": "workspace:*"
46-
},
4744
"devDependencies": {
45+
"@sveltejs/acorn-typescript": "^1.0.1",
4846
"@sveltejs/clack-prompts": "workspace:*",
47+
"@types/estree": "^1.0.6",
48+
"acorn": "^8.14.0",
4949
"decircular": "^1.0.0",
5050
"dedent": "^1.5.3",
51+
"dom-serializer": "^2.0.0",
52+
"domhandler": "^5.0.3",
53+
"domutils": "^3.1.0",
54+
"esrap": "^1.4.5",
55+
"htmlparser2": "^9.1.0",
5156
"magic-string": "^0.30.15",
52-
"picocolors": "^1.1.1"
57+
"picocolors": "^1.1.1",
58+
"postcss": "^8.4.49",
59+
"silver-fleece": "^1.2.1",
60+
"zimmerframe": "^1.1.2"
5361
},
5462
"keywords": [
5563
"create",

packages/core/tests/css/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { join, resolve } from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import prettier from 'prettier';
55
import { describe, expect, test } from 'vitest';
6-
import { parseCss, serializeCss } from '@sveltejs/ast-tooling';
6+
import { parseCss, serializeCss } from '../../tooling/index.ts';
77

88
const baseDir = resolve(fileURLToPath(import.meta.url), '..');
99
const categoryDirectories = getDirectoryNames(baseDir);

packages/core/tests/html/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { join, resolve } from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import prettier from 'prettier';
55
import { describe, expect, test } from 'vitest';
6-
import { parseHtml, serializeHtml } from '@sveltejs/ast-tooling';
6+
import { parseHtml, serializeHtml } from '../../tooling/index.ts';
77

88
const baseDir = resolve(fileURLToPath(import.meta.url), '..');
99
const categoryDirectories = getDirectoryNames(baseDir);

packages/core/tests/js/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { join, resolve } from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import prettier from 'prettier';
55
import { describe, expect, test } from 'vitest';
6-
import { parseScript, serializeScript } from '@sveltejs/ast-tooling';
6+
import { parseScript, serializeScript } from '../../tooling/index.ts';
77

88
const baseDir = resolve(fileURLToPath(import.meta.url), '..');
99
const categoryDirectories = getDirectoryNames(baseDir);

packages/ast-tooling/tests/utils.ts renamed to packages/core/tests/utils.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { expect, test } from 'vitest';
22
import dedent from 'dedent';
3-
import { guessIndentString, guessQuoteStyle } from '../utils.ts';
4-
import { parseScript, serializeScript, type AstTypes } from '../index.ts';
3+
import {
4+
parseScript,
5+
serializeScript,
6+
guessIndentString,
7+
guessQuoteStyle,
8+
type AstTypes
9+
} from '../tooling/index.ts';
510

611
test('guessIndentString - one tab', () => {
712
const code = dedent`

packages/core/tooling/css/index.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
Declaration,
3-
Rule,
4-
AtRule,
5-
Comment,
6-
type CssAst,
7-
type CssChildNode
8-
} from '@sveltejs/ast-tooling';
1+
import { Declaration, Rule, AtRule, Comment, type CssAst, type CssChildNode } from '../index.ts';
92

103
export type { CssAst };
114

packages/core/tooling/html/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
HtmlElement,
66
HtmlElementType,
77
parseHtml
8-
} from '@sveltejs/ast-tooling';
8+
} from '../index.ts';
99
import { addFromString } from '../js/common.ts';
1010

1111
export { HtmlElement, HtmlElementType };

packages/ast-tooling/index.ts renamed to packages/core/tooling/index.ts

+72-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as Walker from 'zimmerframe';
2+
import type { TsEstree } from './js/ts-estree.ts';
13
import { Document, Element, type ChildNode } from 'domhandler';
24
import { ElementType, parseDocument } from 'htmlparser2';
35
import serializeDom from 'dom-serializer';
@@ -11,21 +13,10 @@ import {
1113
type ChildNode as CssChildNode
1214
} from 'postcss';
1315
import * as fleece from 'silver-fleece';
14-
import * as Walker from 'zimmerframe';
15-
import type { TsEstree } from './ts-estree.ts';
16-
import { guessIndentString, guessQuoteStyle } from './utils.ts';
1716
import { print as esrapPrint } from 'esrap';
1817
import * as acorn from 'acorn';
1918
import { tsPlugin } from '@sveltejs/acorn-typescript';
2019

21-
/**
22-
* Most of the AST tooling is pretty big in bundle size and bundling takes forever.
23-
* Nevertheless bundling of these tools seems smart, as they add many dependencies to each install.
24-
* In order to avoid long bundling during development, all of the AST tools have been extracted
25-
* into this separate package and are bundled only here. This package has been marked as external
26-
* and will not be bundled into all other projects / bundles.
27-
*/
28-
2920
export {
3021
// html
3122
Document as HtmlDocument,
@@ -179,3 +170,73 @@ export function serializeJson(originalInput: string, data: unknown): string {
179170

180171
return fleece.stringify(data, { spaces });
181172
}
173+
174+
// Sourced from `golden-fleece`
175+
// https://github.com/Rich-Harris/golden-fleece/blob/f2446f331640f325e13609ed99b74b6a45e755c2/src/patch.ts#L302
176+
export function guessIndentString(str: string | undefined): string {
177+
if (!str) return '\t';
178+
179+
const lines = str.split('\n');
180+
181+
let tabs = 0;
182+
let spaces = 0;
183+
let minSpaces = 8;
184+
185+
lines.forEach((line) => {
186+
const match = /^(?: +|\t+)/.exec(line);
187+
if (!match) return;
188+
189+
const whitespace = match[0];
190+
if (whitespace.length === line.length) return;
191+
192+
if (whitespace[0] === '\t') {
193+
tabs += 1;
194+
} else {
195+
spaces += 1;
196+
if (whitespace.length > 1 && whitespace.length < minSpaces) {
197+
minSpaces = whitespace.length;
198+
}
199+
}
200+
});
201+
202+
if (spaces > tabs) {
203+
let result = '';
204+
while (minSpaces--) result += ' ';
205+
return result;
206+
} else {
207+
return '\t';
208+
}
209+
}
210+
211+
export function guessQuoteStyle(ast: TsEstree.Node): 'single' | 'double' | undefined {
212+
let singleCount = 0;
213+
let doubleCount = 0;
214+
215+
Walker.walk(ast, null, {
216+
Literal(node) {
217+
if (node.raw && node.raw.length >= 2) {
218+
// we have at least two characters in the raw string that could represent both quotes
219+
const quotes = [node.raw[0], node.raw[node.raw.length - 1]];
220+
for (const quote of quotes) {
221+
switch (quote) {
222+
case "'":
223+
singleCount++;
224+
break;
225+
case '"':
226+
doubleCount++;
227+
break;
228+
default:
229+
break;
230+
}
231+
}
232+
}
233+
}
234+
});
235+
236+
if (singleCount === 0 && doubleCount === 0) {
237+
// new file or file without any quotes
238+
return undefined;
239+
}
240+
241+
return singleCount > doubleCount ? 'single' : 'double';
242+
}

packages/core/tooling/js/array.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { areNodesEqual } from './common.ts';
2-
import type { AstTypes } from '@sveltejs/ast-tooling';
2+
import type { AstTypes } from '../index.ts';
33

44
export function createEmpty(): AstTypes.ArrayExpression {
55
const arrayExpression: AstTypes.ArrayExpression = {

packages/core/tooling/js/common.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
type AstTypes,
3-
Walker,
4-
parseScript,
5-
serializeScript,
6-
stripAst
7-
} from '@sveltejs/ast-tooling';
1+
import { type AstTypes, Walker, parseScript, serializeScript, stripAst } from '../index.ts';
82
import decircular from 'decircular';
93
import dedent from 'dedent';
104

packages/core/tooling/js/exports.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AstTypes } from '@sveltejs/ast-tooling';
1+
import type { AstTypes } from '../index.ts';
22

33
export type ExportDefaultReturn<T> = {
44
astNode: AstTypes.ExportDefaultDeclaration;

packages/core/tooling/js/function.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AstTypes } from '@sveltejs/ast-tooling';
1+
import type { AstTypes } from '../index.ts';
22

33
export function call(name: string, args: string[]): AstTypes.CallExpression {
44
const callExpression: AstTypes.CallExpression = {

packages/core/tooling/js/imports.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Walker, type AstTypes } from '@sveltejs/ast-tooling';
1+
import { Walker, type AstTypes } from '../index.ts';
22
import { areNodesEqual } from './common.ts';
33

44
export function addEmpty(ast: AstTypes.Program, importFrom: string): void {

packages/core/tooling/js/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export * as imports from './imports.ts';
66
export * as variables from './variables.ts';
77
export * as exports from './exports.ts';
88
export * as kit from './kit.ts';
9-
export type { AstTypes } from '@sveltejs/ast-tooling';
9+
export type { AstTypes } from '../index.ts';

packages/core/tooling/js/kit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Walker, type AstTypes } from '@sveltejs/ast-tooling';
1+
import { Walker, type AstTypes } from '../index.ts';
22
import { common, functions, imports, variables, exports } from '../js/index.ts';
33

44
export function addGlobalAppInterface(

packages/core/tooling/js/object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AstTypes } from '@sveltejs/ast-tooling';
1+
import type { AstTypes } from '../index.ts';
22

33
export function property<T extends AstTypes.Expression | AstTypes.Identifier>(
44
ast: AstTypes.ObjectExpression,

packages/core/tooling/js/variables.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AstTypes } from '@sveltejs/ast-tooling';
1+
import type { AstTypes } from '../index.ts';
22

33
export function declaration(
44
ast: AstTypes.Program | AstTypes.Declaration,

0 commit comments

Comments
 (0)