|
| 1 | +/* |
| 2 | + * Flow CLI |
| 3 | + * |
| 4 | + * Copyright Flow Foundation |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package mcp |
| 20 | + |
| 21 | +import ( |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "strings" |
| 25 | + |
| 26 | + cdclint "github.com/onflow/cadence-tools/lint" |
| 27 | + cdctests "github.com/onflow/cadence-tools/test/helpers" |
| 28 | + "github.com/onflow/cadence/ast" |
| 29 | + "github.com/onflow/cadence/common" |
| 30 | + cdcerrors "github.com/onflow/cadence/errors" |
| 31 | + "github.com/onflow/cadence/parser" |
| 32 | + "github.com/onflow/cadence/sema" |
| 33 | + "github.com/onflow/cadence/stdlib" |
| 34 | + "github.com/onflow/cadence/tools/analysis" |
| 35 | + "golang.org/x/exp/maps" |
| 36 | + |
| 37 | + "github.com/onflow/flow-cli/internal/util" |
| 38 | +) |
| 39 | + |
| 40 | +// lintCode runs all registered cadence-tools lint analyzers on the given code. |
| 41 | +// Returns analysis diagnostics (not LSP protocol diagnostics). |
| 42 | +func lintCode(code string) ([]analysis.Diagnostic, error) { |
| 43 | + location := common.StringLocation("code.cdc") |
| 44 | + codeBytes := []byte(code) |
| 45 | + |
| 46 | + // Parse |
| 47 | + program, parseErr := parser.ParseProgram(nil, codeBytes, parser.Config{}) |
| 48 | + if parseErr != nil { |
| 49 | + var parentErr cdcerrors.ParentError |
| 50 | + if errors.As(parseErr, &parentErr) { |
| 51 | + return collectPositionedErrors(parentErr, location), nil |
| 52 | + } |
| 53 | + return nil, fmt.Errorf("parse error: %w", parseErr) |
| 54 | + } |
| 55 | + if program == nil { |
| 56 | + return nil, nil |
| 57 | + } |
| 58 | + |
| 59 | + // Choose standard library based on program type |
| 60 | + var stdLib *util.StandardLibrary |
| 61 | + if program.SoleTransactionDeclaration() != nil || program.SoleContractDeclaration() != nil { |
| 62 | + stdLib = util.NewStandardLibrary() |
| 63 | + } else { |
| 64 | + stdLib = util.NewScriptStandardLibrary() |
| 65 | + } |
| 66 | + |
| 67 | + checkerConfig := &sema.Config{ |
| 68 | + BaseValueActivationHandler: func(_ common.Location) *sema.VariableActivation { |
| 69 | + return stdLib.BaseValueActivation |
| 70 | + }, |
| 71 | + AccessCheckMode: sema.AccessCheckModeNotSpecifiedUnrestricted, |
| 72 | + PositionInfoEnabled: true, |
| 73 | + ExtendedElaborationEnabled: true, |
| 74 | + ImportHandler: lintImportHandler, |
| 75 | + } |
| 76 | + |
| 77 | + // Type check |
| 78 | + checker, err := sema.NewChecker(program, location, nil, checkerConfig) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("checker creation error: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + var diagnostics []analysis.Diagnostic |
| 84 | + |
| 85 | + checkErr := checker.Check() |
| 86 | + if checkErr != nil { |
| 87 | + var checkerErr *sema.CheckerError |
| 88 | + if errors.As(checkErr, &checkerErr) { |
| 89 | + diagnostics = append(diagnostics, collectPositionedErrors(checkerErr, location)...) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Run lint analyzers |
| 94 | + analyzers := maps.Values(cdclint.Analyzers) |
| 95 | + analysisProgram := analysis.Program{ |
| 96 | + Program: program, |
| 97 | + Checker: checker, |
| 98 | + Location: location, |
| 99 | + Code: codeBytes, |
| 100 | + } |
| 101 | + analysisProgram.Run(analyzers, func(d analysis.Diagnostic) { |
| 102 | + diagnostics = append(diagnostics, d) |
| 103 | + }) |
| 104 | + |
| 105 | + return diagnostics, nil |
| 106 | +} |
| 107 | + |
| 108 | +// lintImportHandler resolves standard library imports for lint analysis. |
| 109 | +func lintImportHandler( |
| 110 | + checker *sema.Checker, |
| 111 | + importedLocation common.Location, |
| 112 | + _ ast.Range, |
| 113 | +) (sema.Import, error) { |
| 114 | + switch importedLocation { |
| 115 | + case stdlib.TestContractLocation: |
| 116 | + return sema.ElaborationImport{ |
| 117 | + Elaboration: stdlib.GetTestContractType().Checker.Elaboration, |
| 118 | + }, nil |
| 119 | + case cdctests.BlockchainHelpersLocation: |
| 120 | + return sema.ElaborationImport{ |
| 121 | + Elaboration: cdctests.BlockchainHelpersChecker().Elaboration, |
| 122 | + }, nil |
| 123 | + default: |
| 124 | + return nil, fmt.Errorf("cannot resolve import: %s", importedLocation) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +type positionedError interface { |
| 129 | + error |
| 130 | + ast.HasPosition |
| 131 | +} |
| 132 | + |
| 133 | +// collectPositionedErrors extracts positioned errors from a parent error. |
| 134 | +func collectPositionedErrors(err cdcerrors.ParentError, location common.Location) []analysis.Diagnostic { |
| 135 | + var diagnostics []analysis.Diagnostic |
| 136 | + for _, childErr := range err.ChildErrors() { |
| 137 | + var posErr positionedError |
| 138 | + if !errors.As(childErr, &posErr) { |
| 139 | + continue |
| 140 | + } |
| 141 | + diagnostics = append(diagnostics, analysis.Diagnostic{ |
| 142 | + Location: location, |
| 143 | + Category: "error", |
| 144 | + Message: posErr.Error(), |
| 145 | + Range: ast.Range{ |
| 146 | + StartPos: posErr.StartPosition(), |
| 147 | + EndPos: posErr.EndPosition(nil), |
| 148 | + }, |
| 149 | + }) |
| 150 | + } |
| 151 | + return diagnostics |
| 152 | +} |
| 153 | + |
| 154 | +// formatLintDiagnostics formats analysis diagnostics as human-readable text. |
| 155 | +func formatLintDiagnostics(diagnostics []analysis.Diagnostic) string { |
| 156 | + if len(diagnostics) == 0 { |
| 157 | + return "Lint passed — no issues found." |
| 158 | + } |
| 159 | + |
| 160 | + var b strings.Builder |
| 161 | + errors := 0 |
| 162 | + warnings := 0 |
| 163 | + for _, d := range diagnostics { |
| 164 | + severity := "warning" |
| 165 | + if d.Category == "error" || d.Category == "semantic-error" || d.Category == "syntax-error" { |
| 166 | + severity = "error" |
| 167 | + errors++ |
| 168 | + } else { |
| 169 | + warnings++ |
| 170 | + } |
| 171 | + fmt.Fprintf(&b, "[%s] line %d:%d (%s): %s\n", |
| 172 | + severity, |
| 173 | + d.Range.StartPos.Line, |
| 174 | + d.Range.StartPos.Column, |
| 175 | + d.Category, |
| 176 | + d.Message, |
| 177 | + ) |
| 178 | + } |
| 179 | + fmt.Fprintf(&b, "\n%d error(s), %d warning(s)\n", errors, warnings) |
| 180 | + return b.String() |
| 181 | +} |
0 commit comments