-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdim_linter.py
More file actions
337 lines (271 loc) · 10.1 KB
/
dim_linter.py
File metadata and controls
337 lines (271 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# dim_linter.py — Linter for Dim
#
# Static analysis and code quality checks for Dim programs.
import re
from typing import List, Dict, Optional, Any, Set
from dataclasses import dataclass, field
from enum import Enum
class LintLevel(Enum):
ERROR = "error"
WARNING = "warning"
INFO = "info"
HINT = "hint"
@dataclass
class LintIssue:
level: LintLevel
code: str
message: str
file: str
line: int
column: int
class Rule:
def check(self, source: str, ast: Any) -> List[LintIssue]:
return []
class NamingConventionRule(Rule):
def check(self, source: str, ast: Any) -> List[LintIssue]:
issues = []
fn_pattern = re.compile(r"^fn\s+([a-z_][a-z0-9_]*)", re.MULTILINE)
for match in fn_pattern.finditer(source):
name = match.group(1)
line = source[: match.start()].count("\n") + 1
if not name.islower():
issues.append(
LintIssue(
level=LintLevel.WARNING,
code="E001",
message=f"Function name '{name}' should be snake_case",
file="",
line=line,
column=match.start(),
)
)
if name.startswith("_"):
issues.append(
LintIssue(
level=LintLevel.INFO,
code="I001",
message=f"Function '{name}' starts with underscore (private)",
file="",
line=line,
column=match.start(),
)
)
struct_pattern = re.compile(r"^struct\s+([A-Z][a-zA-Z0-9]*)", re.MULTILINE)
for match in struct_pattern.finditer(source):
name = match.group(1)
line = source[: match.start()].count("\n") + 1
if not name[0].isupper():
issues.append(
LintIssue(
level=LintLevel.WARNING,
code="E002",
message=f"Struct name '{name}' should be PascalCase",
file="",
line=line,
column=match.start(),
)
)
return issues
class UnusedVariableRule(Rule):
def check(self, source: str, ast: Any) -> List[LintIssue]:
issues = []
let_pattern = re.compile(r"let\s+(mut\s+)?([a-z_][a-z0-9_]*)", re.MULTILINE)
varsDefined = set()
for match in let_pattern.finditer(source):
varsDefined.add(match.group(2))
for_pattern = re.compile(r"for\s+([a-z_][a-z0-9_]*)\s+in", re.MULTILINE)
for match in for_pattern.finditer(source):
if match.group(1) in varsDefined:
varsDefined.discard(match.group(1))
assign_pattern = re.compile(r"([a-z_][a-z0-9_]*)\s*=", re.MULTILINE)
for match in assign_pattern.finditer(source):
var_name = match.group(1)
if var_name in varsDefined:
varsDefined.discard(var_name)
for var in varsDefined:
pattern = re.compile(rf"let\s+.*\b{var}\b", re.MULTILINE)
match = pattern.search(source)
if match:
line = source[: match.start()].count("\n") + 1
issues.append(
LintIssue(
level=LintLevel.INFO,
code="I002",
message=f"Variable '{var}' appears unused",
file="",
line=line,
column=match.start(),
)
)
return issues
class ComplexityRule(Rule):
def check(self, source: str, ast: Any) -> List[LintIssue]:
issues = []
for match in re.finditer(r"\bfn\s+(\w+)", source):
fn_name = match.group(1)
start = match.start()
line_num = source[:start].count("\n") + 1
brace_count = 0
max_nesting = 0
current_nesting = 0
remaining = source[start:]
for char in remaining:
if char == "{":
brace_count += 1
current_nesting += 1
max_nesting = max(max_nesting, current_nesting)
elif char == "}":
brace_count -= 1
current_nesting = max(0, current_nesting - 1)
if brace_count == 0:
break
if max_nesting > 5:
issues.append(
LintIssue(
level=LintLevel.WARNING,
code="E003",
message=f"Function '{fn_name}' has complexity {max_nesting} (max 5)",
file="",
line=line_num,
column=0,
)
)
return issues
class TODOCommentRule(Rule):
def check(self, source: str, ast: Any) -> List[LintIssue]:
issues = []
patterns = [
(r"TODO", "TODO found"),
(r"FIXME", "FIXME found"),
(r"XXX", "XXX found"),
(r"HACK", "HACK found"),
]
for pattern, msg in patterns:
for match in re.finditer(pattern, source, re.IGNORECASE):
line = source[: match.start()].count("\n") + 1
issues.append(
LintIssue(
level=LintLevel.INFO,
code="I003",
message=f"{msg} - should be addressed",
file="",
line=line,
column=match.start(),
)
)
return issues
class PrintDebugRule(Rule):
def check(self, source: str, ast: Any) -> List[LintIssue]:
issues = []
debug_patterns = [
(r"\bprint\s*\(", "print() found"),
(r"\bprintln\s*\(", "println() found"),
]
for pattern, msg in debug_patterns:
for match in re.finditer(pattern, source):
line = source[: match.start()].count("\n") + 1
context_start = max(0, match.start() - 50)
context = source[context_start : match.start()]
if "fn main" not in context:
issues.append(
LintIssue(
level=LintLevel.HINT,
code="H001",
message=f"{msg} - consider removing for production",
file="",
line=line,
column=match.start(),
)
)
return issues
class ImportOrderRule(Rule):
def check(self, source: str, ast: Any) -> List[LintIssue]:
issues = []
import_lines = []
for match in re.finditer(r"^import\s+", source, re.MULTILINE):
line = source[: match.start()].count("\n")
import_lines.append((line, match.start()))
if len(import_lines) > 1:
modules = []
for line, pos in import_lines:
module_match = re.search(r"import\s+([\w.]+)", source[pos : pos + 50])
if module_match:
modules.append(module_match.group(1))
sorted_modules = sorted(modules)
if modules != sorted_modules:
issues.append(
LintIssue(
level=LintLevel.INFO,
code="I004",
message="Imports not sorted alphabetically",
file="",
line=import_lines[0][0],
column=0,
)
)
return issues
class Linter:
def __init__(self):
self.rules: List[Rule] = [
NamingConventionRule(),
UnusedVariableRule(),
ComplexityRule(),
TODOCommentRule(),
PrintDebugRule(),
ImportOrderRule(),
]
self.enabled_rules: Set[str] = set()
def lint(self, source: str, filename: str = "") -> List[LintIssue]:
all_issues = []
for rule in self.rules:
issues = rule.check(source, None)
for issue in issues:
issue.file = filename
all_issues.extend(issues)
all_issues.sort(key=lambda x: (x.line, x.column))
return all_issues
def lint_file(self, filepath: str) -> List[LintIssue]:
try:
with open(filepath, "r", encoding="utf-8") as f:
source = f.read()
return self.lint(source, filepath)
except Exception as e:
return [
LintIssue(
level=LintLevel.ERROR,
code="E999",
message=f"Failed to read file: {e}",
file=filepath,
line=1,
column=0,
)
]
def run_linter(args: List[str]):
if not args:
print("Usage: dim lint <file.dim>")
return
filepath = args[0]
linter = Linter()
issues = linter.lint_file(filepath)
if not issues:
print(f"✓ No issues found in {filepath}")
return
level_colors = {
LintLevel.ERROR: "\033[31m",
LintLevel.WARNING: "\033[33m",
LintLevel.INFO: "\033[36m",
LintLevel.HINT: "\033[32m",
}
reset = "\033[0m"
errors = sum(1 for i in issues if i.level == LintLevel.ERROR)
warnings = sum(1 for i in issues if i.level == LintLevel.WARNING)
print(f"Linting {filepath}: {errors} errors, {warnings} warnings\n")
for issue in issues:
color = level_colors.get(issue.level, "")
print(
f"{color}{issue.level.value.upper()}[{issue.code}]{reset}: {issue.message}"
)
print(f" --> {issue.file}:{issue.line}:{issue.column}")
if __name__ == "__main__":
import sys
run_linter(sys.argv[1:] if len(sys.argv) > 1 else [])