From 2827d660fc08700f6ad982d1bb06bc3c2e8a4089 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Fri, 14 Oct 2016 22:21:11 +0300 Subject: [PATCH] Make special case for class property initializers in `shadow-functions` (#4502) --- .../internal-plugins/shadow-functions.js | 18 +++++++++++------- .../actual.js | 10 ++++++++++ .../expected.js | 10 ++++++++++ .../options.json | 3 +++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/actual.js create mode 100644 packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/expected.js create mode 100644 packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/options.json diff --git a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js index 4eb08783f2..572b9bb379 100644 --- a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js +++ b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js @@ -49,21 +49,25 @@ function remap(path, key) { let currentFunction; let passedShadowFunction = false; - let fnPath = path.findParent(function (path) { - if (path.isProgram() || path.isFunction()) { + let fnPath = path.find(function (innerPath) { + if (innerPath.parentPath && innerPath.parentPath.isClassProperty() && innerPath.key === "value") { + return true; + } + if (path === innerPath) return false; + if (innerPath.isProgram() || innerPath.isFunction()) { // catch current function in case this is the shadowed one and we can ignore it - currentFunction = currentFunction || path; + currentFunction = currentFunction || innerPath; } - if (path.isProgram()) { + if (innerPath.isProgram()) { passedShadowFunction = true; return true; - } else if (path.isFunction() && !path.isArrowFunctionExpression()) { + } else if (innerPath.isFunction() && !innerPath.isArrowFunctionExpression()) { if (shadowFunction) { - if (path === shadowFunction || path.node === shadowFunction.node) return true; + if (innerPath === shadowFunction || innerPath.node === shadowFunction.node) return true; } else { - if (!path.is("shadow")) return true; + if (!innerPath.is("shadow")) return true; } passedShadowFunction = true; diff --git a/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/actual.js b/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/actual.js new file mode 100644 index 0000000000..0a1fda2178 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/actual.js @@ -0,0 +1,10 @@ +class A { + prop1 = () => this; + static prop2 = () => this; + prop3 = () => arguments; + static prop4 = () => arguments; + prop5 = this; + static prop6 = this; + prop7 = arguments; + static prop8 = arguments; +} diff --git a/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/expected.js b/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/expected.js new file mode 100644 index 0000000000..0a1fda2178 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/expected.js @@ -0,0 +1,10 @@ +class A { + prop1 = () => this; + static prop2 = () => this; + prop3 = () => arguments; + static prop4 = () => arguments; + prop5 = this; + static prop6 = this; + prop7 = arguments; + static prop8 = arguments; +} diff --git a/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/options.json b/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/options.json new file mode 100644 index 0000000000..7b4b869750 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/misc/class-property-initializer-blocks-shadow/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["syntax-class-properties"] +}