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 = `
|
2022-12-04 18:35:32 +11:00
|
|
|
const myVar = 5
|
|
|
|
sketch sk3 {
|
|
|
|
lineTo(1, 2)
|
|
|
|
path yo = lineTo(3, 4)
|
|
|
|
close()
|
|
|
|
}
|
|
|
|
`
|
2022-12-23 07:47:22 +11:00
|
|
|
const subStr = 'lineTo(3, 4)'
|
|
|
|
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
|
|
|
})
|