UNPKG

1.5 kBPlain TextView Raw
1import { ParserErrors as IParserErrors, ParserErrorType } from './types';
2
3export class ParserErrors implements IParserErrors {
4 private _errors: Map<ParserErrorType, string[]> = new Map();
5
6 public get commandErrors(): string[] {
7 return this.getErrors('command');
8 }
9
10 public get argumentErrors(): string[] {
11 return this.getErrors('argument');
12 }
13
14 public get optionErrors(): string[] {
15 return this.getErrors('option');
16 }
17
18 public get allErrors(): string[] {
19 return [...this.commandErrors, ...this.argumentErrors, ...this.optionErrors];
20 }
21
22 public getErrors = (type?: ParserErrorType) => {
23 if (type) {
24 if (!this._errors.has(type)) {
25 this._errors.set(type, []);
26 }
27 return this._errors.get(type);
28 } else {
29 return this.allErrors;
30 }
31 };
32
33 public clearErrors = (type?: ParserErrorType) => {
34 if (type) {
35 this._errors.set(type, []);
36 } else {
37 this._errors.clear();
38 }
39 };
40
41 public push = (type: ParserErrorType, message: string) => {
42 const errs = this.getErrors(type);
43 errs.push(message);
44 this._errors.set(type, errs);
45 };
46
47 public get hasErrors(): boolean {
48 return this.allErrors.length > 0;
49 }
50
51 public get hasNonArgOrOptionErrors(): boolean {
52 return this.commandErrors.length > 0;
53 }
54}