Skip to content

Commit aaa3626

Browse files
committed
fix: improved scopes
1 parent dd16090 commit aaa3626

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

examples/with-nextjs/flytrap.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { defineFlytrapConfig } from 'useflytrap'
33
export default defineFlytrapConfig({
44
projectId: 'nextjs-demo',
55
publicApiKey:
6-
'pk_MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsTAnbcqXBFwKQhf8htpo+dH1r5iIXpr4xrZofn60J4nJGtdkVYXNj+pjlbooZIDBxrVLHKrYwI3TOFgM3KSLQS7YOFyS7a29kqGLUPGnO809pTTDEP66ho7/e+8jdjiyBDNTv6lwS3rxYteHcN7UQERA69gqvessNUNiCJlpL9JxBKnU+KDFXoV8IdUKiJqJUkZ2OT0vf0xMWUXQkdF8/+gFlJJpQlMe7OM9Owjyyl39ceoJ27yR559IVwmAXgIh4rXfb9/tBmq224+k73ohhciLPFBpRhJBp6AI+lTyFZI6GkHQBWyjmTyeEtJ1aDtdoKMSwIc7qOVvnJ9OUZVtsQIDAQAB',
6+
'pk_MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlOV7JSvZ2ve0wwgyICJgfePGgQUHBH5axSg0NJbEVv4OkZ1v1fo/q+rUKv6fhAPfITX4pXyJuTN1p3qAAMZ4k7ACq2KeDjpOQK7sdEGOxM8MwxqkkIsL8/q/fplsikvUzZkU6Tgy6EitYD2SNkMhkr1jGc7Y4FLw7/uCYDtcvV7Z5whygg4K55HtU8IMUwbOzlwMt9IflMG6zyz4C4BbuvNK1wGd4ZEsppKgDtgnGIhEGY55p4G/Vk3nb9+qIfSLpk8kj2fsXPs13zeI5NcesxXGdMLWj0SKWXCJ9UIsYJseVBTXqzNGnNUZEQr/Q881jsm5aFz/uMGqk0GTCCISPQIDAQAB',
7+
secretApiKey: 'sk_Y8dYLe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w1ZiaBOB2iY6CQ56uFvbnHGwdPW2uxxme/1drtTGXDvmJboTYZsl215rSRhLLzWEf0MBLhN8XwUoo4W8MBFyeqr5A+oUC2RgAAEEgV/7x2lGVPRBE/WoJWwWBfPvelzA==',
1010
mode: 'capture',
1111
logging: ['api-calls', 'capture', 'storage'],
1212
packageIgnores: ['next/font']

examples/with-nextjs/src/app/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ function Home() {
5050
height={37}
5151
priority
5252
/>
53-
<input value={inputValue} onChange={(e) => setInputValue(e.target.value)} />
53+
<div className='text-black'>
54+
<input value={inputValue} onChange={(e) => setInputValue(e.target.value)} />
55+
</div>
5456
<button onClick={() => submit()}>Submit</button>
5557
</div>
5658

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"lint:all:eslint": "pnpm lint:eslint --ext .ts,.js,.mjs,.cjs \"{src,test}/**/*.{js,json,ts}\"",
3434
"lint:all:prettier": "pnpm lint:prettier \"{src,test,examples}/**/*.{js,json,ts}\"",
3535
"lint:eslint": "eslint --fix",
36-
"lint:prettier": "prettier --write --log-level warn",
36+
"lint:prettier": "prettier --write --loglevel warn",
3737
"prepublishOnly": "pnpm lint && pnpm test",
3838
"release": "np",
3939
"test": "vitest run",

src/transform/artifacts/artifacts.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
VariableDeclarator
1212
} from '@babel/types'
1313
import { 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(
8889
export 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)

test/artifacts.test.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('extractCurrentScope', () => {
129129
{
130130
name: 'block scope',
131131
fixture: `{ foo() }`,
132-
scopes: ['{}']
132+
scopes: []
133133
},
134134
{
135135
name: 'arrow function scope',
@@ -138,7 +138,7 @@ describe('extractCurrentScope', () => {
138138
foo()
139139
}
140140
`,
141-
scopes: ['{}']
141+
scopes: ['arrowFuncScope']
142142
},
143143
{
144144
name: 'function expression scope',
@@ -147,7 +147,7 @@ describe('extractCurrentScope', () => {
147147
foo()
148148
}
149149
`,
150-
scopes: ['funcExprScope', '{}']
150+
scopes: ['funcExprScope']
151151
},
152152
{
153153
name: 'function declaration scope',
@@ -156,7 +156,16 @@ describe('extractCurrentScope', () => {
156156
foo()
157157
}
158158
`,
159-
scopes: ['funcDeclScope', '{}']
159+
scopes: ['funcDeclScope']
160+
}
161+
]
162+
const scopeFuncFixtures = [
163+
{
164+
name: 'function call scope',
165+
fixture: `
166+
users.find((u) => u.name === '')
167+
`,
168+
scopes: ['find']
160169
}
161170
]
162171

@@ -171,7 +180,16 @@ describe('extractCurrentScope', () => {
171180
}
172181
})
173182

174-
it.todo('extracts scope for functions')
183+
it('extracts scope for functions', () => {
184+
for (let i = 0; i < scopeFuncFixtures.length; i++) {
185+
const ast = parse(scopeFuncFixtures[i].fixture, { parser: babelTsParser })
186+
_babelInterop(babelTraverse)(ast, {
187+
ArrowFunctionExpression(path) {
188+
expect(extractCurrentScope(path, true)).toEqual(scopeFuncFixtures[i].scopes)
189+
}
190+
})
191+
}
192+
})
175193
})
176194

177195
describe('Artifacts for function calls', () => {

0 commit comments

Comments
 (0)