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

30 lines
980 B
TypeScript
Raw Normal View History

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 = `
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-23 07:47:22 +11:00
const ast = abstractSyntaxTree(lexer(code))
const nodePath = getNodePathFromSourceRange(ast, sourceRange)
const node = getNodeFromPath(ast, nodePath)
2022-12-23 07:47:22 +11:00
expect([node.start, node.end]).toEqual(sourceRange)
expect(node.type).toBe('CallExpression')
})
})