Allow parsing of the nullish-coalescing operator
npx @tessl/cli install tessl/npm-babel--plugin-syntax-nullish-coalescing-operator@7.8.0Allow parsing of the nullish coalescing operator (??) in JavaScript code. This Babel syntax plugin extends the parser to recognize and handle the nullish coalescing syntax without transforming the code.
Note: This package was archived by the Babel team as nullish coalescing syntax support was integrated directly into Babel's default parser capabilities in later versions.
npm install --save-dev @babel/plugin-syntax-nullish-coalescing-operatorconst plugin = require("@babel/plugin-syntax-nullish-coalescing-operator");For ES modules:
import plugin from "@babel/plugin-syntax-nullish-coalescing-operator";Add to your Babel configuration:
{
"plugins": ["@babel/plugin-syntax-nullish-coalescing-operator"]
}Or programmatically:
const babel = require("@babel/core");
const plugin = require("@babel/plugin-syntax-nullish-coalescing-operator");
const result = babel.transformSync(code, {
plugins: [plugin]
});This enables parsing of nullish coalescing operator syntax:
// This syntax will be recognized by the parser
const value = someValue ?? defaultValue;
const nested = obj?.prop ?? "fallback";Default export function that creates a Babel plugin enabling nullish coalescing operator syntax parsing.
/**
* Default export plugin factory function created using declare()
* @param {Object} api - Babel API object with assertVersion method
* @returns {Object} Plugin descriptor with name and manipulateOptions
*/
function default(api);The plugin factory returns a descriptor object with these properties:
/**
* Plugin name identifier
*/
name: "syntax-nullish-coalescing-operator"
/**
* Modifies parser options to enable nullish coalescing syntax
* @param {Object} opts - Babel configuration options
* @param {Object} parserOpts - Parser configuration options
*/
manipulateOptions(opts, parserOpts)The manipulateOptions function adds "nullishCoalescingOperator" to the parser's plugins array, enabling recognition of the ?? operator syntax.
/**
* Babel API object passed to plugin factory
*/
interface BabelAPI {
/** Assert minimum Babel version compatibility */
assertVersion(version: number): void;
}
/**
* Plugin descriptor returned by factory function
*/
interface PluginDescriptor {
/** Plugin identifier name */
name: string;
/** Hook to modify parser options */
manipulateOptions(opts: Object, parserOpts: ParserOptions): void;
}
/**
* Parser configuration options
*/
interface ParserOptions {
/** Array of parser plugins to enable */
plugins: string[];
}declare function)??) returns the right-hand operand when the left-hand operand is null or undefined?? operator depends on the JavaScript engine or other transformation plugins