Skip to content

Commit

Permalink
chore: lint and naming refactor (#475)
Browse files Browse the repository at this point in the history
Enable more linters, address the issues and do a major naming refactor
to use golang lower camelCase identifiers for types, functions, methods
and variable names.

Also: 
* Clean up inline generation so it doesn't rely on temporary variables.
* Remove unused functions generated by inline.pl.
  • Loading branch information
stevenh committed Dec 4, 2022
1 parent 70918b6 commit 026a1d9
Show file tree
Hide file tree
Showing 116 changed files with 14,327 additions and 13,913 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/test/tester
.idea
dist/
.vscode/
37 changes: 9 additions & 28 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,25 @@ run:
- terst
skip-files:
- dbg/dbg.go
- token/token_const.go

linters-settings:
govet:
check-shadowing: false
goconst:
min-len: 2
min-occurrences: 4
revive:
enable-all-rules: false
rules:
- name: var-naming
disabled: true

linters:
enable-all: true
disable:
- dupl
- gas
- errcheck
- gofmt
- gosimple
- interfacer
- megacheck
- maligned
- structcheck
- staticcheck
- unconvert
- unparam
- varcheck
- lll
- prealloc
- gochecknoglobals
- gochecknoinits
- scopelint
Expand All @@ -39,35 +32,23 @@ linters:
- goerr113
- wsl
- nlreturn
- tagliatelle
- gomnd
- paralleltest
- wrapcheck
- testpackage
- golint
- gofumpt
- forbidigo
- gocognit
- gocritic
- godot
- nakedret
- nestif
- revive
- errorlint
- exhaustive
- forcetypeassert
- ifshort
- stylecheck
- gocyclo
- misspell
- cyclop
- varnamelen
- nonamedreturns
- maintidx
- ireturn
- exhaustruct
- nosnakecase
- deadcode
- dupword
- gci

issues:
exclude-use-default: false
max-same-issues: 0
75 changes: 44 additions & 31 deletions ast/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,44 @@ import (
"github.com/robertkrimen/otto/file"
)

// CommentPosition determines where the comment is in a given context
// CommentPosition determines where the comment is in a given context.
type CommentPosition int

// Available comment positions.
const (
_ CommentPosition = iota
LEADING // Before the pertinent expression
TRAILING // After the pertinent expression
KEY // Before a key in an object
COLON // After a colon in a field declaration
FINAL // Final comments in a block, not belonging to a specific expression or the comment after a trailing , in an array or object literal
IF // After an if keyword
WHILE // After a while keyword
DO // After do keyword
FOR // After a for keyword
WITH // After a with keyword
_ CommentPosition = iota
// LEADING is before the pertinent expression.
LEADING
// TRAILING is after the pertinent expression.
TRAILING
// KEY is before a key in an object.
KEY
// COLON is after a colon in a field declaration.
COLON
// FINAL is the final comments in a block, not belonging to a specific expression or the comment after a trailing , in an array or object literal.
FINAL
// IF is after an if keyword.
IF
// WHILE is after a while keyword.
WHILE
// DO is after do keyword.
DO
// FOR is after a for keyword.
FOR
// WITH is after a with keyword.
WITH
// TBD is unknown.
TBD
)

// Comment contains the data of the comment
// Comment contains the data of the comment.
type Comment struct {
Begin file.Idx
Text string
Position CommentPosition
}

// NewComment creates a new comment
// NewComment creates a new comment.
func NewComment(text string, idx file.Idx) *Comment {
comment := &Comment{
Begin: idx,
Expand All @@ -42,7 +54,7 @@ func NewComment(text string, idx file.Idx) *Comment {
return comment
}

// String returns a stringified version of the position
// String returns a stringified version of the position.
func (cp CommentPosition) String() string {
switch cp {
case LEADING:
Expand Down Expand Up @@ -70,30 +82,31 @@ func (cp CommentPosition) String() string {
}
}

// String returns a stringified version of the comment
// String returns a stringified version of the comment.
func (c Comment) String() string {
return fmt.Sprintf("Comment: %v", c.Text)
}

// Comments defines the current view of comments from the parser
// Comments defines the current view of comments from the parser.
type Comments struct {
// CommentMap is a reference to the parser comment map
CommentMap CommentMap
// Comments lists the comments scanned, not linked to a node yet
Comments []*Comment
// future lists the comments after a line break during a sequence of comments
future []*Comment
// Current is node for which comments are linked to
Current Expression

// wasLineBreak determines if a line break occured while scanning for comments
// future lists the comments after a line break during a sequence of comments
future []*Comment
// wasLineBreak determines if a line break occurred while scanning for comments
wasLineBreak bool
// primary determines whether or not processing a primary expression
primary bool
// afterBlock determines whether or not being after a block statement
afterBlock bool
}

// NewComments returns a new Comments.
func NewComments() *Comments {
comments := &Comments{
CommentMap: CommentMap{},
Expand All @@ -107,7 +120,7 @@ func (c *Comments) String() string {
}

// FetchAll returns all the currently scanned comments,
// including those from the next line
// including those from the next line.
func (c *Comments) FetchAll() []*Comment {
defer func() {
c.Comments = nil
Expand All @@ -117,7 +130,7 @@ func (c *Comments) FetchAll() []*Comment {
return append(c.Comments, c.future...)
}

// Fetch returns all the currently scanned comments
// Fetch returns all the currently scanned comments.
func (c *Comments) Fetch() []*Comment {
defer func() {
c.Comments = nil
Expand All @@ -126,12 +139,12 @@ func (c *Comments) Fetch() []*Comment {
return c.Comments
}

// ResetLineBreak marks the beginning of a new statement
// ResetLineBreak marks the beginning of a new statement.
func (c *Comments) ResetLineBreak() {
c.wasLineBreak = false
}

// MarkPrimary will mark the context as processing a primary expression
// MarkPrimary will mark the context as processing a primary expression.
func (c *Comments) MarkPrimary() {
c.primary = true
c.wasLineBreak = false
Expand Down Expand Up @@ -205,7 +218,7 @@ func (c *Comments) SetExpression(node Expression) {
c.applyComments(node, previous, TRAILING)
}

// PostProcessNode applies all found comments to the given node
// PostProcessNode applies all found comments to the given node.
func (c *Comments) PostProcessNode(node Node) {
c.applyComments(node, nil, TRAILING)
}
Expand All @@ -228,23 +241,23 @@ func (c *Comments) applyComments(node, previous Node, position CommentPosition)
}
}

// AtLineBreak will mark a line break
// AtLineBreak will mark a line break.
func (c *Comments) AtLineBreak() {
c.wasLineBreak = true
}

// CommentMap is the data structure where all found comments are stored
// CommentMap is the data structure where all found comments are stored.
type CommentMap map[Node][]*Comment

// AddComment adds a single comment to the map
// AddComment adds a single comment to the map.
func (cm CommentMap) AddComment(node Node, comment *Comment) {
list := cm[node]
list = append(list, comment)

cm[node] = list
}

// AddComments adds a slice of comments, given a node and an updated position
// AddComments adds a slice of comments, given a node and an updated position.
func (cm CommentMap) AddComments(node Node, comments []*Comment, position CommentPosition) {
for _, comment := range comments {
if comment.Position == TBD {
Expand All @@ -254,7 +267,7 @@ func (cm CommentMap) AddComments(node Node, comments []*Comment, position Commen
}
}

// Size returns the size of the map
// Size returns the size of the map.
func (cm CommentMap) Size() int {
size := 0
for _, comments := range cm {
Expand All @@ -264,7 +277,7 @@ func (cm CommentMap) Size() int {
return size
}

// MoveComments moves comments with a given position from a node to another
// MoveComments moves comments with a given position from a node to another.
func (cm CommentMap) MoveComments(from, to Node, position CommentPosition) {
for i, c := range cm[from] {
if c.Position == position {
Expand Down
Loading

0 comments on commit 026a1d9

Please sign in to comment.