Skip to content

Commit

Permalink
update analyzers to Cadence 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Dec 18, 2023
1 parent 7d1c054 commit b39d90c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 109 deletions.
48 changes: 1 addition & 47 deletions lint/deprecated_member_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,9 @@ import (
"regexp"

"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/sema"
"github.com/onflow/cadence/tools/analysis"
)

func memberReplacement(memberInfo sema.MemberInfo) string {
memberName := memberInfo.Member.Identifier.Identifier
switch memberInfo.AccessedType {
case sema.AuthAccountType:
switch memberName {
case sema.AuthAccountTypeAddPublicKeyFunctionName:
return "keys.add"

case sema.AuthAccountTypeRemovePublicKeyFunctionName:
return "keys.revoke"

case sema.AuthAccountTypeGetCapabilityFunctionName:
return "capabilities.get"

case sema.AuthAccountTypeLinkFunctionName:
return "capabilities.storage.issue"

case sema.AuthAccountTypeLinkAccountFunctionName:
return "capabilities.account.issue"

case sema.AuthAccountTypeUnlinkFunctionName:
return "capabilities.unpublish"
}
}

return ""
}

var docStringDeprecationWarningPattern = regexp.MustCompile(`(?i)[\t *_]*deprecated\b(?:[*_]*: (.*))?`)

func MemberIsDeprecated(docString string) bool {
Expand Down Expand Up @@ -87,7 +58,7 @@ var DeprecatedMemberAnalyzer = (func() *analysis.Analyzer {
return
}

memberInfo, _ := elaboration.MemberExpressionMemberInfo(memberExpression)
memberInfo, _ := elaboration.MemberExpressionMemberAccessInfo(memberExpression)
member := memberInfo.Member
if member == nil {
return
Expand All @@ -102,22 +73,6 @@ var DeprecatedMemberAnalyzer = (func() *analysis.Analyzer {

identifierRange := ast.NewRangeFromPositioned(nil, memberExpression.Identifier)

var suggestedFixes []analysis.SuggestedFix

replacement := memberReplacement(memberInfo)
if replacement != "" {
suggestedFix := analysis.SuggestedFix{
Message: "replace",
TextEdits: []analysis.TextEdit{
{
Replacement: replacement,
Range: identifierRange,
},
},
}
suggestedFixes = append(suggestedFixes, suggestedFix)
}

report(
analysis.Diagnostic{
Location: location,
Expand All @@ -129,7 +84,6 @@ var DeprecatedMemberAnalyzer = (func() *analysis.Analyzer {
memberName,
),
SecondaryMessage: docStringMatch[1],
SuggestedFixes: suggestedFixes,
},
)
},
Expand Down
11 changes: 7 additions & 4 deletions lint/deprecated_member_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ import (

func TestDeprecatedMemberAnalyzer(t *testing.T) {

// TODO: no more deprecated members
t.SkipNow()

t.Parallel()

diagnostics := testAnalyzers(t,
`
pub contract Test {
access(all) contract Test {
/// **DEPRECATED**
pub fun foo() {}
access(all) fun foo() {}
/// Deprecated: No good
pub fun bar() {}
access(all) fun bar() {}
pub fun test(account: AuthAccount) {
access(all) fun test(account: AuthAccount) {
account.addPublicKey([])
account.removePublicKey(0)
self.foo()
Expand Down
80 changes: 40 additions & 40 deletions lint/number_function_argument_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = Fix64(1)
}
}`,
Expand All @@ -54,8 +54,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 58, Line: 4, Column: 13},
EndPos: ast.Position{Offset: 65, Line: 4, Column: 20},
StartPos: ast.Position{Offset: 74, Line: 4, Column: 13},
EndPos: ast.Position{Offset: 81, Line: 4, Column: 20},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -71,8 +71,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {

t.Parallel()
diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = UFix64(1)
}
}`,
Expand All @@ -84,8 +84,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 63, Line: 4, Column: 20},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 79, Line: 4, Column: 20},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -101,8 +101,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = Fix64(-1)
}
}`,
Expand All @@ -114,8 +114,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 63, Line: 4, Column: 20},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 79, Line: 4, Column: 20},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -133,8 +133,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = UFix64(1.2)
}
}`,
Expand All @@ -146,8 +146,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 65, Line: 4, Column: 22},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 81, Line: 4, Column: 22},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -163,8 +163,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = Fix64(-1.2)
}
}`,
Expand All @@ -176,8 +176,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 65, Line: 4, Column: 22},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 81, Line: 4, Column: 22},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -197,8 +197,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = UInt8(1)
}
}`,
Expand All @@ -210,8 +210,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 62, Line: 4, Column: 19},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 78, Line: 4, Column: 19},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -227,8 +227,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = Int8(1)
}
}`,
Expand All @@ -240,8 +240,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 61, Line: 4, Column: 18},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 77, Line: 4, Column: 18},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -258,8 +258,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = Int8(-1)
}
}`,
Expand All @@ -271,8 +271,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 62, Line: 4, Column: 19},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 78, Line: 4, Column: 19},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -288,8 +288,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = Int(1)
}
}`,
Expand All @@ -301,8 +301,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 60, Line: 4, Column: 17},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 76, Line: 4, Column: 17},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand All @@ -318,8 +318,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
t.Parallel()

