developit / developit/htm

Improve transpilation error handling for missing closing tags

未关闭
#168 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
debugging proposal
主要语言
JavaScript
星标
9k
派生
181
PR 合并指标
30 天内没有已合并 PR

描述

Consider this sample code, which accidentally omits a closing tag.

```js
import htm from 'htm'
const h = (type, props, ...children) => ({type, props, children});
const html = htm.bind(h)

console.log(JSON.stringify(html`

Hello, world!`));
```

In this case, the user intended to include a closing tag `

`, and so the user's desired logged result is `{"type":"h1","props":null,"children":["Hello, world!"]}`

Transpile `htm` away using this script.

shell script

```sh
#!/bin/sh -ex

rm -rf babel-htm-errors
mkdir babel-htm-errors
cd babel-htm-errors
npm init -y
npm i -D @babel/cli @babel/core babel-plugin-htm
cat < .babelrc
{
"plugins": [
["htm", {
"pragma": "h"
}]
]
}
EOF
cat < index.js
import htm from 'htm'
const h = (type, props, ...children) => ({type, props, children});
const html = htm.bind(h)

console.log(JSON.stringify(html\`

Hello, world!\`));
EOF
npx babel index.js > output.mjs
node output.mjs
```

Expected: Transpilation should fail with an error.

Actual: The `html` line transpiles to:

```js
console.log(JSON.stringify("Hello, world!"));
```

The `

` tag is silently dropped.

(Note that this error behavior is different from the runtime behavior of htm with mismatching closing tags; see issue #166.)

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。