Compile ES2015 shorthand properties to ES5
npx @tessl/cli install tessl/npm-babel-plugin-transform-es2015-shorthand-properties@6.24.0A Babel plugin that transforms ES2015 shorthand property syntax into ES5-compatible verbose property syntax. This plugin handles two main transformations: converting shorthand object properties and transforming method shorthand syntax to ensure backward compatibility with ES5 environments.
npm install --save-dev babel-plugin-transform-es2015-shorthand-properties// For direct plugin usage
const plugin = require("babel-plugin-transform-es2015-shorthand-properties");
// Typically used via Babel configuration rather than direct imports{
"plugins": ["transform-es2015-shorthand-properties"]
}babel --plugins transform-es2015-shorthand-properties script.jsrequire("babel-core").transform("code", {
plugins: ["transform-es2015-shorthand-properties"]
});Input:
var o = { a, b, c };Output:
var o = { a: a, b: b, c: c };Input:
var cat = {
getName() {
return name;
}
};Output:
var cat = {
getName: function () {
return name;
}
};The main export is a function that returns a Babel plugin configuration object.
/**
* Main plugin factory function that returns Babel plugin configuration
* @returns {BabelPlugin} Babel plugin configuration object with visitor pattern
*/
function(): BabelPlugin;
interface BabelPlugin {
visitor: {
ObjectMethod: (path: NodePath) => void;
ObjectProperty: (pathObject: { node: Node }) => void;
};
}Transforms ES2015 method shorthand syntax to ES5 function expressions.
/**
* Transforms method shorthand syntax to function expression properties
* @param {NodePath} path - Babel NodePath object containing the ObjectMethod node
* @description Converts method() {} to method: function() {}
*/
ObjectMethod(path: NodePath): void;The visitor specifically handles nodes where node.kind === "method" and:
babel-types.functionExpression()babel-types.objectProperty()Transforms ES2015 property shorthand syntax to ES5 verbose syntax.
/**
* Transforms property shorthand syntax to verbose property syntax
* @param {Object} pathObject - Object containing the node property
* @param {Node} pathObject.node - Babel Node object for ObjectProperty
* @description Converts {x} to {x: x}
*/
ObjectProperty({ node }: { node: Node }): void;The visitor:
node.shorthand === true)node.shorthand = false to convert to verbose syntax{a} to {a: a}interface NodePath {
node: Node;
replaceWith(replacement: Node): void;
}
interface Node {
kind?: string;
key: Node;
params: Node[];
body: Node;
generator: boolean;
async: boolean;
returnType?: Node;
computed: boolean;
shorthand?: boolean;
}This plugin is typically used:
All transformations maintain original code semantics while ensuring ES5 compatibility.