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

Commit

Permalink
refactor: pass target and tree, use acorn and astring for JS
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Dec 11, 2023
1 parent edb1e67 commit 3c7086a
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 155 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
[![GitHub License](https://img.shields.io/github/license/scalar/snippetz)](https://github.com/scalar/snippetz/blob/main/LICENSE)
[![Discord](https://img.shields.io/discord/1135330207960678410?style=flat&color=5865F2)](https://discord.gg/8HeZcRGPFS)

## Installation

```
npm install @scalar/snippetz
```

## Quickstart

```js
Expand All @@ -21,11 +27,11 @@ const snippet = snippetz().get(
<!-- ```js
import { format, print, undici } from '@scalar/snippetz'
const tree = undici({
const source = undici({
url: 'https://example.com'
})
console.log(format(print(tree)))
console.log(format(print(source)))
``` -->

Output:
Expand Down
7 changes: 5 additions & 2 deletions packages/snippetz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
"vitest": "^1.0.4"
},
"dependencies": {
"abstract-syntax-tree": "^2.21.1",
"acorn": "^8.11.2",
"astring": "^1.8.6",
"prettier": "^3.1.1"
},
"files": ["dist"],
"files": [
"dist"
],
"main": "./dist/index.umd.cjs",
"module": "./dist/index.js",
"exports": {
Expand Down
38 changes: 29 additions & 9 deletions packages/snippetz/src/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import { expect, test } from 'vitest'
import { format } from './format'

test('format', async () => {
const source = `import {request} from "undici";
const {statusCode, headers, trailers, body} = await request("http://localhost:3000/foo");
`
const tree = {
type: 'Program',
body: [
{
type: 'VariableDeclaration',
declarations: [
{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: 'answer',
},
init: {
type: 'Literal',
value: 42,
},
},
],
kind: 'const',
},
],
}

expect(await format(source)).toBe(`import { request } from 'undici'
const { statusCode, headers, trailers, body } = await request(
'http://localhost:3000/foo',
)
`)
test('format', async () => {
expect(
await format({
target: 'js',
tree,
})
).toBe(`const answer = 42\n`)
})
19 changes: 13 additions & 6 deletions packages/snippetz/src/format.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import * as prettier from 'prettier'
import { print } from './print'

export async function format(source: string) {
return await prettier.format(source, {
semi: false,
parser: 'babel',
singleQuote: true,
})
export async function format(source: any) {
const target = source.target

if (target === 'js') {
return await prettier.format(print(source), {
semi: false,
parser: 'babel',
singleQuote: true,
})
}

throw new Error(`Unsupported target: ${source.target}`)
}
51 changes: 28 additions & 23 deletions packages/snippetz/src/print.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import { expect, test } from 'vitest'
import { print } from './print'

test('print', async () => {
const tree = {
type: 'Program',
body: [
{
type: 'VariableDeclaration',
declarations: [
{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: 'answer',
},
init: {
type: 'Literal',
value: 42,
},
const tree = {
type: 'Program',
body: [
{
type: 'VariableDeclaration',
declarations: [
{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: 'answer',
},
init: {
type: 'Literal',
value: 42,
},
],
kind: 'const',
},
],
}
},
],
kind: 'const',
},
],
}

expect(await print(tree)).toBe(`const answer = 42;\n`)
test('print', async () => {
expect(
await print({
target: 'js',
tree,
})
).toBe(`const answer = 42;\n`)
})
12 changes: 9 additions & 3 deletions packages/snippetz/src/print.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { generate } from 'abstract-syntax-tree'
import { generate } from 'astring'

export function print(tree: any) {
return generate(tree)
export function print(source: any) {
const target = source.target

if (target === 'js') {
return generate(source.tree)
}

throw new Error(`Unsupported target: ${source.target}`)
}
5 changes: 4 additions & 1 deletion packages/snippetz/src/snippetz.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const tree = {
}

test('snippetz', async () => {
const snippet = await snippetz().get(tree)
const snippet = await snippetz().get({
target: 'js',
tree,
})

expect(snippet).toBe(`const answer = 42\n`)
})
29 changes: 16 additions & 13 deletions packages/snippetz/src/snippetz.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { format } from './format'
import { print } from './print'

export type SnippetOptions = {}

export class Snippetz {
constructor(options?: SnippetOptions) {
// console.log(options)
}
export type SnippetOptions = {
format: boolean
}

get(tree: any) {
return format(print(tree))
}
const defaultOptions = {
format: true,
}

export function snippetz(tree: any) {
return new Snippetz({
tree,
})
export function snippetz() {
return {
get(source: any, options?: Partial<SnippetOptions>) {
options = {
...defaultOptions,
...options,
}

return options.format ? format(source) : print(source)
},
}
}
39 changes: 20 additions & 19 deletions packages/snippetz/src/undici.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { expect, test } from 'vitest'
import { expect, describe, it } from 'vitest'
import { undici } from './undici'
import { print } from './print'
// import { parse } from 'abstract-syntax-tree'

test('undici', () => {
// const source = `import { request } from 'undici'
describe('undici', () => {
it('basic request', () => {
const tree = undici({
url: 'https://example.com',
})

// const {
// statusCode,
// headers,
// trailers,
// body
// } = await request('http://localhost:3000/foo')`

// const tree = parse(source)

// console.log(JSON.stringify(tree))

const tree = undici({
url: 'http://localhost:3000/foo',
expect(print(tree)).toBe(`import {request} from "undici";
const {statusCode, headers, trailers, body} = await request("https://example.com");
`)
})

expect(print(tree)).toBe(`import {request} from "undici";
const {statusCode, headers, trailers, body} = await request("http://localhost:3000/foo");
it('POST request', () => {
const tree = undici({
url: 'https://example.com',
method: 'post',
})

expect(print(tree)).toBe(`import {request} from "undici";
const {statusCode, headers, trailers, body} = await request("https://example.com", {
"method": "POST"
});
`)
})
})
Loading

0 comments on commit 3c7086a

Please sign in to comment.