or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--plugin-transform-property-literals

Babel plugin that ensures reserved words are quoted in object property keys

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/plugin-transform-property-literals@7.27.x

To install, run

npx @tessl/cli install tessl/npm-babel--plugin-transform-property-literals@7.27.0

index.mddocs/

@babel/plugin-transform-property-literals

A Babel plugin that transforms object property keys that are ES3-specific reserved words into quoted string literals. This plugin specifically targets identifiers that are valid in modern JavaScript but reserved in ES3, ensuring compatibility with older JavaScript environments that follow the ES3 specification.

Package Information

  • Package Name: @babel/plugin-transform-property-literals
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-transform-property-literals

Core Imports

const pluginTransformPropertyLiterals = require("@babel/plugin-transform-property-literals");

For ES modules:

import pluginTransformPropertyLiterals from "@babel/plugin-transform-property-literals";

Basic Usage

Babel Configuration

Add the plugin to your Babel configuration:

{
  "plugins": ["@babel/plugin-transform-property-literals"]
}

Programmatic Usage

import { transformSync } from "@babel/core";
import pluginTransformPropertyLiterals from "@babel/plugin-transform-property-literals";

const code = `
const obj = {
  interface: "value",
  package: "name",
  implements: "feature",
  private: "access",
  validName: "unchanged"
};
`;

const result = transformSync(code, {
  plugins: [pluginTransformPropertyLiterals]
});

console.log(result.code);
// Output: const obj = { "interface": "value", "package": "name", "implements": "feature", "private": "access", validName: "unchanged" };

Capabilities

Plugin Export

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

declare function pluginTransformPropertyLiterals(api: PluginAPI): BabelPlugin;

interface PluginAPI {
  assertVersion(range: number | string): void;
  version: string;
  types: typeof BabelTypes;
  template: TemplateAPI;
  env: EnvAPI;
  caller?: (callback: (caller: any) => any) => any;
}

interface BabelPlugin {
  name: string;
  visitor: {
    ObjectProperty: {
      exit(path: NodePath<ObjectProperty>): void;
    };
  };
}

Transformation Logic

The plugin automatically identifies and transforms object property keys that are:

  • Not computed properties (non-bracket notation)
  • Valid JavaScript identifiers
  • NOT valid ES3 identifiers (ES3-specific reserved words)

ES3-specific reserved words that get transformed:

  • abstract, boolean, byte, char, double, enum, final, float, goto
  • implements, int, interface, long, native, package
  • private, protected, public, short, static
  • synchronized, throws, transient, volatile

Examples of transformations:

  • { interface: "value" }{ "interface": "value" }
  • { package: "name" }{ "package": "name" }
  • { implements: true }{ "implements": true }
  • { private: 1 }{ "private": 1 }

Properties that remain unchanged:

  • { default: "value" } (regular JS reserved word, valid ES3 identifier)
  • { class: "name" } (regular JS reserved word, valid ES3 identifier)
  • { validName: "value" } (valid ES3 identifier)
  • { "already-quoted": "value" } (already a string literal)
  • { [computed]: "value" } (computed property)
  • { 123: "value" } (numeric literal)

Types

interface ObjectProperty {
  type: "ObjectProperty";
  key: Identifier | StringLiteral | NumericLiteral | PrivateName;
  value: Expression | PatternLike;
  computed: boolean;
  shorthand: boolean;
  decorators?: Decorator[];
}

interface Identifier {
  type: "Identifier";
  name: string;
}

interface StringLiteral {
  type: "StringLiteral";
  value: string;
}

interface NodePath<T> {
  node: T;
  replaceWith(replacement: any): void;
  remove(): void;
}

Dependencies

This plugin requires:

  • @babel/core: ^7.0.0-0 (peer dependency)
  • @babel/helper-plugin-utils: Internal Babel helper for plugin creation (workspace dependency)

Compatibility

  • Node.js: >=6.9.0
  • Babel: 7.x
  • Target: ES3-compatible JavaScript environments

Use Cases

  • Legacy Browser Support: Ensuring code works in older JavaScript engines
  • Strict ES3 Compliance: Converting modern property syntax to ES3-safe equivalents
  • Build Pipelines: Automatically handling reserved word conflicts in object literals
  • Code Migration: Preparing modern JavaScript for deployment in restrictive environments