All files / shared/parsers hardware-compatibility-parser.ts

93.91% Statements 139/148
82.08% Branches 55/67
100% Functions 15/15
93.47% Lines 129/138

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 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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381                                              11x     76x 76x 6x 6x   70x                                 79x 79x 79x 79x 79x       79x     79x     79x 79x   79x 24x     79x 79x   79x 79x           79x 79x 70x 70x   79x 52x   79x   79x       79x 79x 79x     79x 16x 16x 16x 4x                     79x       79x 79x 79x     79x 14x 14x 10x 10x 2x 2x                       79x       79x 79x     79x 79x   79x 17x       79x 79x 79x 14x 14x 4x 2x                 2x           79x 79x 56x                           79x       79x 79x 52x   79x       237x   237x 474x     474x   39x 39x 39x 39x 39x 39x   39x 39x   39x 27x 156x           237x       39x 63x   39x 30x 30x 30x 1053x   30x 30x           9x 9x       39x       39x   39x 27x   39x               158x           79x     79x   79x 24x 24x 24x 24x 24x       79x 52x     79x                 79x   79x 70x   6x 6x   6x 6x                                           79x                     79x   79x 19x 19x   19x                                             79x      
import type { ParserMessage } from "../schema";
import type { PinMode } from "@shared/types/arduino.types";
import { randomUUID } from "node:crypto";
import {
  PIN_PATTERNS,
  parsePinNumber,
  stripComments,
  findLineNumber,
  FOR_LOOP_TYPED,
  FOR_LOOP_BARE,
} from "@shared/parser-patterns";
 
interface PinModeCall {
  pin: number;
  mode: PinMode;
  line: number;
}
 
interface PinModeEntry {
  modes: Array<PinMode>;
  lines: number[];
}
 
const VALID_PIN_MODES = new Set<string>(["INPUT", "OUTPUT", "INPUT_PULLUP"]);
 
function addPinModeEntry(result: Map<string, PinModeEntry>, pin: string, mode: PinMode, line: number): void {
  const existing = result.get(pin);
  if (existing) {
    existing.modes.push(mode);
    existing.lines.push(line);
  } else {
    result.set(pin, { modes: [mode], lines: [line] });
  }
}
 
/**
 * Parser for hardware compatibility issues
 * 
 * Rules:
 * 1. PWM on non-PWM pins
 * 2. Missing pinMode() for digital I/O
 * 3. Variable pins without pinMode
 * 4. Multiple pinMode() for same pin
 * 5. OUTPUT pins being read
 */
export class HardwareCompatibilityParser {
  private readonly pinModeCalls: Map<string, PinModeEntry>;
 
  constructor(private readonly code: string) {
    this.code = code;
    const uncommentedCode = stripComments(code);
    const pinChecker = new PinCompatibilityChecker(uncommentedCode);
    this.pinModeCalls = pinChecker.getPinModeInfo((c) => this.getLoopPinModeCalls(c));
  }
 
  parse(): ParserMessage[] {
    const messages: ParserMessage[] = [];
 
    // Check analogWrite on non-PWM pins
    messages.push(...this.checkAnalogWritePWM());
 
    // Collect pinMode information
    const pinModeSet = new Set<string>();
    const pinModeRegex = PIN_PATTERNS.MODE;
    let match;
    while ((match = pinModeRegex.exec(this.code)) !== null) {
      pinModeSet.add(match[1]);
    }
 
    const pinChecker = new PinCompatibilityChecker(stripComments(this.code));
    messages.push(...pinChecker.checkPinModeConflicts(this.pinModeCalls));
 
    const loopConfiguredPins = this.getLoopConfiguredPins();
    messages.push(
      ...this.checkDigitalIOSetup(pinModeSet, loopConfiguredPins),
      ...this.checkVariablePinUsage(),
    );
 
    // Check OUTPUT pins being read
    const outputPins = new Set<number>();
    for (const [pin, entry] of this.pinModeCalls.entries()) {
      const pinNum = parsePinNumber(pin);
      if (pinNum !== undefined && entry.modes.includes("OUTPUT")) outputPins.add(pinNum);
    }
    for (const { pin, mode } of this.getLoopPinModeCalls(stripComments(this.code))) {
      if (mode === "OUTPUT") outputPins.add(pin);
    }
    messages.push(...pinChecker.checkOutputPinsReadAsInput(stripComments(this.code), outputPins, parsePinNumber));
 
    return messages;
  }
 
