Babel syntax plugin that enables parsing of object rest/spread syntax in JavaScript code
npx @tessl/cli install tessl/npm-babel-plugin-syntax-object-rest-spread@6.13.0babel-plugin-syntax-object-rest-spread is a Babel syntax plugin that enables parsing of object rest/spread syntax (...) in JavaScript code. It extends Babel's parser capabilities by adding support for the 'objectRestSpread' parser plugin, allowing other Babel plugins and transformations to work with ES2018 object rest/spread syntax during the parsing stage.
npm install babel-plugin-syntax-object-rest-spread// This plugin is not imported directly in code
// It is configured in Babel configuration files{
"plugins": ["syntax-object-rest-spread"]
}babel --plugins syntax-object-rest-spread script.jsrequire("babel-core").transform("code", {
plugins: ["syntax-object-rest-spread"]
});babel-plugin-syntax-object-rest-spread follows the standard Babel plugin architecture:
The plugin works by being loaded by Babel's plugin system, which calls the default function to get the plugin configuration, then calls the manipulateOptions method during Babel's parsing phase to enable object rest/spread syntax recognition.
Main plugin export that provides Babel plugin configuration.
/**
* Default export function that returns Babel plugin configuration
* @param {Object} babel - Babel utilities object (optional, unused by this plugin)
* @param {Object} babel.types - Babel types library for AST manipulation
* @returns {Object} Babel plugin configuration object
*/
function default(babel?: { types: Object }): BabelPluginConfig;
interface BabelPluginConfig {
manipulateOptions: (opts: any, parserOpts: ParserOptions, file: any) => void;
}The default function creates and returns a plugin configuration object that Babel can use to extend its parsing capabilities.
Core functionality that enables object rest/spread syntax parsing.
/**
* Manipulates Babel parser options to enable object rest/spread syntax
* @param {any} opts - Babel transformation options (unused by this plugin)
* @param {ParserOptions} parserOpts - Babel parser options object to modify
* @param {any} file - Babel File instance providing transformation context
* @returns {void} Modifies parserOpts.plugins in place
*/
function manipulateOptions(opts: any, parserOpts: ParserOptions, file: any): void;
interface ParserOptions {
/** Array of parser plugin names to enable specific syntax features */
plugins: string[];
/** Whether to highlight code in error messages */
highlightCode?: boolean;
/** Whether to allow non-standard language features */
nonStandard?: boolean;
/** Source type: "module", "script", or "unambiguous" */
sourceType?: string;
/** Filename of the source being parsed */
filename?: string;
}This method is called during Babel's parsing phase and adds "objectRestSpread" to the parser's plugins array, enabling recognition of object rest/spread syntax patterns like:
const {a, ...rest} = obj; (object rest)const newObj = {a, ...obj}; (object spread)// Core types used by the plugin
interface BabelPluginConfig {
/** Method called during Babel's parsing phase to modify parser options */
manipulateOptions: (opts: any, parserOpts: ParserOptions, file: any) => void;
}
interface ParserOptions {
/** Array of parser plugin names to enable specific syntax features */
plugins: string[];
/** Whether to highlight code in error messages */
highlightCode?: boolean;
/** Whether to allow non-standard language features */
nonStandard?: boolean;
/** Source type: "module", "script", or "unambiguous" */
sourceType?: string;
/** Filename of the source being parsed */
filename?: string;
}With the plugin configured, you can parse object rest patterns:
// Input code that can now be parsed
const {name, age, ...otherProps} = person;With the plugin configured, you can parse object spread patterns:
// Input code that can now be parsed
const newPerson = {
...person,
age: 30,
location: 'New York'
};This syntax plugin is typically used alongside transformation plugins:
{
"plugins": [
"syntax-object-rest-spread",
"transform-object-rest-spread"
]
}The syntax plugin enables parsing, while transform plugins handle the actual code transformation.
This plugin operates at the parsing level and does not throw runtime errors. If object rest/spread syntax is encountered without this plugin enabled, Babel will throw a parsing error indicating unsupported syntax. The plugin itself does not perform validation or transformation, so it cannot generate transformation-related errors.