fix: solve the problem of not being able to format wxml with Prettier #185
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
解决了什么问题:
现在配置
"minapp-vscode.wxmlFormatter": "prettier"
不生效。检查 VSCode 日志,出现了
TypeError: u.replace is not a function
的错误,向上追查,是因为此插件本应返回字符串的格式化结果但却返回了一个对象,
原因就是插件里调用了
prettier.format()
,在新版 Prettier 里此方法返回的是 promise,需要 await 才能取得字符串结果。Prettier 的相关文档:https://prettier.io/docs/en/api#prettierformatsource-options
Prettier 中此 API 变更的 Commit:prettier/prettier@c6d6b25
代码如何实现:
调用 Prettier 的地方改为了
await prettier.format()
,不用担心这会导致旧版 Prettier 运行失败,因为 await 对于非 Promise 值会原样返回。
检查清单: