fix(commonjs): handle global shorthand property correctly #1958
+16
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When
globalis used as a shorthand property in an object literal (e.g.,{global}), the commonjs plugin replacesglobalwithcommonjsHelpers.commonjsGlobal, resulting in invalid syntax{commonjsHelpers.commonjsGlobal}.This causes a parse error like:
Root Cause
As identified in rollup/rollup#6242 by @TrickyPi, the CommonJS plugin transforms
{global}into{commonjsHelpers.commonjsGlobal}, which is invalid syntax because you can't have a dotted expression as a shorthand property.Solution
This fix follows the same pattern already used for the
requireshorthand case (see existingshorthand-requiretest): when detecting a shorthand property, prependglobal:to convert it to a full property before replacing the value.The transformation now correctly produces:
{global}→{commonjsHelpers.commonjsGlobal}(invalid syntax){global}→{global: commonjsHelpers.commonjsGlobal}(valid syntax)Changes
globalidentifier intransform-commonjs.jstest/fixtures/function/shorthand-global/Related Issues
Fixes rollup/rollup#6242