Files
modeling-app/src/lang/getNodePathFromSourceRange.test.ts

32 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-03-03 20:35:48 +11:00
import { getNodePathFromSourceRange, getNodeFromPath } from './queryAst'
import { lexer } from './tokeniser'
2023-03-03 20:35:48 +11:00
import { abstractSyntaxTree } from './abstractSyntaxTree'
import { initPromise } from './rust'
beforeAll(() => initPromise)
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 = `
const myVar = 5
const sk3 = startSketchAt([0, 0])
|> lineTo([1, 2], %)
|> lineTo({ to: [3, 4], tag: 'yo' }, %)
|> close(%)
`
const subStr = "lineTo({ to: [3, 4], tag: 'yo' }, %)"
2022-12-23 07:47:22 +11:00
const lineToSubstringIndex = code.indexOf(subStr)
const sourceRange: [number, number] = [
lineToSubstringIndex,
lineToSubstringIndex + subStr.length,
]
2022-12-23 07:47:22 +11:00
const ast = abstractSyntaxTree(lexer(code))
const nodePath = getNodePathFromSourceRange(ast, sourceRange)
const { node } = getNodeFromPath<any>(ast, nodePath)
2022-12-23 07:47:22 +11:00
expect([node.start, node.end]).toEqual(sourceRange)
expect(node.type).toBe('CallExpression')
})
})