Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/codegen/sdk/typescript/import_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,13 @@ def from_dynamic_import_statement(cls, import_call_node: TSNode, module_node: TS
# TODO: fixme
return []
imports = []
# Grab the first element of dynamic import call expression argument list
module_node = module_node.named_children[0]

# TODO: FIX THIS, is a horrible hack to avoid a crash on the next.js
if len(module_node.named_children) == 0:
return []
else:
# Grab the first element of dynamic import call expression argument list
module_node = module_node.named_children[0]

# Get the top most parent of call expression node that bypasses wrappers that doesn't change the semantics
call_node = find_first_ancestor(import_call_node, ["call_expression"])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import TYPE_CHECKING

from codegen.sdk.codebase.factory.get_session import get_codebase_session
from codegen.sdk.enums import ProgrammingLanguage

if TYPE_CHECKING:
from codegen.sdk.typescript.file import TSFile


def test_import_edge_case(tmpdir) -> None:
# language=typescript
content = """
import './module.js'
// Generic mock function with configurable return value
const mockRequire = (returnValue = 'result') => returnValue
// Generic path constants with configurable values
const MOCK_DIR_PATH = 'mock/directory/path'
const MOCK_FILE_PATH = 'mock/directory/path/file.js'
it('should support CommonJS globals in ESM context', () => {
const require = mockRequire
const __dirname = MOCK_DIR_PATH
const __filename = MOCK_FILE_PATH
expect(require()).toBe('result')
expect(__dirname).toBe(MOCK_DIR_PATH)
expect(__filename).toBe(MOCK_FILE_PATH)
}) """
with get_codebase_session(tmpdir=tmpdir, files={"file.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase:
file: TSFile = codebase.get_file("file.ts")
assert len(file.imports) == 1