@@ -11,6 +11,7 @@ import {
1111 VariableDeclarator
1212} from '@babel/types'
1313import { Artifact } from '../../exports'
14+ import { getRequiredExportsForCapture } from '../imports'
1415
1516/**
1617 * An interop function to make babel's exports work
@@ -54,7 +55,7 @@ export function extractFullFunctionCallName(node: CallExpression): string {
5455 return fullNamespacedFunctionCall . slice ( 0 , lastIndexOfOpeningParen )
5556}
5657
57- export function extractFunctionCallName ( node : CallExpression ) : string {
58+ export function extractFunctionCallName ( node : CallExpression ) : string | 'iife' {
5859 if ( node . callee . type === 'Identifier' ) {
5960 return node . callee . name
6061 }
@@ -88,26 +89,42 @@ export function extractFunctionCallId(
8889export function extractCurrentScope (
8990 path : NodePath <
9091 CallExpression | FunctionDeclaration | FunctionExpression | ArrowFunctionExpression
91- >
92+ > ,
93+ includeCallExpressions = false
9294) : string [ ] {
9395 const scopes : string [ ] = [ ]
94-
9596 let currentPath : NodePath < Node > = path
9697
9798 while ( currentPath . parentPath ) {
9899 currentPath = currentPath . parentPath
99100
100- if ( currentPath . node . type === 'BlockStatement' ) {
101- scopes . push ( '{}' )
101+ if ( includeCallExpressions && currentPath . node . type === 'CallExpression' ) {
102+ const functionCallName = extractFunctionCallName ( currentPath . node )
103+ if (
104+ functionCallName !== 'iife' &&
105+ ! getRequiredExportsForCapture ( ) . includes ( functionCallName )
106+ ) {
107+ scopes . push ( functionCallName )
108+ }
102109 }
103- if ( [ 'FunctionDeclaration' , 'FunctionExpression' ] . includes ( currentPath . node . type ) ) {
110+
111+ if (
112+ [ 'FunctionDeclaration' , 'FunctionExpression' , 'ArrowFunctionExpression' ] . includes (
113+ currentPath . node . type
114+ )
115+ ) {
104116 let scopeName : string
105117 if ( currentPath . node . type === 'FunctionExpression' ) {
106118 if ( currentPath . node . id ?. type === 'Identifier' ) {
107119 scopeName = extractFunctionName ( currentPath . node as FunctionExpression )
108120 } else {
109121 scopeName = extractFunctionName ( currentPath . parent as VariableDeclarator )
110122 }
123+ } else if (
124+ currentPath . node . type === 'ArrowFunctionExpression' &&
125+ currentPath . parent . type === 'VariableDeclarator'
126+ ) {
127+ scopeName = extractFunctionName ( currentPath . parent )
111128 } else {
112129 scopeName = extractFunctionName ( currentPath . node as FunctionDeclaration )
113130 }
@@ -147,7 +164,7 @@ export function extractArtifacts(code: string, filePath: string): Artifact[] {
147164 functionOrCallName : functionName ,
148165 type : 'FUNCTION' ,
149166 params : extractParams ( path . node . params as Identifier [ ] ) ,
150- scopes,
167+ scopes : extractCurrentScope ( path , true ) ,
151168 source : {
152169 filePath,
153170 lineNumber : getLineNumber ( path . node )
@@ -165,7 +182,7 @@ export function extractArtifacts(code: string, filePath: string): Artifact[] {
165182 functionOrCallName : functionName ,
166183 type : 'FUNCTION' ,
167184 params : extractParams ( path . node . params as Identifier [ ] ) ,
168- scopes,
185+ scopes : extractCurrentScope ( path , true ) ,
169186 source : {
170187 filePath,
171188 lineNumber : getLineNumber ( path . node )
@@ -183,7 +200,7 @@ export function extractArtifacts(code: string, filePath: string): Artifact[] {
183200 functionOrCallName : functionName ,
184201 type : 'FUNCTION' ,
185202 params : extractParams ( path . node . params as Identifier [ ] ) ,
186- scopes,
203+ scopes : extractCurrentScope ( path , true ) ,
187204 source : {
188205 filePath,
189206 lineNumber : getLineNumber ( path . node )
@@ -203,7 +220,7 @@ export function extractArtifacts(code: string, filePath: string): Artifact[] {
203220 fullFunctionName : fullFunctionCallName ,
204221 type : 'CALL' ,
205222 params : extractParams ( path . node . arguments as Identifier [ ] ) ,
206- scopes,
223+ scopes : extractCurrentScope ( path , true ) ,
207224 source : {
208225 filePath,
209226 lineNumber : getLineNumber ( path . node )
0 commit comments