Ensure existing context errors don't prevent parsing workflows
This commit is contained in:
@@ -20,9 +20,12 @@ export function parseWorkflow(entryFile: File, contextOrTrace: TraceWriter | Tem
|
||||
: new TemplateContext(new TemplateValidationErrors(), getWorkflowSchema(), contextOrTrace);
|
||||
|
||||
const fileId = context.getFileId(entryFile.name);
|
||||
const reader = new YamlObjectReader(context, fileId, entryFile.content);
|
||||
if (context.errors.count > 0) {
|
||||
const reader = new YamlObjectReader(fileId, entryFile.content);
|
||||
if (reader.errors.length > 0) {
|
||||
// The file is not valid YAML, template errors could be misleading
|
||||
for (const err of reader.errors) {
|
||||
context.error(fileId, err.message, err.range);
|
||||
}
|
||||
return {
|
||||
context,
|
||||
value: undefined
|
||||
|
||||
@@ -68,11 +68,11 @@ it("YAML errors include range information", () => {
|
||||
|
||||
const context = new TemplateContext(new TemplateValidationErrors(), getWorkflowSchema(), nullTrace);
|
||||
const fileId = context.getFileId("test.yaml");
|
||||
new YamlObjectReader(context, fileId, content);
|
||||
const reader = new YamlObjectReader(fileId, content);
|
||||
|
||||
expect(context.errors.count).toBe(1);
|
||||
expect(reader.errors.length).toBe(1);
|
||||
|
||||
const error = context.errors.getErrors()[0];
|
||||
const error = reader.errors[0];
|
||||
expect(error.range).toEqual({
|
||||
start: {line: 7, column: 38},
|
||||
end: {line: 7, column: 63}
|
||||
|
||||
@@ -15,20 +15,27 @@ import {
|
||||
} from "../templates/tokens/index";
|
||||
import {Position, TokenRange} from "../templates/tokens/token-range";
|
||||
|
||||
export type YamlError = {
|
||||
message: string;
|
||||
range: TokenRange | undefined;
|
||||
};
|
||||
|
||||
export class YamlObjectReader implements ObjectReader {
|
||||
private readonly _generator: Generator<ParseEvent>;
|
||||
private _current!: IteratorResult<ParseEvent>;
|
||||
private fileId?: number;
|
||||
private lineCounter = new LineCounter();
|
||||
|
||||
constructor(context: TemplateContext, fileId: number | undefined, content: string) {
|
||||
public errors: YamlError[] = [];
|
||||
|
||||
constructor(fileId: number | undefined, content: string) {
|
||||
const doc = parseDocument(content, {
|
||||
lineCounter: this.lineCounter,
|
||||
keepSourceTokens: true,
|
||||
uniqueKeys: false // Uniqueness is validated by the template reader
|
||||
});
|
||||
for (const err of doc.errors) {
|
||||
context.error(fileId, err.message, rangeFromLinePos(err.linePos));
|
||||
this.errors.push({message: err.message, range: rangeFromLinePos(err.linePos)});
|
||||
}
|
||||
this._generator = this.getNodes(doc);
|
||||
this.fileId = fileId;
|
||||
|
||||
Reference in New Issue
Block a user