Babel plugin that ensures reserved words are quoted in object property keys
npx @tessl/cli install tessl/npm-babel--plugin-transform-property-literals@7.27.0A 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.
npm install --save-dev @babel/plugin-transform-property-literalsconst pluginTransformPropertyLiterals = require("@babel/plugin-transform-property-literals");For ES modules:
import pluginTransformPropertyLiterals from "@babel/plugin-transform-property-literals";Add the plugin to your Babel configuration:
{
"plugins": ["@babel/plugin-transform-property-literals"]
}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" };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;
};
};
}The plugin automatically identifies and transforms object property keys that are:
ES3-specific reserved words that get transformed:
abstract, boolean, byte, char, double, enum, final, float, gotoimplements, int, interface, long, native, packageprivate, protected, public, short, staticsynchronized, throws, transient, volatileExamples 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)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;
}This plugin requires: