Skip to content

Commit debe53f

Browse files
committed
Change to improve error on invalid style
1 parent 787974d commit debe53f

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

lib/handlers/element.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ function parseStyle(value, tagName) {
196196
try {
197197
styleToObject(value, iterator)
198198
} catch (error) {
199-
// To do: use `cause`.
200-
const exception = /** @type {Error} */ (error)
201-
exception.message =
202-
tagName + '[style]' + exception.message.slice('undefined'.length)
203-
throw error
199+
const cause = /** @type {Error} */ (error)
200+
const exception = new Error(
201+
'Could not parse `style` attribute on `' + tagName + '`',
202+
{cause}
203+
)
204+
throw exception
204205
}
205206

206207
return result

lib/state.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
* Casing to use for property names in `style` objects.
100100
*/
101101

102+
import {ok as assert} from 'devlop'
102103
import {html, svg} from 'property-information'
103104
import {position} from 'unist-util-position'
104105
import {zwitch} from 'zwitch'
@@ -185,8 +186,8 @@ function invalid(value) {
185186
* Nothing (crashes).
186187
*/
187188
function unknown(node) {
188-
// To do: use `devlop`.
189-
// @ts-expect-error: JS guarantees there’s a `type`.
189+
assert(node && typeof node === 'object')
190+
assert('type' in node)
190191
throw new Error('Cannot handle unknown node `' + node.type + '`')
191192
}
192193

@@ -331,9 +332,8 @@ function createJsxNameFromString(name) {
331332
if (name.includes('.')) {
332333
const names = name.split('.')
333334
let part = names.shift()
334-
// To do: use `devlop`.
335-
/** @type {JsxMemberExpression} */
336-
// @ts-expect-error: hush, the first is always defined.
335+
assert(part, 'Expected `part` to be defined')
336+
/** @type {JsxIdentifier | JsxMemberExpression} */
337337
let node = {type: 'JSXIdentifier', name: part}
338338

339339
while ((part = names.shift())) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@types/estree-jsx": "^1.0.0",
4545
"@types/hast": "^3.0.0",
4646
"comma-separated-tokens": "^2.0.0",
47+
"devlop": "^1.0.0",
4748
"estree-util-attach-comments": "^3.0.0",
4849
"estree-util-is-identifier-name": "^3.0.0",
4950
"hast-util-whitespace": "^3.0.0",

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ test('toEstree', async function (t) {
362362
await t.test('should crash on an incorrect style string', async function () {
363363
assert.throws(function () {
364364
toEstree(h('a', {style: 'x'}))
365-
}, /a\[style]:1:2: property missing ':'/)
365+
}, /Could not parse `style` attribute on `a`/)
366366
})
367367

368368
await t.test(

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"declaration": true,
66
"emitDeclarationOnly": true,
77
"exactOptionalPropertyTypes": true,
8-
"lib": ["es2020"],
8+
"lib": ["es2022"],
99
"module": "node16",
1010
"strict": true,
1111
"target": "es2020"

0 commit comments

Comments
 (0)