  private checkAnalogWritePWM(): ParserMessage[] {
    const messages: ParserMessage[] = [];
    const PWM_PINS = new Set([3, 5, 6, 9, 10, 11]);
    const analogWriteRegex = PIN_PATTERNS.ANALOG_WRITE;
    let match;
    
    while ((match = analogWriteRegex.exec(this.code)) !== null) {
      const pinStr = match[1];
      const pin = parsePinNumber(pinStr);
      if (pin !== undefined && !PWM_PINS.has(pin)) {
        messages.push({
          id: randomUUID(),
          type: "warning",
          category: "hardware",
          severity: 2,
          message: `analogWrite(${pinStr}, ...) used on pin ${pin}, which doesn't support PWM on Arduino UNO. PWM pins: 3, 5, 6, 9, 10, 11.`,
          suggestion: `// Use PWM pin instead: analogWrite(3, value);`,
          line: findLineNumber(this.code, new RegExp(String.raw`analogWrite\s*\(\s*${pinStr}`)),
        });
      }
    }
    return messages;
  }
 
  private checkDigitalIOSetup(pinModeSet: Set<string>, loopConfiguredPins: Set<number>): ParserMessage[] {
    const messages: ParserMessage[] = [];
    const digitalReadWriteRegex = PIN_PATTERNS.DIGITAL_READ_WRITE;
    const warnedPins = new Set<string>();
    let match;
 
    while ((match = digitalReadWriteRegex.exec(this.code)) !== null) {
      const pinStr = match[1];
      if (/^\d+/.test(pinStr) || /^A\d+/.test(pinStr)) {
        const pin = parsePinNumber(pinStr);
        if (!pinModeSet.has(pinStr) && (pin === undefined || !loopConfiguredPins.has(pin)) && !warnedPins.has(pinStr)) {
          warnedPins.add(pinStr);
          messages.push({
            id: randomUUID(),
            type: "warning",
            category: "hardware",
            severity: 2,
            message: `Pin ${pinStr} used with digitalRead/digitalWrite but pinMode() was not called for this pin.`,
            suggestion: `pinMode(${pinStr}, INPUT);`,
            line: findLineNumber(this.code, new RegExp(String.raw`digital(?:Read|Write)\s*\(\s*${pinStr}`)),
          });
        }
      }
    }
    return messages;
  }
 
  private checkVariablePinUsage(): ParserMessage[] {
    const messages: ParserMessage[] = [];
    const uncommentedCode = stripComments(this.code);
 
    // Identify pins configured via variable names
    const pinModeVarRegex = PIN_PATTERNS.MODE_VAR;
    const pinModeVariables = new Set<string>();
    let match;
    while ((match = pinModeVarRegex.exec(uncommentedCode)) !== null) {
      pinModeVariables.add(match[1]);
    }
 
    // Check if variable pins are used without pinMode and for dynamic usage
    const digitalReadWriteRegex = PIN_PATTERNS.DIGITAL_READ_WRITE;
    let foundUnconfiguredVariable = false;
    while ((match = digitalReadWriteRegex.exec(this.code)) !== null) {
      const pinStr = match[1];
      if (!/^\d+/.test(pinStr) && !/^A\d+/.test(pinStr)) {
        if (!pinModeVariables.has(pinStr) && !foundUnconfiguredVariable) {
          messages.push({
            id: randomUUID(),
            type: "warning",
            category: "hardware",
            severity: 2,
            message: `Variable '${pinStr}' used in digitalRead/digitalWrite but no pinMode() call found for this variable.`,
            suggestion: `pinMode(${pinStr}, INPUT);`,
            line: findLineNumber(this.code, new RegExp(String.raw`digital(?:Read|Write)\s*\(\s*${pinStr}`)),
          });
          foundUnconfiguredVariable = true;
        }
      }
    }
 
    // Check for dynamic pin usage without any pinMode configuration
    const hasPinModeCalls = PIN_PATTERNS.MODE_ANY.test(uncommentedCode);
    if (!hasPinModeCalls && !foundUnconfiguredVariable) {
      Iif (PIN_PATTERNS.DYNAMIC_PIN_READ.test(uncommentedCode) ||
          PIN_PATTERNS.DYNAMIC_PIN_WRITE.test(uncommentedCode)) {
        messages.push({
          id: randomUUID(),
          type: "warning",
          category: "hardware",
          severity: 2,
          message: "digitalRead/digitalWrite uses variable pins without any pinMode() calls. Configure pinMode for the pins being read/written.",
          suggestion: "pinMode(<pin>, INPUT);",
          line: findLineNumber(this.code, PIN_PATTERNS.DIGITAL_READ_WRITE),
        });
      }
    }
 
    return messages;
  }
 
  private getLoopConfiguredPins(): Set<number> {
    const configuredPins = new Set<number>();
    for (const { pin } of this.getLoopPinModeCalls(stripComments(this.code))) {
      configuredPins.add(pin);
    }
    return configuredPins;
  }
 
  private getLoopPinModeCalls(code: string): PinModeCall[] {
    const results: PinModeCall[] = [];
    // Use both typed and bare for-loop regexes to avoid S5843
    for (const forHeaderRe of [FOR_LOOP_TYPED, FOR_LOOP_BARE]) {
      forHeaderRe.lastIndex = 0;
 
      let forMatch: RegExpExecArray | null;
      while ((forMatch = forHeaderRe.exec(code)) !== null) {
        // Groups: [1]=type/empty, [2]=var, [3]=start, [4]=op, [5]=limit
        const varName = forMatch[2];
        const startVal = Number.parseInt(forMatch[3], 10);
        const op = forMatch[4];
        const endVal = Number.parseInt(forMatch[5], 10);
        const lastVal = op === "<=" ? endVal : endVal - 1;
        const forLine = code.slice(0, Math.max(0, forMatch.index)).split("\n").length;
 
        const body = this.extractLoopBodyFromCode(code, forMatch);
        const modes = this.findPinModesInLoopBody(body, varName);
 
        for (const { mode } of modes) {
          for (let pin = startVal; pin <= lastVal; pin++) {
            results.push({ pin, mode, line: forLine });
          }
        }
      }
    }
 
    return results;
  }
 
  private extractLoopBodyFromCode(code: string, forMatch: RegExpExecArray): string {
    let pos = forMatch.index + forMatch[0].length;
    while (pos < code.length && /[ \t\r\n]/.test(code[pos])) pos++;
 
    if (code[pos] === "{") {
      let depth = 0;
      let bodyStart = pos + 1;
      for (let i = pos; i < code.length; i++) {
        if (code[i] === "{") depth++;
        else if (code[i] === "}") {
          depth--;
          Eif (depth === 0) return code.slice(bodyStart, i);
        }
      }
      return "";
    }
 
    const semiIdx = code.indexOf(";", pos);
    return semiIdx >= 0 ? code.slice(pos, semiIdx) : "";
  }
 
  private findPinModesInLoopBody(body: string, varName: string): Array<{ mode: PinMode }> {
    const pinModeRe = new RegExp(
      String.raw`\bpinMode\s*\(\s*${varName}\s*,\s*(INPUT_PULLUP|INPUT|OUTPUT)\s*\)`,
      "g",
    );
    const modes: Array<{ mode: PinMode }> = [];
    let pmMatch: RegExpExecArray | null;
    while ((pmMatch = pinModeRe.exec(body)) !== null) {
      modes.push({ mode: pmMatch[1] as PinMode });
    }
    return modes;
  }
}
 
