Commit f5230285 authored by David Sveningsson's avatar David Sveningsson Committed by David Sveningsson

feat(htmlvalidate): new method `canValidate` to test if a file can be validated

parent 1d874544
Pipeline #101550089 passed with stages
in 9 minutes and 26 seconds
......@@ -646,6 +646,27 @@ describe("config", () => {
});
});
describe("canTransform()", () => {
let config: Config;
beforeEach(() => {
config = Config.fromObject({
transform: {
"^.*\\.foo$": "mock-transform",
},
});
config.init();
});
it("should return true if a transformer can handle the file", () => {
expect(config.canTransform("my-file.foo")).toBeTruthy();
});
it("should return false if no transformer can handle the file", () => {
expect(config.canTransform("my-file.bar")).toBeFalsy();
});
});
describe("init()", () => {
it("should handle being called multiple times", () => {
expect.assertions(1);
......
......@@ -410,6 +410,14 @@ export class Config {
return this.transformSource(source);
}
/**
* Returns true if a transformer matches given filename.
*/
public canTransform(filename: string): boolean {
const entry = this.findTransformer(filename);
return !!entry;
}
private findTransformer(filename: string): TransformerEntry | null {
return this.transformers.find((entry: TransformerEntry) =>
entry.pattern.test(filename)
......
......@@ -236,6 +236,31 @@ describe("HtmlValidate", () => {
});
});
describe("canValidate()", () => {
let htmlvalidate: HtmlValidate;
beforeEach(() => {
htmlvalidate = new HtmlValidate({
root: true,
transform: {
"^.*\\.foo$": "mock-transform",
},
});
});
it("should return true if file extension is .html", () => {
expect(htmlvalidate.canValidate("my-file.html")).toBeTruthy();
});
it("should return true if a transformer can handle the file", () => {
expect(htmlvalidate.canValidate("my-file.foo")).toBeTruthy();
});
it("should return false if no transformer can handle the file", () => {
expect(htmlvalidate.canValidate("my-file.bar")).toBeFalsy();
});
});
it("dumpTokens() should dump tokens", () => {
const htmlvalidate = new HtmlValidate();
const filename = "foo.html";
......
......@@ -5,6 +5,7 @@ import { Engine, EventDump, TokenDump } from "./engine";
import { Parser } from "./parser";
import { Report, Reporter } from "./reporter";
import { RuleDocumentation } from "./rule";
import path from "path";
/**
* Primary API for using HTML-validate.
......@@ -92,6 +93,27 @@ class HtmlValidate {
);
}
/**
* Returns true if the given filename can be validated.
*
* A file is considered to be validatable if the extension is `.html` or if a
* transformer matches the filename.
*
* This is mostly useful for tooling to determine whenever to validate the
* file or not. CLI tools will run on all the given files anyway.
*/
public canValidate(filename: string): boolean {
/* .html is always supported */
const extension = path.extname(filename).toLowerCase();
if (extension === ".html") {
return true;
}
/* test if there is a matching transformer */
const config = this.getConfigFor(filename);
return config.canTransform(filename);
}
/**
* Tokenize filename and output all tokens.
*
......
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