All files / shared/parsers pin-conflicts-parser.ts

100% Statements 20/20
75% Branches 6/8
100% Functions 2/2
100% Lines 18/18

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                                72x     72x 72x     72x 144x 144x 51x 51x       72x 72x 72x 19x 19x     72x 30x 16x 16x                   72x      
import type { ParserMessage } from "../schema";
import { randomUUID } from "node:crypto";
import {
  PIN_PATTERNS,
  parsePinNumber,
} from "@shared/parser-patterns";
 
/**
 * Parser for pin conflicts (same pin used as digital and analog)
 * 
 * Rules:
 * 1. Digital + analog on same pin
 * 2. Multiple conflicts
 * 3. Numeric pin notation
 */
export class PinConflictsParser {
  constructor(private readonly code: string) {}
 
  parse(): ParserMessage[] {
    const messages: ParserMessage[] = [];
    const digitalPins = new Set<number>();
    let match;
 
    for (const re of [PIN_PATTERNS.WRITE_READ_PIN, PIN_PATTERNS.WRITE_READ_DIO]) {
      re.lastIndex = 0;
      while ((match = re.exec(this.code)) !== null) {
        const pin = parsePinNumber(match[1]);
        Eif (pin !== undefined) digitalPins.add(pin);
      }
    }
 
    const analogPins = new Set<number>();
    const analogRegex = PIN_PATTERNS.ANALOG_READ_WRITE;
    while ((match = analogRegex.exec(this.code)) !== null) {
      const pin = parsePinNumber(match[1]);
      Eif (pin !== undefined) analogPins.add(pin);
    }
 
    for (const pin of digitalPins) {
      if (analogPins.has(pin)) {
        const pinStr = pin >= 14 ? `A${pin - 14}` : `${pin}`;
        messages.push({
          id: randomUUID(),
          type: "warning",
          category: "hardware",
          severity: 2,
          message: `Pin ${pinStr} used as both digital and analog. This may be unintended.`,
          suggestion: `// Use separate pins for digital and analog`,
        });
      }
    }
    return messages;
  }
}