Skip to content

Commit 3617448

Browse files
committed
Add improved source on parse errors
1 parent 198ae41 commit 3617448

File tree

62 files changed

+103
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+103
-117
lines changed

lib/index.js

+16-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @typedef {import('hast').Root} Root
3+
* @typedef {import('parse5').ParserError} ParserError
34
* @typedef {import('vfile').Value} Value
45
*/
56

@@ -54,23 +55,6 @@
5455
*
5556
* @typedef {FromParse5Options & ErrorOptions & ExtraOptions} Options
5657
* Configuration.
57-
*
58-
* @typedef Parse5Error
59-
* Error from `parse5`.
60-
* @property {string} code
61-
* Dash cased name of error.
62-
* @property {number} startLine
63-
* Start line (1-indexed).
64-
* @property {number} startCol
65-
* Start column (1-indexed).
66-
* @property {number} startOffset
67-
* Start offset (0-indexed).
68-
* @property {number} endLine
69-
* End line (1-indexed).
70-
* @property {number} endCol
71-
* End column (1-indexed).
72-
* @property {number} endOffset
73-
* End offset (0-indexed).
7458
*/
7559

7660
import {ok as assert} from 'devlop'
@@ -126,7 +110,7 @@ export function fromHtml(value, options) {
126110
/**
127111
* Handle a parse error.
128112
*
129-
* @param {Parse5Error} error
113+
* @param {ParserError} error
130114
* Parse5 error.
131115
* @returns {undefined}
132116
* Nothing.
@@ -143,25 +127,27 @@ export function fromHtml(value, options) {
143127
assert(info, 'expected known error from `parse5`')
144128

145129
const message = new VFileMessage(format(info.reason), {
146-
start: {
147-
line: error.startLine,
148-
column: error.startCol,
149-
offset: error.startOffset
130+
place: {
131+
start: {
132+
line: error.startLine,
133+
column: error.startCol,
134+
offset: error.startOffset
135+
},
136+
end: {
137+
line: error.endLine,
138+
column: error.endCol,
139+
offset: error.endOffset
140+
}
150141
},
151-
end: {
152-
line: error.endLine,
153-
column: error.endCol,
154-
offset: error.endOffset
155-
}
142+
ruleId: code,
143+
source: 'hast-util-from-html'
156144
})
157145

158146
if (file.path) {
159-
message.name = file.path + ':' + message.name
160147
message.file = file.path
148+
message.name = file.path + ':' + message.name
161149
}
162150

163-
message.source = 'parse-error'
164-
message.ruleId = code
165151
message.fatal = fatalities[level]
166152
message.note = format(info.description)
167153
message.url = info.url === false ? undefined : base + code

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ fromHtml(doc, {
424424
},
425425
reason: 'Missing whitespace before doctype name',
426426
ruleId: 'missing-whitespace-before-doctype-name',
427-
source: 'parse-error',
427+
source: 'hast-util-from-html',
428428
note: 'Unexpected `h`. Expected ASCII whitespace instead',
429429
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-before-doctype-name'
430430
}
@@ -440,7 +440,7 @@ fromHtml(doc, {
440440
},
441441
reason: 'Unexpected duplicate attribute',
442442
ruleId: 'duplicate-attribute',
443-
source: 'parse-error',
443+
source: 'hast-util-from-html',
444444
note: 'Unexpectedly double attribute. Expected attributes to occur only once',
445445
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-duplicate-attribute'
446446
}

test/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ test('fromHtml', async function (t) {
9898
},
9999
reason: 'Missing doctype before other content',
100100
ruleId: 'missing-doctype',
101-
source: 'parse-error',
101+
source: 'hast-util-from-html',
102102
note: 'Expected a `<!doctype html>` before anything else'
103103
}
104104
])
@@ -162,7 +162,7 @@ test('fromHtml', async function (t) {
162162
},
163163
reason: 'Unexpected unknown named character reference',
164164
ruleId: 'unknown-named-character-reference',
165-
source: 'parse-error',
165+
source: 'hast-util-from-html',
166166
note: 'Unexpected character reference. Expected known named character references',
167167
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unknown-named-character-reference'
168168
}
@@ -195,7 +195,7 @@ test('fromHtml', async function (t) {
195195
},
196196
reason: 'Unexpected slash at end of closing tag',
197197
ruleId: 'end-tag-with-trailing-solidus',
198-
source: 'parse-error',
198+
source: 'hast-util-from-html',
199199
note: 'Unexpected `/`. Expected `>` instead',
200200
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-end-tag-with-trailing-solidus'
201201
}
@@ -229,7 +229,7 @@ test('fromHtml', async function (t) {
229229
},
230230
reason: 'Invalid first character in tag name',
231231
ruleId: 'invalid-first-character-of-tag-name',
232-
source: 'parse-error',
232+
source: 'hast-util-from-html',
233233
note: 'Unexpected `` ` ``. Expected an ASCII letter instead',
234234
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-invalid-first-character-of-tag-name'
235235
}
@@ -263,7 +263,7 @@ test('fromHtml', async function (t) {
263263
},
264264
reason: 'Unexpected NULL character',
265265
ruleId: 'unexpected-null-character',
266-
source: 'parse-error',
266+
source: 'hast-util-from-html',
267267
note: 'Unexpected code point `0x0`. Do not use NULL characters in HTML',
268268
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-null-character'
269269
}
@@ -294,7 +294,7 @@ test('fromHtml', async function (t) {
294294
},
295295
reason: 'Missing doctype before other content',
296296
ruleId: 'missing-doctype',
297-
source: 'parse-error',
297+
source: 'hast-util-from-html',
298298
file: 'example.html',
299299
note: 'Expected a `<!doctype html>` before anything else'
300300
}
@@ -364,7 +364,7 @@ test('parse-errors: working', async function (t) {
364364
},
365365
reason: 'Unexpected surrogate character',
366366
ruleId: 'surrogate-in-input-stream',
367-
source: 'parse-error',
367+
source: 'hast-util-from-html',
368368
file: 'index.html',
369369
note: 'Unexpected code point `0xD800`. Do not use lone surrogate characters in HTML',
370370
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-surrogate-in-input-stream'

test/parse-error/abandoned-head-element-child/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected metadata element after head",
2121
"ruleId": "abandoned-head-element-child",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected element after head. Expected the element before `</head>`"
2525
}

test/parse-error/abrupt-closing-of-empty-comment/messages.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected abruptly closed empty comment",
2121
"ruleId": "abrupt-closing-of-empty-comment",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected `>` or `->`. Expected `-->` to close comments",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-closing-of-empty-comment"
@@ -44,7 +44,7 @@
4444
},
4545
"reason": "Unexpected abruptly closed empty comment",
4646
"ruleId": "abrupt-closing-of-empty-comment",
47-
"source": "parse-error",
47+
"source": "hast-util-from-html",
4848
"file": "index.html",
4949
"note": "Unexpected `>` or `->`. Expected `-->` to close comments",
5050
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-closing-of-empty-comment"

test/parse-error/abrupt-doctype-public-identifier/messages.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected abruptly closed public identifier",
2121
"ruleId": "abrupt-doctype-public-identifier",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected `>`. Expected a closing `\"` or `'` after the public identifier",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-doctype-public-identifier"
@@ -44,7 +44,7 @@
4444
},
4545
"reason": "Unexpected non-conforming doctype declaration",
4646
"ruleId": "non-conforming-doctype",
47-
"source": "parse-error",
47+
"source": "hast-util-from-html",
4848
"file": "index.html",
4949
"note": "Expected `<!doctype html>` or `<!doctype html system \"about:legacy-compat\">`"
5050
}

test/parse-error/abrupt-doctype-system-identifier/messages.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected abruptly closed system identifier",
2121
"ruleId": "abrupt-doctype-system-identifier",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected `>`. Expected a closing `\"` or `'` after the identifier identifier",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-doctype-system-identifier"
@@ -44,7 +44,7 @@
4444
},
4545
"reason": "Unexpected non-conforming doctype declaration",
4646
"ruleId": "non-conforming-doctype",
47-
"source": "parse-error",
47+
"source": "hast-util-from-html",
4848
"file": "index.html",
4949
"note": "Expected `<!doctype html>` or `<!doctype html system \"about:legacy-compat\">`"
5050
}

test/parse-error/absence-of-digits-in-numeric-character-reference/messages.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected non-digit at start of numeric character reference",
2121
"ruleId": "absence-of-digits-in-numeric-character-reference",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected `a`. Expected `[0-9]` for decimal references or `[0-9a-fA-F]` for hexadecimal references",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-absence-of-digits-in-numeric-character-reference"
@@ -44,7 +44,7 @@
4444
},
4545
"reason": "Unexpected non-digit at start of numeric character reference",
4646
"ruleId": "absence-of-digits-in-numeric-character-reference",
47-
"source": "parse-error",
47+
"source": "hast-util-from-html",
4848
"file": "index.html",
4949
"note": "Unexpected `g`. Expected `[0-9]` for decimal references or `[0-9a-fA-F]` for hexadecimal references",
5050
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-absence-of-digits-in-numeric-character-reference"

test/parse-error/cdata-in-html-content/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected CDATA section in HTML",
2121
"ruleId": "cdata-in-html-content",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected `<![CDATA[` in HTML. Remove it, use a comment, or encode special characters instead",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-cdata-in-html-content"

test/parse-error/character-reference-outside-unicode-range/messages.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected too big numeric character reference",
2121
"ruleId": "character-reference-outside-unicode-range",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpectedly high character reference. Expected character references to be at most hexadecimal 10ffff (or decimal 1114111)",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-character-reference-outside-unicode-range"
@@ -44,7 +44,7 @@
4444
},
4545
"reason": "Unexpected too big numeric character reference",
4646
"ruleId": "character-reference-outside-unicode-range",
47-
"source": "parse-error",
47+
"source": "hast-util-from-html",
4848
"file": "index.html",
4949
"note": "Unexpectedly high character reference. Expected character references to be at most hexadecimal 10ffff (or decimal 1114111)",
5050
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-character-reference-outside-unicode-range"

test/parse-error/closing-of-element-with-open-child-elements/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected closing tag with open child elements",
2121
"ruleId": "closing-of-element-with-open-child-elements",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpectedly closing tag. Expected other tags to be closed first"
2525
}

test/parse-error/control-character-in-input-stream/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected control character",
2121
"ruleId": "control-character-in-input-stream",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected control character `0x7F`. Expected a non-control code point, 0x00, or ASCII whitespace",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-control-character-in-input-stream"

test/parse-error/control-character-reference/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected control character reference",
2121
"ruleId": "control-character-reference",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpectedly control character in reference. Expected a non-control code point, 0x00, or ASCII whitespace",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-control-character-reference"

test/parse-error/disallowed-content-in-noscript-in-head/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Disallowed content inside `<noscript>` in `<head>`",
2121
"ruleId": "disallowed-content-in-noscript-in-head",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected text character `h`. Only use text in `<noscript>`s in `<body>`"
2525
}

test/parse-error/duplicate-attribute/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected duplicate attribute",
2121
"ruleId": "duplicate-attribute",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpectedly double attribute. Expected attributes to occur only once",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-duplicate-attribute"

test/parse-error/end-tag-with-attributes/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected attribute on closing tag",
2121
"ruleId": "end-tag-with-attributes",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected attribute. Expected `>` instead",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-end-tag-with-attributes"

test/parse-error/end-tag-with-trailing-solidus/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected slash at end of closing tag",
2121
"ruleId": "end-tag-with-trailing-solidus",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected `/`. Expected `>` instead",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-end-tag-with-trailing-solidus"

test/parse-error/end-tag-without-matching-open-element/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected unopened end tag",
2121
"ruleId": "end-tag-without-matching-open-element",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected end tag. Expected no end tag or another end tag"
2525
}

test/parse-error/eof-before-tag-name/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected end of file",
2121
"ruleId": "eof-before-tag-name",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected end of file. Expected tag name instead",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-before-tag-name"

test/parse-error/eof-in-cdata/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected end of file in CDATA",
2121
"ruleId": "eof-in-cdata",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected end of file. Expected `]]>` to close the CDATA",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-cdata"

test/parse-error/eof-in-comment/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected end of file in comment",
2121
"ruleId": "eof-in-comment",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected end of file. Expected `-->` to close the comment",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-comment"

test/parse-error/eof-in-doctype/messages.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected end of file in doctype",
2121
"ruleId": "eof-in-doctype",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected end of file. Expected a valid doctype (such as `<!doctype html>`)",
2525
"url": "https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-doctype"
@@ -44,7 +44,7 @@
4444
},
4545
"reason": "Unexpected non-conforming doctype declaration",
4646
"ruleId": "non-conforming-doctype",
47-
"source": "parse-error",
47+
"source": "hast-util-from-html",
4848
"file": "index.html",
4949
"note": "Expected `<!doctype html>` or `<!doctype html system \"about:legacy-compat\">`"
5050
}

test/parse-error/eof-in-element-that-can-contain-only-text/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"reason": "Unexpected end of file in element that can only contain text",
2121
"ruleId": "eof-in-element-that-can-contain-only-text",
22-
"source": "parse-error",
22+
"source": "hast-util-from-html",
2323
"file": "index.html",
2424
"note": "Unexpected end of file. Expected text or a closing tag"
2525
}

0 commit comments

Comments
 (0)