Skip to content

fix: Convert destructuring aliases to import aliases in CommonJS->ESM#1302

Merged
Saga4 merged 2 commits intomainfrom
fix/js-import-destructuring-alias
Feb 3, 2026
Merged

fix: Convert destructuring aliases to import aliases in CommonJS->ESM#1302
Saga4 merged 2 commits intomainfrom
fix/js-import-destructuring-alias

Conversation

@mohammedahmed18
Copy link
Contributor

Problem

When converting CommonJS require statements with destructuring aliases to ESM imports, the code was preserving the JavaScript object destructuring syntax a: b which is invalid in ES6 import statements. This caused syntax errors when running Jest tests:

x Expected ',', got ':'
import { db: dbCore } from '@budibase/backend-core';
             ^

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

  • Modified codeflash/languages/javascript/module_system.py:
    • Added _convert_destructuring_to_imports() helper function
    • Updated replace_destructured() to use the helper
    • Updated docstring for convert_commonjs_to_esm()
  • Added comprehensive unit tests in tests/test_languages/test_javascript_module_system.py:
    • Test simple destructuring
    • Test destructuring with aliases
    • Test mixed destructuring (some aliased, some not)
    • Test real-world Budibase case that was failing

Testing

  • ✅ All 26 unit tests passing
  • ✅ Tested the real-world Budibase case that was failing

Fixes the "Expected ',', got ':'" syntax error in generated Jest tests for TypeScript projects.

mohammedahmed18 and others added 2 commits February 3, 2026 10:51
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>
@Saga4 Saga4 merged commit 7bc7032 into main Feb 3, 2026
18 of 27 checks passed
@Saga4 Saga4 deleted the fix/js-import-destructuring-alias branch February 3, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants