diff --git a/tools/bs-plugins/validation-plugin.ts b/tools/bs-plugins/validation-plugin.ts index 05a80acd..fc97c41c 100644 --- a/tools/bs-plugins/validation-plugin.ts +++ b/tools/bs-plugins/validation-plugin.ts @@ -15,6 +15,7 @@ type Validation = { regex: string; regexFlags?: string; message: string; + regexObject: RegExp | undefined; } export class ValidationPlugin implements CompilerPlugin { @@ -25,6 +26,11 @@ export class ValidationPlugin implements CompilerPlugin { beforeProgramValidate(program: Program) { // @ts-ignore this.validation = program.options.validation as Validation[] | undefined; + if (this.validation) { + this.validation.forEach((validation) => { + validation.regexObject = new RegExp(validation.regex, validation.regexFlags); + }); + } } onFileValidate(event: OnFileValidateEvent) { @@ -34,14 +40,27 @@ export class ValidationPlugin implements CompilerPlugin { return; } + let fileContentsLines: string[] | undefined = undefined; + this.validation.forEach((validation) => { - const regex = new RegExp(validation.regex, validation.regexFlags); + const regex = validation.regexObject; const fileContents = file.fileContents; let match: RegExpExecArray | null; while ((match = regex.exec(fileContents)) !== null) { + if (!fileContentsLines) { + fileContentsLines = fileContents.split('\n'); + } + const line = fileContents.substring(0, match.index).split('\n').length - 1; const column = match.index - fileContents.lastIndexOf('\n', match.index) - 1; + + const lineContent = fileContentsLines[line]; + // Skip if the line is a comment + if (lineContent.trim().startsWith("'")) { + continue; + } + file.diagnostics.push({ code: validation.code, message: validation.message,