Transforms the nullish coalescing operator (??) into conditional expressions compatible with older JavaScript environments.
npx @tessl/cli install tessl/npm-babel--plugin-transform-nullish-coalescing-operator@7.27.0@babel/plugin-transform-nullish-coalescing-operator is a Babel plugin that transforms the nullish coalescing operator (??) into conditional expressions compatible with older JavaScript environments. It provides comprehensive transformation handling edge cases like document.all behavior, supports loose mode for simpler transformations, and includes optimizations for pure getters and identifiers.
npm install --save-dev @babel/plugin-transform-nullish-coalescing-operator// ESM
import nullishCoalescingPlugin from "@babel/plugin-transform-nullish-coalescing-operator";
// ESM with Options interface
import nullishCoalescingPlugin, { Options } from "@babel/plugin-transform-nullish-coalescing-operator";
// TypeScript with type imports
import nullishCoalescingPlugin, { type Options } from "@babel/plugin-transform-nullish-coalescing-operator";
// CommonJS
const nullishCoalescingPlugin = require("@babel/plugin-transform-nullish-coalescing-operator");
const { Options } = require("@babel/plugin-transform-nullish-coalescing-operator");// babel.config.js
module.exports = {
plugins: [
"@babel/plugin-transform-nullish-coalescing-operator"
]
};// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-transform-nullish-coalescing-operator", {
loose: true
}]
]
};Input JavaScript with nullish coalescing:
function getUserName(user) {
return user.name ?? "Anonymous";
}
const config = {
timeout: options.timeout ?? 5000,
retries: options.retries ?? 3
};Output after transformation:
function getUserName(user) {
var _user$name;
return (_user$name = user.name) !== null && _user$name !== undefined ? _user$name : "Anonymous";
}
const config = {
timeout: options.timeout != null ? options.timeout : 5000,
retries: options.retries != null ? options.retries : 3
};The package exports a Babel plugin created using the declare helper from @babel/helper-plugin-utils. The plugin transforms nullish coalescing operators into conditional expressions.
/**
* Main plugin export created using Babel's declare helper
* The plugin transforms nullish coalescing operators into conditional expressions
*/
declare const plugin: BabelPlugin;
export default plugin;
export { Options };Configuration options for customizing the transformation behavior.
interface Options {
/**
* Enable loose mode transformation
* When true, uses `!= null` instead of explicit null and undefined checks
* @default false
*/
loose?: boolean;
}The plugin returns a standard Babel plugin object with visitor methods.
interface BabelPlugin {
/** Plugin name identifier */
name: "transform-nullish-coalescing-operator";
/** Parser plugin configuration (Babel 7 only) */
manipulateOptions?: (opts: any, parser: any) => void;
/** AST visitor methods */
visitor: {
LogicalExpression: (path: NodePath<LogicalExpression>) => void;
};
}In standard mode, the plugin performs spec-compliant transformations that handle the document.all edge case:
a ?? b becomes a !== null && a !== undefined ? a : bIn loose mode, the plugin uses simpler transformations for better performance:
a ?? b becomes a != null ? a : b!= null) which covers both null and undefinedThe plugin includes several optimizations based on Babel assumptions:
When pureGetters assumption is enabled:
obj.prop)When noDocumentAll assumption is enabled:
loose: falsedocument.all behavior is not a concern in the target environmentThe plugin handles nullish coalescing in various JavaScript contexts:
// Input
const value = a ?? b;
// Output (standard mode)
const value = a !== null && a !== undefined ? a : b;// Input
function example(param = a ?? b) { }
// Output
function example(param = (() => a ?? b)()) { }// Input
const { prop = a ?? b } = obj;
// Output (transformed to avoid scope issues)
const { prop = (() => a ?? b)() } = obj;// Input
const result = (obj.prop ?? defaultObj).method();
// Output
var _obj$prop;
const result = ((_obj$prop = obj.prop) !== null && _obj$prop !== undefined ? _obj$prop : defaultObj).method();@babel/core: ^7.0.0-0 (peer dependency)@babel/helper-plugin-utils: For plugin declaration utilitiesnullishCoalescingOperator parser plugin in Babel 7The plugin automatically configures the Babel parser to recognize nullish coalescing syntax:
// Automatically added parser plugin
parser.plugins.push("nullishCoalescingOperator");The plugin respects Babel's assumption system for optimization:
// babel.config.js with assumptions
module.exports = {
assumptions: {
"pureGetters": true,
"noDocumentAll": true
},
plugins: [
"@babel/plugin-transform-nullish-coalescing-operator"
]
};interface BabelAPI {
assertVersion: (version: number) => void;
assumption: (name: string) => boolean | undefined;
}
interface NodePath<T> {
node: T;
scope: Scope;
replaceWith: (node: any) => void;
}
interface LogicalExpression {
operator: string;
left: Expression;
right: Expression;
}
interface Scope {
generateUidIdentifierBasedOnNode: (node: any) => Identifier;
push: (binding: { id: Identifier }) => void;
hasBinding: (name: string) => boolean;
buildUndefinedNode: () => Identifier;
path: NodePath<any>;
}
interface Expression {
// Base AST node type for expressions
}
interface Identifier extends Expression {
name: string;
}