Are you an LLM? You can read better optimized documentation at /docs/guide/usage/linter/rules/unicorn/require-post-message-target-origin.md for this page in Markdown format
unicorn/require-post-message-target-origin Suspicious
💡 A suggestion is available for this rule.
它的作用
强制在使用 window.postMessage() 时传入 targetOrigin 参数。
请注意,此规则可能会产生误报,因为在没有类型信息的情况下,它无法正确检测所有情况。因此,在 postMessage() 可能与 BroadcastChannel 或 worker/service worker 上下文一起使用的场景中,启用此规则可能并不是个好主意(例如,WorkerGlobalScope#postMessage,其中第二个参数是传递列表或选项对象,而不是 targetOrigin)。
为什么这很糟糕?
在调用 window.postMessage() 时如果不传入 targetOrigin 参数,任何窗口都无法接收该消息。
示例
此规则的错误代码示例:
js
window.postMessage(message);此规则的正确代码示例:
js
window.postMessage(message, "https://example.com");
window.postMessage(message, "*");如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"unicorn/require-post-message-target-origin": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"unicorn/require-post-message-target-origin": "error",
},
});bash
oxlint --deny unicorn/require-post-message-target-origin版本
此规则于 v0.15.15 中添加。
