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
  • Loading branch information
TrickyPi committed Feb 26, 2024
1 parent 2a19079 commit a526635
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ export default async function transformCommonjs(
case 'Identifier': {
const { name } = node;
if (!isReference(node, parent) || scope.contains(name)) return;
const isClassBodyPropertyKey =
parent.type === 'PropertyDefinition' && parent.key === node;
if (isClassBodyPropertyKey && parent.value) return;
const prependLeftToClassBodyPropertyKey = (value) => {
if (isClassBodyPropertyKey && !parent.value) {
magicString.prependLeft(node.start, value);
}
};
switch (name) {
case 'require':
uses.require = true;
Expand All @@ -305,6 +313,7 @@ export default async function transformCommonjs(
skippedNodes.add(parent.value);
magicString.prependRight(node.start, 'require: ');
}
prependLeftToClassBodyPropertyKey('require = ');
replacedDynamicRequires.push(node);
}
return;
Expand All @@ -316,10 +325,12 @@ export default async function transformCommonjs(
case 'global':
uses.global = true;
if (!ignoreGlobal) {
prependLeftToClassBodyPropertyKey('global = ');
replacedGlobal.push(node);
}
return;
case 'define':
prependLeftToClassBodyPropertyKey('define = ');
magicString.overwrite(node.start, node.end, 'undefined', {
storeName: 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;
30 changes: 30 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,36 @@ 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 commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
␊
function commonjsRequire(path) {␊
throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊
}␊
␊
var main = {};␊
␊
class Rollup {␊
define = undefined;␊
require = commonjsRequire;␊
global = commonjsGlobal;␊
}␊
␊
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 a526635

Please sign in to comment.