Commit de80d96c authored by David Sveningsson's avatar David Sveningsson

feat(rules): support deprecating rules

parent 36550132
Pipeline #88528692 passed with stages
in 6 minutes and 58 seconds
@ngdoc rule
@module rules
@name deprecated-rule
@summary Disallow usage of deprecated rules
@description
# Disallow usage of deprecated rules (`deprecated-rule`)
Rules (both builtin and from plugins) can be deprecated in later releases.
This rule disallows the usage of deprecated rules.
......@@ -7,6 +7,7 @@ module.exports = {
"close-attr": "error",
"close-order": "error",
deprecated: "error",
"deprecated-rule": "warn",
"doctype-html": "error",
"element-case": "error",
"element-name": "error",
......
......@@ -65,6 +65,15 @@ export abstract class Rule<T = any> {
this.enabled = enabled;
}
/**
* Returns `true` if rule is deprecated.
*
* Overridden by subclasses.
*/
public get deprecated(): boolean {
return false;
}
/**
* Test if rule is enabled.
*
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`rule deprecated-rule should contain documentation 1`] = `
Object {
"description": "This rule is deprecated and should not be used any longer, consult documentation for further information.",
"url": "https://html-validate.org/rules/deprecated-rule.html",
}
`;
import HtmlValidate from "../htmlvalidate";
import "../matchers";
import { Rule } from "../rule";
class RuleRegular extends Rule {
public setup(): void {
/* do nothing */
}
}
class RuleDeprecated extends Rule {
public get deprecated(): boolean {
return true;
}
public setup(): void {
/* do nothing */
}
}
jest.mock(
"my-plugin",
() => ({
rules: {
"custom/regular": RuleRegular,
"custom/deprecated": RuleDeprecated,
},
}),
{ virtual: true }
);
describe("rule deprecated-rule", () => {
it("should not report error when no rule is deprecated", () => {
const htmlvalidate = new HtmlValidate({
plugins: ["my-plugin"],
rules: {
"custom/regular": "error",
"deprecated-rule": "error",
},
});
const report = htmlvalidate.validateString("<div></div>");
expect(report).toBeValid();
});
it("should report error when a rule is deprecated", () => {
const htmlvalidate = new HtmlValidate({
plugins: ["my-plugin"],
rules: {
"custom/deprecated": "error",
"deprecated-rule": "error",
},
});
const report = htmlvalidate.validateString("<div></div>");
expect(report).toBeInvalid();
expect(report).toHaveError(
"deprecated-rule",
'Usage of deprecated rule "custom/deprecated"'
);
});
it("should contain documentation", () => {
const htmlvalidate = new HtmlValidate();
expect(
htmlvalidate.getRuleDocumentation("deprecated-rule")
).toMatchSnapshot();
});
});
import { Severity } from "../config";
import { ConfigReadyEvent } from "../event";
import { Rule, RuleDocumentation, ruleDocumentationUrl } from "../rule";
class DeprecatedRule extends Rule {
public documentation(): RuleDocumentation {
return {
description:
"This rule is deprecated and should not be used any longer, consult documentation for further information.",
url: ruleDocumentationUrl(__filename),
};
}
public setup(): void {
this.on("config:ready", (event: ConfigReadyEvent) => {
for (const rule of this.getDeprecatedRules(event)) {
if (rule.getSeverity() > Severity.DISABLED) {
this.report(null, `Usage of deprecated rule "${rule.name}"`);
continue;
}
}
});
}
private getDeprecatedRules(event: ConfigReadyEvent): Rule[] {
const rules = Object.values(event.rules);
return rules.filter(rule => rule.deprecated);
}
}
module.exports = DeprecatedRule;
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