Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add detection for more model errors #485

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,43 @@ export class Config implements ConfigInterface {
if (equalIndex === -1) {
throw new Error(`parse the content error : line ${lineNum}`);
}
const key = line.substring(0, equalIndex);
const value = line.substring(equalIndex + 1);
this.addConfig(section, key.trim(), value.trim());
const key = line.substring(0, equalIndex).trim();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract all grammar checking code into a "validator"

const value = line.substring(equalIndex + 1).trim();

if (section === 'matchers') {
this.validateMatcher(value, lineNum);
}

this.addConfig(section, key, value);
}

private validateMatcher(matcherStr: string, lineNumber: number): void {
const errors: string[] = [];

const validProps = ['r.sub', 'r.obj', 'r.act', 'r.dom', 'p.sub', 'p.obj', 'p.act', 'p.dom', 'p.eft', 'p.sub_rule'];
const usedProps = matcherStr.match(/[rp]\.\w+/g) || [];
const invalidProps = usedProps.filter((prop) => !validProps.includes(prop));
if (invalidProps.length > 0) {
errors.push(`Invalid properties: ${invalidProps.join(', ')}`);
}

if (matcherStr.includes('..')) {
errors.push('Found extra dots');
}

if (matcherStr.trim().endsWith(',')) {
errors.push('Unnecessary comma');
}

const openBrackets = (matcherStr.match(/\(/g) || []).length;
const closeBrackets = (matcherStr.match(/\)/g) || []).length;
if (openBrackets !== closeBrackets) {
errors.push('Mismatched parentheses');
}

if (errors.length > 0) {
throw new Error(`${errors.join(', ')}`);
}
}

public getBool(key: string): boolean {
Expand Down
Loading