diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index fd0fe0a9d..4601094e9 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -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; diff --git a/packages/commonjs/test/fixtures/function/class-body-property-keys-are-special-keywords/_config.js b/packages/commonjs/test/fixtures/function/class-body-property-keys-are-special-keywords/_config.js new file mode 100644 index 000000000..bca2bd171 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/class-body-property-keys-are-special-keywords/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'preserve the class body property keys even if they are special keywords', +}; diff --git a/packages/commonjs/test/fixtures/function/class-body-property-keys-are-special-keywords/main.js b/packages/commonjs/test/fixtures/function/class-body-property-keys-are-special-keywords/main.js new file mode 100644 index 000000000..67ac52d62 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/class-body-property-keys-are-special-keywords/main.js @@ -0,0 +1,7 @@ +class Rollup { + define; + require; + global; +} + +exports.Rollup = Rollup; diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index bffef3a99..2c31aab43 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -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 diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 1d0e1a45e..97a1e03b0 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