Skip to main content

@babel/plugin-transform-duplicate-keys

信息

此插件包含在 @babel/preset-env

¥This plugin is included in @babel/preset-env

这个插件实际上将对象中的重复键转换为计算属性,然后必须由 @babel/plugin-transform-computed-properties 插件处理。最终结果将不包含任何具有重复键的对象字面量。

¥This plugin actually converts duplicate keys in objects to be computed properties, which then must be handled by the @babel/plugin-transform-computed-properties plugin. The final result won't contain any object literals with duplicate keys.

示例

¥Example

输入

¥In

JavaScript
var x = { a: 5, a: 6 };
var y = {
get a() {},
set a(x) {},
a: 3,
};

输出

¥Out

JavaScript
var x = { a: 5, ["a"]: 6 };
var y = {
get a() {},
set a(x) {},
["a"]: 3,
};

安装

¥Installation

npm install --save-dev @babel/plugin-transform-duplicate-keys

用法

¥Usage

¥With a configuration file (Recommended)

babel.config.json
{
"plugins": ["@babel/plugin-transform-duplicate-keys"]
}

通过 CLI

¥Via CLI

Shell
babel --plugins @babel/plugin-transform-duplicate-keys script.js

通过 Node API

¥Via Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-duplicate-keys"],
});