Skip to content

Commit

Permalink
#69 Change issue args format again so that it's friendly to both inde…
Browse files Browse the repository at this point in the history
…xed and by-name interpolation
  • Loading branch information
alessiostalla committed Jun 12, 2024
1 parent 212b016 commit f0acb6c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/parsing/tylasu-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ export abstract class TylasuParser<
issues.push(Issue.syntactic(message, IssueSeverity.ERROR, Position.ofParseTree(it),
undefined,
ERROR_NODE_FOUND,
{
type: it.symbol?.type?.toString() || "",
text: it.symbol?.text || ""
}));
[
{ name: "type", value: it.symbol?.type?.toString() || "" },
{ name: "text", value: it.symbol?.text || "" },
]));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/transformation/transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class ASTTransformer {
origin?.position,
origin instanceof Node ? origin : undefined,
SOURCE_NODE_NOT_MAPPED,
{ nodeType }
[{ name: "nodeType", value: nodeType }]
)
);
} else {
Expand Down
13 changes: 9 additions & 4 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export enum IssueType { LEXICAL, SYNTACTIC, SEMANTIC}

export enum IssueSeverity { ERROR, WARNING, INFO}

export interface IssueArg {
name: string;
value: string;
}

export class Issue {

constructor(
Expand All @@ -14,25 +19,25 @@ export class Issue {
public readonly position?: Position,
public readonly node?: Node,
public readonly code?: string,
public readonly args: { [key: string]: string } = {}
public readonly args: IssueArg[] = []
) {
if (!position) {
this.position = node?.position;
}
}

static lexical(message: string, severity: IssueSeverity = IssueSeverity.ERROR, position?: Position,
node?: Node, code?: string, args: { [key: string]: string } = {}): Issue {
node?: Node, code?: string, args: IssueArg[] = []): Issue {
return new Issue(IssueType.LEXICAL, message, severity, position, node, code, args);
}

static syntactic(message: string, severity: IssueSeverity = IssueSeverity.ERROR, position?: Position,
node?: Node, code?: string, args: { [key: string]: string } = {}): Issue {
node?: Node, code?: string, args: IssueArg[] = []): Issue {
return new Issue(IssueType.SYNTACTIC, message, severity, position, node, code, args);
}

static semantic(message: string, severity: IssueSeverity = IssueSeverity.ERROR, position?: Position,
node?: Node, code?: string, args: { [key: string]: string } = {}): Issue {
node?: Node, code?: string, args: IssueArg[] = []): Issue {
return new Issue(IssueType.SEMANTIC, message, severity, position, node, code, args);
}
}
4 changes: 2 additions & 2 deletions tests/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Issues', function() {
let issue = Issue.syntactic("Unexpected token: foo", IssueSeverity.ERROR, undefined, undefined, SYNTAX_ERROR);
expect(i18next.t(issue.code!)).to.equal("A syntax error occurred!");
issue = Issue.semantic("Node not mapped: SomeNode", IssueSeverity.ERROR, undefined, undefined,
SOURCE_NODE_NOT_MAPPED, { nodeType: "SomeNode" });
expect(i18next.t(issue.code!, { type: issue.args.nodeType })).to.equal("Source node not mapped: SomeNode");
SOURCE_NODE_NOT_MAPPED, [{ name: "nodeType", value: "SomeNode" }]);
expect(i18next.t(issue.code!, { type: issue.args[0].value })).to.equal("Source node not mapped: SomeNode");
});
});

0 comments on commit f0acb6c

Please sign in to comment.