From f1314a1683c2098094346c286232dc42c3c8a41a Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Fri, 22 Jan 2021 10:58:33 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20correctly=C2=A0transform=20`=5F=5Fproto?= =?UTF-8?q?=5F=5F`=C2=A0properties=20(#12664)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: correctly transform `__proto__` properties * fixup! fix: correctly transform `__proto__` properties --- .../src/index.js | 2 -- .../test/fixtures/spec/proto-shorthand/input.js | 11 +++++++++++ .../fixtures/spec/proto-shorthand/options.json | 7 +++++++ .../fixtures/spec/proto-shorthand/output.js | 3 +++ .../test/fixtures/spec/proto/input.js | 3 +++ .../test/fixtures/spec/proto/output.js | 1 + .../src/index.js | 17 ++++++++++++++--- .../shorthand-properties/proto/input.js | 7 +++++++ .../shorthand-properties/proto/output.js | 6 ++++++ 9 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/input.js create mode 100644 packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/options.json create mode 100644 packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto-shorthand/output.js create mode 100644 packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/input.js create mode 100644 packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/output.js create mode 100644 packages/babel-plugin-transform-shorthand-properties/test/fixtures/shorthand-properties/proto/input.js create mode 100644 packages/babel-plugin-transform-shorthand-properties/test/fixtures/shorthand-properties/proto/output.js 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 () {} +};