Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pkg/function/deno.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand Down
15 changes: 15 additions & 0 deletions pkg/function/deno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions pkg/function/testdata/modules/import_types.ts
Original file line number Diff line number Diff line change
@@ -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'
11 changes: 11 additions & 0 deletions pkg/function/testdata/types/database.ts
Original file line number Diff line number Diff line change
@@ -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[]