Skip to content

Commit 0bb872b

Browse files
authored
fix(commonjs): preserve the class body property keys even if they are special keywords (#1688)
1 parent 9e7f576 commit 0bb872b

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

β€Žpackages/commonjs/src/transform-commonjs.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,12 @@ export default async function transformCommonjs(
291291
return;
292292
case 'Identifier': {
293293
const { name } = node;
294-
if (!isReference(node, parent) || scope.contains(name)) return;
294+
if (
295+
!isReference(node, parent) ||
296+
scope.contains(name) ||
297+
(parent.type === 'PropertyDefinition' && parent.key === node)
298+
)
299+
return;
295300
switch (name) {
296301
case 'require':
297302
uses.require = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'preserve the class body property keys even if they are special keywords',
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Rollup {
2+
define;
3+
require;
4+
global;
5+
}
6+
7+
exports.Rollup = Rollup;

β€Žpackages/commonjs/test/snapshots/function.js.md

+24
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,30 @@ Generated by [AVA](https://avajs.dev).
241241
`,
242242
}
243243

244+
## class-body-property-keys-are-special-keywords
245+
246+
> Snapshot 1
247+
248+
{
249+
'main.js': `'use strict';␊
250+
␊
251+
Object.defineProperty(exports, '__esModule', { value: true });␊
252+
␊
253+
var main = {};␊
254+
␊
255+
class Rollup {␊
256+
define;␊
257+
require;␊
258+
global;␊
259+
}␊
260+
␊
261+
var Rollup_1 = main.Rollup = Rollup;␊
262+
␊
263+
exports.Rollup = Rollup_1;␊
264+
exports.default = main;␊
265+
`,
266+
}
267+
244268
## compiled-esm-default-is-module-exports-false
245269

246270
> Snapshot 1
Binary file not shown.

0 commit comments

Comments
Β (0)