or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

babel-plugin-transform-es2015-duplicate-keys

This Babel plugin transforms JavaScript object literals containing duplicate property keys into computed properties, ensuring compatibility with ES5 strict mode. It identifies duplicate keys across data properties, getters, and setters, converting subsequent duplicates to computed property syntax.

Package Information

  • Package Name: babel-plugin-transform-es2015-duplicate-keys
  • Package Type: npm
  • Language: JavaScript (ES6)
  • Installation: npm install --save-dev babel-plugin-transform-es2015-duplicate-keys

Core Imports

The plugin is designed to be used as part of the Babel transformation pipeline rather than imported directly:

// Not typically imported directly in user code
// Plugin is configured in .babelrc or Babel configuration

Basic Usage

Via .babelrc (Recommended)

{
  "plugins": ["transform-es2015-duplicate-keys"]
}

Via CLI

babel --plugins transform-es2015-duplicate-keys script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["transform-es2015-duplicate-keys"]
});

Transformation Examples

Input:

var x = { a: 5, a: 6 };
var y = {
  get a() {},
  set a(x) {},
  a: 3
};

Output:

var x = { a: 5, ["a"]: 6 };
var y = {
  get a() {},
  set a(x) {},
  ["a"]: 3
};

Capabilities

Main Plugin Export

The plugin exports a factory function that returns a Babel plugin configuration.

/**
 * Main plugin factory function
 * @returns {Object} Babel plugin configuration object with visitor pattern
 */
export default function(): BabelPlugin;

interface BabelPlugin {
  visitor: {
    ObjectExpression: (path: NodePath<ObjectExpression>) => void;
  };
}

Duplicate Key Detection

The plugin analyzes object expressions and identifies duplicate keys according to these rules:

  • A data property is duplicate if preceded by any data, getter, or setter property of the same name
  • A getter property is duplicate if preceded by any data or getter property of the same name
  • A setter property is duplicate if preceded by any data or setter property of the same name

Key Transformation Logic

When a duplicate key is detected:

  1. The property is marked as computed (prop.computed = true)
  2. The key is converted to a string literal (prop.key = t.stringLiteral(name))
  3. The transform-es2015-computed-properties plugin handles the final transformation

ObjectExpression Visitor

Processes object expression nodes in the AST to identify and transform duplicate keys.

/**
 * Visitor method for ObjectExpression AST nodes
 * @param {NodePath<ObjectExpression>} path - Babel AST node path
 */
ObjectExpression(path: NodePath<ObjectExpression>): void;

Helper Functions

/**
 * Extracts property name from AST key node
 * @param {Identifier | Literal} key - AST key node (Identifier or Literal)
 * @returns {string} Property name as string
 */
function getName(key: Identifier | Literal): string;

Dependencies

  • babel-types: AST node type checking and creation utilities
  • babel-runtime: Babel runtime helpers for compatibility

Coordination with Other Plugins

This plugin works in coordination with babel-plugin-transform-es2015-computed-properties. The duplicate keys plugin converts duplicate keys to computed property syntax, then the computed properties plugin handles the final ES5 transformation.

Error Handling

The plugin relies on Babel's core error handling mechanisms. It performs defensive type checking using babel-types utilities to ensure proper AST node handling.

Types

// From babel-types
interface Identifier {
  type: "Identifier";
  name: string;
}

interface Literal {
  type: "Literal" | "StringLiteral" | "NumericLiteral";
  value: string | number;
}

interface ObjectExpression {
  type: "ObjectExpression";
  properties: ObjectProperty[];
}

interface ObjectProperty {
  type: "ObjectProperty";
  key: Identifier | Literal;
  value: Expression;
  kind: "init" | "get" | "set";
  computed: boolean;
}

interface NodePath<T> {
  node: T;
  // Additional Babel NodePath properties...
}