diagnostics := testAnalyzers(t, `
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = Int(-1)
}
}`,
Expand All @@ -331,8 +331,8 @@ func TestCheckNumberConversionReplacementHint(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 55, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 61, Line: 4, Column: 18},
StartPos: ast.Position{Offset: 71, Line: 4, Column: 12},
EndPos: ast.Position{Offset: 77, Line: 4, Column: 18},
},
Location: testLocation,
Category: lint.ReplacementCategory,
Expand Down
24 changes: 12 additions & 12 deletions lint/redundant_cast_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestRedundantCastAnalyzer(t *testing.T) {

diagnostics := testAnalyzers(t,
`
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = true as Bool
}
}
Expand All @@ -52,8 +52,8 @@ func TestRedundantCastAnalyzer(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 66, Line: 4, Column: 21},
EndPos: ast.Position{Offset: 69, Line: 4, Column: 24},
StartPos: ast.Position{Offset: 82, Line: 4, Column: 21},
EndPos: ast.Position{Offset: 85, Line: 4, Column: 24},
},
Location: testLocation,
Category: lint.UnnecessaryCastCategory,
Expand All @@ -70,8 +70,8 @@ func TestRedundantCastAnalyzer(t *testing.T) {

diagnostics := testAnalyzers(t,
`
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = true as! Bool
}
}
Expand All @@ -84,8 +84,8 @@ func TestRedundantCastAnalyzer(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 58, Line: 4, Column: 13},
EndPos: ast.Position{Offset: 70, Line: 4, Column: 25},
StartPos: ast.Position{Offset: 74, Line: 4, Column: 13},
EndPos: ast.Position{Offset: 86, Line: 4, Column: 25},
},
Location: testLocation,
Category: lint.UnnecessaryCastCategory,
Expand All @@ -102,8 +102,8 @@ func TestRedundantCastAnalyzer(t *testing.T) {

diagnostics := testAnalyzers(t,
`
pub contract Test {
pub fun test() {
access(all) contract Test {
access(all) fun test() {
let x = true as? Bool
}
}
Expand All @@ -116,8 +116,8 @@ func TestRedundantCastAnalyzer(t *testing.T) {
[]analysis.Diagnostic{
{
Range: ast.Range{
StartPos: ast.Position{Offset: 58, Line: 4, Column: 13},
EndPos: ast.Position{Offset: 70, Line: 4, Column: 25},
StartPos: ast.Position{Offset: 74, Line: 4, Column: 13},
EndPos: ast.Position{Offset: 86, Line: 4, Column: 25},
},
Location: testLocation,
Category: lint.UnnecessaryCastCategory,
Expand Down
Loading

0 comments on commit b39d90c

Please sign in to comment.