diff --git a/_js/main.go b/_js/main.go index 5ea891d9a..13070361a 100644 --- a/_js/main.go +++ b/_js/main.go @@ -27,7 +27,7 @@ func main() { } node, ok := v.(syntax.Node) if !ok { - throw("NodeType requires a Node argument") + panic("NodeType requires a Node argument") } typ := fmt.Sprintf("%T", node) if i := strings.LastIndexAny(typ, "*.]"); i >= 0 { @@ -47,13 +47,14 @@ func main() { } return jp }) + stx.Set("IsIncomplete", syntax.IsIncomplete) stx.Set("KeepComments", func(v interface{}) { syntax.KeepComments(&v.(*jsParser).Parser) }) stx.Set("Variant", func(l syntax.LangVariant) func(interface{}) { if math.IsNaN(float64(l)) { - throw("Variant requires a LangVariant argument") + panic("Variant requires a LangVariant argument") } return func(v interface{}) { syntax.Variant(l)(&v.(*jsParser).Parser) @@ -94,8 +95,8 @@ func main() { }) } -func throw(v interface{}) { - js.Global.Call("$throwRuntimeError", fmt.Sprint(v)) +func throw(err error) { + js.Global.Call("$throw", js.MakeFullWrapper(err)) } // streamReader is an io.Reader wrapper for Node's stream.Readable. See diff --git a/_js/testmain.js b/_js/testmain.js index 73088642d..450f312d6 100644 --- a/_js/testmain.js +++ b/_js/testmain.js @@ -34,12 +34,37 @@ const printer = syntax.NewPrinter() } { - // parse errors + // fatal parse error + const src = "echo )" + try { + parser.Parse(src, "src") + assert.fail("did not error") + } catch (err) { + assert.equal(err.Filename, "src") + assert.equal(err.Pos.Line(), 1) + assert.equal(syntax.IsIncomplete(err), false) + } +} + +{ + // incomplete parse error const src = "echo ${" try { parser.Parse(src, "src") assert.fail("did not error") } catch (err) { + assert.equal(syntax.IsIncomplete(err), true) + } +} + +{ + // js error from the wrapper layer + var foo = {} + try { + syntax.NodeType(foo) + assert.fail("did not error") + } catch (err) { + assert.equal(err.message.includes("requires a Node argument"), true) } }