Skip to content

Commit 527c6e1

Browse files
author
Christopher Quadflieg
committed
feat: use recommended as default
1 parent b8585cb commit 527c6e1

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/core/configuration.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Configuration } from './types'
2+
3+
export function isConfiguration(value: unknown): value is Configuration {
4+
// Config must be an object
5+
if (typeof value !== 'object') {
6+
return false
7+
}
8+
9+
// Config must not be null
10+
if (value === null) {
11+
return false
12+
}
13+
14+
// This helps to get better support for TypeScript
15+
const config = value as Record<string, unknown>
16+
17+
// If extends is defined, it must be an array
18+
if (config.extends !== undefined) {
19+
if (!Array.isArray(config.extends)) {
20+
return false
21+
}
22+
23+
// Any value within extens must be a string
24+
for (const extension of config.extends) {
25+
if (typeof extension !== 'string') {
26+
return false
27+
}
28+
}
29+
}
30+
31+
// If extends is defined, it must be an object
32+
if (config.rules !== undefined) {
33+
if (typeof config.rules !== 'object' && config.rules !== null) {
34+
return false
35+
}
36+
}
37+
38+
return true
39+
}

src/core/core.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isConfiguration } from './configuration'
12
import HTMLParser from './htmlparser'
23
import Reporter, { ReportMessageCallback } from './reporter'
34
import * as HTMLRules from './rules'
@@ -59,10 +60,19 @@ class HTMLHintCore {
5960

6061
public verify(
6162
html: string,
62-
config: Configuration = { extends: [HTMLHINT_LEGACY] }
63+
config: Configuration = { extends: [HTMLHINT_RECOMMENDED] }
6364
) {
65+
if (!isConfiguration(config)) {
66+
throw new Error('The HTMLHint configuration is invalid')
67+
}
68+
6469
let ruleset: Ruleset = {}
6570

71+
// If an empty configuration is passed, use the recommended ruleset
72+
if (config.extends === undefined && config.rules === undefined) {
73+
config.extends = [HTMLHINT_RECOMMENDED]
74+
}
75+
6676
// Iterate through extensions and merge rulesets into ruleset
6777
for (const extend of config.extends ?? []) {
6878
if (typeof extend === 'string') {

test/core.spec.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
const expect = require('expect.js')
22

3+
/** @type import('../src/core/core').HTMLHint */
34
const HTMLHint = require('../dist/htmlhint.js').HTMLHint
45

56
describe('Core', () => {
67
describe('Defaults', () => {
7-
it('Not load default ruleset when use undefined ruleset should result in an error', () => {
8+
it('Should use the recommended ruleset, if configuration is not defined', () => {
89
const code =
910
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
1011
const messages = HTMLHint.verify(code)
1112
expect(messages.length).to.be(9)
1213
})
1314

14-
it('Not load default ruleset when use empty ruleset should result in an error', () => {
15+
it('Should use the recommended ruleset, if empty configuration is passed', () => {
1516
const code =
1617
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
1718
const messages = HTMLHint.verify(code, {})
1819
expect(messages.length).to.be(9)
1920
})
21+
22+
it('Should use the legacy ruleset, if it is passed', () => {
23+
const code =
24+
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
25+
const messages = HTMLHint.verify(code, {
26+
extends: ['htmlhint:legacy'],
27+
})
28+
expect(messages.length).to.be(9)
29+
})
30+
31+
it('Should use the recommended ruleset, if it is passed', () => {
32+
const code =
33+
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
34+
const messages = HTMLHint.verify(code, {
35+
extends: ['htmlhint:recommended'],
36+
})
37+
expect(messages.length).to.be(9)
38+
})
39+
40+
it('Should use no ruleset, if extends is not defined and empty ruleset is passed', () => {
41+
const code =
42+
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
43+
const messages = HTMLHint.verify(code, { rules: {} })
44+
expect(messages.length).to.be(0)
45+
})
2046
})
2147

2248
describe('Customization', () => {

0 commit comments

Comments
 (0)