summaryrefslogtreecommitdiffstats
path: root/docs/code-quality/lint/linters/eslint-plugin-mozilla/rules/use-isInstance.rst
blob: 7ba41632282c5dde1792651b66a636621da7d3c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use-isInstance
==============

Prefer ``.isInstance()`` in chrome scripts over the standard ``instanceof``
operator for DOM interfaces, since the latter will return false when the object
is created from a different context.

These files are covered:

- ``*.sys.mjs``
- ``*.jsm``
- ``*.xhtml`` with ``there.is.only.xul``
- ``*.js`` with a heuristic

Since there is no straightforward way to detect chrome scripts, currently the
linter assumes that any script including the following words are chrome
privileged. This of course may not be sufficient and is open for change:

- ``ChromeUtils``, but not ``SpecialPowers.ChromeUtils``
- ``BrowserTestUtils``, ``PlacesUtils``
- ``document.createXULElement``
- ``loader.lazyRequireGetter``
- ``Services.foo``, but not ``SpecialPowers.Services.foo``

Examples of incorrect code for this rule:
-----------------------------------------

.. code-block:: js

    node instanceof Node
    text instanceof win.Text
    target instanceof this.contentWindow.HTMLAudioElement

Examples of correct code for this rule:
---------------------------------------

.. code-block:: js

    Node.isInstance(node)
    win.Text.isInstance(text)
    this.contentWindow.HTMLAudioElement.isInstance(target)