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
2 changes: 1 addition & 1 deletion tsc/internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3629,7 +3629,7 @@ func IsTypeDeclaration(node *Node) bool {
case KindTypeParameter, KindClassDeclaration, KindInterfaceDeclaration, KindTypeAliasDeclaration, KindJSTypeAliasDeclaration, KindEnumDeclaration:
return true
case KindImportClause:
return node.IsTypeOnly()
return node.IsTypeOnly() && node.AsImportClause().Name() != nil
case KindImportSpecifier, KindExportSpecifier:
return node.Parent.Parent.IsTypeOnly()
default:
Expand Down
56 changes: 56 additions & 0 deletions tsc/internal/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,62 @@ foo.bar;`
}
}

func TestGetTypeAtLocationOfTypeOnlyImportClause(t *testing.T) {
t.Parallel()

fs := vfstest.FromMap(map[string]string{
"/types.ts": `export type U = number;
export default interface D { x: number }`,
"/main.ts": `import type { U } from "./types";
import type * as types from "./types";
import { U as V } from "./types";
import type D from "./types";
export const u: U = 1;
export const v: V = 1;
export type W = types.U;
export type E = D;`,
"/tsconfig.json": `
{
"compilerOptions": {},
"files": ["types.ts", "main.ts"]
}
`,
}, false /*useCaseSensitiveFileNames*/)
fs = bundled.WrapFS(fs)

cd := "/"
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil, nil, nil)

parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, nil, host, nil)
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")

p := compiler.NewProgram(compiler.ProgramOptions{
Config: parsed,
Host: host,
})
p.BindSourceFiles()
c, done := p.GetTypeChecker(t.Context())
defer done()
file := p.GetSourceFile("/main.ts")
importClauseAt := func(index int) *ast.Node {
return file.Statements.Nodes[index].AsImportDeclaration().ImportClause
}
// An import clause without a default binding has no symbol of its own. A type-only one
// should get the same type as the equivalent regular import instead of crashing.
regular := c.GetTypeAtLocation(importClauseAt(2))
for _, index := range []int{0, 1} {
typ := c.GetTypeAtLocation(importClauseAt(index))
if typ == nil {
t.Fatalf("Expected type of import clause %d to be non-nil", index)
}
assert.Equal(t, typ, regular)
}

defaultClause := c.GetTypeAtLocation(importClauseAt(3))
defaultReference := c.GetTypeAtLocation(file.Statements.Nodes[7].AsTypeAliasDeclaration().Type)
assert.Equal(t, defaultClause, defaultReference)
}

func BenchmarkNewChecker(b *testing.B) {
fs := bundled.WrapFS(osvfs.FS())
rootPath := tspath.NormalizeSlashes(filepath.Join(repo.TestDataPath(), "fixtures/compiler"))
Expand Down