]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-core): add current filename to TransformContext (#8950)
authorAdrien Foulon <6115458+Tofandel@users.noreply.github.com>
Fri, 8 Dec 2023 07:23:50 +0000 (08:23 +0100)
committerGitHub <noreply@github.com>
Fri, 8 Dec 2023 07:23:50 +0000 (15:23 +0800)
packages/compiler-core/__tests__/transform.spec.ts
packages/compiler-core/src/transform.ts

index f0e91108e43ec8da5ab3ef364cb07d330ef99aee..7657e74f7e80dc9043c8b6481e17bb62e979b879 100644 (file)
@@ -200,6 +200,26 @@ describe('compiler: transform', () => {
     expect((ast as any).children[0].props[0].exp.content).toBe(`_hoisted_1`)
     expect((ast as any).children[1].props[0].exp.content).toBe(`_hoisted_2`)
   })
+  
+  test('context.filename and selfName', () => {
+    const ast = baseParse(`<div />`)
+    
+    const calls: any[] = []
+    const plugin: NodeTransform = (node, context) => {
+      calls.push({ ...context })
+    }
+    
+    transform(ast, {
+      filename: '/the/fileName.vue',
+      nodeTransforms: [plugin]
+    })
+    
+    expect(calls.length).toBe(2)
+    expect(calls[1]).toMatchObject({
+      filename: '/the/fileName.vue',
+      selfName: 'FileName'
+    })
+  })
 
   test('onError option', () => {
     const ast = baseParse(`<div/>`)
index f128b3b6e9df0275e4f74a882e5e71b2af610b64..3a568a0729825409b559343f4a10d7d5d5475302 100644 (file)
@@ -84,7 +84,7 @@ export interface ImportItem {
 
 export interface TransformContext
   extends Required<
-      Omit<TransformOptions, 'filename' | keyof CompilerCompatOptions>
+      Omit<TransformOptions, keyof CompilerCompatOptions>
     >,
     CompilerCompatOptions {
   selfName: string | null
@@ -153,6 +153,7 @@ export function createTransformContext(
   const nameMatch = filename.replace(/\?.*$/, '').match(/([^/\\]+)\.\w+$/)
   const context: TransformContext = {
     // options
+    filename,
     selfName: nameMatch && capitalize(camelize(nameMatch[1])),
     prefixIdentifiers,
     hoistStatic,