/**
 * Specialized analyzer for pin mode conflicts and hardware compatibility
 */
class PinCompatibilityChecker {
  constructor(private readonly uncommentedCode: string) {}
 
  /**
   * Extract all pins configured with pinMode calls (direct and loop-based)
   */
  getPinModeInfo(getLoopPinModeCalls: (code: string) => PinModeCall[]): Map<string, PinModeEntry> {
    const result = new Map<string, PinModeEntry>();
 
    // Direct pinMode() calls
    const pinModeWithModeRegex = PIN_PATTERNS.MODE_WITH_MODE;
    let match;
    while ((match = pinModeWithModeRegex.exec(this.uncommentedCode)) !== null) {
      const pin = match[1];
      const rawMode = match[2];
      const mode: PinMode = VALID_PIN_MODES.has(rawMode) ? (rawMode as PinMode) : "INPUT";
      const line = this.uncommentedCode.slice(0, Math.max(0, match.index)).split("\n").length;
      addPinModeEntry(result, pin, mode, line);
    }
 
    // Loop-based pinMode() calls
    for (const { pin, mode, line } of getLoopPinModeCalls(this.uncommentedCode)) {
      addPinModeEntry(result, String(pin), mode, line);
    }
 
    return result;
  }
 
  /**
   * Check for conflicting pin mode declarations
   */
  checkPinModeConflicts(
    pinModeCalls: Map<string, PinModeEntry>,
  ): ParserMessage[] {
    const messages: ParserMessage[] = [];
 
    for (const [pin, entry] of pinModeCalls.entries()) {
      if (entry.modes.length < 2) continue;
 
      const uniqueModes = Array.from(new Set(entry.modes));
      const line = entry.lines[1];
 
      if (uniqueModes.length > 1) {
        messages.push({
          id: randomUUID(),
          type: "warning",
          category: "pins",
          severity: 2,
          message: `Pin ${pin} has multiple pinMode() calls with different modes: ${[...uniqueModes].join(", ")}.`,
          suggestion: `Use a single pinMode(${pin}, <MODE>) call in setup().`,
          line,
        });
      } else E{
        messages.push({
          id: randomUUID(),
          type: "warning",
          category: "pins",
          severity: 2,
          message: `Pin ${pin} has pinMode() called multiple times (${entry.modes.length}x).`,
          suggestion: `Remove duplicate pinMode(${pin}, ${uniqueModes[0]}) calls.`,
          line,
        });
      }
    }
 
    return messages;
  }
 
  /**
   * Check for OUTPUT pins being read with digitalRead()
   */
  checkOutputPinsReadAsInput(
    uncommentedCode: string,
    outputPins: Set<number>,
    parsePinNumber: (pin: string) => number | undefined,
  ): ParserMessage[] {
    const messages: ParserMessage[] = [];
 
    if (outputPins.size > 0) {
      const digitalReadLiteralRe = PIN_PATTERNS.DIGITAL_READ_LITERAL;
      const outputReadWarnedPins = new Set<number>();
      let match;
      while ((match = digitalReadLiteralRe.exec(uncommentedCode)) !== null) {
        const pinNum = parsePinNumber(match[1]);
        if (
          pinNum !== undefined &&
          outputPins.has(pinNum) &&
          !outputReadWarnedPins.has(pinNum)
        ) {
          outputReadWarnedPins.add(pinNum);
          const pinStr = pinNum >= 14 ? `A${pinNum - 14}` : `${pinNum}`;
          const line = uncommentedCode.slice(0, Math.max(0, match.index)).split("\n").length;
          messages.push({
            id: randomUUID(),
            type: "warning",
            category: "pins",
            severity: 2,
            message: `Pin ${pinStr} is configured as OUTPUT but read with digitalRead(). Reading an OUTPUT pin may return unexpected values.`,
            suggestion: `If you need to read the pin, use pinMode(${pinStr}, INPUT) or INPUT_PULLUP instead.`,
            line,
          });
        }
      }
    }
 
    return messages;
  }
}