diff --git a/packages/babel-helper-module-transforms/package.json b/packages/babel-helper-module-transforms/package.json index f1c64d3c8c18..e97bd5332481 100644 --- a/packages/babel-helper-module-transforms/package.json +++ b/packages/babel-helper-module-transforms/package.json @@ -12,6 +12,7 @@ "main": "lib/index.js", "dependencies": { "@babel/helper-module-imports": "^7.8.3", + "@babel/helper-replace-supers": "^7.8.3", "@babel/helper-simple-access": "^7.8.3", "@babel/helper-split-export-declaration": "^7.8.3", "@babel/template": "^7.8.3", diff --git a/packages/babel-helper-module-transforms/src/rewrite-this.js b/packages/babel-helper-module-transforms/src/rewrite-this.js index 1c572c2d40e9..cf03366b0ae6 100644 --- a/packages/babel-helper-module-transforms/src/rewrite-this.js +++ b/packages/babel-helper-module-transforms/src/rewrite-this.js @@ -1,3 +1,4 @@ +import { skipAllButComputedKey } from "@babel/helper-replace-supers"; export default function rewriteThis(programPath: NodePath) { // Rewrite "this" to be "undefined". programPath.traverse(rewriteThisVisitor); @@ -12,10 +13,11 @@ const rewriteThisVisitor = { path.replaceWith(path.scope.buildUndefinedNode()); }, Function(path) { - if (!path.isArrowFunctionExpression()) path.skip(); + if (path.isMethod()) skipAllButComputedKey(path); + else if (!path.isArrowFunctionExpression()) path.skip(); }, ClassProperty(path) { - path.skip(); + skipAllButComputedKey(path); }, ClassPrivateProperty(path) { path.skip(); diff --git a/packages/babel-helper-replace-supers/src/index.js b/packages/babel-helper-replace-supers/src/index.js index 8f117a7a6b70..5c7faee42ce4 100644 --- a/packages/babel-helper-replace-supers/src/index.js +++ b/packages/babel-helper-replace-supers/src/index.js @@ -26,7 +26,7 @@ function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) { return t.callExpression(file.addHelper("getPrototypeOf"), [targetRef]); } -function skipAllButComputedKey(path: NodePath) { +export function skipAllButComputedKey(path: NodePath) { // If the path isn't computed, just skip everything. if (!path.node.computed) { path.skip(); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/input.mjs new file mode 100644 index 000000000000..803624cdb476 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/input.mjs @@ -0,0 +1 @@ +export class C { [this.name]() {} } diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/options.json b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/options.json new file mode 100644 index 000000000000..60ad58ace53a --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "external-helpers", + "transform-modules-commonjs", + "syntax-class-properties" + ] +} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/output.js new file mode 100644 index 000000000000..c2b9efb88c0d --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-1/output.js @@ -0,0 +1,13 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.C = void 0; + +class C { + [(void 0).name]() {} + +} + +exports.C = C; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/input.mjs new file mode 100644 index 000000000000..55156089e6a8 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/input.mjs @@ -0,0 +1,3 @@ +class A { + [() => this.name]() {} +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/options.json b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/options.json new file mode 100644 index 000000000000..60ad58ace53a --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "external-helpers", + "transform-modules-commonjs", + "syntax-class-properties" + ] +} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/output.js new file mode 100644 index 000000000000..8536a6332e60 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-2/output.js @@ -0,0 +1,6 @@ +"use strict"; + +class A { + [() => (void 0).name]() {} + +} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/input.mjs new file mode 100644 index 000000000000..878eeffad744 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/input.mjs @@ -0,0 +1,3 @@ +class A { + [function () { this.name; }]() {} +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/options.json b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/options.json new file mode 100644 index 000000000000..60ad58ace53a --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "external-helpers", + "transform-modules-commonjs", + "syntax-class-properties" + ] +} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/output.js new file mode 100644 index 000000000000..4f99944fc431 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-method-3/output.js @@ -0,0 +1,8 @@ +"use strict"; + +class A { + [function () { + this.name; + }]() {} + +} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/input.mjs new file mode 100644 index 000000000000..f65287402e51 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/input.mjs @@ -0,0 +1 @@ +export class C { [this.name] = 42 } \ No newline at end of file diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/options.json b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/options.json new file mode 100644 index 000000000000..60ad58ace53a --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "external-helpers", + "transform-modules-commonjs", + "syntax-class-properties" + ] +} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/output.js new file mode 100644 index 000000000000..9c05bd6e9a4e --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/undefined-this-computed-class-property-name/output.js @@ -0,0 +1,12 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.C = void 0; + +class C { + [(void 0).name] = 42; +} + +exports.C = C;