Skip to content

Commit 1aae74e

Browse files
authored
Add files via upload
1 parent 997d6a1 commit 1aae74e

1 file changed

Lines changed: 76 additions & 101 deletions

File tree

src/main/java/cod/debug/Linter.java

Lines changed: 76 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ private void checkNamingConventions(TypeNode type) {
8888
addWarning(type.name, "TYPE", "Type name '" + type.name + "' should use PascalCase");
8989
}
9090

91-
// Check method names
92-
for (MethodNode method : type.methods) {
93-
if (!NamingValidator.startsWithLowerCase(method.name)) {
94-
addWarning(type.name, method.name, "Method '" + method.name + "' should start with lowercase letter");
91+
// Check node names
92+
for (MethodNode node : type.methods) {
93+
if (!NamingValidator.startsWithLowerCase(node.methodName)) {
94+
addWarning(type.name, node.methodName, "Method '" + node.methodName + "' should start with lowercase letter");
9595
}
9696
}
9797

@@ -126,9 +126,9 @@ private void visitType(TypeNode type) {
126126
checkNamingConventions(type);
127127

128128
// First pass: Find all defined methods in this type
129-
for (MethodNode method : type.methods) {
130-
definedMethods.add(method.name);
131-
methodMap.put(method.name, method);
129+
for (MethodNode node : type.methods) {
130+
definedMethods.add(node.methodName);
131+
methodMap.put(node.methodName, node);
132132
}
133133

134134
// "main" is a special entry point, so it's always considered "used"
@@ -137,15 +137,15 @@ private void visitType(TypeNode type) {
137137
}
138138

139139
// Second pass: Visit all methods to find calls and analyze bodies
140-
for (MethodNode method : type.methods) {
141-
visitMethod(method, type.name);
140+
for (MethodNode node : type.methods) {
141+
visitMethod(node, type.name);
142142
}
143143

144144
// Third pass: Analyze the results for this type
145145
for (String methodName : definedMethods) {
146146
if (!calledMethods.contains(methodName)) {
147-
MethodNode method = methodMap.get(methodName);
148-
if ("local".equals(method.visibility)) {
147+
MethodNode node = methodMap.get(methodName);
148+
if ("local".equals(node.visibility)) {
149149
addWarning(
150150
type.name,
151151
methodName,
@@ -156,33 +156,33 @@ private void visitType(TypeNode type) {
156156
}
157157
}
158158

159-
private void visitMethod(MethodNode method, String typeName) {
160-
// Initialize sets for this method
159+
private void visitMethod(MethodNode node, String typeName) {
160+
// Initialize sets for this node
161161
this.definedVariables = new HashSet<String>();
162162
this.usedVariables = new HashSet<String>();
163163

164164
// Add parameters to both sets (parameters are defined and "used" by the caller)
165-
for (ParamNode param : method.parameters) {
165+
for (ParamNode param : node.parameters) {
166166
definedVariables.add(param.name);
167167
usedVariables.add(param.name);
168168
}
169169

170170
// Add return slots (they are implicitly defined)
171-
for (SlotNode slot : method.returnSlots) {
171+
for (SlotNode slot : node.returnSlots) {
172172
definedVariables.add(slot.name);
173173
}
174174

175-
// Visit all statements in the method body
176-
for (StmtNode stmt : method.body) {
175+
// Visit all statements in the node body
176+
for (StmtNode stmt : node.body) {
177177
visitStatement(stmt);
178178
}
179179

180-
// Analyze the results for this method
180+
// Analyze the results for this node
181181
for (String varName : definedVariables) {
182182
if (!usedVariables.contains(varName)) {
183183
// Don't warn about unused slots, that's a different kind of check
184184
boolean isSlot = false;
185-
for (SlotNode slot : method.returnSlots) {
185+
for (SlotNode slot : node.returnSlots) {
186186
if (slot.name.equals(varName)) {
187187
isSlot = true;
188188
break;
@@ -192,7 +192,7 @@ private void visitMethod(MethodNode method, String typeName) {
192192
if (!isSlot) {
193193
addWarning(
194194
typeName,
195-
method.name,
195+
node.methodName,
196196
"Local variable '" + varName + "' is defined but its value is never used."
197197
);
198198
}
@@ -203,96 +203,71 @@ private void visitMethod(MethodNode method, String typeName) {
203203
// --- Statement Visitor ---
204204

205205
private void visitStatement(StmtNode stmt) {
206-
if (stmt == null) {
207-
return;
208-
}
206+
if (stmt == null) {
207+
return;
208+
}
209209

210-
if (stmt instanceof VarNode) {
211-
VarNode var = (VarNode) stmt;
212-
definedVariables.add(var.name);
213-
// Visit the assignment expression (if it exists)
214-
visitExpression(var.value);
210+
if (stmt instanceof VarNode) {
211+
VarNode var = (VarNode) stmt;
212+
definedVariables.add(var.name);
213+
visitExpression(var.value);
215214

216-
} else if (stmt instanceof AssignmentNode) {
217-
AssignmentNode assign = (AssignmentNode) stmt;
218-
// The right side is "used"
219-
visitExpression(assign.right);
220-
221-
// The left side is "defined"
222-
if (assign.left instanceof ExprNode) {
223-
ExprNode left = (ExprNode) assign.left;
224-
if (left.name != null) {
225-
// This is a simple assignment like 'x = 5'
226-
// It counts as a "use" if we are reading 'x' from an outer scope
227-
// But it counts as a "definition" for the *current* scope.
228-
definedVariables.add(left.name);
229-
}
230-
}
231-
// Visit the expression on the left (e.g., for array index)
232-
visitExpression(assign.left);
233-
234-
} else if (stmt instanceof SlotAssignmentNode) {
235-
SlotAssignmentNode assign = (SlotAssignmentNode) stmt;
236-
// The value is "used"
237-
visitExpression(assign.value);
238-
// The slot itself is "used" (written to)
239-
usedVariables.add(assign.slotName);
240-
241-
} else if (stmt instanceof OutputNode) {
242-
OutputNode output = (OutputNode) stmt;
243-
for (ExprNode arg : output.arguments) {
244-
visitExpression(arg);
245-
}
246-
247-
} else if (stmt instanceof StmtIfNode) {
248-
StmtIfNode ifNode = (StmtIfNode) stmt;
249-
visitExpression(ifNode.condition);
250-
for (StmtNode s : ifNode.thenBlock.statements) {
251-
visitStatement(s);
252-
}
253-
for (StmtNode s : ifNode.elseBlock.statements) {
254-
visitStatement(s);
215+
} else if (stmt instanceof AssignmentNode) {
216+
AssignmentNode assign = (AssignmentNode) stmt;
217+
visitExpression(assign.right);
218+
219+
if (assign.left instanceof ExprNode) {
220+
ExprNode left = (ExprNode) assign.left;
221+
if (left.name != null) {
222+
definedVariables.add(left.name);
255223
}
224+
}
225+
visitExpression(assign.left);
226+
227+
} else if (stmt instanceof SlotAssignmentNode) {
228+
SlotAssignmentNode assign = (SlotAssignmentNode) stmt;
229+
visitExpression(assign.value);
230+
usedVariables.add(assign.slotName);
231+
} else if (stmt instanceof StmtIfNode) {
232+
StmtIfNode ifNode = (StmtIfNode) stmt;
233+
visitExpression(ifNode.condition);
234+
for (StmtNode s : ifNode.thenBlock.statements) {
235+
visitStatement(s);
236+
}
237+
for (StmtNode s : ifNode.elseBlock.statements) {
238+
visitStatement(s);
239+
}
256240

257-
} else if (stmt instanceof ForNode) {
258-
ForNode forNode = (ForNode) stmt;
259-
260-
// Iterator is defined in this scope
261-
definedVariables.add(forNode.iterator);
262-
263-
// Range expressions are "used"
241+
} else if (stmt instanceof ForNode) {
242+
ForNode forNode = (ForNode) stmt;
243+
244+
definedVariables.add(forNode.iterator);
245+
246+
if (forNode.range != null) {
264247
visitExpression(forNode.range.start);
265248
visitExpression(forNode.range.end);
266249
visitExpression(forNode.range.step);
267-
268-
// Body statements are visited
269-
for (StmtNode s : forNode.body.statements) {
270-
visitStatement(s);
271-
}
272-
273-
} else if (stmt instanceof MethodCallNode) {
274-
// This is a statement like 'myMethod()'
275-
visitExpression((ExprNode) stmt);
276-
277-
} else if (stmt instanceof ReturnSlotAssignmentNode) {
278-
ReturnSlotAssignmentNode assign = (ReturnSlotAssignmentNode) stmt;
279-
// The method call is "used"
280-
visitExpression(assign.methodCall);
281-
// The variables being assigned to are "defined"
282-
for (String varName : assign.variableNames) {
283-
definedVariables.add(varName);
284-
}
250+
} else if (forNode.arraySource != null) {
251+
visitExpression(forNode.arraySource);
252+
}
253+
254+
for (StmtNode s : forNode.body.statements) {
255+
visitStatement(s);
256+
}
285257

286-
} else if (stmt instanceof InputNode) {
287-
InputNode input = (InputNode) stmt;
288-
// The variable is "defined"
289-
definedVariables.add(input.variableName);
258+
} else if (stmt instanceof MethodCallNode) {
259+
visitExpression((ExprNode) stmt);
290260

291-
} else if (stmt instanceof ExprNode) {
292-
// This is an expression used as a statement, like '5 + 5'
293-
visitExpression((ExprNode) stmt);
261+
} else if (stmt instanceof ReturnSlotAssignmentNode) {
262+
ReturnSlotAssignmentNode assign = (ReturnSlotAssignmentNode) stmt;
263+
visitExpression(assign.methodCall);
264+
for (String varName : assign.variableNames) {
265+
definedVariables.add(varName);
294266
}
267+
} else if (stmt instanceof ExprNode) {
268+
visitExpression((ExprNode) stmt);
295269
}
270+
}
296271

297272
// --- Expression Visitor ---
298273

@@ -325,7 +300,7 @@ private void visitExpression(ExprNode expr) {
325300
} else if (expr instanceof MethodCallNode) {
326301
MethodCallNode call = (MethodCallNode) expr;
327302

328-
// This method is "called"
303+
// This node is "called"
329304
// We only check for local calls, not qualified 'lib.math.sqrt'
330305
if (call.qualifiedName == null || !call.qualifiedName.contains(".")) {
331306
calledMethods.add(call.name);
@@ -345,7 +320,7 @@ private void visitExpression(ExprNode expr) {
345320

346321
private void addWarning(String typeName, String methodName, String message) {
347322
// We don't have line numbers on the nodes yet, so we'll just
348-
// provide the type and method context.
323+
// provide the type and node context.
349324
warnings.add("Warning [" + typeName + "." + methodName + "]: " + message);
350325
}
351326

0 commit comments

Comments
 (0)