safe parse (#996)

* safe parse

* use safe parse on mouse up, stream
This commit is contained in:
Kurt Hutten
2023-11-06 17:47:45 +11:00
committed by GitHub
parent 6b2603b1c4
commit 4bd08f7444
2 changed files with 24 additions and 5 deletions

View File

@ -342,7 +342,8 @@ export const Stream = ({ className = '' }) => {
// update artifact map ranges now that we have updated the ast. // update artifact map ranges now that we have updated the ast.
code = recast(modded.modifiedAst) code = recast(modded.modifiedAst)
const astWithCurrentRanges = parse(code) const astWithCurrentRanges = kclManager.safeParse(code)
if (!astWithCurrentRanges) return
const updateNode = getNodeFromPath<CallExpression>( const updateNode = getNodeFromPath<CallExpression>(
astWithCurrentRanges, astWithCurrentRanges,
modded.pathToNode modded.pathToNode

View File

@ -49,7 +49,8 @@ class KclManager {
engineCommandManager: EngineCommandManager engineCommandManager: EngineCommandManager
private _defferer = deferExecution((code: string) => { private _defferer = deferExecution((code: string) => {
const ast = parse(code) const ast = this.safeParse(code)
if (!ast) return
try { try {
const fmtAndStringify = (ast: Program) => const fmtAndStringify = (ast: Program) =>
JSON.stringify(parse(recast(ast))) JSON.stringify(parse(recast(ast)))
@ -202,6 +203,19 @@ class KclManager {
this._executeCallback = callback this._executeCallback = callback
} }
safeParse(code: string): Program | null {
try {
return parse(code)
} catch (e) {
console.error('error parsing code', e)
if (e instanceof KCLError) {
this.kclErrors = [e]
if (e.msg === 'file is empty') engineCommandManager.endSession()
}
return null
}
}
async ensureWasmInit() { async ensureWasmInit() {
try { try {
await initPromise await initPromise
@ -234,7 +248,8 @@ class KclManager {
async executeAstMock(ast: Program = this._ast, updateCode = false) { async executeAstMock(ast: Program = this._ast, updateCode = false) {
await this.ensureWasmInit() await this.ensureWasmInit()
const newCode = recast(ast) const newCode = recast(ast)
const newAst = parse(newCode) const newAst = this.safeParse(newCode)
if (!newAst) return
await this?.engineCommandManager?.waitForReady await this?.engineCommandManager?.waitForReady
if (updateCode) { if (updateCode) {
this.setCode(recast(ast)) this.setCode(recast(ast))
@ -301,7 +316,9 @@ class KclManager {
this.engineCommandManager.endSession() this.engineCommandManager.endSession()
} }
format() { format() {
this.code = recast(parse(kclManager.code)) const ast = this.safeParse(this.code)
if (!ast) return
this.code = recast(ast)
} }
// There's overlapping responsibility between updateAst and executeAst. // There's overlapping responsibility between updateAst and executeAst.
// updateAst was added as it was used a lot before xState migration so makes the port easier. // updateAst was added as it was used a lot before xState migration so makes the port easier.
@ -314,7 +331,8 @@ class KclManager {
} }
): Promise<Selections | null> { ): Promise<Selections | null> {
const newCode = recast(ast) const newCode = recast(ast)
const astWithUpdatedSource = parse(newCode) const astWithUpdatedSource = this.safeParse(newCode)
if (!astWithUpdatedSource) return null
let returnVal: Selections | null = null let returnVal: Selections | null = null
if (optionalParams?.focusPath) { if (optionalParams?.focusPath) {