diff --git a/pkg/function/deno.go b/pkg/function/deno.go index ec785c246..6c98b367b 100644 --- a/pkg/function/deno.go +++ b/pkg/function/deno.go @@ -111,7 +111,7 @@ func resolveHostPath(jsonPath, hostPath string) string { } // Ref: https://regex101.com/r/DfBdJA/1 -var importPathPattern = regexp.MustCompile(`(?i)(?:import|export)\s+(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"](.*?)['"]|import\(\s*['"](.*?)['"]\)`) +var importPathPattern = regexp.MustCompile(`(?i)(?:import|export)\s+(?:type\s+)?(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"](.*?)['"]|import\(\s*['"](.*?)['"]\)`) func (importMap *ImportMap) WalkImportPaths(srcPath string, readFile func(curr string, w io.Writer) error) error { seen := map[string]struct{}{} diff --git a/pkg/function/deno_test.go b/pkg/function/deno_test.go index ee3ac7795..aa9677f5a 100644 --- a/pkg/function/deno_test.go +++ b/pkg/function/deno_test.go @@ -86,6 +86,21 @@ func TestImportPaths(t *testing.T) { assert.NoError(t, err) fsys.AssertExpectations(t) }) + + t.Run("iterates multiline import type statements", func(t *testing.T) { + // This test verifies that multiline import type statements are correctly parsed. + // The (?:type\s+)? group consumes the type keyword so braced imports hit the {[^{}]+} branch. + // Setup in-memory fs + fsys := MockFS{} + fsys.On("ReadFile", "testdata/modules/import_types.ts").Once() + fsys.On("ReadFile", "testdata/types/database.ts").Once() + // Run test + im := ImportMap{} + err := im.WalkImportPaths("testdata/modules/import_types.ts", fsys.ReadFile) + // Check error + assert.NoError(t, err) + fsys.AssertExpectations(t) + }) } func TestResolveImports(t *testing.T) { diff --git a/pkg/function/testdata/modules/import_types.ts b/pkg/function/testdata/modules/import_types.ts new file mode 100644 index 000000000..d285ab0ee --- /dev/null +++ b/pkg/function/testdata/modules/import_types.ts @@ -0,0 +1,22 @@ +// Test file for multiline import type statements +// This pattern requires (?:type\s+)? to route braced imports into the {[^{}]+} branch + +// Multiline import type - should be matched by the regex +import type { + Database, + Json +} from '../types/database.ts' + +// Single line import type - should also work +import type { Database as DB } from '../types/database.ts' + +// Re-export type to verify export pattern +export type { Database } from '../types/database.ts' + +// Multiline export type +export type { + Json +} from '../types/database.ts' + +// Non-braced default type import - exercises the .*? branch on single-line +import type Database from '../types/database.ts' diff --git a/pkg/function/testdata/types/database.ts b/pkg/function/testdata/types/database.ts new file mode 100644 index 000000000..4c49d8e2d --- /dev/null +++ b/pkg/function/testdata/types/database.ts @@ -0,0 +1,11 @@ +export type Database = { + public: { + Tables: { + users: { + Row: { id: string; name: string } + } + } + } +} + +export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]