|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "sort" |
| 10 | + |
| 11 | + "github.com/mgomes/vibescript/vibes" |
| 12 | +) |
| 13 | + |
| 14 | +type lintWarning struct { |
| 15 | + Function string |
| 16 | + Pos vibes.Position |
| 17 | + Message string |
| 18 | +} |
| 19 | + |
| 20 | +func analyzeCommand(args []string) error { |
| 21 | + fs := flag.NewFlagSet("analyze", flag.ContinueOnError) |
| 22 | + fs.SetOutput(new(flagErrorSink)) |
| 23 | + if err := fs.Parse(args); err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + |
| 27 | + remaining := fs.Args() |
| 28 | + if len(remaining) == 0 { |
| 29 | + return errors.New("vibes analyze: script path required") |
| 30 | + } |
| 31 | + |
| 32 | + scriptPath, err := filepath.Abs(remaining[0]) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("resolve script path: %w", err) |
| 35 | + } |
| 36 | + input, err := os.ReadFile(scriptPath) |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("read script: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + engine := vibes.MustNewEngine(vibes.Config{}) |
| 42 | + script, err := engine.Compile(string(input)) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("analysis compile failed: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + warnings := analyzeScriptWarnings(script) |
| 48 | + if len(warnings) == 0 { |
| 49 | + fmt.Println("No issues found") |
| 50 | + return nil |
| 51 | + } |
| 52 | + |
| 53 | + for _, warning := range warnings { |
| 54 | + line := warning.Pos.Line |
| 55 | + column := warning.Pos.Column |
| 56 | + if line <= 0 { |
| 57 | + line = 1 |
| 58 | + } |
| 59 | + if column <= 0 { |
| 60 | + column = 1 |
| 61 | + } |
| 62 | + fmt.Printf("%s:%d:%d: %s (%s)\n", scriptPath, line, column, warning.Message, warning.Function) |
| 63 | + } |
| 64 | + |
| 65 | + return fmt.Errorf("analysis found %d issue(s)", len(warnings)) |
| 66 | +} |
| 67 | + |
| 68 | +func analyzeScriptWarnings(script *vibes.Script) []lintWarning { |
| 69 | + warnings := make([]lintWarning, 0) |
| 70 | + for _, fn := range script.Functions() { |
| 71 | + lintStatements(fn.Name, fn.Body, &warnings) |
| 72 | + } |
| 73 | + for _, classDef := range script.Classes() { |
| 74 | + for _, method := range sortedFunctionsByName(classDef.Methods) { |
| 75 | + lintStatements(classDef.Name+"#"+method.Name, method.Body, &warnings) |
| 76 | + } |
| 77 | + for _, method := range sortedFunctionsByName(classDef.ClassMethods) { |
| 78 | + lintStatements(classDef.Name+"."+method.Name, method.Body, &warnings) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + sort.SliceStable(warnings, func(i, j int) bool { |
| 83 | + if warnings[i].Pos.Line != warnings[j].Pos.Line { |
| 84 | + return warnings[i].Pos.Line < warnings[j].Pos.Line |
| 85 | + } |
| 86 | + if warnings[i].Pos.Column != warnings[j].Pos.Column { |
| 87 | + return warnings[i].Pos.Column < warnings[j].Pos.Column |
| 88 | + } |
| 89 | + return warnings[i].Function < warnings[j].Function |
| 90 | + }) |
| 91 | + |
| 92 | + return warnings |
| 93 | +} |
| 94 | + |
| 95 | +func sortedFunctionsByName(functions map[string]*vibes.ScriptFunction) []*vibes.ScriptFunction { |
| 96 | + names := make([]string, 0, len(functions)) |
| 97 | + for name := range functions { |
| 98 | + names = append(names, name) |
| 99 | + } |
| 100 | + sort.Strings(names) |
| 101 | + sorted := make([]*vibes.ScriptFunction, 0, len(names)) |
| 102 | + for _, name := range names { |
| 103 | + sorted = append(sorted, functions[name]) |
| 104 | + } |
| 105 | + return sorted |
| 106 | +} |
| 107 | + |
| 108 | +func lintStatements(function string, statements []vibes.Statement, warnings *[]lintWarning) bool { |
| 109 | + terminated := false |
| 110 | + for _, stmt := range statements { |
| 111 | + if terminated { |
| 112 | + *warnings = append(*warnings, lintWarning{ |
| 113 | + Function: function, |
| 114 | + Pos: stmt.Pos(), |
| 115 | + Message: "unreachable statement", |
| 116 | + }) |
| 117 | + continue |
| 118 | + } |
| 119 | + if statementTerminates(function, stmt, warnings) { |
| 120 | + terminated = true |
| 121 | + } |
| 122 | + } |
| 123 | + return terminated |
| 124 | +} |
| 125 | + |
| 126 | +func statementTerminates(function string, stmt vibes.Statement, warnings *[]lintWarning) bool { |
| 127 | + switch typed := stmt.(type) { |
| 128 | + case *vibes.ReturnStmt, *vibes.RaiseStmt: |
| 129 | + return true |
| 130 | + case *vibes.IfStmt: |
| 131 | + return ifStatementTerminates(function, typed, warnings) |
| 132 | + case *vibes.ForStmt: |
| 133 | + lintStatements(function, typed.Body, warnings) |
| 134 | + return false |
| 135 | + case *vibes.WhileStmt: |
| 136 | + lintStatements(function, typed.Body, warnings) |
| 137 | + return false |
| 138 | + case *vibes.UntilStmt: |
| 139 | + lintStatements(function, typed.Body, warnings) |
| 140 | + return false |
| 141 | + case *vibes.TryStmt: |
| 142 | + bodyTerminated := lintStatements(function, typed.Body, warnings) |
| 143 | + rescueTerminated := false |
| 144 | + if len(typed.Rescue) > 0 { |
| 145 | + rescueTerminated = lintStatements(function, typed.Rescue, warnings) |
| 146 | + } |
| 147 | + ensureTerminated := false |
| 148 | + if len(typed.Ensure) > 0 { |
| 149 | + ensureTerminated = lintStatements(function, typed.Ensure, warnings) |
| 150 | + } |
| 151 | + if ensureTerminated { |
| 152 | + return true |
| 153 | + } |
| 154 | + if len(typed.Rescue) == 0 { |
| 155 | + return bodyTerminated |
| 156 | + } |
| 157 | + return bodyTerminated && rescueTerminated |
| 158 | + default: |
| 159 | + return false |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func ifStatementTerminates(function string, stmt *vibes.IfStmt, warnings *[]lintWarning) bool { |
| 164 | + consequentTerminated := lintStatements(function, stmt.Consequent, warnings) |
| 165 | + elseIfAllTerminated := true |
| 166 | + for _, elseIf := range stmt.ElseIf { |
| 167 | + if !lintStatements(function, elseIf.Consequent, warnings) { |
| 168 | + elseIfAllTerminated = false |
| 169 | + } |
| 170 | + } |
| 171 | + if len(stmt.Alternate) == 0 { |
| 172 | + return false |
| 173 | + } |
| 174 | + alternateTerminated := lintStatements(function, stmt.Alternate, warnings) |
| 175 | + return consequentTerminated && elseIfAllTerminated && alternateTerminated |
| 176 | +} |
0 commit comments