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 (rollup#1688)
  • Loading branch information
TrickyPi authored and younggglcy committed Jun 6, 2024
1 parent 3d6a8c8 commit a4c9f45
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 a4c9f45

Please sign in to comment.