Skip to content

Commit df4099d

Browse files
authored
Ed/hack around parse error (#230)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ x] I have added tests for my changes - [x ] I have updated the documentation or added new documentation as needed
1 parent a09435a commit df4099d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/codegen/sdk/typescript/import_resolution.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ def from_dynamic_import_statement(cls, import_call_node: TSNode, module_node: TS
406406
# TODO: fixme
407407
return []
408408
imports = []
409+
410+
# TODO: FIX THIS, is a horrible hack to avoid a crash on the next.js
411+
if len(module_node.named_children) == 0:
412+
return []
413+
409414
# Grab the first element of dynamic import call expression argument list
410415
module_node = module_node.named_children[0]
411416

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import TYPE_CHECKING
2+
3+
from codegen.sdk.codebase.factory.get_session import get_codebase_session
4+
from codegen.sdk.enums import ProgrammingLanguage
5+
6+
if TYPE_CHECKING:
7+
from codegen.sdk.typescript.file import TSFile
8+
9+
10+
def test_import_edge_case(tmpdir) -> None:
11+
# language=typescript
12+
content = """
13+
import './module.js'
14+
15+
// Generic mock function with configurable return value
16+
const mockRequire = (returnValue = 'result') => returnValue
17+
18+
// Generic path constants with configurable values
19+
const MOCK_DIR_PATH = 'mock/directory/path'
20+
const MOCK_FILE_PATH = 'mock/directory/path/file.js'
21+
22+
it('should support CommonJS globals in ESM context', () => {
23+
const require = mockRequire
24+
const __dirname = MOCK_DIR_PATH
25+
const __filename = MOCK_FILE_PATH
26+
27+
expect(require()).toBe('result')
28+
expect(__dirname).toBe(MOCK_DIR_PATH)
29+
expect(__filename).toBe(MOCK_FILE_PATH)
30+
}) """
31+
with get_codebase_session(tmpdir=tmpdir, files={"file.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase:
32+
file: TSFile = codebase.get_file("file.ts")
33+
assert len(file.imports) == 1

0 commit comments

Comments
 (0)