To ensure proper parsing of JavaScript code, it is important to set the ecmaVersion
to either "latest"
or 2021
, also known as 12
or higher when using ESLint. This allows ESLint to understand which version of JavaScript you are using (more details in the configuration guide). Here's an example on an ESLint playground where ??=
can be successfully used with the version set to "latest"
; and here's another one where it does not work because the version is set to 2020.
In your provided update with this code snippet:
var ObjectUtils ??= ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm").ObjectUtils;
...it's worth noting that this issue is not specific to ESLint or ??=
; a compound assignment operator cannot be used in that context. For instance, using +=
in JavaScript will result in a SyntaxError (no linter):
var a += 42;
// ^−−−−−−−− SyntaxError: Unexpected token '+='
console.log(a);
In most cases, you would simply use an assignment operator (preferably with let
or const
, avoiding the use of var
in new code):
let ObjectUtils = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm").ObjectUtils;
In rare situations where you need to handle variable redeclaration and utilize its current value if it's not nullish, you would need to split the process into two parts:
// **VERY** rare use case, there are better options
var ObjectUtils; // Possibly a redeclaration
ObjectUtils ??= ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm").ObjectUtils;
However, it is advisable to avoid such constructs in new code. :-)