diff --git a/pkg/function/deno.go b/pkg/function/deno.go index ec785c246..ce0434edb 100644 --- a/pkg/function/deno.go +++ b/pkg/function/deno.go @@ -111,7 +111,8 @@ 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*['"](.*?)['"]\)`) +// Note: Using [\s\S]*? instead of .*? to match newlines in multiline import type statements +var importPathPattern = regexp.MustCompile(`(?i)(?:import|export)\s+(?:{[^{}]+}|[\s\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..c9114499e 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 regex must use [\s\S]*? instead of .*? to match newlines + // 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..6fdcbd9bd --- /dev/null +++ b/pkg/function/testdata/modules/import_types.ts @@ -0,0 +1,22 @@ +// Test file for multiline import type statements +// This pattern was broken before the [\s\S]*? fix + +// 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 [\s\S]*? 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[]