or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

babel-plugin-syntax-object-rest-spread

babel-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.

Package Information

  • Package Name: babel-plugin-syntax-object-rest-spread
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install babel-plugin-syntax-object-rest-spread

Core Imports

// This plugin is not imported directly in code
// It is configured in Babel configuration files

Basic Usage

Via .babelrc (Recommended)

{
  "plugins": ["syntax-object-rest-spread"]
}

Via CLI

babel --plugins syntax-object-rest-spread script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["syntax-object-rest-spread"]
});

Architecture

babel-plugin-syntax-object-rest-spread follows the standard Babel plugin architecture:

  • Plugin Function: Default export function that returns a Babel plugin configuration object
  • Parser Integration: Manipulates Babel's parser options to enable syntax recognition
  • Syntax Enhancement: Adds "objectRestSpread" to the parser's plugin list during parsing phase
  • Passive Operation: Only enables parsing - does not transform or modify the AST

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.

Capabilities

Default Plugin Function

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.

Parser Options Manipulation

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)

Types

// 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;
}

Usage Examples

Enabling Object Rest Syntax

With the plugin configured, you can parse object rest patterns:

// Input code that can now be parsed
const {name, age, ...otherProps} = person;

Enabling Object Spread Syntax

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'
};

Integration with Other Babel Plugins

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.

Error Handling

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.