Skip to content

Commit

Permalink
fix(commonjs): preserve the class body property keys even if they are…
Browse files Browse the repository at this point in the history
… special keywords (#1688)
  • Loading branch information
TrickyPi authored May 22, 2024
1 parent 9e7f576 commit 0bb872b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ export default async function transformCommonjs(
return;
case 'Identifier': {
const { name } = node;
if (!isReference(node, parent) || scope.contains(name)) return;
if (
!isReference(node, parent) ||
scope.contains(name) ||
(parent.type === 'PropertyDefinition' && parent.key === node)
)
return;
switch (name) {
case 'require':
uses.require = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'preserve the class body property keys even if they are special keywords',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Rollup {
define;
require;
global;
}

exports.Rollup = Rollup;
24 changes: 24 additions & 0 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,30 @@ Generated by [AVA](https://avajs.dev).
`,
}

## class-body-property-keys-are-special-keywords

> Snapshot 1
{
'main.js': `'use strict';␊
␊
Object.defineProperty(exports, '__esModule', { value: true });␊
␊
var main = {};␊
␊
class Rollup {␊
define;␊
require;␊
global;␊
}␊
␊
var Rollup_1 = main.Rollup = Rollup;␊
␊
exports.Rollup = Rollup_1;␊
exports.default = main;␊
`,
}

## compiled-esm-default-is-module-exports-false

> Snapshot 1
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 0bb872b

Please sign in to comment.