Skip to content
Open
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
3 changes: 2 additions & 1 deletion pkg/function/deno.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
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 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) {
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 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'
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[]
Loading