2023-03-03 20:35:48 +11:00
|
|
|
import { getNodePathFromSourceRange, getNodeFromPath } from './queryAst'
|
2023-11-20 11:19:08 -06:00
|
|
|
import { Identifier, parse, initPromise, Parameter } from './wasm'
|
2023-02-21 09:42:41 +11:00
|
|
|
|
|
|
|
beforeAll(() => initPromise)
|
2022-12-04 18:35:32 +11:00
|
|
|
|
|
|
|
describe('testing getNodePathFromSourceRange', () => {
|
2022-12-23 07:47:22 +11:00
|
|
|
it('test it gets the right path for a `lineTo` CallExpression within a SketchExpression', () => {
|
|
|
|
const code = `
|
2023-02-12 10:56:45 +11:00
|
|
|
const myVar = 5
|
|
|
|
const sk3 = startSketchAt([0, 0])
|
|
|
|
|> lineTo([1, 2], %)
|
2024-03-15 17:03:42 -04:00
|
|
|
|> lineTo([3, 4], %, 'yo')
|
2023-02-12 10:56:45 +11:00
|
|
|
|> close(%)
|
|
|
|
`
|
2024-03-15 17:03:42 -04:00
|
|
|
const subStr = "lineTo([3, 4], %, 'yo')"
|
2022-12-23 07:47:22 +11:00
|
|
|
const lineToSubstringIndex = code.indexOf(subStr)
|
|
|
|
const sourceRange: [number, number] = [
|
|
|
|
lineToSubstringIndex,
|
|
|
|
lineToSubstringIndex + subStr.length,
|
|
|
|
]
|
2022-12-04 18:35:32 +11:00
|
|
|
|
2023-09-29 11:11:01 -07:00
|
|
|
const ast = parse(code)
|
2022-12-23 07:47:22 +11:00
|
|
|
const nodePath = getNodePathFromSourceRange(ast, sourceRange)
|
2023-01-13 17:58:37 +11:00
|
|
|
const { node } = getNodeFromPath<any>(ast, nodePath)
|
2022-12-04 18:35:32 +11:00
|
|
|
|
2022-12-23 07:47:22 +11:00
|
|
|
expect([node.start, node.end]).toEqual(sourceRange)
|
|
|
|
expect(node.type).toBe('CallExpression')
|
|
|
|
})
|
2023-09-21 15:40:41 +10:00
|
|
|
it('gets path right for function definition params', () => {
|
|
|
|
const code = `fn cube = (pos, scale) => {
|
|
|
|
const sg = startSketchAt(pos)
|
|
|
|
|> line([0, scale], %)
|
|
|
|
|> line([scale, 0], %)
|
|
|
|
|> line([0, -scale], %)
|
|
|
|
|
|
|
|
return sg
|
|
|
|
}
|
|
|
|
|
|
|
|
const b1 = cube([0,0], 10)`
|
|
|
|
const subStr = 'pos, scale'
|
|
|
|
const subStrIndex = code.indexOf(subStr)
|
|
|
|
const sourceRange: [number, number] = [
|
|
|
|
subStrIndex,
|
|
|
|
subStrIndex + 'pos'.length,
|
|
|
|
]
|
|
|
|
|
2023-09-29 11:11:01 -07:00
|
|
|
const ast = parse(code)
|
2023-09-21 15:40:41 +10:00
|
|
|
const nodePath = getNodePathFromSourceRange(ast, sourceRange)
|
2023-11-20 11:19:08 -06:00
|
|
|
const node = getNodeFromPath<Parameter>(ast, nodePath).node
|
2023-09-21 15:40:41 +10:00
|
|
|
|
|
|
|
expect(nodePath).toEqual([
|
|
|
|
['body', ''],
|
|
|
|
[0, 'index'],
|
|
|
|
['declarations', 'VariableDeclaration'],
|
|
|
|
[0, 'index'],
|
|
|
|
['init', ''],
|
|
|
|
['params', 'FunctionExpression'],
|
|
|
|
[0, 'index'],
|
|
|
|
])
|
2023-11-20 11:19:08 -06:00
|
|
|
expect(node.type).toBe('Parameter')
|
|
|
|
expect(node.identifier.name).toBe('pos')
|
2023-09-21 15:40:41 +10:00
|
|
|
})
|
|
|
|
it('gets path right for deep within function definition body', () => {
|
|
|
|
const code = `fn cube = (pos, scale) => {
|
|
|
|
const sg = startSketchAt(pos)
|
|
|
|
|> line([0, scale], %)
|
|
|
|
|> line([scale, 0], %)
|
|
|
|
|> line([0, -scale], %)
|
|
|
|
|
|
|
|
return sg
|
|
|
|
}
|
|
|
|
|
|
|
|
const b1 = cube([0,0], 10)`
|
|
|
|
const subStr = 'scale, 0'
|
|
|
|
const subStrIndex = code.indexOf(subStr)
|
|
|
|
const sourceRange: [number, number] = [
|
|
|
|
subStrIndex,
|
|
|
|
subStrIndex + 'scale'.length,
|
|
|
|
]
|
|
|
|
|
2023-09-29 11:11:01 -07:00
|
|
|
const ast = parse(code)
|
2023-09-21 15:40:41 +10:00
|
|
|
const nodePath = getNodePathFromSourceRange(ast, sourceRange)
|
|
|
|
const node = getNodeFromPath<Identifier>(ast, nodePath).node
|
|
|
|
expect(nodePath).toEqual([
|
|
|
|
['body', ''],
|
|
|
|
[0, 'index'],
|
|
|
|
['declarations', 'VariableDeclaration'],
|
|
|
|
[0, 'index'],
|
|
|
|
['init', ''],
|
|
|
|
['body', 'FunctionExpression'],
|
|
|
|
['body', 'FunctionExpression'],
|
|
|
|
[0, 'index'],
|
|
|
|
['declarations', 'VariableDeclaration'],
|
|
|
|
[0, 'index'],
|
|
|
|
['init', ''],
|
|
|
|
['body', 'PipeExpression'],
|
|
|
|
[2, 'index'],
|
|
|
|
['arguments', 'CallExpression'],
|
|
|
|
[0, 'index'],
|
|
|
|
['elements', 'ArrayExpression'],
|
|
|
|
[0, 'index'],
|
|
|
|
])
|
|
|
|
expect(node.type).toBe('Identifier')
|
|
|
|
expect(node.name).toBe('scale')
|
|
|
|
})
|
2022-12-04 18:35:32 +11:00
|
|
|
})
|