Skip to content

Commit

Permalink
fix(commonjs): add classBodyDepth flag
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed May 27, 2023
1 parent 394ca39 commit 568aa2a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 49 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');
116 changes: 71 additions & 45 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,6 @@ Generated by [AVA](https://avajs.dev).
`,
}

## custom-options

> Snapshot 1
{
'main.js': `'use strict';␊
␊
t.is('main', 'main');␊
`,
}

## dash-name

> Snapshot 1
Expand Down Expand Up @@ -2965,6 +2954,8 @@ Generated by [AVA](https://avajs.dev).
return circular;␊
}␊
␊
customModule.exports;␊
␊
var hasRequiredCustomModule;␊
␊
function requireCustomModule () {␊
Expand Down Expand Up @@ -3854,8 +3845,8 @@ Generated by [AVA](https://avajs.dev).
␊
var esmFunction = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
bar: bar$1,␊
default: foo$1␊
default: foo$1,␊
bar: bar$1␊
});␊
␊
var require$$0 = /*@__PURE__*/getAugmentedNamespace(esmFunction);␊
Expand All @@ -3872,8 +3863,8 @@ Generated by [AVA](https://avajs.dev).
␊
var esmConstructor = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
bar: bar,␊
default: Foo$1␊
default: Foo$1,␊
bar: bar␊
});␊
␊
var require$$1 = /*@__PURE__*/getAugmentedNamespace(esmConstructor);␊
Expand Down Expand Up @@ -3945,8 +3936,8 @@ Generated by [AVA](https://avajs.dev).
␊
var esm$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
__esModule: __esModule,␊
foo: foo␊
foo: foo,␊
__esModule: __esModule␊
});␊
␊
var require$$0 = /*@__PURE__*/getAugmentedNamespace(esm$1);␊
Expand Down Expand Up @@ -4015,6 +4006,8 @@ Generated by [AVA](https://avajs.dev).
␊
var dep$1 = {exports: {}};␊
␊
dep$1.exports;␊
␊
(function (module, exports) {␊
exports.foo = 'foo';␊
module.exports = { replaced: true };␊
Expand Down Expand Up @@ -4273,8 +4266,8 @@ Generated by [AVA](https://avajs.dev).
␊
var mixed$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
default: mixed,␊
foo: foo␊
foo: foo,␊
default: mixed␊
});␊
␊
var require$$5 = 'bar';␊
Expand Down Expand Up @@ -4385,8 +4378,8 @@ Generated by [AVA](https://avajs.dev).
␊
var mixed$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
default: mixed,␊
foo: foo␊
foo: foo,␊
default: mixed␊
});␊
␊
var require$$4 = /*@__PURE__*/getAugmentedNamespace(mixed$1);␊
Expand Down Expand Up @@ -4725,8 +4718,8 @@ Generated by [AVA](https://avajs.dev).
␊
var mixed$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
default: mixed,␊
foo: foo␊
foo: foo,␊
default: mixed␊
});␊
␊
var _default = 'bar';␊
Expand Down Expand Up @@ -4961,8 +4954,8 @@ Generated by [AVA](https://avajs.dev).
␊
var mixed$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
default: mixed,␊
foo: foo␊
foo: foo,␊
default: mixed␊
});␊
␊
var require$$4 = /*@__PURE__*/getAugmentedNamespace(mixed$1);␊
Expand Down Expand Up @@ -5047,8 +5040,8 @@ Generated by [AVA](https://avajs.dev).
␊
var lib$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
get bar () { return bar; },␊
get default () { return foo; },␊
get bar () { return bar; },␊
update: update␊
});␊
␊
Expand Down Expand Up @@ -5204,6 +5197,8 @@ Generated by [AVA](https://avajs.dev).
␊
var other = {exports: {}};␊
␊
other.exports;␊
␊
(function (module, exports) {␊
exports.default = 42;␊
␊
Expand Down Expand Up @@ -5235,6 +5230,8 @@ Generated by [AVA](https://avajs.dev).
␊
var other = {exports: {}};␊
␊
other.exports;␊
␊
(function (module) {␊
addDefaultExport(module.exports);␊
␊
Expand Down Expand Up @@ -5540,6 +5537,8 @@ Generated by [AVA](https://avajs.dev).
␊
var x = {exports: {}};␊
␊
x.exports;␊
␊
(function (module) {␊
window.addExport = (key, value) => {␊
module.exports[key] = value;␊
Expand All @@ -5565,6 +5564,8 @@ Generated by [AVA](https://avajs.dev).
␊
var x = {exports: {}};␊
␊
x.exports;␊
␊
(function (module) {␊
Object.defineProperty(module.exports, 'named', {␊
enumerable: true,␊
Expand Down Expand Up @@ -6110,6 +6111,8 @@ Generated by [AVA](https://avajs.dev).
␊
var dep = {exports: {}};␊
␊
dep.exports;␊
␊
var hasRequiredDep;␊
␊
function requireDep () {␊
Expand Down Expand Up @@ -6233,8 +6236,8 @@ Generated by [AVA](https://avajs.dev).
␊
var other$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
default: other,␊
foo: foo␊
foo: foo,␊
default: other␊
});␊
␊
var dep$1 = 'default';␊
Expand Down Expand Up @@ -6294,8 +6297,8 @@ Generated by [AVA](https://avajs.dev).
␊
var other$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
default: other,␊
foo: foo␊
foo: foo,␊
default: other␊
});␊
␊
const dep = other$1;␊
Expand Down Expand Up @@ -6999,6 +7002,8 @@ Generated by [AVA](https://avajs.dev).
␊
var wrappedModuleExports = {exports: {}};␊
␊
wrappedModuleExports.exports;␊
␊
var hasRequiredWrappedModuleExports;␊
␊
function requireWrappedModuleExports () {␊
Expand All @@ -7007,7 +7012,7 @@ Generated by [AVA](https://avajs.dev).
(function (module, exports) {␊
module.exports = { foo: 'foo' };␊
commonjsGlobal.hasWrappedModuleExportsRun = true; ␊
} (wrappedModuleExports));␊
} (wrappedModuleExports, wrappedModuleExports.exports));␊
return wrappedModuleExports.exports;␊
}␊
␊
Expand Down Expand Up @@ -7269,20 +7274,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 Expand Up @@ -7364,6 +7388,8 @@ Generated by [AVA](https://avajs.dev).
␊
var foo$1 = {exports: {}};␊
␊
foo$1.exports;␊
␊
(function (module) {␊
␊
module.exports = 'bar';␊
Expand Down Expand Up @@ -7597,7 +7623,7 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
{
'entry-70303bf1.js': `'use strict';␊
'entry-4415ca7c.js': `'use strict';␊
␊
function _mergeNamespaces(n, m) {␊
m.forEach(function (e) {␊
Expand All @@ -7622,8 +7648,8 @@ Generated by [AVA](https://avajs.dev).
␊
var entry = /*#__PURE__*/_mergeNamespaces({␊
__proto__: null,␊
default: _default,␊
named: named␊
named: named,␊
default: _default␊
}, [entry$1]);␊
␊
exports._default = _default;␊
Expand All @@ -7634,7 +7660,7 @@ Generated by [AVA](https://avajs.dev).
␊
Object.defineProperty(exports, '__esModule', { value: true });␊
␊
var entry = require('./entry-70303bf1.js');␊
var entry = require('./entry-4415ca7c.js');␊
␊
␊
␊
Expand All @@ -7643,7 +7669,7 @@ Generated by [AVA](https://avajs.dev).
`,
'main.js': `'use strict';␊
␊
var entry = require('./entry-70303bf1.js');␊
var entry = require('./entry-4415ca7c.js');␊
␊
t.deepEqual(entry.entry, { default: 'default', named: 'named' });␊
`,
Expand Down Expand Up @@ -7689,8 +7715,8 @@ Generated by [AVA](https://avajs.dev).
␊
var entry = /*#__PURE__*/_mergeNamespaces({␊
__proto__: null,␊
default: entry$1,␊
named: named␊
named: named,␊
default: entry$1␊
}, [entry$1]);␊
␊
exports.entry = entry;␊
Expand Down Expand Up @@ -7871,8 +7897,8 @@ Generated by [AVA](https://avajs.dev).
␊
var dep = /*#__PURE__*/_mergeNamespaces({␊
__proto__: null,␊
default: _default,␊
named: named␊
named: named,␊
default: _default␊
}, [dep$1]);␊
␊
t.deepEqual(dep, {␊
Expand Down Expand Up @@ -7938,8 +7964,8 @@ Generated by [AVA](https://avajs.dev).
␊
var dep = /*#__PURE__*/_mergeNamespaces({␊
__proto__: null,␊
default: dep$1,␊
named: named␊
named: named,␊
default: dep$1␊
}, [dep$1]);␊
␊
t.deepEqual(dep, {␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 568aa2a

Please sign in to comment.