Skip to content

Commit e62f9e1

Browse files
committed
refactor: simplify additional messages
1 parent 6f671b2 commit e62f9e1

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/validator.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ export const useValidator = (options: ConfigData = defaultOptions) => {
1818
return { validator }
1919
}
2020

21-
export const useChecker = (validator: HtmlValidate, usePrettier = false, reporter = consola.withTag('html-validate')) => async (url: string, html: string) => {
21+
export const useChecker = (
22+
validator: HtmlValidate,
23+
usePrettier = false,
24+
reporter = consola.withTag('html-validate')
25+
) => async (url: string, html: string) => {
2226
let couldFormat = false
2327
try {
2428
if (usePrettier) {
2529
const { format } = await import('prettier')
2630
html = format(html, { parser: 'html' })
2731
couldFormat = true
2832
}
29-
// eslint-disable-next-line
33+
// eslint-disable-next-line
3034
} catch (e) {
3135
reporter.error(e)
3236
}
@@ -38,7 +42,7 @@ export const useChecker = (validator: HtmlValidate, usePrettier = false, reporte
3842

3943
if (valid) {
4044
return reporter.success(
41-
`No HTML validation errors found for ${chalk.bold(url)}`
45+
`No HTML validation errors found for ${chalk.bold(url)}`
4246
)
4347
}
4448

@@ -48,22 +52,17 @@ export const useChecker = (validator: HtmlValidate, usePrettier = false, reporte
4852

4953
const formattedResult = formatter(results)
5054

51-
let additionalInfo = ''
52-
53-
const messages = results[0].messages
54-
55-
messages.forEach(({ ruleId }: any) => {
56-
const url = `https://html-validate.org/rules/${ruleId}.html`
57-
additionalInfo = additionalInfo + url + '\n '
58-
})
55+
const urls =
56+
results[0]?.messages.map(
57+
({ ruleId }: any) => `https://html-validate.org/rules/${ruleId}.html`
58+
) || []
5959

6060
reporter.error(
61+
[
6162
`HTML validation errors found for ${chalk.bold(url)}`,
62-
'\n',
6363
formattedResult,
64-
'\n',
65-
`${chalk.bold('More information:')}`,
66-
'\n',
67-
additionalInfo
64+
urls.length ? `${chalk.bold('More information:')}` : '',
65+
...urls
66+
].join('\n')
6867
)
6968
}

test/module.test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,24 @@ describe('Nuxt module', () => {
2727
const { body } = await get('/')
2828

2929
expect(body).toContain('This is an invalid nested anchor tag')
30-
expect(mockReporter.error).toHaveBeenCalledWith(expect.not.stringContaining('index.html'), expect.stringContaining('element-permitted-content'))
30+
expect(mockReporter.error).toHaveBeenCalledWith(
31+
expect.stringContaining('element-permitted-content')
32+
)
3133
}, 50000)
3234

3335
test('should add hook for generating HTML', async () => {
34-
await generate('/')
36+
await generate()
3537

3638
const html = readFileSync(resolve(getNuxt().options.generate.dir, 'index.html'), 'utf8')
3739

3840
expect(html).toContain('This is an invalid nested anchor tag')
39-
expect(mockReporter.error).toHaveBeenCalledWith(expect.stringContaining('index.html'), expect.stringContaining('Element <a> is not permitted as descendant of <a>'))
41+
expect(mockReporter.error).toHaveBeenCalledWith(
42+
expect.stringContaining('index.html')
43+
)
44+
expect(mockReporter.error).toHaveBeenCalledWith(
45+
expect.stringContaining(
46+
'Element <a> is not permitted as descendant of <a>'
47+
)
48+
)
4049
}, 50000)
4150
})

0 commit comments

Comments
 (0)