Improve validation plugin

This commit is contained in:
Brahim Hadriche committed 2024-12-12 10:18:50 -05:00
1 parent 0c3f8cea2a
commit dd2262ec28
1 file changed
+20 -1
+20 -1
View File
@@ -15,6 +15,7 @@ type Validation = {
regex: string; regex: string;
regexFlags?: string; regexFlags?: string;
message: string; message: string;
regexObject: RegExp | undefined;
} }
export class ValidationPlugin implements CompilerPlugin { export class ValidationPlugin implements CompilerPlugin {
@@ -25,6 +26,11 @@ export class ValidationPlugin implements CompilerPlugin {
beforeProgramValidate(program: Program) { beforeProgramValidate(program: Program) {
// @ts-ignore // @ts-ignore
this.validation = program.options.validation as Validation[] | undefined; 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<BscFile>) { onFileValidate(event: OnFileValidateEvent<BscFile>) {
@@ -34,14 +40,27 @@ export class ValidationPlugin implements CompilerPlugin {
return; return;
} }
let fileContentsLines: string[] | undefined = undefined;
this.validation.forEach((validation) => { this.validation.forEach((validation) => {
const regex = new RegExp(validation.regex, validation.regexFlags); const regex = validation.regexObject;
const fileContents = file.fileContents; const fileContents = file.fileContents;
let match: RegExpExecArray | null; let match: RegExpExecArray | null;
while ((match = regex.exec(fileContents)) !== 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 line = fileContents.substring(0, match.index).split('\n').length - 1;
const column = match.index - fileContents.lastIndexOf('\n', match.index) - 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({ file.diagnostics.push({
code: validation.code, code: validation.code,
message: validation.message, message: validation.message,