Babel plugin that ensures reserved words are quoted in object property keys
npx @tessl/cli install tessl/npm-babel-plugin-transform-es3-property-literals@6.22.0A Babel plugin that transforms object property keys using reserved words or invalid identifiers into quoted string literals, ensuring ES3 compatibility for legacy JavaScript environments like Internet Explorer 8 and below.
npm install --save-dev babel-plugin-transform-es3-property-literalsThe plugin is imported as a standard Babel plugin - there are no direct imports in user code since Babel handles the plugin loading.
Plugin Export Structure:
// ESM (source)
export default function ({ types: t }) { /* ... */ }
// CommonJS (built)
module.exports = function ({ types: t }) { /* ... */ }
module.exports.default = module.exports;Babel automatically handles the plugin loading when configured in .babelrc or via API.
{
"plugins": ["transform-es3-property-literals"]
}babel --plugins transform-es3-property-literals script.jsrequire("babel-core").transform("code", {
plugins: ["transform-es3-property-literals"]
});Input:
var obj = {
catch: function () {},
default: "value",
class: "MyClass",
try: "reserved",
finally: "keyword",
switch: "statement",
validIdentifier: "unchanged"
};Output:
var obj = {
"catch": function () {},
"default": "value",
"class": "MyClass",
"try": "reserved",
"finally": "keyword",
"switch": "statement",
validIdentifier: "unchanged"
};Properties that ARE transformed:
catch, default, class, try, finally, switch, case, return, function, var, new, this, with, typeof, instanceof, in, void, delete, throw, debugger, etc.t.isValidIdentifier() checkProperties that are NOT transformed:
"catch": value → remains unchanged[key]: value → remains unchangedvalidName: value → remains unchanged42: value → remains unchangedThis plugin operates within Babel's transformation pipeline using the visitor pattern:
t.isValidIdentifier() utility to determine if property keys need quotingIntegration with Babel Ecosystem:
babel-plugin-transform-es3-member-expression-literals)babel-runtimeThe main export that creates the Babel plugin instance.
/**
* Babel plugin factory function that returns a plugin object
* @param {Object} params - Babel plugin parameters
* @param {Object} params.types - Babel types utilities (destructured as 't')
* @returns {Object} Babel plugin object with visitor pattern
*/
export default function ({ types: t }) {
return {
visitor: {
ObjectProperty: {
exit({ node }) {
// Plugin transformation logic
}
}
}
};
}Plugin Structure:
visitor propertyObjectProperty nodes specificallyThe visitor method that processes object property nodes in the Abstract Syntax Tree (AST).
/**
* ObjectProperty visitor that transforms invalid identifiers to string literals
* @param {Object} path - Babel path object containing the AST node
* @param {Object} path.node - The ObjectProperty AST node being visited
*/
ObjectProperty: {
exit({ node }) {
const key = node.key;
if (!node.computed && t.isIdentifier(key) && !t.isValidIdentifier(key.name)) {
// Transform: default: "bar" -> "default": "bar"
node.key = t.stringLiteral(key.name);
}
}
}Transformation Logic:
!node.computed && t.isIdentifier(key))t.isValidIdentifier(key.name)t.stringLiteral(key.name)interface BabelPlugin {
visitor: {
[NodeType: string]: {
enter?: (path: BabelPath) => void;
exit?: (path: BabelPath) => void;
} | ((path: BabelPath) => void);
};
}
interface BabelPath {
node: ASTNode;
// Additional Babel path properties...
}interface ObjectProperty {
type: "ObjectProperty";
key: Identifier | StringLiteral | Expression;
value: Expression;
computed: boolean;
// Additional properties...
}
interface Identifier {
type: "Identifier";
name: string;
}
interface StringLiteral {
type: "StringLiteral";
value: string;
}This plugin ensures JavaScript code runs in ES3 environments by addressing these issues:
class, default, catch, try, etc. cannot be used as unquoted property names in ES333rd) or containing special characters (!@#$) must be quotedThe transformation is safe and backward-compatible - quoted property names work in all JavaScript environments, while unquoted invalid identifiers only work in ES5+ environments.
The plugin operates during the Babel compilation phase and does not throw runtime errors. Any AST manipulation errors would be caught by Babel's internal error handling system during the build process.