fix(commonjs): handle global shorthand property correctly#1958
Open
kaigritun wants to merge 1 commit intorollup:masterfrom
Open
fix(commonjs): handle global shorthand property correctly#1958kaigritun wants to merge 1 commit intorollup:masterfrom
kaigritun wants to merge 1 commit intorollup:masterfrom
Conversation
When `global` is used as a shorthand property in an object literal (e.g.,
`{global}`), the plugin was replacing `global` with
`commonjsHelpers.commonjsGlobal`, resulting in invalid syntax
`{commonjsHelpers.commonjsGlobal}`.
This fix follows the same pattern used for the `require` shorthand case:
when detecting a shorthand property, prepend `global: ` to convert it to a
full property before replacing the value.
Fixes rollup/rollup#6242
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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