@babel/plugin-proposal-object-rest-spread is a Babel plugin that transforms ECMAScript object rest and spread syntax into ES5-compatible code. It enables developers to use modern object destructuring and spreading features (like {a, ...rest} = obj and {...obj, b}) while maintaining compatibility with older JavaScript environments.
npm install --save-dev @babel/plugin-proposal-object-rest-spreadThe plugin can be imported directly or used through Babel configuration:
// Direct import (TypeScript/ES modules)
import objectRestSpreadPlugin from "@babel/plugin-proposal-object-rest-spread";// CommonJS import
const objectRestSpreadPlugin = require("@babel/plugin-proposal-object-rest-spread");Babel Configuration Usage:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-proposal-object-rest-spread"]
};With options:
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-proposal-object-rest-spread", {
useBuiltIns: true,
loose: false
}]
]
};This plugin automatically transforms object rest and spread syntax when included in your Babel configuration:
Input (ES2018):
// Object rest in destructuring
const { a, ...rest } = obj;
// Object spread in expressions
const newObj = { ...obj, b: 2 };
// In function parameters
function fn({ a, ...rest }) {
console.log(rest);
}Output (ES5):
// Object rest becomes helper calls
const { a } = obj;
const rest = _objectWithoutProperties(obj, ["a"]);
// Object spread becomes Object.assign or helpers
const newObj = _objectSpread({}, obj, { b: 2 });
// Function parameters are transformed
function fn(_ref) {
const { a } = _ref;
const rest = _objectWithoutProperties(_ref, ["a"]);
console.log(rest);
}Creates a Babel plugin that transforms object rest and spread syntax.
/**
* Main plugin factory function that creates the Babel plugin
* @param api - Babel API object providing compilation context and utilities
* @param opts - Plugin configuration options (optional)
* @returns Babel plugin object with name, inheritance, and visitor methods
*/
function objectRestSpreadPlugin(api: BabelAPI, opts?: Options): BabelPlugin;
// Default export (the actual exported function)
export default objectRestSpreadPlugin;Complete type definitions for the plugin API:
interface BabelPlugin {
name: string;
inherits: any;
visitor: {
Function(path: NodePath<t.Function>): void;
VariableDeclarator(path: NodePath<t.VariableDeclarator>, file: PluginPass): void;
ExportNamedDeclaration(path: NodePath<t.ExportNamedDeclaration>): void;
CatchClause(path: NodePath<t.CatchClause>): void;
AssignmentExpression(path: NodePath<t.AssignmentExpression>, file: PluginPass): void;
ForXStatement(path: NodePath<t.ForXStatement>): void;
ArrayPattern(path: NodePath<t.ArrayPattern>): void;
ObjectExpression(path: NodePath<t.ObjectExpression>, file: PluginPass): void;
};
}
interface BabelAPI {
assertVersion(version: number): void;
targets(): any;
assumption(name: string): boolean | undefined;
}Plugin options interface for customizing transformation behavior.
interface Options {
/**
* Whether to use built-in Object.assign instead of Babel helpers
* @default Determined by compilation targets (true if Object.assign is supported)
*/
useBuiltIns?: boolean;
/**
* Whether to use loose transformations for better performance
* @default false
*/
loose?: boolean;
}The plugin handles these specific object rest and spread patterns:
const { a, ...rest } = objfunction fn({ a, ...rest }) {}({ a, ...rest } = obj)for (const { a, ...rest } of items) {}catch ({ message, ...error }) {}const [{ a, ...rest }] = array{ ...obj, b: 2 }{ ...obj1, ...obj2, c: 3 }{ a: 1, ...obj, b: 2 }The plugin implements these AST visitor methods for transformation:
const { a, ...rest } = obj)({ a, ...rest } = obj)){ ...obj })The plugin respects Babel's assumption system:
ignoreFunctionLength: Affects parameter transformation behavior (default: loose value)objectRestNoSymbols: Uses loose object rest helpers without symbol support (default: loose value)pureGetters: Assumes object property getters are pure (default: loose value)setSpreadProperties: Uses Object.assign-style property setting for spreads (default: loose value)The plugin generates calls to various Babel helper functions based on the source code analysis:
extends: Core helper for object extension (or Object.assign when useBuiltIns=true)objectWithoutProperties: Standard object rest helperobjectWithoutPropertiesLoose: Loose mode object rest helper (when objectRestNoSymbols assumption is true)objectSpread2: Modern object spread helper (preferred)objectSpread: Legacy object spread helper (fallback)objectDestructuringEmpty: For empty object destructuring patternstoPropertyKey: For computed property key normalizationThe plugin validates configuration options:
// Throws error if loose option is not boolean
if (typeof loose !== "boolean") {
throw new Error(".loose must be a boolean, or undefined");
}The plugin requires these Babel packages (from actual package.json):
@babel/helper-plugin-utils: Plugin creation utilities@babel/plugin-syntax-object-rest-spread: Syntax parsing support (^7.8.3)@babel/plugin-transform-parameters: Parameter transformation utilities@babel/helper-compilation-targets: Target environment compatibility@babel/compat-data: Browser/environment compatibility dataPeer Dependencies:
@babel/core: ^7.0.0-0Dev Dependencies:
@babel/helper-plugin-test-runner: For plugin testing@babel/parser: AST parsing utilities