Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uninitialized variables #171

Merged
merged 2 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"fmt"
"reflect"
"strconv"
"strings"

Expand Down Expand Up @@ -107,7 +108,9 @@ func (p *parser) parseProgram() *ast.Program {
}
}

if stmt != nil && strings.TrimSpace(stmt.String()) != "" {
if stmt != nil &&
(reflect.ValueOf(stmt).Kind() == reflect.Ptr && !reflect.ValueOf(stmt).IsNil()) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to prevent using reflect in this loop. Too much reflection, and actually this is not necessary. I will leave another comment for the solution, based on the real issue.

Copy link
Contributor Author

@Mido-sys Mido-sys Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sio4 , Yes, there's another way to update the parseLetStatement() function

if !p.expectPeek(token.ASSIGN) {
   return nil
}

to return stmt instead of nil. However, we will have to update all the parser functions to not return nil as calling stmt.String() will panic if any of the parser functions return nil. I made the decision to use reflection as it's the safest route considering how heavily plush relies on interfaces{} and reflections.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. also, we can fix parseStatement(), and that could be the best. (See my another comment) With your PR, the for loop on tokens will reflect all statements returned and it is expensive and not necessary.

strings.TrimSpace(stmt.String()) != "" {
program.Statements = append(program.Statements, stmt)
}

Expand Down
12 changes: 12 additions & 0 deletions variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ func Test_Let_Reassignment(t *testing.T) {
r.NoError(err)
r.Equal("bar\n \n \nbaz", strings.TrimSpace(s))
}
func Test_Let_Ident_NotInitialized(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a line before function start and end

r := require.New(t)
input := `<% let foo
if (foo){
foo = 1
}
%>`

ctx := NewContext()

_, err := Render(input, ctx)
r.Error(err)
}
func Test_Let_Reassignment_UnknownIdent(t *testing.T) {
r := require.New(t)
input := `<% foo = "baz" %>`
Expand Down