Skip to content

Commit 638f1ab

Browse files
authoredDec 8, 2023
feat(compiler-core): add current filename to TransformContext (#8950)
1 parent 5b2bd1d commit 638f1ab

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
 

‎packages/compiler-core/__tests__/transform.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,26 @@ describe('compiler: transform', () => {
200200
expect((ast as any).children[0].props[0].exp.content).toBe(`_hoisted_1`)
201201
expect((ast as any).children[1].props[0].exp.content).toBe(`_hoisted_2`)
202202
})
203+
204+
test('context.filename and selfName', () => {
205+
const ast = baseParse(`<div />`)
206+
207+
const calls: any[] = []
208+
const plugin: NodeTransform = (node, context) => {
209+
calls.push({ ...context })
210+
}
211+
212+
transform(ast, {
213+
filename: '/the/fileName.vue',
214+
nodeTransforms: [plugin]
215+
})
216+
217+
expect(calls.length).toBe(2)
218+
expect(calls[1]).toMatchObject({
219+
filename: '/the/fileName.vue',
220+
selfName: 'FileName'
221+
})
222+
})
203223

204224
test('onError option', () => {
205225
const ast = baseParse(`<div/>`)

‎packages/compiler-core/src/transform.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export interface ImportItem {
8484

8585
export interface TransformContext
8686
extends Required<
87-
Omit<TransformOptions, 'filename' | keyof CompilerCompatOptions>
87+
Omit<TransformOptions, keyof CompilerCompatOptions>
8888
>,
8989
CompilerCompatOptions {
9090
selfName: string | null
@@ -153,6 +153,7 @@ export function createTransformContext(
153153
const nameMatch = filename.replace(/\?.*$/, '').match(/([^/\\]+)\.\w+$/)
154154
const context: TransformContext = {
155155
// options
156+
filename,
156157
selfName: nameMatch && capitalize(camelize(nameMatch[1])),
157158
prefixIdentifiers,
158159
hoistStatic,

0 commit comments

Comments
 (0)
Please sign in to comment.