Skip to content

Commit e523e1a

Browse files
committed
fix: generate artifacts that correspond to transformed code
1 parent 5732ade commit e523e1a

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/transform/artifacts/artifacts.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,9 @@ export function extractCurrentScope(
9494
if (currentPath.node.type === 'BlockStatement') {
9595
scopes.push('{}')
9696
}
97-
if (
98-
['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression'].includes(
99-
currentPath.node.type
100-
)
101-
) {
97+
if (['FunctionDeclaration', 'FunctionExpression'].includes(currentPath.node.type)) {
10298
let scopeName: string
103-
if (currentPath.node.type === 'ArrowFunctionExpression') {
104-
scopeName = extractFunctionName(currentPath.parent as VariableDeclarator | ObjectProperty)
105-
} else if (currentPath.node.type === 'FunctionExpression') {
99+
if (currentPath.node.type === 'FunctionExpression') {
106100
if (currentPath.node.id?.type === 'Identifier') {
107101
scopeName = extractFunctionName(currentPath.node as FunctionExpression)
108102
} else {

test/artifacts.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('extractCurrentScope', () => {
131131
foo()
132132
}
133133
`,
134-
scopes: ['arrowFuncScope', '{}']
134+
scopes: ['{}']
135135
},
136136
{
137137
name: 'function expression scope',

test/regression.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { flytrapTransformArtifacts } from '../src/transform/index'
2+
import { describe, it } from 'vitest'
3+
import { extractArtifacts } from '../src/transform/artifacts/artifacts'
4+
5+
describe('Regression', () => {
6+
it('regression #1 > generates correct artifacts that match the transformed code', async () => {
7+
const fixture = `
8+
export const useRequestAccess = create((set) => ({
9+
setOpen: (open: boolean) => set((state) => ({ open }))
10+
}))
11+
`
12+
13+
const transformedCode = await flytrapTransformArtifacts(fixture, '/stores.ts').code
14+
const artifacts = extractArtifacts(fixture, '/stores.ts')
15+
16+
const functionOrCallIds = artifacts.map((a) => a.functionOrCallId)
17+
const missingIds = new Set<string>([])
18+
for (let i = 0; i < functionOrCallIds.length; i++) {
19+
if (!transformedCode.includes(functionOrCallIds[i])) {
20+
missingIds.add(functionOrCallIds[i])
21+
}
22+
}
23+
24+
if (missingIds.size > 0) {
25+
throw new Error(
26+
`Transformed code is missing ${missingIds.size} IDs. Following are missing: ${new Array(
27+
...missingIds
28+
)
29+
.map((m) => `"${m}"`)
30+
.join(', ')}`
31+
)
32+
}
33+
})
34+
})

0 commit comments

Comments
 (0)