From 013f3e3e5ab54d5086daa591ba2dfd884ec3f119 Mon Sep 17 00:00:00 2001 From: Kamil Chmielewski <45183584+ChmielewskiKamil@users.noreply.github.com> Date: Sat, 7 Sep 2024 00:19:09 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Rename=20identifier=20name?= =?UTF-8?q?=20to=20value=20to=20dedup=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- analyzer/screamingsnakeconst/detector.go | 4 +- ast/ast.go | 10 ++-- parser/exprparsing.go | 2 +- parser/exprparsing_test.go | 4 +- parser/parser.go | 16 +++--- parser/parser_test.go | 65 ++++++++++++++---------- 6 files changed, 55 insertions(+), 46 deletions(-) diff --git a/analyzer/screamingsnakeconst/detector.go b/analyzer/screamingsnakeconst/detector.go index 1617a44..907536b 100644 --- a/analyzer/screamingsnakeconst/detector.go +++ b/analyzer/screamingsnakeconst/detector.go @@ -36,14 +36,14 @@ func (*Detector) Detect(node ast.Node) *reporter.Finding { } // @TODO: Add immutable variables as well (but they can only be contract level) if v.Mutability == ast.Constant { - if !isScreamingSnakeCase(v.Name.Name) { + if !isScreamingSnakeCase(v.Name.Value) { finding.Locations = append( finding.Locations, reporter.Location{ Position: token.Position{ Offset: v.Name.Pos, }, // Save ident name for the report. - Context: v.Name.Name, + Context: v.Name.Value, }) matches++ } diff --git a/ast/ast.go b/ast/ast.go index 8002c94..4c59d5f 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -53,8 +53,8 @@ func (c *Comment) End() token.Pos { type ( Identifier struct { - Pos token.Pos // identifier position - Name string // identifier name + Pos token.Pos // identifier position + Value string // identifier name } NumberLiteral struct { @@ -86,7 +86,7 @@ type ( func (x *Identifier) Start() token.Pos { return x.Pos } func (x *Identifier) End() token.Pos { - return token.Pos(int(x.Pos) + len(x.Name)) + return token.Pos(int(x.Pos) + len(x.Value)) } func (x *NumberLiteral) Start() token.Pos { return x.Pos } func (x *NumberLiteral) End() token.Pos { @@ -120,7 +120,7 @@ func (*InfixExpression) expressionNode() {} // String() implementations for Expressions -func (x *Identifier) String() string { return x.Name } +func (x *Identifier) String() string { return x.Value } func (x *NumberLiteral) String() string { return x.Kind.Literal } func (x *BooleanLiteral) String() string { if x.Value { @@ -489,7 +489,7 @@ func (d *StateVariableDeclaration) String() string { return out.String() } -// @TODO: Is debug view for function declarations necessary? +// @TODO: Implement String() for FunctionDeclaration func (d *FunctionDeclaration) String() string { return "TO BE IMPLEMENTED" } /*~*~*~*~*~*~*~*~*~*~*~*~*~* Files ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/ diff --git a/parser/exprparsing.go b/parser/exprparsing.go index 13c83e3..92ea410 100644 --- a/parser/exprparsing.go +++ b/parser/exprparsing.go @@ -177,7 +177,7 @@ func (p *Parser) parseIdentifier() ast.Expression { defer un(trace("parseIdentifier")) } - ident := &ast.Identifier{Pos: p.currTkn.Pos, Name: p.currTkn.Literal} + ident := &ast.Identifier{Pos: p.currTkn.Pos, Value: p.currTkn.Literal} return ident } diff --git a/parser/exprparsing_test.go b/parser/exprparsing_test.go index b9ee50b..b2d1091 100644 --- a/parser/exprparsing_test.go +++ b/parser/exprparsing_test.go @@ -319,8 +319,8 @@ func test_Identifier(t *testing.T, exp ast.Expression, value string) { t.Fatalf("Expected Identifier, got %T", exp) } - if ident.Name != value { - t.Fatalf("Expected %s, got %s", value, ident.Name) + if ident.Value != value { + t.Fatalf("Expected %s, got %s", value, ident.Value) } } diff --git a/parser/parser.go b/parser/parser.go index 0a254d8..4dd2228 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -150,8 +150,8 @@ func (p *Parser) parseFunctionDeclaration() *ast.FunctionDeclaration { } decl.Name = &ast.Identifier{ - Pos: p.currTkn.Pos, - Name: p.currTkn.Literal, + Pos: p.currTkn.Pos, + Value: p.currTkn.Literal, } // 3. ( Param List ) @@ -208,8 +208,8 @@ func (p *Parser) parseFunctionDeclaration() *ast.FunctionDeclaration { if p.peekTknIs(token.IDENTIFIER) { p.nextToken() prm.Name = &ast.Identifier{ - Pos: p.currTkn.Pos, - Name: p.currTkn.Literal, + Pos: p.currTkn.Pos, + Value: p.currTkn.Literal, } } @@ -276,8 +276,8 @@ func (p *Parser) parseStateVariableDeclaration() *ast.StateVariableDeclaration { p.nextToken() case tkType == token.IDENTIFIER: decl.Name = &ast.Identifier{ - Pos: p.currTkn.Pos, - Name: p.currTkn.Literal, + Pos: p.currTkn.Pos, + Value: p.currTkn.Literal, } p.nextToken() case token.IsVarVisibility(tkType): @@ -424,8 +424,8 @@ func (p *Parser) parseVariableDeclarationStatement() *ast.VariableDeclarationSta p.nextToken() case tkType == token.IDENTIFIER: vdStmt.Name = &ast.Identifier{ - Pos: p.currTkn.Pos, - Name: p.currTkn.Literal, + Pos: p.currTkn.Pos, + Value: p.currTkn.Literal, } p.nextToken() case token.IsDataLocation(tkType): diff --git a/parser/parser_test.go b/parser/parser_test.go index b1fd211..3394562 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -85,36 +85,45 @@ func Test_ParseFunctionDeclaration(t *testing.T) { t.Fatalf("Expected FunctionDeclaration, got %T", decl) } - if fd.Name.Name != "getBalance" { - t.Errorf("Expected function name getBalance, got %s", fd.Name.Name) + if fd.Name.Value != "getBalance" { + t.Errorf("Expected function name getBalance, got %s", fd.Name.Value) } if fd.Params == nil { t.Fatalf("Expected ParamList, got nil") } - // if len(fd.Type.Params.List) != 1 { - // t.Fatalf("Expected 1 parameter, got %d", len(fd.Type.Params.List)) - // } - // - // param := fd.Type.Params.List[0] - // if param.Name.Name != "owner" { - // t.Errorf("Expected parameter name owner, got %s", param.Name.Name) - // } - - // @TODO: We skip the type for now since it is an expression. - // if param.Type == nil { - // t.Fatalf("Expected ElementaryType, got nil") - // } - - // et, ok := param.Type.(*ast.ElementaryType) - // if !ok { - // t.Fatalf("Expected ElementaryType, got %T", param.Type) - // } - // - // if et.Kind.Type != token.ADDRESS { - // t.Errorf("Expected token type ADDRESS, got %T", et.Kind.Type) - // } + if len(fd.Params.List) != 2 { + t.Fatalf("Expected 2 parameter, got %d", len(fd.Params.List)) + } + + tests := []struct { + expectedType interface{} + expectedIdentifier string + }{ + {token, "owner"}, + } + + for i, tt := range tests { + param := fd.Params.List[i] + if param.Name.Value != tt.expectedIdentifier { + t.Errorf("Expected parameter name %s, got %s", tt.expectedIdentifier, param.Name.Value) + } + + if param.Type == nil { + t.Fatalf("Expected ElementaryType, got nil") + } + + et, ok := param.Type.(*ast.ElementaryType) + if !ok { + t.Fatalf("Expected ElementaryType, got %T", param.Type) + } + + if et != tt.expectedType { + t.Errorf("Expected token type %T, got %T", tt.expectedType, et) + } + + } if fd.Body == nil { t.Fatalf("Expected BlockStatement, got nil") @@ -143,8 +152,8 @@ func Test_ParseFunctionDeclaration(t *testing.T) { t.Fatalf("Expected Identifier, got nil") } - if vdStmt.Name.Name != "balance" { - t.Errorf("Expected balance, got %s", vdStmt.Name.Name) + if vdStmt.Name.Value != "balance" { + t.Errorf("Expected balance, got %s", vdStmt.Name.Value) } if vdStmt.DataLocation != ast.NO_DATA_LOCATION { @@ -362,9 +371,9 @@ func testParseElementaryType(t *testing.T, decl ast.Declaration, return false } - if vd.Name.Name != expectedIdentifier { + if vd.Name.Value != expectedIdentifier { t.Errorf("Expected identifier %s, got %s", - expectedIdentifier, vd.Name.Name) + expectedIdentifier, vd.Name.Value) return false }