All files / shared/parsers structure-parser.ts

93.33% Statements 14/15
91.66% Branches 11/12
100% Functions 2/2
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                  72x     72x   72x 72x   72x 2x                 70x 6x                   72x 72x   72x                   72x 8x                   72x      
/**
 * Structure Parser
 * 
 * Analyzes Arduino code for structure issues:
 * - Missing void setup()
 * - Missing void loop()
 * - setup()/loop() with parameters
 */
 
import type { ParserMessage } from "../schema";
import { randomUUID } from "node:crypto";
import { STRUCTURE_PATTERNS, findLineNumber } from "../parser-patterns";
 
/**
 * Analyzer for structure (setup/loop) issues
 */
export class StructureParser {
  constructor(private readonly code: string) {}
 
  parse(): ParserMessage[] {
    const messages: ParserMessage[] = [];
 
    const setupMatch = STRUCTURE_PATTERNS.SETUP_FUNCTION.test(this.code);
    const anySetup = STRUCTURE_PATTERNS.SETUP_ANY.test(this.code);
 
    if (!setupMatch && anySetup) {
      messages.push({
        id: randomUUID(),
        type: "warning",
        category: "structure",
        severity: 2,
        message: "setup() has parameters, but Arduino setup() should have no parameters.",
        suggestion: "void setup()",
        line: findLineNumber(this.code, STRUCTURE_PATTERNS.SETUP_ANY),
      });
    } else if (!setupMatch) {
      messages.push({
        id: randomUUID(),
        type: "error",
        category: "structure",
        severity: 3,
        message: "Missing void setup() function. Every Arduino program needs setup().",
        suggestion: "void setup() { }",
      });
    }
 
    const loopMatch = STRUCTURE_PATTERNS.LOOP_FUNCTION.test(this.code);
    const anyLoop = STRUCTURE_PATTERNS.LOOP_ANY.test(this.code);
 
    Iif (!loopMatch && anyLoop) {
      messages.push({
        id: randomUUID(),
        type: "warning",
        category: "structure",
        severity: 2,
        message: "loop() has parameters, but Arduino loop() should have no parameters.",
        suggestion: "void loop()",
        line: findLineNumber(this.code, STRUCTURE_PATTERNS.LOOP_ANY),
      });
    } else if (!loopMatch) {
      messages.push({
        id: randomUUID(),
        type: "error",
        category: "structure",
        severity: 3,
        message: "Missing void loop() function. Every Arduino program needs loop().",
        suggestion: "void loop() { }",
      });
    }
 
    return messages;
  }
}