Skip to content

Commit

Permalink
fix(commonjs): add classBodyDepth flag (#1507)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi authored Jun 19, 2023
1 parent bcc26db commit b844adb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
7 changes: 6 additions & 1 deletion packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -258,6 +259,9 @@ export default async function transformCommonjs(
}
return;
}
case 'ClassBody':
classBodyDepth += 1;
return;
case 'ConditionalExpression':
case 'IfStatement':
// skip dead branches
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
});

Expand Down
14 changes: 13 additions & 1 deletion packages/commonjs/test/fixtures/function/this/foo.js
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 7 additions & 2 deletions packages/commonjs/test/fixtures/function/this/main.js
Original file line number Diff line number Diff line change
@@ -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');
25 changes: 22 additions & 3 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;␊
`,
}
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit b844adb

Please sign in to comment.