From 382e835ed41f942e1676510bd9dc1f131f062403 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 15 Dec 2023 14:22:05 -0600 Subject: [PATCH] fix: handle nil pointer when expression is final node in file --- .changeset/big-crabs-burn.md | 5 +++++ internal/parser.go | 3 +++ internal/printer/printer_test.go | 7 +++++++ 3 files changed, 15 insertions(+) create mode 100644 .changeset/big-crabs-burn.md diff --git a/.changeset/big-crabs-burn.md b/.changeset/big-crabs-burn.md new file mode 100644 index 000000000..988075964 --- /dev/null +++ b/.changeset/big-crabs-burn.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Fixes a memory reference error when an expression is the final node in a file diff --git a/internal/parser.go b/internal/parser.go index c99d134d2..c4b426dac 100644 --- a/internal/parser.go +++ b/internal/parser.go @@ -2786,6 +2786,9 @@ func inExpressionIM(p *parser) bool { p.addLoc() p.oe.pop() nextOpenElement := p.oe.top() + if nextOpenElement == nil { + return true + } // only switch the insertion mode when we're no longer inside an expression if !nextOpenElement.Parent.Expression { p.im = textIM diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go index 68eab698f..0bf42dc38 100644 --- a/internal/printer/printer_test.go +++ b/internal/printer/printer_test.go @@ -2950,6 +2950,13 @@ const items = ["Dog", "Cat", "Platipus"]; code: `${$$maybeRenderHead($$result)}
`, }, }, + { + name: "trailing expression", + source: `{}`, + want: want{ + code: `${$$renderComponent($$result,'Component',Component,{})}${(void 0)}`, + }, + }, } for _, tt := range tests {