2022-12-04 18:35:32 +11:00
|
|
|
import { getNodePathFromSourceRange } from './abstractSyntaxTree'
|
|
|
|
import { lexer } from './tokeniser'
|
|
|
|
import { abstractSyntaxTree, getNodeFromPath } from './abstractSyntaxTree'
|
|
|
|
|
|
|
|
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], %)
|
|
|
|
|> 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-04 18:35:32 +11:00
|
|
|
|
2022-12-23 07:47:22 +11:00
|
|
|
const ast = abstractSyntaxTree(lexer(code))
|
|
|
|
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')
|
|
|
|
})
|
2022-12-04 18:35:32 +11:00
|
|
|
})
|