Commit 20929425 authored by David Sveningsson's avatar David Sveningsson

fix(lexer): handle missing `Source` properties (like `offset`)

the new `offset` field must not be required as there isn't any transformer
setting it yet so it causes breakage.
parent d668146b
Pipeline #99863757 passed with stages
in 8 minutes and 39 seconds
......@@ -18,10 +18,10 @@ export class Context {
public constructor(source: Source) {
this.state = undefined;
this.string = source.data;
this.filename = source.filename;
this.offset = source.offset;
this.line = source.line;
this.column = source.column;
this.filename = source.filename ?? "";
this.offset = source.offset ?? 0;
this.line = source.line ?? 1;
this.column = source.column ?? 1;
this.contentModel = ContentModel.TEXT;
}
......
......@@ -109,3 +109,36 @@ it("should compute correct line, column and offset when using transformed source
}
`);
});
it("should handle source missing properties", () => {
expect.assertions(2);
const source = {
data: "<p>lorem ipsum</i>",
};
const htmlvalidate = new HtmlValidate({
root: true,
extends: ["htmlvalidate:recommended"],
});
const report = htmlvalidate.validateSource(source as Source);
expect(report).toBeInvalid();
expect(report.results[0]).toMatchInlineSnapshot(`
Object {
"errorCount": 1,
"filePath": "",
"messages": Array [
Object {
"column": 16,
"context": undefined,
"line": 1,
"message": "Mismatched close-tag, expected '</p>' but found '</i>'.",
"offset": 15,
"ruleId": "close-order",
"severity": 2,
"size": 2,
},
],
"source": "<p>lorem ipsum</i>",
"warningCount": 0,
}
`);
});
......@@ -61,10 +61,10 @@ export class Parser {
/* reset DOM in case there are multiple calls in the same session */
this.dom = new DOMTree({
filename: source.filename,
offset: source.offset,
line: source.line,
column: source.column,
filename: source.filename ?? "",
offset: source.offset ?? 0,
line: source.line ?? 1,
column: source.column ?? 1,
});
/* trigger any rules waiting for DOM load event */
......
......@@ -135,7 +135,7 @@ export class Reporter {
results: Object.keys(this.result).map(filePath => {
const messages = [].concat(this.result[filePath]).sort(messageSort);
const source = (sources || []).find(
(source: Source) => source.filename === filePath
(source: Source) => filePath === (source.filename ?? "")
);
return {
filePath,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment