eslint-plugin-unicorn: `require-post-message-target-origin` shouldn't trigger for incompatible APIs

What’s wrong

I’m currently developing a browser extension that communicates with native software:

import { browser, Runtime } from 'webextension-polyfill-ts'
const nativePort: Runtime.Port = browser.runtime.connectNative(APPLICATION_ID)
nativePort.postMessage({ cmd: 'Version' }) // (x) Missing the `targetOrigin` argument.

Which rule is buggy

require-post-message-target-origin is triggered but the method only accepts one argument (see runtime.Port).

Ideas

  1. Enforce the name of the variable to be window, document or the likes.
  2. Check the type of the variable or the existence of the import 'webextension-polyfill'/'webextension-polyfill-ts'.
  3. Add a warning for web extension developers here: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v34.0.1/docs/rules/require-post-message-target-origin.md

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 20 (2 by maintainers)

Most upvoted comments

That will be meaningless if this #1396 (comment) is true.

So require-post-message-target-origin should be disabled by default for .ts files in the recommended config.

I think we should turn this rule off…

That will be meaningless if this #1396 (comment) is true.

Indeed it is! So this rule can be dropped altogether as far as I’m concerned.

Screen Shot

Ok got it, the problem is more that browsers fail silently instead of throwing an exception. What if this rule only triggers for window.postMessage by default in unicorn/recommended, and add an option to trigger on any variable like foo.postMessage?

rules: {
  'unicorn/require-post-message-target-origin': ['error', 'window' | 'any'],
}

Do you think disable this rule in your file or project is a good idea? If you are developing a browser extension, you probably don’t need call window.postMessage.