Commit f0089c68 authored by David Sveningsson's avatar David Sveningsson

fix(htmlvalidate): more verbose output from `--dump-source`

parent 3db6edc8
Pipeline #101516604 passed with stages
in 9 minutes and 10 seconds
......@@ -358,6 +358,9 @@ describe("config", () => {
"line": 1,
"offset": 0,
"originalData": "original data",
"transformedBy": Array [
"mock-transform",
],
},
]
`);
......@@ -413,6 +416,9 @@ describe("config", () => {
"filename": "foo.unnamed",
"line": 2,
"offset": 4,
"transformedBy": Array [
"mock-plugin-unnamed",
],
},
]
`);
......@@ -429,6 +435,9 @@ describe("config", () => {
"filename": "bar.named",
"line": 2,
"offset": 4,
"transformedBy": Array [
"mock-plugin-named:foobar",
],
},
]
`);
......@@ -446,6 +455,9 @@ describe("config", () => {
"line": 1,
"offset": 0,
"originalData": "original data",
"transformedBy": Array [
"mock-transform",
],
},
]
`);
......@@ -501,6 +513,10 @@ describe("config", () => {
"line": 1,
"offset": 0,
"originalData": "original data",
"transformedBy": Array [
"mock-transform",
"mock-transform-chain-foo",
],
},
]
`);
......@@ -524,6 +540,10 @@ describe("config", () => {
"line": 1,
"offset": 0,
"originalData": "original data",
"transformedBy": Array [
"mock-transform",
"mock-transform-optional-chain",
],
},
]
`);
......@@ -547,6 +567,9 @@ describe("config", () => {
"line": 1,
"offset": 0,
"originalData": "original data",
"transformedBy": Array [
"<rootDir>/src/transform/__mocks__/mock-transform",
],
},
]
`);
......
......@@ -14,6 +14,7 @@ import { parseSeverity, Severity } from "./severity";
interface TransformerEntry {
pattern: RegExp;
name: string;
fn: Transformer;
}
......@@ -367,7 +368,16 @@ export class Config {
};
if (transformer) {
try {
return Array.from(transformer.fn.call(context, source));
return Array.from(
transformer.fn.call(context, source),
(cur: Source) => {
/* keep track of which transformers that has been run on this source
* by appending this entry to the transformedBy array */
cur.transformedBy = cur.transformedBy || [];
cur.transformedBy.push(transformer.name);
return cur;
}
);
} catch (err) {
throw new NestedError(
`When transforming "${source.filename}": ${err.message}`,
......@@ -423,6 +433,7 @@ export class Config {
// eslint-disable-next-line security/detect-non-literal-regexp
pattern: new RegExp(pattern),
name,
fn,
};
} catch (err) {
......
......@@ -76,4 +76,11 @@ export interface Source {
* Hooks for processing the source as it is being parsed.
*/
hooks?: SourceHooks;
/**
* Internal property to keep track of what transformers has run on this
* source. Entries are in reverse-order, e.g. the last applied transform is
* first.
*/
transformedBy?: string[];
}
......@@ -289,13 +289,14 @@ describe("HtmlValidate", () => {
const filename = "foo.html";
const config = Config.empty();
config.init();
config.transformFilename = jest.fn((filename: string) => [
config.transformFilename = jest.fn((filename: string): Source[] => [
{
data: `first markup`,
filename,
line: 1,
column: 1,
offset: 0,
transformedBy: ["bar", "foo"],
},
{
data: `second markup`,
......@@ -303,22 +304,30 @@ describe("HtmlValidate", () => {
line: 5,
column: 3,
offset: 29,
hooks: {
processElement: () => null,
},
},
]);
jest.spyOn(htmlvalidate, "getConfigFor").mockImplementation(() => config);
const output = htmlvalidate.dumpSource(filename);
expect(output).toMatchInlineSnapshot(`
Array [
"Source [email protected]:1",
"---",
"first markup",
"---",
"Source [email protected]:3",
"---",
"second markup",
"---",
]
`);
Array [
"Source [email protected]:1 (offset: 0)",
"Transformed by:",
" - foo",
" - bar",
"---",
"first markup",
"---",
"Source [email protected]:3 (offset: 29)",
"Hooks",
" - processElement",
"---",
"second markup",
"---",
]
`);
});
it("getRuleDocumentation() should delegate call to engine", () => {
......
......@@ -149,7 +149,23 @@ class HtmlValidate {
const config = this.getConfigFor(filename);
const sources = config.transformFilename(filename);
return sources.reduce((result: string[], source: Source) => {
result.push(`Source ${source.filename}@${source.line}:${source.column}`);
result.push(
`Source ${source.filename}@${source.line}:${source.column} (offset: ${source.offset})`
);
if (source.transformedBy) {
result.push("Transformed by:");
result = result.concat(
source.transformedBy.reverse().map(name => ` - ${name}`)
);
}
if (source.hooks) {
result.push("Hooks");
for (const [key, present] of Object.entries(source.hooks)) {
if (present) {
result.push(` - ${key}`);
}
}
}
result.push("---");
result = result.concat(source.data.split("\n"));
result.push("---");
......
......@@ -325,6 +325,9 @@ describe("Plugin", () => {
"line": 2,
"offset": 4,
"originalData": "original data",
"transformedBy": Array [
"mock-plugin",
],
},
]
`);
......@@ -371,6 +374,9 @@ describe("Plugin", () => {
"line": 2,
"offset": 4,
"originalData": "original data",
"transformedBy": Array [
"mock-plugin:foobar",
],
},
]
`);
......
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