Merge pull request #186 from github/joshmgross/lint-parser
Lint the workflow parser package
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||||
import {nullTrace} from "../test-utils/null-trace";
|
import {nullTrace} from "../test-utils/null-trace";
|
||||||
import {parseWorkflow} from "../workflows/workflow-parser";
|
import {parseWorkflow} from "../workflows/workflow-parser";
|
||||||
import {convertWorkflowTemplate, ErrorPolicy} from "./convert";
|
import {convertWorkflowTemplate, ErrorPolicy} from "./convert";
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export class JSONObjectReader implements ObjectReader {
|
|||||||
|
|
||||||
public constructor(fileId: number | undefined, input: string) {
|
public constructor(fileId: number | undefined, input: string) {
|
||||||
this._fileId = fileId;
|
this._fileId = fileId;
|
||||||
const value = JSON.parse(input);
|
const value: unknown = JSON.parse(input);
|
||||||
this._generator = this.getParseEvents(value, true);
|
this._generator = this.getParseEvents(value, true);
|
||||||
this._current = this._generator.next();
|
this._current = this._generator.next();
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ export class JSONObjectReader implements ObjectReader {
|
|||||||
|
|
||||||
public validateEnd(): void {
|
public validateEnd(): void {
|
||||||
if (!this._current.done) {
|
if (!this._current.done) {
|
||||||
const parseEvent = this._current.value as ParseEvent;
|
const parseEvent = this._current.value;
|
||||||
if (parseEvent.type === EventType.DocumentEnd) {
|
if (parseEvent.type === EventType.DocumentEnd) {
|
||||||
this._current = this._generator.next();
|
this._current = this._generator.next();
|
||||||
return;
|
return;
|
||||||
@@ -88,7 +88,7 @@ export class JSONObjectReader implements ObjectReader {
|
|||||||
|
|
||||||
public validateStart(): void {
|
public validateStart(): void {
|
||||||
if (!this._current.done) {
|
if (!this._current.done) {
|
||||||
const parseEvent = this._current.value as ParseEvent;
|
const parseEvent = this._current.value;
|
||||||
if (parseEvent.type === EventType.DocumentStart) {
|
if (parseEvent.type === EventType.DocumentStart) {
|
||||||
this._current = this._generator.next();
|
this._current = this._generator.next();
|
||||||
return;
|
return;
|
||||||
@@ -101,7 +101,7 @@ export class JSONObjectReader implements ObjectReader {
|
|||||||
/**
|
/**
|
||||||
* Returns all tokens (depth first)
|
* Returns all tokens (depth first)
|
||||||
*/
|
*/
|
||||||
private *getParseEvents(value: any, root?: boolean): Generator<ParseEvent, void> {
|
private *getParseEvents(value: unknown, root?: boolean): Generator<ParseEvent, void> {
|
||||||
if (root) {
|
if (root) {
|
||||||
yield new ParseEvent(EventType.DocumentStart, undefined);
|
yield new ParseEvent(EventType.DocumentStart, undefined);
|
||||||
}
|
}
|
||||||
@@ -124,9 +124,9 @@ export class JSONObjectReader implements ObjectReader {
|
|||||||
yield new ParseEvent(EventType.Literal, new NullToken(this._fileId, undefined, undefined));
|
yield new ParseEvent(EventType.Literal, new NullToken(this._fileId, undefined, undefined));
|
||||||
}
|
}
|
||||||
// array
|
// array
|
||||||
else if (Object.prototype.hasOwnProperty.call(value, "length")) {
|
else if (Array.isArray(value)) {
|
||||||
yield new ParseEvent(EventType.SequenceStart, new SequenceToken(this._fileId, undefined, undefined));
|
yield new ParseEvent(EventType.SequenceStart, new SequenceToken(this._fileId, undefined, undefined));
|
||||||
for (const item of value as []) {
|
for (const item of value) {
|
||||||
for (const e of this.getParseEvents(item)) {
|
for (const e of this.getParseEvents(item)) {
|
||||||
yield e;
|
yield e;
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,7 @@ export class JSONObjectReader implements ObjectReader {
|
|||||||
yield new ParseEvent(EventType.MappingStart, new MappingToken(this._fileId, undefined, undefined));
|
yield new ParseEvent(EventType.MappingStart, new MappingToken(this._fileId, undefined, undefined));
|
||||||
for (const key of Object.keys(value)) {
|
for (const key of Object.keys(value)) {
|
||||||
yield new ParseEvent(EventType.Literal, new StringToken(this._fileId, undefined, key, undefined));
|
yield new ParseEvent(EventType.Literal, new StringToken(this._fileId, undefined, key, undefined));
|
||||||
for (const e of this.getParseEvents(value[key])) {
|
for (const e of this.getParseEvents(value[key as keyof typeof value])) {
|
||||||
yield e;
|
yield e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export class TemplateContext {
|
|||||||
public readonly errors: TemplateValidationErrors;
|
public readonly errors: TemplateValidationErrors;
|
||||||
public readonly schema: TemplateSchema;
|
public readonly schema: TemplateSchema;
|
||||||
public readonly trace: TraceWriter;
|
public readonly trace: TraceWriter;
|
||||||
public readonly state: {[key: string]: any} = {};
|
public readonly state: {[key: string]: unknown} = {};
|
||||||
|
|
||||||
public constructor(errors: TemplateValidationErrors, schema: TemplateSchema, trace: TraceWriter) {
|
public constructor(errors: TemplateValidationErrors, schema: TemplateSchema, trace: TraceWriter) {
|
||||||
this.errors = errors;
|
this.errors = errors;
|
||||||
@@ -47,7 +47,7 @@ export class TemplateContext {
|
|||||||
const token = tokenOrFileId as TemplateToken | undefined;
|
const token = tokenOrFileId as TemplateToken | undefined;
|
||||||
const range = tokenRange || token?.range;
|
const range = tokenRange || token?.range;
|
||||||
const prefix = this.getErrorPrefix(token?.file ?? (tokenOrFileId as number | undefined), token?.line, token?.col);
|
const prefix = this.getErrorPrefix(token?.file ?? (tokenOrFileId as number | undefined), token?.line, token?.col);
|
||||||
const message = (err as Error | undefined)?.message ?? `${err}`;
|
const message = (err as Error | undefined)?.message ?? String(err);
|
||||||
|
|
||||||
const e = new TemplateValidationError(message, prefix, undefined, range);
|
const e = new TemplateValidationError(message, prefix, undefined, range);
|
||||||
this.errors.add(e);
|
this.errors.add(e);
|
||||||
@@ -84,7 +84,7 @@ export class TemplateContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getErrorPrefix(fileId?: number, line?: number, column?: number): string {
|
private getErrorPrefix(fileId?: number, line?: number, column?: number): string {
|
||||||
const fileName = fileId !== undefined ? this.getFileName(fileId as number) : undefined;
|
const fileName = fileId !== undefined ? this.getFileName(fileId) : undefined;
|
||||||
if (fileName) {
|
if (fileName) {
|
||||||
if (line !== undefined && column !== undefined) {
|
if (line !== undefined && column !== undefined) {
|
||||||
return `${fileName} (Line: ${line}, Col: ${column})`;
|
return `${fileName} (Line: ${line}, Col: ${column})`;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// template-reader *just* does schema validation
|
// template-reader *just* does schema validation
|
||||||
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||||
|
|
||||||
import {ObjectReader} from "./object-reader";
|
import {ObjectReader} from "./object-reader";
|
||||||
import {TemplateSchema} from "./schema";
|
import {TemplateSchema} from "./schema";
|
||||||
|
|||||||
@@ -7,9 +7,15 @@ export interface TraceWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class NoOperationTraceWriter implements TraceWriter {
|
export class NoOperationTraceWriter implements TraceWriter {
|
||||||
public error(message: string): void {}
|
public error(): void {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
public info(message: string): void {}
|
public info(): void {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
public verbose(message: string): void {}
|
public verbose(): void {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
import {TraceWriter} from "../templates/trace-writer";
|
import {NoOperationTraceWriter} from "../templates/trace-writer";
|
||||||
|
|
||||||
export const nullTrace: TraceWriter = {
|
export const nullTrace = new NoOperationTraceWriter();
|
||||||
info: x => {},
|
|
||||||
verbose: x => {},
|
|
||||||
error: x => {}
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import type {LinePos} from "yaml/dist/errors";
|
|||||||
import type {NodeBase} from "yaml/dist/nodes/Node";
|
import type {NodeBase} from "yaml/dist/nodes/Node";
|
||||||
import {ObjectReader} from "../templates/object-reader";
|
import {ObjectReader} from "../templates/object-reader";
|
||||||
import {EventType, ParseEvent} from "../templates/parse-event";
|
import {EventType, ParseEvent} from "../templates/parse-event";
|
||||||
import {TemplateContext} from "../templates/template-context";
|
|
||||||
import {
|
import {
|
||||||
BooleanToken,
|
BooleanToken,
|
||||||
LiteralToken,
|
LiteralToken,
|
||||||
@@ -72,7 +71,7 @@ export class YamlObjectReader implements ObjectReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isScalar(node)) {
|
if (isScalar(node)) {
|
||||||
yield new ParseEvent(EventType.Literal, YamlObjectReader.getLiteralToken(this.fileId, range, node as Scalar));
|
yield new ParseEvent(EventType.Literal, YamlObjectReader.getLiteralToken(this.fileId, range, node));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPair(node)) {
|
if (isPair(node)) {
|
||||||
@@ -191,7 +190,7 @@ export class YamlObjectReader implements ObjectReader {
|
|||||||
|
|
||||||
public validateEnd(): void {
|
public validateEnd(): void {
|
||||||
if (!this._current.done) {
|
if (!this._current.done) {
|
||||||
const parseEvent = this._current.value as ParseEvent;
|
const parseEvent = this._current.value;
|
||||||
if (parseEvent.type === EventType.DocumentEnd) {
|
if (parseEvent.type === EventType.DocumentEnd) {
|
||||||
this._current = this._generator.next();
|
this._current = this._generator.next();
|
||||||
return;
|
return;
|
||||||
@@ -207,7 +206,7 @@ export class YamlObjectReader implements ObjectReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this._current.done) {
|
if (!this._current.done) {
|
||||||
const parseEvent = this._current.value as ParseEvent;
|
const parseEvent = this._current.value;
|
||||||
if (parseEvent.type === EventType.DocumentStart) {
|
if (parseEvent.type === EventType.DocumentStart) {
|
||||||
this._current = this._generator.next();
|
this._current = this._generator.next();
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user