Merge pull request #3280 from AgentME/dupkeys
Fix T2462, compile duplicate keys in objects to valid strict ES5
This commit is contained in:
commit
6aff776124
@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
src
|
||||
test
|
||||
@ -0,0 +1,40 @@
|
||||
# babel-plugin-transform-es2015-duplicate-keys
|
||||
|
||||
Compile objects with duplicate keys to valid strict ES5.
|
||||
|
||||
This plugin actually converts duplicate keys in objects to be computed
|
||||
properties, which then must be handled by the
|
||||
transform-es2015-computed-properties plugin. The final result won't contain any
|
||||
object literals with duplicate keys.
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
$ npm install babel-plugin-transform-es2015-duplicate-keys
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["transform-es2015-duplicate-keys"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
$ babel --plugins transform-es2015-duplicate-keys script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("babel-core").transform("code", {
|
||||
plugins: ["transform-es2015-duplicate-keys"]
|
||||
});
|
||||
```
|
||||
@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "babel-plugin-transform-es2015-duplicate-keys",
|
||||
"version": "6.4.0",
|
||||
"description": "Compile objects with duplicate keys to valid strict ES5",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-duplicate-keys",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"dependencies": {
|
||||
"babel-runtime": "^5.0.0",
|
||||
"babel-types": "^6.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-helper-plugin-test-runner": "^6.3.13"
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
import * as t from "babel-types";
|
||||
|
||||
function getName(key) {
|
||||
if (t.isIdentifier(key)) {
|
||||
return key.name;
|
||||
}
|
||||
return key.value.toString();
|
||||
}
|
||||
|
||||
export default function() {
|
||||
return {
|
||||
visitor: {
|
||||
ObjectExpression(path) {
|
||||
const { node } = path;
|
||||
const plainProps = node.properties.filter((prop) => !t.isSpreadProperty(prop) && !prop.computed);
|
||||
|
||||
const alreadySeenNames = Object.create(null);
|
||||
|
||||
for (let prop of plainProps) {
|
||||
const name = getName(prop.key);
|
||||
if (!alreadySeenNames[name]) {
|
||||
alreadySeenNames[name] = true;
|
||||
} else {
|
||||
// Rely on the computed properties transform to split the property
|
||||
// assignment out of the object literal.
|
||||
prop.computed = true;
|
||||
prop.key = t.stringLiteral(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, a: 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = babelHelpers.defineProperty({ a: 5 }, "a", 6);
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, b: 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, b: 6 };
|
||||
3
packages/babel-plugin-transform-es2015-duplicate-keys/test/fixtures/combination/options.json
vendored
Normal file
3
packages/babel-plugin-transform-es2015-duplicate-keys/test/fixtures/combination/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", "transform-es2015-duplicate-keys", "transform-es2015-computed-properties"]
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
var x = { "a\n b": 5, "a\n b": 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = { "a\n b": 5, ["a\n b"]: 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, a: 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, ["a"]: 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, get a() {return 6;} };
|
||||
@ -0,0 +1,3 @@
|
||||
var x = { a: 5, get ["a"]() {
|
||||
return 6;
|
||||
} };
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, b: 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, b: 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, "a": 6 };
|
||||
@ -0,0 +1 @@
|
||||
var x = { a: 5, ["a"]: 6 };
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-es2015-duplicate-keys"]
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
require("babel-helper-plugin-test-runner")(__dirname);
|
||||
@ -8,6 +8,7 @@ module.exports = {
|
||||
require("babel-plugin-transform-es2015-classes"),
|
||||
require("babel-plugin-transform-es2015-object-super"),
|
||||
require("babel-plugin-transform-es2015-shorthand-properties"),
|
||||
require("babel-plugin-transform-es2015-duplicate-keys"),
|
||||
require("babel-plugin-transform-es2015-computed-properties"),
|
||||
require("babel-plugin-transform-es2015-for-of"),
|
||||
require("babel-plugin-transform-es2015-sticky-regex"),
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
"babel-plugin-transform-es2015-object-super": "^6.3.13",
|
||||
"babel-plugin-transform-es2015-shorthand-properties": "^6.3.13",
|
||||
"babel-plugin-transform-es2015-computed-properties": "^6.3.13",
|
||||
"babel-plugin-transform-es2015-duplicate-keys": "^6.4.0",
|
||||
"babel-plugin-transform-es2015-for-of": "^6.3.13",
|
||||
"babel-plugin-transform-es2015-sticky-regex": "^6.3.13",
|
||||
"babel-plugin-transform-es2015-unicode-regex": "^6.3.13",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user