1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-26 20:28:49 +08:00

feat: add github action tests and linting (#418)

Leverage github actions for tests and linting.

This includes fixing a bunch of issues highlighted by golangci
including:
* Dead code.
* Ineffectual assigns.
* Goto warnings.
* Nil return err.
* Reused literal strings.
* Test parameter order.

Also:
* Setup clog.
This commit is contained in:
Steven Hartland
2021-09-27 16:19:28 +01:00
committed by GitHub
parent 209b1fb8e8
commit 9297a9abe4
64 changed files with 1004 additions and 1078 deletions

View File

@@ -2,9 +2,10 @@ package parser
import (
"fmt"
"github.com/robertkrimen/otto/ast"
"reflect"
"testing"
"github.com/robertkrimen/otto/ast"
)
func checkComments(actual []*ast.Comment, expected []string, position ast.CommentPosition) error {
@@ -31,13 +32,6 @@ func checkComments(actual []*ast.Comment, expected []string, position ast.Commen
return nil
}
func displayStatements(statements []ast.Statement) {
fmt.Printf("Printing statements (%v)\n", len(statements))
for k, v := range statements {
fmt.Printf("Type of line %v: %v\n", k, reflect.TypeOf(v))
}
}
func displayComments(m ast.CommentMap) {
fmt.Printf("Displaying comments:\n")
for n, comments := range m {
@@ -404,7 +398,7 @@ t2 = "Nothing happens."
is(checkComments((parser.comments.CommentMap)[program.Body[1]], []string{"Test", "Test2"}, ast.LEADING), nil)
// Misc
parser, program = test(`
parser, _ = test(`
var x = Object.create({y: {
},
// a
@@ -413,7 +407,7 @@ var x = Object.create({y: {
is(parser.comments.CommentMap.Size(), 1)
// Misc 2
parser, program = test(`
parser, _ = test(`
var x = Object.create({y: {
},
// a
@@ -818,7 +812,7 @@ if(a) {
is(checkComments((parser.comments.CommentMap)[program.Body[0].(*ast.IfStatement).Alternate], []string{"comment"}, ast.TRAILING), nil)
// If then else pt 8
parser, program = test(`
parser, _ = test(`
if
/*comment*/
(a) {
@@ -830,7 +824,7 @@ if
is(parser.comments.CommentMap.Size(), 1)
// If then else pt 9
parser, program = test(`
parser, _ = test(`
if
(a)
/*comment*/{
@@ -842,7 +836,7 @@ if
is(parser.comments.CommentMap.Size(), 1)
// If then else pt 10
parser, program = test(`
parser, _ = test(`
if(a){
b
}
@@ -971,7 +965,7 @@ while(a) {
is(checkComments((parser.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body.(*ast.BlockStatement)], []string{"comment"}, ast.FINAL), nil)
// While pt 8
parser, program = test(`
parser, _ = test(`
while
/*comment*/(a) {
@@ -980,7 +974,7 @@ while
is(parser.comments.CommentMap.Size(), 1)
// While pt 9
parser, program = test(`
parser, _ = test(`
while
(a)
/*comment*/{
@@ -1109,7 +1103,7 @@ with(a) {
is(checkComments((parser.comments.CommentMap)[program.Body[0].(*ast.WithStatement).Body], []string{"comment"}, ast.TRAILING), nil)
// With pt 7
parser, program = test(`
parser, _ = test(`
with
/*comment*/(a) {
}
@@ -1117,7 +1111,7 @@ with
is(parser.comments.CommentMap.Size(), 1)
// With pt 8
parser, program = test(`
parser, _ = test(`
with
(a)
/*comment*/{
@@ -1176,7 +1170,7 @@ var a, b;
is(checkComments((parser.comments.CommentMap)[program], []string{"comment"}, ast.TRAILING), nil)
// Return
parser, program = test(`
parser, _ = test(`
function f() {
/*comment*/return o
}
@@ -1356,7 +1350,7 @@ try {
is(checkComments((parser.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Finally], []string{"comment"}, ast.LEADING), nil)
// Switch / comment
parser, program = test(`
parser, _ = test(`
var volvo = 1
//comment
switch(abra) {
@@ -1365,7 +1359,7 @@ switch(abra) {
is(parser.comments.CommentMap.Size(), 1)
// Switch / comment
parser, program = test(`
parser, _ = test(`
f("string",{
key: "val"
//comment

View File

@@ -1,9 +0,0 @@
// This file was AUTOMATICALLY GENERATED by dbg-import (smuggol) for github.com/robertkrimen/dbg
package parser
import (
Dbg "github.com/robertkrimen/otto/dbg"
)
var dbg, dbgf = Dbg.New()

View File

@@ -11,7 +11,6 @@ import (
const (
err_UnexpectedToken = "Unexpected token %v"
err_UnexpectedEndOfInput = "Unexpected end of input"
err_UnexpectedEscape = "Unexpected escape"
)
// UnexpectedNumber: 'Unexpected number',
@@ -70,7 +69,7 @@ func (self Error) Error() string {
}
func (self *_parser) error(place interface{}, msg string, msgValues ...interface{}) *Error {
idx := file.Idx(0)
var idx file.Idx
switch place := place.(type) {
case int:
idx = self.idxOf(place)
@@ -120,8 +119,7 @@ func (self *_parser) errorUnexpectedToken(tkn token.Token) error {
}
// ErrorList is a list of *Errors.
//
type ErrorList []*Error
type ErrorList []*Error // nolint: errname
// Add adds an Error with given position and message to an ErrorList.
func (self *ErrorList) Add(position file.Position, msg string) {

View File

@@ -352,7 +352,7 @@ func (self *_parser) parseArrayLiteral() ast.Expression {
for self.token != token.RIGHT_BRACKET && self.token != token.EOF {
if self.token == token.COMMA {
// This kind of comment requires a special empty expression node.
empty := &ast.EmptyExpression{self.idx, self.idx}
empty := &ast.EmptyExpression{Begin: self.idx, End: self.idx}
if self.mode&StoreComments != 0 {
self.comments.SetExpression(empty)

View File

@@ -628,14 +628,14 @@ func parseNumberLiteral(literal string) (value interface{}, err error) {
// TODO Is Uint okay? What about -MAX_UINT
value, err = strconv.ParseInt(literal, 0, 64)
if err == nil {
return
return value, nil
}
parseIntErr := err // Save this first error, just in case
value, err = strconv.ParseFloat(literal, 64)
if err == nil {
return
return value, nil
} else if err.(*strconv.NumError).Err == strconv.ErrRange {
// Infinity, etc.
return value, nil
@@ -651,7 +651,7 @@ func parseNumberLiteral(literal string) (value interface{}, err error) {
for _, chr := range literal {
digit := digitValue(chr)
if digit >= 16 {
goto error
return nil, errors.New("Illegal numeric literal")
}
value = value*16 + float64(digit)
}
@@ -659,7 +659,6 @@ func parseNumberLiteral(literal string) (value interface{}, err error) {
}
}
error:
return nil, errors.New("Illegal numeric literal")
}

View File

@@ -854,8 +854,6 @@ func TestParserAST(t *testing.T) {
]
`)
return
test(`
if (!abc && abc.jkl(def) && abc[0] === +abc[0] && abc.length < ghi) {
}

View File

@@ -98,9 +98,9 @@ func TestParserErr(t *testing.T) {
return program, parser
}
program, parser := test("", nil)
test("", nil)
program, parser = test(`
program, parser := test(`
var abc;
break; do {
} while(true);
@@ -523,7 +523,7 @@ func TestParser(t *testing.T) {
abc()
`, nil)
program := test("", nil)
test("", nil)
test("//", nil)
@@ -541,7 +541,7 @@ func TestParser(t *testing.T) {
test("new +", "(anonymous): Line 1:5 Unexpected token +")
program = test(";", nil)
program := test(";", nil)
is(len(program.Body), 1)
is(program.Body[0].(*ast.EmptyStatement).Semicolon, file.Idx(1))
@@ -1012,6 +1012,7 @@ func TestPosition(t *testing.T) {
parser = _newParser("", "this.style", 1, nil)
program, err = parser.parse()
is(err, nil)
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.DotExpression).Left.(*ast.ThisExpression)
is(node.Idx0(), file.Idx(1))
is(node.Idx1(), file.Idx(5))