What is @babel/plugin-transform-property-literals?
The @babel/plugin-transform-property-literals package is a Babel plugin that transforms object properties to use literals when they are valid identifiers. This transformation can help improve code readability and potentially optimize execution by leveraging engine optimizations for property access.
What are @babel/plugin-transform-property-literals's main functionalities?
Transforming Reserved Word Properties
Transforms reserved word properties into literal properties if they are valid identifiers, ensuring compatibility and potentially improving execution speed.
{"var": "value"} // Before transformation
{"var": "value"} // After transformation
Transforming Numeric Properties
Automatically transforms numeric properties into their literal form, enhancing code readability and leveraging engine optimizations.
{"123": "value"} // Before transformation
{123: "value"} // After transformation
Other packages similar to @babel/plugin-transform-property-literals
@babel/plugin-transform-reserved-words
This plugin transforms reserved words into a form that can be safely used as property keys. It is similar to @babel/plugin-transform-property-literals but focuses more on reserved words in JavaScript.
@babel/plugin-transform-property-mutators
Transforms property mutators to use a more compatible syntax. While it deals with properties, its focus is more on the mutation aspect rather than transforming property keys into literals.
@babel/plugin-transform-property-literals
Ensure that reserved words are quoted in object property keys
Example
In
var foo = {
catch: function () {}
};
Out
var foo = {
"catch": function () {}
};
Installation
npm install --save-dev @babel/plugin-transform-property-literals
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["@babel/plugin-transform-property-literals"]
}
Via CLI
babel --plugins @babel/plugin-transform-property-literals script.js
Via Node API
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-property-literals"]
});