fix: Convert destructuring aliases to import aliases in CommonJS->ESM#1302
Merged
fix: Convert destructuring aliases to import aliases in CommonJS->ESM#1302
Conversation
When converting CommonJS require with destructuring aliases to ESM imports,
the code was preserving the colon syntax (a: b) which is invalid in ES6
import statements. ES6 requires 'as' keyword for aliases.
Converts:
const { db: dbCore } = require('@budibase/backend-core');
To:
import { db as dbCore } from '@budibase/backend-core';
This fixes the syntax error "Expected ',', got ':'" that occurred when
running Jest tests on generated test files in TypeScript projects.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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 converting CommonJS require statements with destructuring aliases to ESM imports, the code was preserving the JavaScript object destructuring syntax
a: bwhich is invalid in ES6 import statements. This caused syntax errors when running Jest tests:Solution
Added a helper function
_convert_destructuring_to_imports()that converts destructuring alias syntax to import alias syntax:{ a: b }→{ a as b }{ foo, bar: aliasB }→{ foo, bar as aliasB }Changes
codeflash/languages/javascript/module_system.py:_convert_destructuring_to_imports()helper functionreplace_destructured()to use the helperconvert_commonjs_to_esm()tests/test_languages/test_javascript_module_system.py:Testing
Fixes the "Expected ',', got ':'" syntax error in generated Jest tests for TypeScript projects.