Commit ae40706f authored by David Sveningsson's avatar David Sveningsson
Browse files

feat(config): add `StaticConfigData` for simple static config

parent 29fb843f
......@@ -3,6 +3,7 @@
export { default as HtmlValidate } from "./htmlvalidate";
export { AttributeData } from "./parser";
export { Config, ConfigData, ConfigError, ConfigLoader, Severity, configPresets } from "./config";
export { StaticConfigLoader } from "./config/loaders/static";
export { DynamicValue, HtmlElement, NodeClosed, TextNode } from "./dom";
export { EventDump, TokenDump } from "./engine";
export { UserError, SchemaValidationError } from "./error";
......
......@@ -98,14 +98,6 @@ describe("FileSystemConfigLoader", () => {
});
describe("getConfigFor()", () => {
it("should query configuration loader with passed filename", () => {
expect.assertions(1);
const loader = new ExposedFileSystemConfigLoader();
const fromFilename = jest.spyOn(loader, "fromFilename").mockImplementation(() => null);
loader.getConfigFor("my-file.html");
expect(fromFilename).toHaveBeenCalledWith("my-file.html");
});
it("should use global configuration by default", () => {
expect.assertions(1);
/* constructor global config */
......
import { StaticConfigLoader } from "./static";
describe("StaticConfigLoader", () => {
describe("getConfigFor()", () => {
it("should use default configuration", () => {
expect.assertions(1);
const loader = new StaticConfigLoader();
const config = loader.getConfigFor("my-file.html");
expect(config.get()).toEqual({
extends: ["html-validate:recommended"],
elements: ["html5"],
plugins: [],
rules: expect.anything(),
transform: {},
});
});
it("should use explicitly passed configuration", () => {
expect.assertions(1);
const loader = new StaticConfigLoader({
rules: {
a: "error",
},
});
const config = loader.getConfigFor("my-file.html");
expect(config.get()).toEqual({
extends: [],
plugins: [],
rules: {
a: "error",
},
transform: {},
});
});
it("should merge global configuration with override if provided", () => {
expect.assertions(1);
const loader = new StaticConfigLoader({
rules: {
a: "error",
b: "error",
},
});
const config = loader.getConfigFor("my-file.html", {
rules: {
a: "warn",
c: "warn",
},
});
expect(config.get()).toEqual({
extends: [],
plugins: [],
rules: {
a: "warn",
b: "error",
c: "warn",
},
transform: {},
});
});
it("should not use global configuration if override config is root", () => {
expect.assertions(1);
const loader = new StaticConfigLoader({
rules: {
a: "error",
},
});
const config = loader.getConfigFor("my-file.html", {
root: true,
rules: {
b: "error",
},
});
expect(config.get()).toEqual({
root: true,
extends: [],
plugins: [],
rules: {
b: "error",
},
transform: {},
});
});
});
describe("flushCache()", () => {
it("should not crash", () => {
expect.assertions(1);
const loader = new StaticConfigLoader();
expect(() => loader.flushCache()).not.toThrow();
});
});
});
import { Config } from "../config";
import { ConfigData } from "../config-data";
import { ConfigLoader } from "../config-loader";
/**
* The static configuration loader does not do any per-handle lookup. Only the
* global or per-call configuration is used.
*
* In practice this means no configuration is fetch by traversing the
* filesystem.
*/
export class StaticConfigLoader extends ConfigLoader {
public override getConfigFor(handle: string, configOverride?: ConfigData): Config {
const override = this.loadFromObject(configOverride || {});
if (override.isRootFound()) {
override.init();
return override;
}
const merged = this.globalConfig.merge(override);
merged.init();
return merged;
}
public override flushCache(): void {
/* do nothing */
}
protected defaultConfig(): Config {
return this.loadFromObject({
extends: ["html-validate:recommended"],
elements: ["html5"],
});
}
}
Supports Markdown
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