1
1
import { module , test } from "qunit" ;
2
- import { builder , Builder } from "./helpers.ts" ;
2
+ import { builder } from "./helpers.ts" ;
3
3
import { type LegacyClassDecorator } from "../src/runtime.ts" ;
4
4
import * as runtimeImpl from "../src/runtime.ts" ;
5
5
import ourDecorators from "../src/index.ts" ;
6
6
import { createRequire } from "node:module" ;
7
7
import { mkdtemp , writeFile } from "node:fs/promises" ;
8
8
import { join } from "node:path" ;
9
9
import { tmpdir } from "node:os" ;
10
+ import type * as Babel from "@babel/core" ;
10
11
11
12
const require = createRequire ( import . meta. url ) ;
12
13
const Colocation = require ( "@embroider/shared-internals/src/template-colocation-plugin" ) ;
13
14
14
- module ( "plugin-interop" , ( hooks ) => {
15
- let build : Builder ;
16
-
17
- hooks . before ( async ( ) => {
15
+ module ( "plugin-interop" , ( ) => {
16
+ test ( "colocation" , async ( assert ) => {
18
17
let dir = await mkdtemp ( join ( tmpdir ( ) , "decorator-transforms-" ) ) ;
19
18
await writeFile ( join ( dir , "example.hbs" ) , "" ) ;
20
- build = builder (
19
+ let build = builder (
21
20
[ ] ,
22
21
[
23
22
[
@@ -29,9 +28,6 @@ module("plugin-interop", (hooks) => {
29
28
[ ] ,
30
29
join ( dir , "example.js" )
31
30
) ;
32
- } ) ;
33
-
34
- test ( "colocation" , async ( assert ) => {
35
31
let red : LegacyClassDecorator = ( target ) => {
36
32
return class extends target {
37
33
get red ( ) {
@@ -77,4 +73,53 @@ module("plugin-interop", (hooks) => {
77
73
assert . strictEqual ( new Example ( ) . red , "#ff0000" ) ;
78
74
assert . strictEqual ( setComponents , 1 ) ;
79
75
} ) ;
76
+
77
+ // This models a behavior that @embroider /macros uses when trying to optimize
78
+ // `getConfig().with.a.path`, since it uses parentPath to go higher and
79
+ // replace a larger part of the expression.
80
+ test ( "parentPath replace" , async ( assert ) => {
81
+ function inserter ( babel : typeof Babel ) : Babel . PluginObj {
82
+ let t = babel . types ;
83
+ return {
84
+ visitor : {
85
+ CallExpression : {
86
+ enter ( path ) {
87
+ let callee = path . node . callee ;
88
+ if ( callee . type === "Identifier" && callee . name === "expandMe" ) {
89
+ path . parentPath . replaceWith (
90
+ t . objectExpression ( [
91
+ t . objectProperty (
92
+ t . identifier ( "value" ) ,
93
+ t . stringLiteral ( "it worked" )
94
+ ) ,
95
+ ] )
96
+ ) ;
97
+ }
98
+ } ,
99
+ } ,
100
+ } ,
101
+ } ;
102
+ }
103
+
104
+ let build = builder (
105
+ [ ] ,
106
+ [
107
+ [
108
+ ourDecorators ,
109
+ { runtime : { import : "decorator-transforms/runtime" } } ,
110
+ ] ,
111
+ [ inserter ] ,
112
+ ] ,
113
+ [ ]
114
+ ) ;
115
+
116
+ let { default : Example } = await build . module (
117
+ `
118
+ export default expandMe().a;
119
+ ` ,
120
+ { }
121
+ ) ;
122
+
123
+ assert . strictEqual ( Example . value , "it worked" ) ;
124
+ } ) ;
80
125
} ) ;
0 commit comments