diff --git a/packages/babel-plugin-transform-computed-properties/src/index.js b/packages/babel-plugin-transform-computed-properties/src/index.js index dc34b5b5b0..eabb277b3e 100644 --- a/packages/babel-plugin-transform-computed-properties/src/index.js +++ b/packages/babel-plugin-transform-computed-properties/src/index.js @@ -90,8 +90,6 @@ export default declare((api, options) => { if (prop.kind === "get" || prop.kind === "set") { pushMutatorDefine(info, prop); - } else if (t.isStringLiteral(key, { value: "__proto__" })) { - pushAssign(objId, prop, body); } else { if (computedProps.length === 1) { return t.callExpression(state.addHelper("defineProperty"), [ diff --git a/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/input.js b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/input.js new file mode 100644 index 0000000000..c48d156bc1 --- /dev/null +++ b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/input.js @@ -0,0 +1,11 @@ +var shorthand = { + __proto__, +} + +var method = { + __proto__() {} +} + +var methodComputed = { + ["__proto__"]() {} +} diff --git a/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/options.json b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/options.json new file mode 100644 index 0000000000..ab2481ec9f --- /dev/null +++ b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "external-helpers", + "transform-computed-properties", + "transform-shorthand-properties" + ] +} diff --git a/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/output.js b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/output.js new file mode 100644 index 0000000000..f658adb0b4 --- /dev/null +++ b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/output.js @@ -0,0 +1,3 @@ +var shorthand = babelHelpers.defineProperty({}, "__proto__", __proto__); +var method = babelHelpers.defineProperty({}, "__proto__", function () {}); +var methodComputed = babelHelpers.defineProperty({}, "__proto__", function () {}); diff --git a/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/input.js b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/input.js new file mode 100644 index 0000000000..6638af89d5 --- /dev/null +++ b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/input.js @@ -0,0 +1,3 @@ +var obj = { + ['__proto__']: 123 +}; diff --git a/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/output.js b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/output.js new file mode 100644 index 0000000000..347e6e060e --- /dev/null +++ b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/output.js @@ -0,0 +1 @@ +var obj = babelHelpers.defineProperty({}, '__proto__', 123); diff --git a/packages/babel-plugin-transform-shorthand-properties/src/index.js b/packages/babel-plugin-transform-shorthand-properties/src/index.js index 191e67d726..61927f050a 100644 --- a/packages/babel-plugin-transform-shorthand-properties/src/index.js +++ b/packages/babel-plugin-transform-shorthand-properties/src/index.js @@ -20,13 +20,24 @@ export default declare(api => { ); func.returnType = node.returnType; - path.replaceWith(t.objectProperty(node.key, func, node.computed)); + const computedKey = t.toComputedKey(node); + if (t.isStringLiteral(computedKey, { value: "__proto__" })) { + path.replaceWith(t.objectProperty(computedKey, func, true)); + } else { + path.replaceWith(t.objectProperty(node.key, func, node.computed)); + } } }, - ObjectProperty({ node }) { + ObjectProperty(path) { + const { node } = path; if (node.shorthand) { - node.shorthand = false; + const computedKey = t.toComputedKey(node); + if (t.isStringLiteral(computedKey, { value: "__proto__" })) { + path.replaceWith(t.objectProperty(computedKey, node.value, true)); + } else { + node.shorthand = false; + } } }, }, diff --git a/packages/babel-plugin-transform-shorthand-properties/test/fixtures/shorthand-properties/proto/input.js b/packages/babel-plugin-transform-shorthand-properties/test/fixtures/shorthand-properties/proto/input.js new file mode 100644 index 0000000000..f0389cab12 --- /dev/null +++ b/packages/babel-plugin-transform-shorthand-properties/test/fixtures/shorthand-properties/proto/input.js @@ -0,0 +1,7 @@ +var shorthand = { + __proto__, +} + +var method = { + __proto__() {} +} diff --git a/packages/babel-plugin-transform-shorthand-properties/test/fixtures/shorthand-properties/proto/output.js b/packages/babel-plugin-transform-shorthand-properties/test/fixtures/shorthand-properties/proto/output.js new file mode 100644 index 0000000000..252e4cac7a --- /dev/null +++ b/packages/babel-plugin-transform-shorthand-properties/test/fixtures/shorthand-properties/proto/output.js @@ -0,0 +1,6 @@ +var shorthand = { + ["__proto__"]: __proto__ +}; +var method = { + ["__proto__"]: function () {} +};