diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index 4209fa8f4..84280cd1e 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -76,6 +76,7 @@ export default async function transformCommonjs( let scope = attachScopes(ast, 'scope'); let lexicalDepth = 0; let programDepth = 0; + let classBodyDepth = 0; let currentTryBlockEnd = null; let shouldWrap = false; @@ -258,6 +259,9 @@ export default async function transformCommonjs( } return; } + case 'ClassBody': + classBodyDepth += 1; + return; case 'ConditionalExpression': case 'IfStatement': // skip dead branches @@ -354,7 +358,7 @@ export default async function transformCommonjs( return; case 'ThisExpression': // rewrite top-level `this` as `commonjsHelpers.commonjsGlobal` - if (lexicalDepth === 0) { + if (lexicalDepth === 0 && !classBodyDepth) { uses.global = true; if (!ignoreGlobal) { replacedGlobal.push(node); @@ -405,6 +409,7 @@ export default async function transformCommonjs( programDepth -= 1; if (node.scope) scope = scope.parent; if (functionType.test(node.type)) lexicalDepth -= 1; + if (node.type === 'ClassBody') classBodyDepth -= 1; } }); diff --git a/packages/commonjs/test/fixtures/function/this/foo.js b/packages/commonjs/test/fixtures/function/this/foo.js index 82e5ddd92..692fb802a 100644 --- a/packages/commonjs/test/fixtures/function/this/foo.js +++ b/packages/commonjs/test/fixtures/function/this/foo.js @@ -1,5 +1,17 @@ -module.exports = function augmentThis() { +exports.augmentThis = function augmentThis() { this.x = 'x'; }; this.y = 'y'; + +exports.classThis = class classThis { + constructor(){ + class _classThis { + y = 'yyy' + yyy = this.y + } + this._instance = new _classThis() + } + y = 'yy' + yy = this.y +} diff --git a/packages/commonjs/test/fixtures/function/this/main.js b/packages/commonjs/test/fixtures/function/this/main.js index 051cca398..d0c275253 100644 --- a/packages/commonjs/test/fixtures/function/this/main.js +++ b/packages/commonjs/test/fixtures/function/this/main.js @@ -1,7 +1,12 @@ -const foo = require('./foo'); +const { augmentThis, classThis } = require('./foo'); const obj = {}; -foo.call(obj); +augmentThis.call(obj); t.is(obj.x, 'x'); t.is(this.y, 'y'); + +const instance = new classThis(); + +t.is(instance.yy,'yy'); +t.is(instance._instance.yyy,'yyy'); diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index b6651016f..4ca5ab0b8 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -7269,20 +7269,39 @@ Generated by [AVA](https://avajs.dev). ␊ var main = {};␊ ␊ - var foo$1 = function augmentThis() {␊ + var foo = {};␊ + ␊ + foo.augmentThis = function augmentThis() {␊ this.x = 'x';␊ };␊ ␊ commonjsGlobal.y = 'y';␊ ␊ - const foo = foo$1;␊ + foo.classThis = class classThis {␊ + constructor(){␊ + class _classThis {␊ + y = 'yyy'␊ + yyy = this.y␊ + }␊ + this._instance = new _classThis();␊ + }␊ + y = 'yy'␊ + yy = this.y␊ + };␊ + ␊ + const { augmentThis, classThis } = foo;␊ ␊ const obj = {};␊ - foo.call(obj);␊ + augmentThis.call(obj);␊ ␊ t.is(obj.x, 'x');␊ t.is(commonjsGlobal.y, 'y');␊ ␊ + const instance = new classThis();␊ + ␊ + t.is(instance.yy,'yy');␊ + t.is(instance._instance.yyy,'yyy');␊ + ␊ module.exports = main;␊ `, } diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 20c8563ca..031a6268c 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