Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
211 additions
and
96 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { toArray } from '@antfu/utils' | ||
import type { Arrayable } from '@antfu/utils' | ||
import type { CallExpression } from '@babel/types' | ||
import type { Transformer } from '../types' | ||
|
||
/** | ||
* Removes wrapper function. e.g `defineComponent`, `defineConfig`... | ||
* @param functionNames - function names to remove | ||
* @returns Transformer | ||
*/ | ||
export const RemoveWrapperFunction = ( | ||
functionNames: Arrayable<string> | ||
): Transformer<CallExpression> => ({ | ||
onNode: (node) => | ||
node.type === 'CallExpression' && | ||
node.callee.type === 'Identifier' && | ||
toArray(functionNames).includes(node.callee.name), | ||
|
||
transform(node) { | ||
return node.arguments[0] | ||
}, | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Awaitable } from '@antfu/utils' | ||
import type { Node } from '@babel/types' | ||
|
||
export interface TransformerParsed { | ||
transformer: Transformer | ||
nodes: NodeRef[] | ||
} | ||
|
||
export interface NodeRef { | ||
value: Node | ||
set: (node: Node) => void | ||
} | ||
|
||
export interface Transformer<T extends Node = Node> { | ||
transformInclude?: (id: string) => Awaitable<boolean> | ||
onNode?: | ||
| ((node: Node, parent: Node) => Awaitable<boolean>) | ||
| ((node: Node, parent: Node) => node is T) | ||
transform: ( | ||
node: T, | ||
code: string, | ||
context: { | ||
id: string | ||
} | ||
) => Awaitable<string | Node | undefined | null | void> | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './core/presets/remove-wrapper-function' |
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,68 +1,78 @@ | ||
import { expect, test } from 'vitest' | ||
import { transform } from '../src/core/transform' | ||
import type { CallExpression, Identifier, StringLiteral } from '@babel/types' | ||
import type { Identifier, NumericLiteral, StringLiteral } from '@babel/types' | ||
import type { OptionsResolved, Transformer } from '../src/core/options' | ||
|
||
const changeString: Transformer<StringLiteral> = { | ||
onNode: (node): node is StringLiteral => node.type === 'StringLiteral', | ||
transform() { | ||
return "'Hello'" | ||
}, | ||
} | ||
|
||
const changeVarName: Transformer<Identifier> = { | ||
onNode: (node): node is Identifier => | ||
node.type === 'Identifier' && node.name === 'foo', | ||
transform() { | ||
return 'newName' | ||
}, | ||
} | ||
|
||
const overwriteVarName: Transformer<Identifier> = { | ||
onNode: (node): node is Identifier => node.type === 'Identifier', | ||
transform(node) { | ||
return `overwrite_${node.name}` | ||
}, | ||
} | ||
|
||
const timesTen: Transformer<NumericLiteral> = { | ||
onNode: (node): node is NumericLiteral => node.type === 'NumericLiteral', | ||
transform(node) { | ||
return `${node.value * 10}` | ||
}, | ||
} | ||
|
||
test('basic', async () => { | ||
const source = `const foo = 'string'` | ||
const source = `const foo = 'string'\nlet i = 10` | ||
const options: Pick<OptionsResolved, 'parserOptions' | 'transformer'> = { | ||
transformer: [], | ||
parserOptions: {}, | ||
} | ||
let code = await transform(source, 'foo.js', options) | ||
let code = (await transform(source, 'foo.js', options))?.code | ||
expect(code).toMatchInlineSnapshot('undefined') | ||
|
||
const transformer: Transformer<StringLiteral> = { | ||
filterNode: (node): node is StringLiteral => node.type === 'StringLiteral', | ||
transform(node, code) { | ||
code.overwrite(node.start!, node.end!, '"Hello"') | ||
}, | ||
} | ||
options.transformer.push(transformer) | ||
code = await transform(source, 'foo.js', options) | ||
expect(code).toMatchInlineSnapshot('"const foo = \\"Hello\\""') | ||
options.transformer = [changeString] | ||
code = (await transform(source, 'foo.js', options))?.code | ||
expect(code).toMatchInlineSnapshot(` | ||
"const foo = 'Hello' | ||
let i = 10" | ||
`) | ||
|
||
const transformer2: Transformer<Identifier> = { | ||
filterNode: (node): node is Identifier => node.type === 'Identifier', | ||
transform(node, code) { | ||
code.overwrite(node.start!, node.end!, 'newName') | ||
}, | ||
} | ||
options.transformer.push(transformer2) | ||
code = await transform(source, 'foo.js', options) | ||
expect(code).toMatchInlineSnapshot('"const newName = \\"Hello\\""') | ||
options.transformer = [changeVarName] | ||
code = (await transform(source, 'foo.js', options))?.code | ||
expect(code).toMatchInlineSnapshot(` | ||
"const newName = 'string' | ||
let i = 10" | ||
`) | ||
|
||
options.transformer.splice(0, 1) | ||
code = await transform(source, 'foo.js', options) | ||
expect(code).toMatchInlineSnapshot('"const newName = \'string\'"') | ||
}) | ||
options.transformer = [changeString, changeVarName] | ||
code = (await transform(source, 'foo.js', options))?.code | ||
expect(code).toMatchInlineSnapshot(` | ||
"const newName = 'Hello' | ||
let i = 10" | ||
`) | ||
|
||
test('remove wrapper function', async () => { | ||
const source = `const comp = defineComponent({ | ||
render() { | ||
return [] | ||
} | ||
})` | ||
const transformer: Transformer<CallExpression> = { | ||
filterNode: (node) => | ||
node.type === 'CallExpression' && | ||
node.callee.type === 'Identifier' && | ||
node.callee.name === 'defineComponent', | ||
transform(node, code) { | ||
const [arg] = node.arguments | ||
code.overwrite(node.start!, node.end!, code.slice(arg.start!, arg.end!)) | ||
}, | ||
} | ||
const options: Pick<OptionsResolved, 'parserOptions' | 'transformer'> = { | ||
transformer: [transformer], | ||
parserOptions: {}, | ||
} | ||
const code = await transform(source, 'foo.js', options) | ||
options.transformer = [changeString, changeVarName, overwriteVarName] | ||
code = (await transform(source, 'foo.js', options))?.code | ||
expect(code).toMatchInlineSnapshot(` | ||
"const overwrite_newName = 'Hello' | ||
let overwrite_i = 10" | ||
`) | ||
|
||
options.transformer = [timesTen, timesTen, timesTen] | ||
code = (await transform(source, 'foo.js', options))?.code | ||
expect(code).toMatchInlineSnapshot(` | ||
"const comp = { | ||
render() { | ||
return [] | ||
} | ||
}" | ||
"const foo = 'string' | ||
let i = 10000" | ||
`) | ||
}) |
Oops, something went wrong.