Skip to content

Commit

Permalink
Merge pull request #29 from onflow/bastian/refactor-linter
Browse files Browse the repository at this point in the history
[lint] Refactor so it can be embedded
  • Loading branch information
turbolent authored Sep 29, 2022
2 parents 497724c + 60b59f2 commit 2e41612
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 211 deletions.
2 changes: 1 addition & 1 deletion lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The linter for Cadence. Find programming errors, bugs, stylistic errors and susp
## How to Build

```shell
go build -o .
go build ./cmd/lint
```

### Analyzing contracts of an account
Expand Down
2 changes: 1 addition & 1 deletion lint/analyzers/analyzer.go → lint/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package analyzers
package lint

import (
"fmt"
Expand Down
6 changes: 4 additions & 2 deletions lint/analyzers/analyzers_test.go → lint/analyzers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package analyzers_test
package lint_test

import (
"testing"
Expand All @@ -25,14 +25,16 @@ import (

"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/tools/analysis"

"github.com/onflow/cadence-tools/lint"
)

var testLocation = common.StringLocation("test")

func testAnalyzers(t *testing.T, code string, analyzers ...*analysis.Analyzer) []analysis.Diagnostic {

config := analysis.NewSimpleConfig(
analysis.NeedTypes|analysis.NeedExtendedElaboration,
lint.LoadMode,
map[common.Location][]byte{
testLocation: []byte(code),
},
Expand Down
File renamed without changes.
125 changes: 125 additions & 0 deletions lint/cmd/lint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Cadence-lint - The Cadence linter
*
* Copyright 2019-2022 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"flag"
"fmt"
"log"
"os"
"sort"

"github.com/onflow/cadence/tools/analysis"
"github.com/onflow/flow-go-sdk"

"github.com/onflow/cadence-tools/lint"
)

var csvPathFlag = flag.String("csv", "", "analyze all programs in the given CSV file")
var directoryPathFlag = flag.String("directory", "", "analyze all programs in the given directory")
var networkFlag = flag.String("network", "", "name of network")
var addressFlag = flag.String("address", "", "analyze contracts in the given account")
var transactionFlag = flag.String("transaction", "", "analyze transaction with given ID")
var loadOnlyFlag = flag.Bool("load-only", false, "only load (parse and check) programs")
var silentFlag = flag.Bool("silent", false, "only show parsing/checking success/failure")
var colorFlag = flag.Bool("color", true, "format using colors")
var analyzersFlag stringSliceFlag

func init() {
flag.Var(&analyzersFlag, "analyze", "enable analyzer")
}

func main() {
defaultUsage := flag.Usage
flag.Usage = func() {
defaultUsage()
_, _ = fmt.Fprintf(os.Stderr, "\nAvailable analyzers:\n")

names := make([]string, 0, len(lint.Analyzers))
for name := range lint.Analyzers {
names = append(names, name)
}

sort.Strings(names)

for _, name := range names {
analyzer := lint.Analyzers[name]
_, _ = fmt.Fprintf(
os.Stderr,
" - %s:\n %s\n",
name,
analyzer.Description,
)
}
}

flag.Parse()

var enabledAnalyzers []*analysis.Analyzer

loadOnly := *loadOnlyFlag
if !loadOnly {
if len(analyzersFlag) > 0 {
for _, analyzerName := range analyzersFlag {
analyzer, ok := lint.Analyzers[analyzerName]
if !ok {
log.Panic(fmt.Errorf("unknown analyzer: %s", analyzerName))
}

enabledAnalyzers = append(enabledAnalyzers, analyzer)
}
} else {
// Use all analyzers
for _, analyzer := range lint.Analyzers {
enabledAnalyzers = append(enabledAnalyzers, analyzer)
}
}
}

linter := lint.NewLinter(lint.Config{
Analyzers: enabledAnalyzers,
Silent: *silentFlag,
UseColor: *colorFlag,
})

cvsPath := *csvPathFlag
directoryPath := *directoryPathFlag
address := *addressFlag
transaction := *transactionFlag

switch {
case cvsPath != "":
linter.AnalyzeCSV(cvsPath)

case directoryPath != "":
linter.AnalyzeDirectory(directoryPath)

case address != "":
network := *networkFlag
linter.AnalyzeAccount(address, network)

case transaction != "":
transactionID := flow.HexToID(transaction)
network := *networkFlag
linter.AnalyzeTransaction(transactionID, network)

default:
println("Nothing to do. Please provide -address, -transaction, -directory, or -csv. See -help")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package analyzers
package lint

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package analyzers_test
package lint_test

import (
"testing"
Expand All @@ -25,7 +25,7 @@ import (
"github.com/onflow/cadence/tools/analysis"
"github.com/stretchr/testify/require"

"github.com/onflow/cadence-tools/lint/analyzers"
"github.com/onflow/cadence-tools/lint"
)

func TestDeprecatedKeyFunctionsAnalyzer(t *testing.T) {
Expand All @@ -41,7 +41,7 @@ func TestDeprecatedKeyFunctionsAnalyzer(t *testing.T) {
}
}
`,
analyzers.DeprecatedKeyFunctionsAnalyzer,
lint.DeprecatedKeyFunctionsAnalyzer,
)

require.Equal(
Expand All @@ -53,7 +53,7 @@ func TestDeprecatedKeyFunctionsAnalyzer(t *testing.T) {
EndPos: ast.Position{Offset: 119, Line: 4, Column: 37},
},
Location: testLocation,
Category: analyzers.UpdateCategory,
Category: lint.UpdateCategory,
Message: "deprecated function 'addPublicKey' will get removed",
SecondaryMessage: "replace with 'keys.add'",
},
Expand All @@ -63,7 +63,7 @@ func TestDeprecatedKeyFunctionsAnalyzer(t *testing.T) {
EndPos: ast.Position{Offset: 165, Line: 5, Column: 40},
},
Location: testLocation,
Category: analyzers.UpdateCategory,
Category: lint.UpdateCategory,
Message: "deprecated function 'removePublicKey' will get removed",
SecondaryMessage: "replace with 'keys.revoke'",
},
Expand Down
2 changes: 1 addition & 1 deletion lint/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package main
package lint

import (
"fmt"
Expand Down
1 change: 1 addition & 0 deletions lint/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)

Loading

0 comments on commit 2e41612

Please sign in to comment.