This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: pass target and tree, use acorn and astring for JS
- Loading branch information
Showing
11 changed files
with
183 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}); | ||
`) | ||
}) | ||
}) |
Oops, something went wrong.