Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
chore: don’t use ASTs anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Dec 12, 2023
1 parent 81cedab commit f4bee8a
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 170 deletions.
17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -69,7 +58,7 @@ import { format } from '@scalar/snippetz'

console.log(format({
target: 'js',
tree:
code: `const answer= 42 ;`
}))
```

Expand Down
2 changes: 0 additions & 2 deletions packages/snippetz-plugin-undici/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
"vitest": "^1.0.4"
},
"dependencies": {
"acorn": "^8.11.2",
"astring": "^1.8.6",
"prettier": "^3.1.1",
"@scalar/snippetz": "workspace:*"
},
Expand Down
49 changes: 30 additions & 19 deletions packages/snippetz-plugin-undici/src/undici.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { expect, describe, it } from 'vitest'
import { undici } from './undici'
import { print } from '@scalar/snippetz'

describe('undici', () => {
it('has import', () => {
const source = 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', () => {
const source = 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")
`)
})

Expand All @@ -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"
});
})
`)
})

Expand All @@ -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"
}
});
})
`)
})

Expand All @@ -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"
}
});
})
`)
})

Expand All @@ -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"
}
});
})
`)
})

Expand All @@ -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"
}
});
})
`)
})
})
12 changes: 2 additions & 10 deletions packages/snippetz-plugin-undici/src/undici.ts
Original file line number Diff line number Diff line change
@@ -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' } */
Expand Down Expand Up @@ -78,9 +76,7 @@ export function undici(request: Partial<Request>) {
: ''

// Code Template
const code = `
import { request } from "undici"
const code = `import { request } from "undici"
const { statusCode, headers, body } = await request("${normalizedRequest.url}${queryString}"${jsonOptions})
Expand All @@ -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,
}
}
2 changes: 0 additions & 2 deletions packages/snippetz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
"vitest": "^1.0.4"
},
"dependencies": {
"acorn": "^8.11.2",
"astring": "^1.8.6",
"prettier": "^3.1.1"
},
"files": [
Expand Down
25 changes: 1 addition & 24 deletions packages/snippetz/src/format.test.ts
Original file line number Diff line number Diff line change
@@ -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`)
})
Expand Down
3 changes: 1 addition & 2 deletions packages/snippetz/src/format.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 0 additions & 1 deletion packages/snippetz/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './format'
export * from './print'
export * from './snippetz'
36 changes: 0 additions & 36 deletions packages/snippetz/src/print.test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/snippetz/src/print.ts

This file was deleted.

25 changes: 1 addition & 24 deletions packages/snippetz/src/snippetz.test.ts
Original file line number Diff line number Diff line change
@@ -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`)
Expand Down
11 changes: 3 additions & 8 deletions packages/snippetz/src/snippetz.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,7 +12,7 @@ export function snippetz() {
...options,
}

return options.format ? format(source) : print(source)
return format(source)
},
}
}
Loading

0 comments on commit f4bee8a

Please sign in to comment.