diff --git a/.changeset/itchy-snails-exist.md b/.changeset/itchy-snails-exist.md new file mode 100644 index 0000000..6ab3f10 --- /dev/null +++ b/.changeset/itchy-snails-exist.md @@ -0,0 +1,5 @@ +--- +"sh-syntax": patch +--- + +fix: do not panic, return the error instead diff --git a/main.go b/main.go index ca5b7b1..e751a95 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ package main import ( "container/list" - "fmt" "github.com/mailru/easyjson" "github.com/rx-ts/sh-syntax/processor" @@ -117,8 +116,7 @@ func process( bytes, err := easyjson.Marshal(&result) if err != nil { - fmt.Println(err) - panic(err) + bytes = []byte(err.Error()) } return &bytes[0] diff --git a/main.wasm b/main.wasm index f038282..023dfeb 100755 Binary files a/main.wasm and b/main.wasm differ diff --git a/src/processor.ts b/src/processor.ts index 6633bda..ceef077 100644 --- a/src/processor.ts +++ b/src/processor.ts @@ -1,10 +1,10 @@ import { IParseError, File, ShOptions, LangVariant } from './types.js' export class ParseError extends Error implements IParseError { - Filename: string + Filename?: string Incomplete: boolean Text: string - Pos: { + Pos?: { Col: number Line: number Offset: number @@ -39,7 +39,7 @@ export const getProcessor = ( originalText: string }, ): Promise - + // eslint-disable-next-line sonarjs/cognitive-complexity async function processor( textOrAst: File | string, { @@ -168,6 +168,15 @@ export const getProcessor = ( const string = decoder.decode(result.subarray(0, end)) + // naive check whether the string is a json + if (!string.startsWith('{"') && !string.endsWith('}')) { + throw new ParseError({ + Filename: filepath, + Incomplete: true, + Text: string, + }) + } + const { file, text: processedText, diff --git a/src/types.ts b/src/types.ts index 3f49a51..e0abe43 100644 --- a/src/types.ts +++ b/src/types.ts @@ -85,8 +85,8 @@ export interface File extends Node { } export interface IParseError { - Filename: string + Filename?: string Incomplete: boolean Text: string - Pos: Pos + Pos?: Pos }