Skip to content

Commit

Permalink
feat: use recommended as default
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Quadflieg committed Jul 10, 2020
1 parent 5ec5687 commit 838cfb8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
39 changes: 39 additions & 0 deletions src/core/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Configuration } from './types'

export function isConfiguration(value: unknown): value is Configuration {
// Config must be an object
if (typeof value !== 'object') {
return false
}

// Config must not be null
if (value === null) {
return false
}

// This helps to get better support for TypeScript
const config = value as Record<string, unknown>

// If extends is defined, it must be an array
if (config.extends !== undefined) {
if (!Array.isArray(config.extends)) {
return false
}

// Any value within extens must be a string
for (const extension of config.extends) {
if (typeof extension !== 'string') {
return false
}
}
}

// If extends is defined, it must be an object
if (config.rules !== undefined) {
if (typeof config.rules !== 'object' && config.rules !== null) {
return false
}
}

return true
}
12 changes: 11 additions & 1 deletion src/core/core.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isConfiguration } from './configuration'
import HTMLParser from './htmlparser'
import Reporter, { ReportMessageCallback } from './reporter'
import * as HTMLRules from './rules'
Expand Down Expand Up @@ -59,10 +60,19 @@ class HTMLHintCore {

public verify(
html: string,
config: Configuration = { extends: [HTMLHINT_LEGACY] }
config: Configuration = { extends: [HTMLHINT_RECOMMENDED] }
) {
if (!isConfiguration(config)) {
throw new Error('The HTMLHint configuration is invalid')
}

let ruleset: Ruleset = {}

// If an empty configuration is passed, use the recommended ruleset
if (config.extends === undefined && config.rules === undefined) {
config.extends = [HTMLHINT_RECOMMENDED]
}

// Iterate through extensions and merge rulesets into ruleset
for (const extend of config.extends ?? []) {
if (typeof extend === 'string') {
Expand Down
30 changes: 28 additions & 2 deletions test/core.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
const expect = require('expect.js')

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

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

it('Not load default ruleset when use empty ruleset should result in an error', () => {
it('Should use the recommended ruleset, if empty configuration is passed', () => {
const code =
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
const messages = HTMLHint.verify(code, {})
expect(messages.length).to.be(9)
})

it('Should use the legacy ruleset, if it is passed', () => {
const code =
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
const messages = HTMLHint.verify(code, {
extends: ['htmlhint:legacy'],
})
expect(messages.length).to.be(9)
})

it('Should use the recommended ruleset, if it is passed', () => {
const code =
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
const messages = HTMLHint.verify(code, {
extends: ['htmlhint:recommended'],
})
expect(messages.length).to.be(9)
})

it('Should use no ruleset, if extends is not defined and empty ruleset is passed', () => {
const code =
'<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>'
const messages = HTMLHint.verify(code, { rules: {} })
expect(messages.length).to.be(0)
})
})

describe('Customization', () => {
Expand Down

0 comments on commit 838cfb8

Please sign in to comment.