diff --git a/packages/babel-helper-member-expression-to-functions/src/index.ts b/packages/babel-helper-member-expression-to-functions/src/index.ts index 6cdddbefb2f2..a6530540f92e 100644 --- a/packages/babel-helper-member-expression-to-functions/src/index.ts +++ b/packages/babel-helper-member-expression-to-functions/src/index.ts @@ -462,8 +462,13 @@ const handle = { return; } - // MEMBER -> _get(MEMBER) - member.replaceWith(this.get(member)); + if (parentPath.isTaggedTemplateExpression()) { + // MEMBER -> _get(MEMBER).bind(this) + member.replaceWith(this.boundGet(member)); + } else { + // MEMBER -> _get(MEMBER) + member.replaceWith(this.get(member)); + } }, }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/exec.js new file mode 100644 index 000000000000..23be68550f2f --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/exec.js @@ -0,0 +1,9 @@ +class Foo { + static #tag = function () { return this }; + + static getReceiver() { + return this.#tag``; + } +} + +expect(Foo.getReceiver()).toBe(Foo); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/input.js new file mode 100644 index 000000000000..e02e5f447d7a --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/input.js @@ -0,0 +1,8 @@ +class Foo { + static #tag = function () { return this }; + + static getReceiver() { + return this.#tag``; + } +} + \ No newline at end of file diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/options.json new file mode 100644 index 000000000000..a842b4d9452d --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "proposal-class-properties", + "proposal-private-methods" + ] +} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/output.js new file mode 100644 index 000000000000..0f7a2d29767e --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template-static/output.js @@ -0,0 +1,13 @@ +class Foo { + static getReceiver() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _tag).bind(this)``; + } + +} + +var _tag = { + writable: true, + value: function () { + return this; + } +}; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/exec.js new file mode 100644 index 000000000000..e79968dd8d1b --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/exec.js @@ -0,0 +1,17 @@ +class Foo { + #tag() { + return this; + } + + #tag2 = function() { return this; }; + + constructor() { + const receiver = this.#tag`tagged template`; + expect(receiver).toBe(this); + + const receiver2 = this.#tag2`tagged template`; + expect(receiver2).toBe(this); + } +} + + new Foo(); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/input.js new file mode 100644 index 000000000000..a93e2db67c12 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/input.js @@ -0,0 +1,9 @@ +class Foo { + #tag; + + test() { + this.#tag``; + } +} + +new Foo(); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/options.json new file mode 100644 index 000000000000..d0c00d1ba3de --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/options.json @@ -0,0 +1,13 @@ +{ + "presets": [ + [ + "@babel/preset-env", + { + "shippedProposals": true, + "targets": { + "chrome": "75" + } + } + ] + ] + } \ No newline at end of file diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/output.js new file mode 100644 index 000000000000..b06497c1fcdc --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/tagged-template/output.js @@ -0,0 +1,24 @@ +var _tag = /*#__PURE__*/new WeakMap(); + +var Foo = /*#__PURE__*/function () { + "use strict"; + + function Foo() { + babelHelpers.classCallCheck(this, Foo); + + _tag.set(this, { + writable: true, + value: void 0 + }); + } + + babelHelpers.createClass(Foo, [{ + key: "test", + value: function test() { + babelHelpers.classPrivateFieldGet(this, _tag).bind(this)``; + } + }]); + return Foo; +}(); + +new Foo(); diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/exec.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/exec.js new file mode 100644 index 000000000000..c3c22f83dadb --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/exec.js @@ -0,0 +1,10 @@ +class Foo { + get #tag() { + return function() { return this; }; + } + + constructor() { + const receiver = this.#tag``; + expect(receiver).toBe(this); + } +} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/input.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/input.js new file mode 100644 index 000000000000..0a8c6fa3bc47 --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/input.js @@ -0,0 +1,11 @@ +class Foo { + get #tag() { + return () => this; + } + + constructor() { + this.#tag``; + } +} + +new Foo(); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/output.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/output.js new file mode 100644 index 000000000000..90a909b22bbe --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/tagged-template/output.js @@ -0,0 +1,19 @@ +var _tag = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor() { + _tag.set(this, { + get: _get_tag, + set: void 0 + }); + + babelHelpers.classPrivateFieldGet(this, _tag).bind(this)``; + } + +} + +function _get_tag() { + return () => this; +} + +new Foo(); diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/exec.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/exec.js new file mode 100644 index 000000000000..85a02b5d72ac --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/exec.js @@ -0,0 +1,11 @@ +class Foo { + #tag() { + return this; + } + + constructor() { + const receiver = this.#tag`tagged template`; + expect(receiver).toBe(this); + } +} +new Foo(); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/input.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/input.js new file mode 100644 index 000000000000..51020020b73e --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/input.js @@ -0,0 +1,6 @@ +class Foo { + #tag() { + this.#tag``; + } +} +new Foo(); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/output.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/output.js new file mode 100644 index 000000000000..21029095ae9a --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/tagged-template/output.js @@ -0,0 +1,14 @@ +var _tag = /*#__PURE__*/new WeakSet(); + +class Foo { + constructor() { + _tag.add(this); + } + +} + +function _tag2() { + babelHelpers.classPrivateMethodGet(this, _tag, _tag2).bind(this)``; +} + +new Foo(); diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/exec.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/exec.js new file mode 100644 index 000000000000..b7b2c9fdc575 --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/exec.js @@ -0,0 +1,11 @@ +class Foo { + static #tag() { + return this; + } + + static getReceiver() { + return this.#tag``; + } +} + +expect(Foo.getReceiver()).toBe(Foo); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/input.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/input.js new file mode 100644 index 000000000000..31dc6eea4747 --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/input.js @@ -0,0 +1,6 @@ +class Foo { + static #tag() { + this.#tag``; + } +} +new Foo(); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/output.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/output.js new file mode 100644 index 000000000000..a2ef9d854ce4 --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/private-static-method/tagged-template/output.js @@ -0,0 +1,7 @@ +class Foo {} + +function _tag() { + babelHelpers.classStaticPrivateMethodGet(this, Foo, _tag).bind(this)``; +} + +new Foo(); diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/static-accessors/tagged-template/input.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/static-accessors/tagged-template/input.js new file mode 100644 index 000000000000..bcae85f476f4 --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/static-accessors/tagged-template/input.js @@ -0,0 +1,10 @@ +class Foo { + static get #tag() { + return function() { return this; }; + } + + static test() { + const receiver = this.#tag``; + expect(receiver).toBe(this); + } +} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-private-methods/test/fixtures/static-accessors/tagged-template/output.js b/packages/babel-plugin-proposal-private-methods/test/fixtures/static-accessors/tagged-template/output.js new file mode 100644 index 000000000000..89ff9d565f65 --- /dev/null +++ b/packages/babel-plugin-proposal-private-methods/test/fixtures/static-accessors/tagged-template/output.js @@ -0,0 +1,18 @@ +class Foo { + static test() { + var receiver = babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _tag).bind(this)``; + expect(receiver).toBe(this); + } + +} + +function _get_tag() { + return function () { + return this; + }; +} + +var _tag = { + get: _get_tag, + set: void 0 +};