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 ,
@@ -3365,7 +3364,7 @@ function lowerFunction(
3365
3364
> ,
3366
3365
) : LoweredFunction | null {
3367
3366
const componentScope : Scope = builder . parentFunction . scope ;
3368
- const captured = gatherCapturedDeps ( builder , expr , componentScope ) ;
3367
+ const capturedContext = gatherCapturedContext ( expr , componentScope ) ;
3369
3368
3370
3369
/*
3371
3370
* TODO(gsn): In the future, we could only pass in the context identifiers
@@ -3379,7 +3378,7 @@ function lowerFunction(
3379
3378
expr ,
3380
3379
builder . environment ,
3381
3380
builder . bindings ,
3382
- [ ...builder . context , ...captured . identifiers ] ,
3381
+ [ ...builder . context , ...capturedContext ] ,
3383
3382
builder . parentFunction ,
3384
3383
) ;
3385
3384
let loweredFunc : HIRFunction ;
@@ -3392,7 +3391,6 @@ function lowerFunction(
3392
3391
loweredFunc = lowering . unwrap ( ) ;
3393
3392
return {
3394
3393
func : loweredFunc ,
3395
- dependencies : captured . refs ,
3396
3394
} ;
3397
3395
}
3398
3396
@@ -4066,14 +4064,6 @@ function lowerAssignment(
4066
4064
}
4067
4065
}
4068
4066
4069
- function isValidDependency ( path : NodePath < t . MemberExpression > ) : boolean {
4070
- const parent : NodePath < t . Node > = path . parentPath ;
4071
- return (
4072
- ! path . node . computed &&
4073
- ! ( parent . isCallExpression ( ) && parent . get ( 'callee' ) === path )
4074
- ) ;
4075
- }
4076
-
4077
4067
function captureScopes ( { from, to} : { from : Scope ; to : Scope } ) : Set < Scope > {
4078
4068
let scopes : Set < Scope > = new Set ( ) ;
4079
4069
while ( from ) {
@@ -4088,19 +4078,16 @@ function captureScopes({from, to}: {from: Scope; to: Scope}): Set<Scope> {
4088
4078
return scopes ;
4089
4079
}
4090
4080
4091
- function gatherCapturedDeps (
4092
- builder : HIRBuilder ,
4081
+ function gatherCapturedContext (
4093
4082
fn : NodePath <
4094
4083
| t . FunctionExpression
4095
4084
| t . ArrowFunctionExpression
4096
4085
| t . FunctionDeclaration
4097
4086
| t . ObjectMethod
4098
4087
> ,
4099
4088
componentScope : Scope ,
4100
- ) : { identifiers : Array < t . Identifier > ; refs : Array < Place > } {
4101
- const capturedIds : Map < t . Identifier , number > = new Map ( ) ;
4102
- const capturedRefs : Set < Place > = new Set ( ) ;
4103
- const seenPaths : Set < string > = new Set ( ) ;
4089
+ ) : Array < t . Identifier > {
4090
+ const capturedIds = new Set < t . Identifier > ( ) ;
4104
4091
4105
4092
/*
4106
4093
* Capture all the scopes from the parent of this function up to and including
@@ -4111,33 +4098,11 @@ function gatherCapturedDeps(
4111
4098
to : componentScope ,
4112
4099
} ) ;
4113
4100
4114
- function addCapturedId ( bindingIdentifier : t . Identifier ) : number {
4115
- if ( ! capturedIds . has ( bindingIdentifier ) ) {
4116
- const index = capturedIds . size ;
4117
- capturedIds . set ( bindingIdentifier , index ) ;
4118
- return index ;
4119
- } else {
4120
- return capturedIds . get ( bindingIdentifier ) ! ;
4121
- }
4122
- }
4123
-
4124
4101
function handleMaybeDependency (
4125
- path :
4126
- | NodePath < t . MemberExpression >
4127
- | NodePath < t . Identifier >
4128
- | NodePath < t . JSXOpeningElement > ,
4102
+ path : NodePath < t . Identifier > | NodePath < t . JSXOpeningElement > ,
4129
4103
) : void {
4130
4104
// Base context variable to depend on
4131
4105
let baseIdentifier : NodePath < t . Identifier > | NodePath < t . JSXIdentifier > ;
4132
- /*
4133
- * Base expression to depend on, which (for now) may contain non side-effectful
4134
- * member expressions
4135
- */
4136
- let dependency :
4137
- | NodePath < t . MemberExpression >
4138
- | NodePath < t . JSXMemberExpression >
4139
- | NodePath < t . Identifier >
4140
- | NodePath < t . JSXIdentifier > ;
4141
4106
if ( path . isJSXOpeningElement ( ) ) {
4142
4107
const name = path . get ( 'name' ) ;
4143
4108
if ( ! ( name . isJSXMemberExpression ( ) || name . isJSXIdentifier ( ) ) ) {
@@ -4153,115 +4118,20 @@ function gatherCapturedDeps(
4153
4118
'Invalid logic in gatherCapturedDeps' ,
4154
4119
) ;
4155
4120
baseIdentifier = current ;
4156
-
4157
- /*
4158
- * Get the expression to depend on, which may involve PropertyLoads
4159
- * for member expressions
4160
- */
4161
- let currentDep :
4162
- | NodePath < t . JSXMemberExpression >
4163
- | NodePath < t . Identifier >
4164
- | NodePath < t . JSXIdentifier > = baseIdentifier ;
4165
-
4166
- while ( true ) {
4167
- const nextDep : null | NodePath < t . Node > = currentDep . parentPath ;
4168
- if ( nextDep && nextDep . isJSXMemberExpression ( ) ) {
4169
- currentDep = nextDep ;
4170
- } else {
4171
- break ;
4172
- }
4173
- }
4174
- dependency = currentDep ;
4175
- } else if ( path . isMemberExpression ( ) ) {
4176
- // Calculate baseIdentifier
4177
- let currentId : NodePath < Expression > = path ;
4178
- while ( currentId . isMemberExpression ( ) ) {
4179
- currentId = currentId . get ( 'object' ) ;
4180
- }
4181
- if ( ! currentId . isIdentifier ( ) ) {
4182
- return ;
4183
- }
4184
- baseIdentifier = currentId ;
4185
-
4186
- /*
4187
- * Get the expression to depend on, which may involve PropertyLoads
4188
- * for member expressions
4189
- */
4190
- let currentDep :
4191
- | NodePath < t . MemberExpression >
4192
- | NodePath < t . Identifier >
4193
- | NodePath < t . JSXIdentifier > = baseIdentifier ;
4194
-
4195
- while ( true ) {
4196
- const nextDep : null | NodePath < t . Node > = currentDep . parentPath ;
4197
- if (
4198
- nextDep &&
4199
- nextDep . isMemberExpression ( ) &&
4200
- isValidDependency ( nextDep )
4201
- ) {
4202
- currentDep = nextDep ;
4203
- } else {
4204
- break ;
4205
- }
4206
- }
4207
-
4208
- dependency = currentDep ;
4209
4121
} else {
4210
4122
baseIdentifier = path ;
4211
- dependency = path ;
4212
4123
}
4213
4124
4214
4125
/*
4215
4126
* Skip dependency path, as we already tried to recursively add it (+ all subexpressions)
4216
4127
* as a dependency.
4217
4128
*/
4218
- dependency . skip ( ) ;
4129
+ path . skip ( ) ;
4219
4130
4220
4131
// Add the base identifier binding as a dependency.
4221
4132
const binding = baseIdentifier . scope . getBinding ( baseIdentifier . node . name ) ;
4222
- if ( binding === undefined || ! pureScopes . has ( binding . scope ) ) {
4223
- return ;
4224
- }
4225
- const idKey = String ( addCapturedId ( binding . identifier ) ) ;
4226
-
4227
- // Add the expression (potentially a memberexpr path) as a dependency.
4228
- let exprKey = idKey ;
4229
- if ( dependency . isMemberExpression ( ) ) {
4230
- let pathTokens = [ ] ;
4231
- let current : NodePath < Expression > = dependency ;
4232
- while ( current . isMemberExpression ( ) ) {
4233
- const property = current . get ( 'property' ) as NodePath < t . Identifier > ;
4234
- pathTokens . push ( property . node . name ) ;
4235
- current = current . get ( 'object' ) ;
4236
- }
4237
-
4238
- exprKey += '.' + pathTokens . reverse ( ) . join ( '.' ) ;
4239
- } else if ( dependency . isJSXMemberExpression ( ) ) {
4240
- let pathTokens = [ ] ;
4241
- let current : NodePath < t . JSXMemberExpression | t . JSXIdentifier > =
4242
- dependency ;
4243
- while ( current . isJSXMemberExpression ( ) ) {
4244
- const property = current . get ( 'property' ) ;
4245
- pathTokens . push ( property . node . name ) ;
4246
- current = current . get ( 'object' ) ;
4247
- }
4248
- }
4249
-
4250
- if ( ! seenPaths . has ( exprKey ) ) {
4251
- let loweredDep : Place ;
4252
- if ( dependency . isJSXIdentifier ( ) ) {
4253
- loweredDep = lowerValueToTemporary ( builder , {
4254
- kind : 'LoadLocal' ,
4255
- place : lowerIdentifier ( builder , dependency ) ,
4256
- loc : path . node . loc ?? GeneratedSource ,
4257
- } ) ;
4258
- } else if ( dependency . isJSXMemberExpression ( ) ) {
4259
- loweredDep = lowerJsxMemberExpression ( builder , dependency ) ;
4260
- } else {
4261
- loweredDep = lowerExpressionToTemporary ( builder , dependency ) ;
4262
- }
4263
- capturedRefs . add ( loweredDep ) ;
4264
- seenPaths . add ( exprKey ) ;
4133
+ if ( binding !== undefined && pureScopes . has ( binding . scope ) ) {
4134
+ capturedIds . add ( binding . identifier ) ;
4265
4135
}
4266
4136
}
4267
4137
@@ -4292,13 +4162,13 @@ function gatherCapturedDeps(
4292
4162
return ;
4293
4163
} else if ( path . isJSXElement ( ) ) {
4294
4164
handleMaybeDependency ( path . get ( 'openingElement' ) ) ;
4295
- } else if ( path . isMemberExpression ( ) || path . isIdentifier ( ) ) {
4165
+ } else if ( path . isIdentifier ( ) ) {
4296
4166
handleMaybeDependency ( path ) ;
4297
4167
}
4298
4168
} ,
4299
4169
} ) ;
4300
4170
4301
- return { identifiers : [ ...capturedIds . keys ( ) ] , refs : [ ... capturedRefs ] } ;
4171
+ return [ ...capturedIds . keys ( ) ] ;
4302
4172
}
4303
4173
4304
4174
function notNull < T > ( value : T | null ) : value is T {
0 commit comments