Babel plugin that transforms member expressions to ensure ES3 compatibility by converting dot notation with reserved words to bracket notation
npx @tessl/cli install tessl/npm-babel-plugin-transform-es3-member-expression-literals@6.22.0A Babel plugin that transforms JavaScript member expressions to ensure ES3 compatibility by converting dot notation property accesses that use reserved words or invalid identifiers into bracket notation with string literals.
npm install --save-dev babel-plugin-transform-es3-member-expression-literalsThe plugin is imported as a default export:
import babelPluginTransformEs3MemberExpressionLiterals from "babel-plugin-transform-es3-member-expression-literals";For CommonJS:
const babelPluginTransformEs3MemberExpressionLiterals = require("babel-plugin-transform-es3-member-expression-literals");The plugin is typically used through Babel configuration rather than direct import:
Via .babelrc (Recommended):
{
"plugins": ["transform-es3-member-expression-literals"]
}Via Babel CLI:
babel --plugins transform-es3-member-expression-literals script.jsVia Node API:
const babel = require("babel-core");
const result = babel.transform(code, {
plugins: ["transform-es3-member-expression-literals"]
});Transformation Example:
// Input
foo.catch;
foo.default;
obj.class;
// Output
foo["catch"];
foo["default"];
obj["class"];The plugin implements the standard Babel plugin architecture:
The main export is a Babel plugin factory function that creates the plugin configuration.
/**
* Babel plugin factory function that creates ES3 member expression transformation plugin
* @param {Object} context - Babel plugin context
* @param {Object} context.types - Babel types utility (aliased as 't')
* @returns {BabelPlugin} Plugin configuration object with visitor pattern
*/
export default function ({ types: t }) { ... }
interface BabelPlugin {
visitor: {
MemberExpression: {
exit: Function;
};
};
}The plugin implements a visitor for MemberExpression AST nodes that transforms dot notation to bracket notation when necessary.
/**
* AST visitor exit handler for MemberExpression nodes
* Transforms dot notation member expressions using invalid ES3 identifiers
* @param {Object} context - Visitor context
* @param {MemberExpression} context.node - The member expression AST node
*/
visitor: {
MemberExpression: {
exit({ node }) {
// Implementation transforms dot notation to bracket notation
// for invalid ES3 identifiers
}
}
}The plugin performs the following transformation logic:
!node.computed)t.isIdentifier(prop))!t.isValidIdentifier(prop.name))node.property = t.stringLiteral(prop.name)node.computed = trueExamples of transformed identifiers:
catch, class, default, enum, export, import, superlet, const, staticimplements, interface, package, private, protected, publicinterface MemberExpression {
/** The object being accessed (e.g., 'foo' in 'foo.bar') */
object: Expression;
/** The property being accessed (e.g., 'bar' in 'foo.bar') */
property: Identifier | Expression;
/** Whether the property access uses bracket notation */
computed: boolean;
}
interface Identifier {
/** The identifier name */
name: string;
}
interface StringLiteral {
/** The string value */
value: string;
}isValidIdentifier() function that uses esutils to determine if an identifier is valid in ES3The plugin operates during the Babel compilation phase and will fail compilation if:
No runtime errors are thrown by the transformed code itself, as the transformation only changes syntax while preserving semantics.