From 6127a26000b33a3a20a3458c5363be7c8917f467 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Wed, 20 Jan 2021 11:20:00 +0100 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20correctly=C2=A0transform=20`=5F=5Fpr?= =?UTF-8?q?oto=5F=5F`=C2=A0properties?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/index.js | 2 -- .../test/fixtures/spec/proto/input.js | 3 +++ .../test/fixtures/spec/proto/output.js | 1 + .../src/index.js | 11 ++++++++++- .../test/fixtures/shorthand-properties/proto/input.js | 7 +++++++ .../fixtures/shorthand-properties/proto/output.js | 6 ++++++ 6 files changed, 27 insertions(+), 3 deletions(-) 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 dc34b5b5b061..eabb277b3e42 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/input.js b/packages/babel-plugin-transform-computed-properties/test/fixtures/spec/proto/input.js new file mode 100644 index 000000000000..6638af89d586 --- /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 000000000000..347e6e060e9e --- /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 191e67d72691..74c6ffb1bfaa 100644 --- a/packages/babel-plugin-transform-shorthand-properties/src/index.js +++ b/packages/babel-plugin-transform-shorthand-properties/src/index.js @@ -20,13 +20,22 @@ 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 }) { if (node.shorthand) { node.shorthand = false; + if (t.isIdentifier(node.key, { name: "__proto__" })) { + node.key = t.stringLiteral(node.key.name); + node.computed = true; + } } }, }, 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 000000000000..f0389cab1283 --- /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 000000000000..252e4cac7a24 --- /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 () {} +}; From 0357fe1d8e072236ab5c169759d1434c679ad62a Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Thu, 21 Jan 2021 11:10:00 +0100 Subject: [PATCH 2/2] =?UTF-8?q?fixup!=20fix:=20correctly=C2=A0transform=20?= =?UTF-8?q?`=5F=5Fproto=5F=5F`=C2=A0properties?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/fixtures/spec/proto-shorthand/input.js | 11 +++++++++++ .../test/fixtures/spec/proto-shorthand/options.json | 7 +++++++ .../test/fixtures/spec/proto-shorthand/output.js | 3 +++ .../src/index.js | 12 +++++++----- 4 files changed, 28 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 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 000000000000..c48d156bc168 --- /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 000000000000..ab2481ec9f28 --- /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 000000000000..f658adb0b495 --- /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-shorthand-properties/src/index.js b/packages/babel-plugin-transform-shorthand-properties/src/index.js index 74c6ffb1bfaa..61927f050a1f 100644 --- a/packages/babel-plugin-transform-shorthand-properties/src/index.js +++ b/packages/babel-plugin-transform-shorthand-properties/src/index.js @@ -29,12 +29,14 @@ export default declare(api => { } }, - ObjectProperty({ node }) { + ObjectProperty(path) { + const { node } = path; if (node.shorthand) { - node.shorthand = false; - if (t.isIdentifier(node.key, { name: "__proto__" })) { - node.key = t.stringLiteral(node.key.name); - node.computed = true; + const computedKey = t.toComputedKey(node); + if (t.isStringLiteral(computedKey, { value: "__proto__" })) { + path.replaceWith(t.objectProperty(computedKey, node.value, true)); + } else { + node.shorthand = false; } } },