From b4ef3641372063abf7fefa1a5761059117f621ac Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 16:32:04 -0500 Subject: [PATCH 1/7] Hack for crash when parsing Next.js --- src/codegen/sdk/typescript/import_resolution.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/codegen/sdk/typescript/import_resolution.py b/src/codegen/sdk/typescript/import_resolution.py index 810843483..1c504db6a 100644 --- a/src/codegen/sdk/typescript/import_resolution.py +++ b/src/codegen/sdk/typescript/import_resolution.py @@ -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: + module_node = None + 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"]) From a6c46ceb2d749f0fa6c829779b06ee5e30e74fa9 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 16:33:21 -0500 Subject: [PATCH 2/7] . --- src/codegen/sdk/typescript/import_resolution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/sdk/typescript/import_resolution.py b/src/codegen/sdk/typescript/import_resolution.py index 1c504db6a..0206da716 100644 --- a/src/codegen/sdk/typescript/import_resolution.py +++ b/src/codegen/sdk/typescript/import_resolution.py @@ -409,7 +409,7 @@ def from_dynamic_import_statement(cls, import_call_node: TSNode, module_node: TS #TODO: FIX THIS, is a horrible hack to avoid a crash on the next.js if len(module_node.named_children) == 0: - module_node = None + return [] else: # Grab the first element of dynamic import call expression argument list module_node = module_node.named_children[0] From 921c75158532f9a98538b6550d53731234540de6 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 16:33:58 -0500 Subject: [PATCH 3/7] tests --- .../import_resolution/test_edge_case.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py diff --git a/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py b/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py new file mode 100644 index 000000000..ea8ed1599 --- /dev/null +++ b/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py @@ -0,0 +1,25 @@ +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' + +const require = () => 'result' +const __dirname = 'something' +const __filename = 'something/else' + +it('should allow declaring CJS globals in ESM', () => { + expect(require()).toBe('result') + expect(__dirname).toBe('something') + expect(__filename).toBe('something/else') +}) + """ + 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 \ No newline at end of file From c23f2ca6c7bb688bec49ff840854d80497cedcfb Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 16:35:26 -0500 Subject: [PATCH 4/7] tests --- .../import_resolution/test_edge_case.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py b/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py index ea8ed1599..35bbd19d6 100644 --- a/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py +++ b/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py @@ -10,16 +10,22 @@ def test_import_edge_case(tmpdir) -> None: content = """ import './module.js' -const require = () => 'result' -const __dirname = 'something' -const __filename = 'something/else' +// 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 -it('should allow declaring CJS globals in ESM', () => { expect(require()).toBe('result') - expect(__dirname).toBe('something') - expect(__filename).toBe('something/else') -}) - """ + 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 \ No newline at end of file From 8804719b5c58c3944c5321d0d674ce770fd3e4a0 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 16:42:22 -0500 Subject: [PATCH 5/7] ruff --- .../codegen/sdk/typescript/import_resolution/test_edge_case.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py b/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py index 35bbd19d6..7eef1d381 100644 --- a/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py +++ b/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py @@ -2,6 +2,7 @@ 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 @@ -28,4 +29,4 @@ def test_import_edge_case(tmpdir) -> None: }) """ 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 \ No newline at end of file + assert len(file.imports) == 1 From 185e4db2c4100a9ce3786475d0f88e0751ed4d7e Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 16:42:40 -0500 Subject: [PATCH 6/7] ruff --- src/codegen/sdk/typescript/import_resolution.py | 2 +- .../codegen/sdk/typescript/import_resolution/test_edge_case.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/codegen/sdk/typescript/import_resolution.py b/src/codegen/sdk/typescript/import_resolution.py index 0206da716..33f6e6228 100644 --- a/src/codegen/sdk/typescript/import_resolution.py +++ b/src/codegen/sdk/typescript/import_resolution.py @@ -407,7 +407,7 @@ def from_dynamic_import_statement(cls, import_call_node: TSNode, module_node: TS return [] imports = [] - #TODO: FIX THIS, is a horrible hack to avoid a crash on the next.js + # TODO: FIX THIS, is a horrible hack to avoid a crash on the next.js if len(module_node.named_children) == 0: return [] else: diff --git a/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py b/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py index 7eef1d381..e85c3d9d3 100644 --- a/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py +++ b/tests/unit/codegen/sdk/typescript/import_resolution/test_edge_case.py @@ -6,6 +6,7 @@ if TYPE_CHECKING: from codegen.sdk.typescript.file import TSFile + def test_import_edge_case(tmpdir) -> None: # language=typescript content = """ From 85525277db18df73f68a4f9bfb06d111f16f6d30 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 16:52:18 -0500 Subject: [PATCH 7/7] fix --- src/codegen/sdk/typescript/import_resolution.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/codegen/sdk/typescript/import_resolution.py b/src/codegen/sdk/typescript/import_resolution.py index 33f6e6228..578ef9354 100644 --- a/src/codegen/sdk/typescript/import_resolution.py +++ b/src/codegen/sdk/typescript/import_resolution.py @@ -410,9 +410,9 @@ def from_dynamic_import_statement(cls, import_call_node: TSNode, module_node: TS # 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] + + # 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"])