Commit 4d32a0d6 authored by David Sveningsson's avatar David Sveningsson

feat(cli): add `--dump-source` to debug transformers

parent d723e04b
Pipeline #92958359 passed with stages
in 7 minutes and 23 seconds
......@@ -16,6 +16,7 @@ enum Mode {
DUMP_EVENTS,
DUMP_TOKENS,
DUMP_TREE,
DUMP_SOURCE,
PRINT_CONFIG,
}
......@@ -28,6 +29,10 @@ function getMode(argv: { [key: string]: any }): Mode {
return Mode.DUMP_EVENTS;
}
if (argv["dump-source"]) {
return Mode.DUMP_SOURCE;
}
if (argv["dump-tokens"]) {
return Mode.DUMP_TOKENS;
}
......@@ -74,6 +79,11 @@ function dump(files: string[], mode: Mode): string {
case Mode.DUMP_TREE:
lines = files.map((filename: string) => htmlvalidate.dumpTree(filename));
break;
case Mode.DUMP_SOURCE:
lines = files.map((filename: string) =>
htmlvalidate.dumpSource(filename)
);
break;
default:
throw new Error(`Unknown mode "${mode}"`);
}
......@@ -104,6 +114,7 @@ const argv: minimist.ParsedArgs = minimist(process.argv.slice(2), {
boolean: [
"init",
"dump-events",
"dump-source",
"dump-tokens",
"dump-tree",
"h",
......@@ -142,11 +153,6 @@ Common options:
--stdin process markup from stdin.
--stdin-filename=STRING specify filename to report when using stdin
Debugging options:
--dump-events output events during parsing.
--dump-tokens output tokens from lexing stage.
--dump-tree output nodes from the dom tree.
Miscellaneous:
-c, --config=STRING use custom configuration file.
--init initialize project with a new configuration
......@@ -154,6 +160,12 @@ Miscellaneous:
-h, --help show help.
--version show version.
Debugging options:
--dump-events output events during parsing.
--dump-source output post-transformed source data.
--dump-tokens output tokens from lexing stage.
--dump-tree output nodes from the dom tree.
Formatters:
Multiple formatters can be specified with a comma-separated list,
......
......@@ -277,6 +277,41 @@ describe("HtmlValidate", () => {
]);
});
it("dumpSources() should dump sources", () => {
const htmlvalidate = new HtmlValidate();
const filename = "foo.html";
const config = Config.empty();
config.init();
config.transform = jest.fn((filename: string) => [
{
column: 1,
data: `first markup`,
filename,
line: 1,
},
{
column: 3,
data: `second markup`,
filename,
line: 5,
},
]);
jest.spyOn(htmlvalidate, "getConfigFor").mockImplementation(() => config);
const output = htmlvalidate.dumpSource(filename);
expect(output).toMatchInlineSnapshot(`
Array [
"Source foo.html@1:1",
"---",
"first markup",
"---",
"Source foo.html@5:3",
"---",
"second markup",
"---",
]
`);
});
it("getRuleDocumentation() should delegate call to engine", () => {
const htmlvalidate = new HtmlValidate();
const config = Config.empty();
......
......@@ -135,6 +135,31 @@ class HtmlValidate {
return engine.dumpTree(source);
}
/**
* Transform filename and output source data.
*
* Using CLI this is enabled with `--dump-source`. Mostly useful for
* debugging.
*
* @param filename - Filename to dump source from.
*/
public dumpSource(filename: string): string[] {
const config = this.getConfigFor(filename);
const sources = config.transform(filename);
return sources.reduce(
(result: string[], source: Source) => {
result.push(
`Source ${source.filename}@${source.line}:${source.column}`
);
result.push("---");
result = result.concat(source.data.split("\n"));
result.push("---");
return result;
},
[] as string[]
);
}
/**
* Get contextual documentation for the given rule.
*
......
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