From f4bee8ae2c6e49f2806dc3f8f9fb7cfb07223421 Mon Sep 17 00:00:00 2001 From: Hans Pagel Date: Tue, 12 Dec 2023 12:01:26 +0100 Subject: [PATCH] =?UTF-8?q?chore:=20don=E2=80=99t=20use=20ASTs=20anymore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 ++----- packages/snippetz-plugin-undici/package.json | 2 - .../snippetz-plugin-undici/src/undici.test.ts | 49 ++++++++++++------- packages/snippetz-plugin-undici/src/undici.ts | 12 +---- packages/snippetz/package.json | 2 - packages/snippetz/src/format.test.ts | 25 +--------- packages/snippetz/src/format.ts | 3 +- packages/snippetz/src/index.ts | 1 - packages/snippetz/src/print.test.ts | 36 -------------- packages/snippetz/src/print.ts | 11 ----- packages/snippetz/src/snippetz.test.ts | 25 +--------- packages/snippetz/src/snippetz.ts | 11 ++--- pnpm-lock.yaml | 18 +------ 13 files changed, 42 insertions(+), 170 deletions(-) delete mode 100644 packages/snippetz/src/print.test.ts delete mode 100644 packages/snippetz/src/print.ts diff --git a/README.md b/README.md index 5a1d459..112bf31 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ const snippet = snippetz().get( ## API -### Generate an abstract tree (AST) +### Generate a snippet ```js import { undici } from '@scalar/snippetz-plugin-undici' @@ -48,18 +48,7 @@ const source = undici({ url: 'https://example.com' }) -console.log(source) -``` - -### Print the source code - -```js -import { print } from '@scalar/snippetz' - -console.log(print({ - target: 'js', - tree: … -})) +console.log(source.code) ``` ### Format the source code @@ -69,7 +58,7 @@ import { format } from '@scalar/snippetz' console.log(format({ target: 'js', - tree: … + code: `const answer= 42 ;` })) ``` diff --git a/packages/snippetz-plugin-undici/package.json b/packages/snippetz-plugin-undici/package.json index 35cf4c9..77411c4 100644 --- a/packages/snippetz-plugin-undici/package.json +++ b/packages/snippetz-plugin-undici/package.json @@ -15,8 +15,6 @@ "vitest": "^1.0.4" }, "dependencies": { - "acorn": "^8.11.2", - "astring": "^1.8.6", "prettier": "^3.1.1", "@scalar/snippetz": "workspace:*" }, diff --git a/packages/snippetz-plugin-undici/src/undici.test.ts b/packages/snippetz-plugin-undici/src/undici.test.ts index 8b9703e..adee068 100644 --- a/packages/snippetz-plugin-undici/src/undici.test.ts +++ b/packages/snippetz-plugin-undici/src/undici.test.ts @@ -1,6 +1,5 @@ import { expect, describe, it } from 'vitest' import { undici } from './undici' -import { print } from '@scalar/snippetz' describe('undici', () => { it('has import', () => { @@ -8,7 +7,7 @@ describe('undici', () => { url: 'https://example.com', }) - expect(print(source)).toContain(`import {request} from "undici"`) + expect(source.code).toContain(`import { request } from "undici"`) }) it('returns a basic request', () => { @@ -16,8 +15,10 @@ describe('undici', () => { url: 'https://example.com', }) - expect(print(source)).toBe(`import {request} from "undici"; -const {statusCode, headers, body} = await request("https://example.com"); + expect(source.code).toBe(`import { request } from "undici" + +const { statusCode, headers, body } = await request("https://example.com") + `) }) @@ -27,10 +28,12 @@ const {statusCode, headers, body} = await request("https://example.com"); method: 'post', }) - expect(print(source)).toBe(`import {request} from "undici"; -const {statusCode, headers, body} = await request("https://example.com", { + expect(source.code).toBe(`import { request } from "undici" + +const { statusCode, headers, body } = await request("https://example.com", { "method": "POST" -}); +}) + `) }) @@ -45,12 +48,14 @@ const {statusCode, headers, body} = await request("https://example.com", { ], }) - expect(print(source)).toBe(`import {request} from "undici"; -const {statusCode, headers, body} = await request("https://example.com", { + expect(source.code).toBe(`import { request } from "undici" + +const { statusCode, headers, body } = await request("https://example.com", { "headers": { "Content-Type": "application/json" } -}); +}) + `) }) @@ -71,15 +76,17 @@ const {statusCode, headers, body} = await request("https://example.com", { }, }) - expect(print(source)).toBe(`import {request} from "undici"; -const {statusCode, headers, body} = await request("https://example.com", { + expect(source.code).toBe(`import { request } from "undici" + +const { statusCode, headers, body } = await request("https://example.com", { "headers": { "Content-Type": "application/json" }, "body": { "hello": "world" } -}); +}) + `) }) @@ -104,12 +111,14 @@ const {statusCode, headers, body} = await request("https://example.com", { ], }) - expect(print(source)).toBe(`import {request} from "undici"; -const {statusCode, headers, body} = await request("https://example.com?foo=bar&bar=foo", { + expect(source.code).toBe(`import { request } from "undici" + +const { statusCode, headers, body } = await request("https://example.com?foo=bar&bar=foo", { "headers": { "Content-Type": "application/json" } -}); +}) + `) }) @@ -128,12 +137,14 @@ const {statusCode, headers, body} = await request("https://example.com?foo=bar&b ], }) - expect(print(source)).toBe(`import {request} from "undici"; -const {statusCode, headers, body} = await request("https://example.com", { + expect(source.code).toBe(`import { request } from "undici" + +const { statusCode, headers, body } = await request("https://example.com", { "headers": { "Set-Cookie": "foo=bar; bar=foo" } -}); +}) + `) }) }) diff --git a/packages/snippetz-plugin-undici/src/undici.ts b/packages/snippetz-plugin-undici/src/undici.ts index 5faccce..10e72cd 100644 --- a/packages/snippetz-plugin-undici/src/undici.ts +++ b/packages/snippetz-plugin-undici/src/undici.ts @@ -1,5 +1,3 @@ -import { Parser } from 'acorn' - import type { Request } from 'har-format' /** Helper function to map { name: 'foo', value: 'bar' } to { foo: 'bar' } */ @@ -78,9 +76,7 @@ export function undici(request: Partial) { : '' // Code Template - const code = ` - -import { request } from "undici" + const code = `import { request } from "undici" const { statusCode, headers, body } = await request("${normalizedRequest.url}${queryString}"${jsonOptions}) @@ -89,10 +85,6 @@ const { statusCode, headers, body } = await request("${normalizedRequest.url}${q // Create an AST return { target: 'js', - tree: Parser.parse(code, { - ecmaVersion: 2022, - locations: false, - sourceType: 'module', - }), + code, } } diff --git a/packages/snippetz/package.json b/packages/snippetz/package.json index ecf6119..5d59c5d 100644 --- a/packages/snippetz/package.json +++ b/packages/snippetz/package.json @@ -13,8 +13,6 @@ "vitest": "^1.0.4" }, "dependencies": { - "acorn": "^8.11.2", - "astring": "^1.8.6", "prettier": "^3.1.1" }, "files": [ diff --git a/packages/snippetz/src/format.test.ts b/packages/snippetz/src/format.test.ts index 2a29c2f..d79f85f 100644 --- a/packages/snippetz/src/format.test.ts +++ b/packages/snippetz/src/format.test.ts @@ -1,35 +1,12 @@ import { expect, describe, it } from 'vitest' import { format } from './format' -const tree = { - type: 'Program', - body: [ - { - type: 'VariableDeclaration', - declarations: [ - { - type: 'VariableDeclarator', - id: { - type: 'Identifier', - name: 'answer', - }, - init: { - type: 'Literal', - value: 42, - }, - }, - ], - kind: 'const', - }, - ], -} - describe('format', async () => { it('formats basic JS', async () => { expect( await format({ target: 'js', - tree, + code: 'const answer=42', }) ).toBe(`const answer = 42\n`) }) diff --git a/packages/snippetz/src/format.ts b/packages/snippetz/src/format.ts index 733f379..0810289 100644 --- a/packages/snippetz/src/format.ts +++ b/packages/snippetz/src/format.ts @@ -1,11 +1,10 @@ import * as prettier from 'prettier' -import { print } from './print' export async function format(source: any) { const target = source.target if (target === 'js') { - return await prettier.format(print(source), { + return await prettier.format(source.code, { semi: false, parser: 'babel', singleQuote: true, diff --git a/packages/snippetz/src/index.ts b/packages/snippetz/src/index.ts index 3600c89..3e832ab 100644 --- a/packages/snippetz/src/index.ts +++ b/packages/snippetz/src/index.ts @@ -1,3 +1,2 @@ export * from './format' -export * from './print' export * from './snippetz' diff --git a/packages/snippetz/src/print.test.ts b/packages/snippetz/src/print.test.ts deleted file mode 100644 index 13adb42..0000000 --- a/packages/snippetz/src/print.test.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { expect, describe, it } from 'vitest' -import { print } from './print' - -const tree = { - type: 'Program', - body: [ - { - type: 'VariableDeclaration', - declarations: [ - { - type: 'VariableDeclarator', - id: { - type: 'Identifier', - name: 'answer', - }, - init: { - type: 'Literal', - value: 42, - }, - }, - ], - kind: 'const', - }, - ], -} - -describe('print', async () => { - it('prints basic JS', async () => { - expect( - print({ - target: 'js', - tree, - }) - ).toBe(`const answer = 42;\n`) - }) -}) diff --git a/packages/snippetz/src/print.ts b/packages/snippetz/src/print.ts deleted file mode 100644 index c49dcbe..0000000 --- a/packages/snippetz/src/print.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { generate } from 'astring' - -export function print(source: any) { - const target = source.target - - if (target === 'js') { - return generate(source.tree) - } - - throw new Error(`Unsupported target: ${source.target}`) -} diff --git a/packages/snippetz/src/snippetz.test.ts b/packages/snippetz/src/snippetz.test.ts index 2b1211d..8eaffbb 100644 --- a/packages/snippetz/src/snippetz.test.ts +++ b/packages/snippetz/src/snippetz.test.ts @@ -1,34 +1,11 @@ import { expect, describe, it } from 'vitest' import { snippetz } from './snippetz' -const tree = { - type: 'Program', - body: [ - { - type: 'VariableDeclaration', - declarations: [ - { - type: 'VariableDeclarator', - id: { - type: 'Identifier', - name: 'answer', - }, - init: { - type: 'Literal', - value: 42, - }, - }, - ], - kind: 'const', - }, - ], -} - describe('snippetz', async () => { it('formats basic JS', async () => { const snippet = await snippetz().get({ target: 'js', - tree, + code: 'const answer=42', }) expect(snippet).toBe(`const answer = 42\n`) diff --git a/packages/snippetz/src/snippetz.ts b/packages/snippetz/src/snippetz.ts index 5f48bc6..6aa934c 100644 --- a/packages/snippetz/src/snippetz.ts +++ b/packages/snippetz/src/snippetz.ts @@ -1,13 +1,8 @@ import { format } from './format' -import { print } from './print' -export type SnippetOptions = { - format: boolean -} +export type SnippetOptions = {} -const defaultOptions = { - format: true, -} +const defaultOptions = {} export function snippetz() { return { @@ -17,7 +12,7 @@ export function snippetz() { ...options, } - return options.format ? format(source) : print(source) + return format(source) }, } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 34ea5ca..93c36ca 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,12 +20,6 @@ importers: packages/snippetz: dependencies: - acorn: - specifier: ^8.11.2 - version: 8.11.2 - astring: - specifier: ^1.8.6 - version: 1.8.6 prettier: specifier: ^3.1.1 version: 3.1.1 @@ -45,12 +39,6 @@ importers: '@scalar/snippetz': specifier: workspace:* version: link:../snippetz - acorn: - specifier: ^8.11.2 - version: 8.11.2 - astring: - specifier: ^1.8.6 - version: 1.8.6 prettier: specifier: ^3.1.1 version: 3.1.1 @@ -710,6 +698,7 @@ packages: resolution: {integrity: sha512-nc0Axzp/0FILLEVsm4fNwLCwMttvhEI263QtVPQcbpfZZ3ts0hLsZGOpE6czNlid7CJ9MlyH8reXkpsf3YUY4w==} engines: {node: '>=0.4.0'} hasBin: true + dev: true /ansi-colors@4.1.3: resolution: {integrity: sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==} @@ -790,11 +779,6 @@ packages: resolution: {integrity: sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==} dev: true - /astring@1.8.6: - resolution: {integrity: sha512-ISvCdHdlTDlH5IpxQJIex7BWBywFWgjJSVdwst+/iQCoEYnyOaQ95+X1JGshuBjGp6nxKUy1jMgE3zPqN7fQdg==} - hasBin: true - dev: false - /available-typed-arrays@1.0.5: resolution: {integrity: sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw==} engines: {node: '>= 0.4'}