Files
modeling-app/src/components/ModelingSidebar/ModelingPanes/MemoryPane.test.tsx
Jonathan Tran 1b8688f274 Add lexical scope and redefining variables in functions (#3015)
* Fix to allow variable shadowing inside functions

* Implement closures

* Fix KCL test code to not reference future tag definition

* Remove tag declarator from function parameters

This is an example where the scoping change revealed a subtle issue
with TagDeclarators.  You cannot bind a new tag using a function
parameter.

The issue is that evaluating a TagDeclarator like $foo binds an
identifier to its corresponding TagIdentifier, but returns the
TagDeclarator.  If you have a TagDeclarator passed in as a parameter
to a function, you can never get its corresponding TagIdentifier.

This seems like a case where TagDeclarator evaluation needs to be
revisited, especially now that we have scoped tags.

* Fix to query return, functions, and tag declarator AST nodes correctly
2024-07-22 19:43:40 -04:00

64 lines
1.8 KiB
TypeScript

import { processMemory } from './MemoryPane'
import { enginelessExecutor } from '../../../lib/testHelpers'
import { initPromise, parse, ProgramMemory } from '../../../lang/wasm'
beforeAll(async () => {
await initPromise
})
describe('processMemory', () => {
it('should grab the values and remove and geo data', async () => {
// Enable rotations #152
const code = `
const myVar = 5
fn myFn = (a) => {
return a - 2
}
const otherVar = myFn(5)
const theExtrude = startSketchOn('XY')
|> startProfileAt([0, 0], %)
|> lineTo([-2.4, myVar], %)
|> lineTo([-0.76, otherVar], %)
|> extrude(4, %)
const theSketch = startSketchOn('XY')
|> startProfileAt([0, 0], %)
|> lineTo([-3.35, 0.17], %)
|> lineTo([0.98, 5.16], %)
|> lineTo([2.15, 4.32], %)
// |> rx(90, %)`
const ast = parse(code)
const programMemory = await enginelessExecutor(ast, ProgramMemory.empty())
const output = processMemory(programMemory)
expect(output.myVar).toEqual(5)
expect(output.otherVar).toEqual(3)
expect(output).toEqual({
myVar: 5,
myFn: '__function(a)__',
otherVar: 3,
theExtrude: [
{
type: 'extrudePlane',
tag: null,
id: expect.any(String),
faceId: expect.any(String),
sourceRange: [170, 194],
},
{
type: 'extrudePlane',
tag: null,
id: expect.any(String),
faceId: expect.any(String),
sourceRange: [202, 230],
},
],
theSketch: [
{ type: 'ToPoint', to: [-3.35, 0.17], from: [0, 0], tag: null },
{ type: 'ToPoint', to: [0.98, 5.16], from: [-3.35, 0.17], tag: null },
{ type: 'ToPoint', to: [2.15, 4.32], from: [0.98, 5.16], tag: null },
],
})
})
})