7
7
8
8
import { NodePath , Scope } from '@babel/traverse' ;
9
9
import * as t from '@babel/types' ;
10
- import { Expression } from '@babel/types' ;
11
10
import invariant from 'invariant' ;
12
11
import {
13
12
CompilerError ,
@@ -75,7 +74,7 @@ export function lower(
75
74
parent : NodePath < t . Function > | null = null ,
76
75
) : Result < HIRFunction , CompilerError > {
77
76
const builder = new HIRBuilder ( env , parent ?? func , bindings , capturedRefs ) ;
78
- const context : Array < Place > = [ ] ;
77
+ const context : HIRFunction [ 'context' ] = [ ] ;
79
78
80
79
for ( const ref of capturedRefs ?? [ ] ) {
81
80
context . push ( {
@@ -3377,7 +3376,7 @@ function lowerFunction(
3377
3376
> ,
3378
3377
) : LoweredFunction | null {
3379
3378
const componentScope : Scope = builder . parentFunction . scope ;
3380
- const captured = gatherCapturedDeps ( builder , expr , componentScope ) ;
3379
+ const capturedContext = gatherCapturedContext ( expr , componentScope ) ;
3381
3380
3382
3381
/*
3383
3382
* TODO(gsn): In the future, we could only pass in the context identifiers
@@ -3391,7 +3390,7 @@ function lowerFunction(
3391
3390
expr ,
3392
3391
builder . environment ,
3393
3392
builder . bindings ,
3394
- [ ...builder . context , ...captured . identifiers ] ,
3393
+ [ ...builder . context , ...capturedContext ] ,
3395
3394
builder . parentFunction ,
3396
3395
) ;
3397
3396
let loweredFunc : HIRFunction ;
@@ -3404,7 +3403,6 @@ function lowerFunction(
3404
3403
loweredFunc = lowering . unwrap ( ) ;
3405
3404
return {
3406
3405
func : loweredFunc ,
3407
- dependencies : captured . refs ,
3408
3406
} ;
3409
3407
}
3410
3408
@@ -4078,14 +4076,6 @@ function lowerAssignment(
4078
4076
}
4079
4077
}
4080
4078
4081
- function isValidDependency ( path : NodePath < t . MemberExpression > ) : boolean {
4082
- const parent : NodePath < t . Node > = path . parentPath ;
4083
- return (
4084
- ! path . node . computed &&
4085
- ! ( parent . isCallExpression ( ) && parent . get ( 'callee' ) === path )
4086
- ) ;
4087
- }
4088
-
4089
4079
function captureScopes ( { from, to} : { from : Scope ; to : Scope } ) : Set < Scope > {
4090
4080
let scopes : Set < Scope > = new Set ( ) ;
4091
4081
while ( from ) {
@@ -4100,19 +4090,16 @@ function captureScopes({from, to}: {from: Scope; to: Scope}): Set<Scope> {
4100
4090
return scopes ;
4101
4091
}
4102
4092
4103
- function gatherCapturedDeps (
4104
- builder : HIRBuilder ,
4093
+ function gatherCapturedContext (
4105
4094
fn : NodePath <
4106
4095
| t . FunctionExpression
4107
4096
| t . ArrowFunctionExpression
4108
4097
| t . FunctionDeclaration
4109
4098
| t . ObjectMethod
4110
4099
> ,
4111
4100
componentScope : Scope ,
4112
- ) : { identifiers : Array < t . Identifier > ; refs : Array < Place > } {
4113
- const capturedIds : Map < t . Identifier , number > = new Map ( ) ;
4114
- const capturedRefs : Set < Place > = new Set ( ) ;
4115
- const seenPaths : Set < string > = new Set ( ) ;
4101
+ ) : Array < t . Identifier > {
4102
+ const capturedIds = new Set < t . Identifier > ( ) ;
4116
4103
4117
4104
/*
4118
4105
* Capture all the scopes from the parent of this function up to and including
@@ -4123,33 +4110,11 @@ function gatherCapturedDeps(
4123
4110
to : componentScope ,
4124
4111
} ) ;
4125
4112
4126
- function addCapturedId ( bindingIdentifier : t . Identifier ) : number {
4127
- if ( ! capturedIds . has ( bindingIdentifier ) ) {
4128
- const index = capturedIds . size ;
4129
- capturedIds . set ( bindingIdentifier , index ) ;
4130
- return index ;
4131
- } else {
4132
- return capturedIds . get ( bindingIdentifier ) ! ;
4133
- }
4134
- }
4135
-
4136
4113
function handleMaybeDependency (
4137
- path :
4138
- | NodePath < t . MemberExpression >
4139
- | NodePath < t . Identifier >
4140
- | NodePath < t . JSXOpeningElement > ,
4114
+ path : NodePath < t . Identifier > | NodePath < t . JSXOpeningElement > ,
4141
4115
) : void {
4142
4116
// Base context variable to depend on
4143
4117
let baseIdentifier : NodePath < t . Identifier > | NodePath < t . JSXIdentifier > ;
4144
- /*
4145
- * Base expression to depend on, which (for now) may contain non side-effectful
4146
- * member expressions
4147
- */
4148
- let dependency :
4149
- | NodePath < t . MemberExpression >
4150
- | NodePath < t . JSXMemberExpression >
4151
- | NodePath < t . Identifier >
4152
- | NodePath < t . JSXIdentifier > ;
4153
4118
if ( path . isJSXOpeningElement ( ) ) {
4154
4119
const name = path . get ( 'name' ) ;
4155
4120
if ( ! ( name . isJSXMemberExpression ( ) || name . isJSXIdentifier ( ) ) ) {
@@ -4165,115 +4130,20 @@ function gatherCapturedDeps(
4165
4130
'Invalid logic in gatherCapturedDeps' ,
4166
4131
) ;
4167
4132
baseIdentifier = current ;
4168
-
4169
- /*
4170
- * Get the expression to depend on, which may involve PropertyLoads
4171
- * for member expressions
4172
- */
4173
- let currentDep :
4174
- | NodePath < t . JSXMemberExpression >
4175
- | NodePath < t . Identifier >
4176
- | NodePath < t . JSXIdentifier > = baseIdentifier ;
4177
-
4178
- while ( true ) {
4179
- const nextDep : null | NodePath < t . Node > = currentDep . parentPath ;
4180
- if ( nextDep && nextDep . isJSXMemberExpression ( ) ) {
4181
- currentDep = nextDep ;
4182
- } else {
4183
- break ;
4184
- }
4185
- }
4186
- dependency = currentDep ;
4187
- } else if ( path . isMemberExpression ( ) ) {
4188
- // Calculate baseIdentifier
4189
- let currentId : NodePath < Expression > = path ;
4190
- while ( currentId . isMemberExpression ( ) ) {
4191
- currentId = currentId . get ( 'object' ) ;
4192
- }
4193
- if ( ! currentId . isIdentifier ( ) ) {
4194
- return ;
4195
- }
4196
- baseIdentifier = currentId ;
4197
-
4198
- /*
4199
- * Get the expression to depend on, which may involve PropertyLoads
4200
- * for member expressions
4201
- */
4202
- let currentDep :
4203
- | NodePath < t . MemberExpression >
4204
- | NodePath < t . Identifier >
4205
- | NodePath < t . JSXIdentifier > = baseIdentifier ;
4206
-
4207
- while ( true ) {
4208
- const nextDep : null | NodePath < t . Node > = currentDep . parentPath ;
4209
- if (
4210
- nextDep &&
4211
- nextDep . isMemberExpression ( ) &&
4212
- isValidDependency ( nextDep )
4213
- ) {
4214
- currentDep = nextDep ;
4215
- } else {
4216
- break ;
4217
- }
4218
- }
4219
-
4220
- dependency = currentDep ;
4221
4133
} else {
4222
4134
baseIdentifier = path ;
4223
- dependency = path ;
4224
4135
}
4225
4136
4226
4137
/*
4227
4138
* Skip dependency path, as we already tried to recursively add it (+ all subexpressions)
4228
4139
* as a dependency.
4229
4140
*/
4230
- dependency . skip ( ) ;
4141
+ path . skip ( ) ;
4231
4142
4232
4143
// Add the base identifier binding as a dependency.
4233
4144
const binding = baseIdentifier . scope . getBinding ( baseIdentifier . node . name ) ;
4234
- if ( binding === undefined || ! pureScopes . has ( binding . scope ) ) {
4235
- return ;
4236
- }
4237
- const idKey = String ( addCapturedId ( binding . identifier ) ) ;
4238
-
4239
- // Add the expression (potentially a memberexpr path) as a dependency.
4240
- let exprKey = idKey ;
4241
- if ( dependency . isMemberExpression ( ) ) {
4242
- let pathTokens = [ ] ;
4243
- let current : NodePath < Expression > = dependency ;
4244
- while ( current . isMemberExpression ( ) ) {
4245
- const property = current . get ( 'property' ) as NodePath < t . Identifier > ;
4246
- pathTokens . push ( property . node . name ) ;
4247
- current = current . get ( 'object' ) ;
4248
- }
4249
-
4250
- exprKey += '.' + pathTokens . reverse ( ) . join ( '.' ) ;
4251
- } else if ( dependency . isJSXMemberExpression ( ) ) {
4252
- let pathTokens = [ ] ;
4253
- let current : NodePath < t . JSXMemberExpression | t . JSXIdentifier > =
4254
- dependency ;
4255
- while ( current . isJSXMemberExpression ( ) ) {
4256
- const property = current . get ( 'property' ) ;
4257
- pathTokens . push ( property . node . name ) ;
4258
- current = current . get ( 'object' ) ;
4259
- }
4260
- }
4261
-
4262
- if ( ! seenPaths . has ( exprKey ) ) {
4263
- let loweredDep : Place ;
4264
- if ( dependency . isJSXIdentifier ( ) ) {
4265
- loweredDep = lowerValueToTemporary ( builder , {
4266
- kind : 'LoadLocal' ,
4267
- place : lowerIdentifier ( builder , dependency ) ,
4268
- loc : path . node . loc ?? GeneratedSource ,
4269
- } ) ;
4270
- } else if ( dependency . isJSXMemberExpression ( ) ) {
4271
- loweredDep = lowerJsxMemberExpression ( builder , dependency ) ;
4272
- } else {
4273
- loweredDep = lowerExpressionToTemporary ( builder , dependency ) ;
4274
- }
4275
- capturedRefs . add ( loweredDep ) ;
4276
- seenPaths . add ( exprKey ) ;
4145
+ if ( binding !== undefined && pureScopes . has ( binding . scope ) ) {
4146
+ capturedIds . add ( binding . identifier ) ;
4277
4147
}
4278
4148
}
4279
4149
@@ -4304,13 +4174,13 @@ function gatherCapturedDeps(
4304
4174
return ;
4305
4175
} else if ( path . isJSXElement ( ) ) {
4306
4176
handleMaybeDependency ( path . get ( 'openingElement' ) ) ;
4307
- } else if ( path . isMemberExpression ( ) || path . isIdentifier ( ) ) {
4177
+ } else if ( path . isIdentifier ( ) ) {
4308
4178
handleMaybeDependency ( path ) ;
4309
4179
}
4310
4180
} ,
4311
4181
} ) ;
4312
4182
4313
- return { identifiers : [ ...capturedIds . keys ( ) ] , refs : [ ... capturedRefs ] } ;
4183
+ return [ ...capturedIds . keys ( ) ] ;
4314
4184
}
4315
4185
4316
4186
function notNull < T > ( value : T | null ) : value is T {
0 commit comments