Skip to content

Commit

Permalink
♻️ Rename identifier name to value to dedup names
Browse files Browse the repository at this point in the history
  • Loading branch information
ChmielewskiKamil committed Sep 6, 2024
1 parent 39f3b56 commit 013f3e3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
4 changes: 2 additions & 2 deletions analyzer/screamingsnakeconst/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
Expand Down
10 changes: 5 additions & 5 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/
Expand Down
2 changes: 1 addition & 1 deletion parser/exprparsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions parser/exprparsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
16 changes: 8 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
65 changes: 37 additions & 28 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},

Check failure on line 104 in parser/parser_test.go

View workflow job for this annotation

GitHub Actions / build

use of package token not in selector
}

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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 013f3e3

Please sign in to comment.