doctype-style.ts 1.43 KB
Newer Older
1
import { DoctypeEvent } from "../event";
2
import { Rule, RuleDocumentation, ruleDocumentationUrl, SchemaObject } from "../rule";
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

interface RuleContext {
	style: "uppercase" | "lowercase";
}

interface RuleOptions {
	style: "uppercase" | "lowercase";
}

const defaults: RuleOptions = {
	style: "uppercase",
};

export default class DoctypeStyle extends Rule<RuleContext, RuleOptions> {
	public constructor(options: Partial<RuleOptions>) {
		super({ ...defaults, ...options });
	}

21
22
23
24
25
26
27
28
29
	public static schema(): SchemaObject {
		return {
			style: {
				enum: ["lowercase", "uppercase"],
				type: "string",
			},
		};
	}

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
	public documentation(context?: RuleContext): RuleDocumentation {
		const doc: RuleDocumentation = {
			description: `While DOCTYPE is case-insensitive in the standard the current configuration requires a specific style.`,
			url: ruleDocumentationUrl(__filename),
		};
		if (context) {
			doc.description = `While DOCTYPE is case-insensitive in the standard the current configuration requires it to be ${context.style}`;
		}
		return doc;
	}

	public setup(): void {
		this.on("doctype", (event: DoctypeEvent) => {
			if (this.options.style === "uppercase" && event.tag !== "DOCTYPE") {
				this.report(null, "DOCTYPE should be uppercase", event.location, this.options);
			}
			if (this.options.style === "lowercase" && event.tag !== "doctype") {
				this.report(null, "DOCTYPE should be lowercase", event.location, this.options);
			}
		});
	}
}