1
1
import MagicString from 'magic-string' ;
2
2
import { Node } from 'estree-walker' ;
3
- import ts from 'typescript' ;
3
+ import ts , { VariableDeclaration } from 'typescript' ;
4
4
import { getBinaryAssignmentExpr , isNotPropertyNameOfImport , moveNode } from './utils/tsAst' ;
5
5
import { ExportedNames , is$$PropsDeclaration } from './nodes/ExportedNames' ;
6
6
import { ImplicitTopLevelNames } from './nodes/ImplicitTopLevelNames' ;
@@ -32,6 +32,7 @@ interface PendingStoreResolution {
32
32
node : ts . Identifier ;
33
33
parent : ts . Node ;
34
34
scope : Scope ;
35
+ isPropsId : boolean ;
35
36
}
36
37
37
38
export function processInstanceScriptContent (
@@ -82,13 +83,18 @@ export function processInstanceScriptContent(
82
83
//track if we are in a declaration scope
83
84
let isDeclaration = false ;
84
85
86
+ //track the variable declaration node
87
+ let variableDeclarationNode : VariableDeclaration | null = null ;
88
+
85
89
//track $store variables since we are only supposed to give top level scopes special treatment, and users can declare $blah variables at higher scopes
86
90
//which prevents us just changing all instances of Identity that start with $
87
- const pendingStoreResolutions : PendingStoreResolution [ ] = [ ] ;
91
+ let pendingStoreResolutions : PendingStoreResolution [ ] = [ ] ;
88
92
89
93
let scope = new Scope ( ) ;
90
94
const rootScope = scope ;
91
95
96
+ let isPropsDeclarationRune = false ;
97
+
92
98
const pushScope = ( ) => ( scope = new Scope ( scope ) ) ;
93
99
const popScope = ( ) => ( scope = scope . parent ) ;
94
100
@@ -124,6 +130,16 @@ export function processInstanceScriptContent(
124
130
return ;
125
131
}
126
132
133
+ if (
134
+ ident . text === 'props' &&
135
+ variableDeclarationNode &&
136
+ variableDeclarationNode . initializer &&
137
+ ts . isCallExpression ( variableDeclarationNode . initializer ) &&
138
+ variableDeclarationNode . initializer . getText ( ) === '$props()'
139
+ ) {
140
+ isPropsDeclarationRune = true ;
141
+ }
142
+
127
143
if ( isDeclaration || ts . isParameter ( parent ) ) {
128
144
if (
129
145
isNotPropertyNameOfImport ( ident ) &&
@@ -148,14 +164,25 @@ export function processInstanceScriptContent(
148
164
! ts . isTypeAliasDeclaration ( parent ) &&
149
165
! ts . isInterfaceDeclaration ( parent )
150
166
) {
167
+ let isPropsId = false ;
168
+ if (
169
+ text === '$props' &&
170
+ ts . isPropertyAccessExpression ( parent ) &&
171
+ parent . parent &&
172
+ ts . isCallExpression ( parent . parent ) &&
173
+ parent . parent . arguments . length === 0
174
+ ) {
175
+ const text = parent . getText ( ) ;
176
+ isPropsId = text === '$props.id' ;
177
+ }
151
178
// Handle the const { ...props } = $props() case
152
179
const is_rune =
153
180
( text === '$props' || text === '$derived' || text === '$state' ) &&
154
181
ts . isCallExpression ( parent ) &&
155
182
ts . isVariableDeclaration ( parent . parent ) &&
156
183
parent . parent . name . getText ( ) . includes ( text . slice ( 1 ) ) ;
157
184
if ( ! is_rune ) {
158
- pendingStoreResolutions . push ( { node : ident , parent, scope } ) ;
185
+ pendingStoreResolutions . push ( { node : ident , parent, scope, isPropsId } ) ;
159
186
}
160
187
}
161
188
}
@@ -234,7 +261,11 @@ export function processInstanceScriptContent(
234
261
235
262
if ( ts . isVariableDeclaration ( parent ) && parent . name == node ) {
236
263
isDeclaration = true ;
237
- onLeaveCallbacks . push ( ( ) => ( isDeclaration = false ) ) ;
264
+ variableDeclarationNode = parent ;
265
+ onLeaveCallbacks . push ( ( ) => {
266
+ isDeclaration = false ;
267
+ variableDeclarationNode = null ;
268
+ } ) ;
238
269
}
239
270
240
271
if ( ts . isBindingElement ( parent ) && parent . name == node ) {
@@ -295,6 +326,9 @@ export function processInstanceScriptContent(
295
326
tsAst . forEachChild ( ( n ) => walk ( n , tsAst ) ) ;
296
327
297
328
//resolve stores
329
+ if ( isPropsDeclarationRune ) {
330
+ pendingStoreResolutions = pendingStoreResolutions . filter ( ( { isPropsId } ) => ! isPropsId ) ;
331
+ }
298
332
pendingStoreResolutions . map ( resolveStore ) ;
299
333
300
334
// declare implicit reactive variables we found in the script
0 commit comments