From 838cfb841087ddba4f4fd91bb24000673d22e338 Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg Date: Fri, 10 Jul 2020 19:04:06 +0200 Subject: [PATCH] feat: use recommended as default --- src/core/configuration.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/core/core.ts | 12 +++++++++++- test/core.spec.js | 30 ++++++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/core/configuration.ts diff --git a/src/core/configuration.ts b/src/core/configuration.ts new file mode 100644 index 000000000..e56853cbe --- /dev/null +++ b/src/core/configuration.ts @@ -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 + + // 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 +} diff --git a/src/core/core.ts b/src/core/core.ts index 0909be7da..9b09aa231 100644 --- a/src/core/core.ts +++ b/src/core/core.ts @@ -1,3 +1,4 @@ +import { isConfiguration } from './configuration' import HTMLParser from './htmlparser' import Reporter, { ReportMessageCallback } from './reporter' import * as HTMLRules from './rules' @@ -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') { diff --git a/test/core.spec.js b/test/core.spec.js index d9792c233..c719911fa 100644 --- a/test/core.spec.js +++ b/test/core.spec.js @@ -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 = '

>

' 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 = '

>

' const messages = HTMLHint.verify(code, {}) expect(messages.length).to.be(9) }) + + it('Should use the legacy ruleset, if it is passed', () => { + const code = + '

>

' + 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 = + '

>

' + 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 = + '

>

' + const messages = HTMLHint.verify(code, { rules: {} }) + expect(messages.length).to.be(0) + }) }) describe('Customization', () => {