Skip to content
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 6 additions & 12 deletions internal/checker/nodebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

type NodeBuilder struct {
ctxStack []*NodeBuilderContext
basicHost Host
impl *nodeBuilderImpl
ctxStack []*NodeBuilderContext
host Host
impl *nodeBuilderImpl
}

// EmitContext implements NodeBuilderInterface.
Expand All @@ -20,6 +20,7 @@ func (b *NodeBuilder) EmitContext() *printer.EmitContext {
func (b *NodeBuilder) enterContext(enclosingDeclaration *ast.Node, flags nodebuilder.Flags, internalFlags nodebuilder.InternalFlags, tracker nodebuilder.SymbolTracker) {
b.ctxStack = append(b.ctxStack, b.impl.ctx)
b.impl.ctx = &NodeBuilderContext{
host: b.host,
tracker: tracker,
flags: flags,
internalFlags: internalFlags,
Expand All @@ -32,14 +33,7 @@ func (b *NodeBuilder) enterContext(enclosingDeclaration *ast.Node, flags nodebui
enclosingSymbolTypes: make(map[ast.SymbolId]*Type),
remappedSymbolReferences: make(map[ast.SymbolId]*ast.Symbol),
}
// TODO: always provide this; see https://github.com/microsoft/typescript-go/pull/1588#pullrequestreview-3125218673
var moduleResolverHost Host
if tracker != nil {
moduleResolverHost = tracker.GetModuleSpecifierGenerationHost()
} else if internalFlags&nodebuilder.InternalFlagsDoNotIncludeSymbolChain != 0 {
moduleResolverHost = b.basicHost
}
tracker = NewSymbolTrackerImpl(b.impl.ctx, tracker, moduleResolverHost)
tracker = NewSymbolTrackerImpl(b.impl.ctx, tracker)
b.impl.ctx.tracker = tracker
}

Expand Down Expand Up @@ -178,7 +172,7 @@ func (b *NodeBuilder) TypeToTypeNode(typ *Type, enclosingDeclaration *ast.Node,

func NewNodeBuilder(ch *Checker, e *printer.EmitContext) *NodeBuilder {
impl := newNodeBuilderImpl(ch, e)
return &NodeBuilder{impl: impl, ctxStack: make([]*NodeBuilderContext, 0, 1), basicHost: ch.program}
return &NodeBuilder{impl: impl, ctxStack: make([]*NodeBuilderContext, 0, 1), host: ch.program}
}

func (c *Checker) getNodeBuilder() *NodeBuilder {
Expand Down
5 changes: 3 additions & 2 deletions internal/checker/nodebuilderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type NodeBuilderSymbolLinks struct {
specifierCache module.ModeAwareCache[string]
}
type NodeBuilderContext struct {
host Host
tracker nodebuilder.SymbolTracker
approximateLength int
encounteredError bool
Expand Down Expand Up @@ -1152,7 +1153,7 @@ func (b *nodeBuilderImpl) getSpecifierForModuleSymbol(symbol *ast.Symbol, overri
return stringutil.StripQuotes(symbol.Name)
}
}
if b.ctx.enclosingFile == nil || b.ctx.tracker.GetModuleSpecifierGenerationHost() == nil {
if b.ctx.enclosingFile == nil {
if isAmbientModuleSymbolName(symbol.Name) {
return stringutil.StripQuotes(symbol.Name)
}
Expand Down Expand Up @@ -1185,7 +1186,7 @@ func (b *nodeBuilderImpl) getSpecifierForModuleSymbol(symbol *ast.Symbol, overri
// just like how the declaration emitter does for the ambient module declarations - we can easily accomplish this
// using the `baseUrl` compiler option (which we would otherwise never use in declaration emit) and a non-relative
// specifier preference
host := b.ctx.tracker.GetModuleSpecifierGenerationHost()
host := b.ctx.host
specifierCompilerOptions := b.ch.compilerOptions
specifierPref := modulespecifiers.ImportModuleSpecifierPreferenceProjectRelative
endingPref := modulespecifiers.ImportModuleSpecifierEndingPreferenceNone
Expand Down
13 changes: 2 additions & 11 deletions internal/checker/symboltracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package checker

import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/modulespecifiers"
"github.com/microsoft/typescript-go/internal/nodebuilder"
)

type SymbolTrackerImpl struct {
context *NodeBuilderContext
inner nodebuilder.SymbolTracker
DisableTrackSymbol bool
tchost Host
}

func NewSymbolTrackerImpl(context *NodeBuilderContext, tracker nodebuilder.SymbolTracker, tchost Host) *SymbolTrackerImpl {
func NewSymbolTrackerImpl(context *NodeBuilderContext, tracker nodebuilder.SymbolTracker) *SymbolTrackerImpl {
if tracker != nil {
for {
t, ok := tracker.(*SymbolTrackerImpl)
Expand All @@ -24,14 +22,7 @@ func NewSymbolTrackerImpl(context *NodeBuilderContext, tracker nodebuilder.Symbo
}
}

return &SymbolTrackerImpl{context, tracker, false, tchost}
}

func (this *SymbolTrackerImpl) GetModuleSpecifierGenerationHost() modulespecifiers.ModuleSpecifierGenerationHost {
if this.inner == nil {
return this.tchost
}
return this.inner.GetModuleSpecifierGenerationHost()
return &SymbolTrackerImpl{context, tracker, false}
}

func (this *SymbolTrackerImpl) TrackSymbol(symbol *ast.Symbol, enclosingDeclaration *ast.Node, meaning ast.SymbolFlags) bool {
Expand Down
2 changes: 2 additions & 0 deletions internal/fourslash/_scripts/manualTests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ completionsWithDeprecatedTag4
renameDefaultKeyword
renameForDefaultExport01
tsxCompletion12
moduleReexportedIntoGlobalQuickInfo
quickInfoImportNonunicodePath
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ declare global {
// @Filename: /index.ts
let v = new /*1*/THREE.Vector3();`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyQuickInfoAt(t, "1", "const THREE: typeof import(\"/node_modules/@types/three/index\")", "")
f.VerifyQuickInfoAt(t, "1", "const THREE: typeof import(\"three\")", "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export const foo = 1;
// @Filename: /test.ts
import { foo } from "./江南/*1*/今何在/tmp";`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyQuickInfoAt(t, "1", "module \"/江南今何在/tmp\"", "")
f.VerifyQuickInfoAt(t, "1", "module \"./江南今何在/tmp\"", "")
}
3 changes: 0 additions & 3 deletions internal/nodebuilder/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package nodebuilder

import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/modulespecifiers"
)

// TODO: previously all symboltracker methods were optional, but now they're required.
type SymbolTracker interface {
GetModuleSpecifierGenerationHost() modulespecifiers.ModuleSpecifierGenerationHost

TrackSymbol(symbol *ast.Symbol, enclosingDeclaration *ast.Node, meaning ast.SymbolFlags) bool
ReportInaccessibleThisError()
ReportPrivateInBaseOfClassExpression(propertyName string)
Expand Down
6 changes: 0 additions & 6 deletions internal/transformers/declarations/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/microsoft/typescript-go/internal/checker"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/modulespecifiers"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/scanner"
)
Expand All @@ -17,11 +16,6 @@ type SymbolTrackerImpl struct {
fallbackStack []*ast.Node
}

// GetModuleSpecifierGenerationHost implements checker.SymbolTracker.
func (s *SymbolTrackerImpl) GetModuleSpecifierGenerationHost() modulespecifiers.ModuleSpecifierGenerationHost {
return s.host
}

// PopErrorFallbackNode implements checker.SymbolTracker.
func (s *SymbolTrackerImpl) PopErrorFallbackNode() {
s.fallbackStack = s.fallbackStack[:len(s.fallbackStack)-1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ declare namespace Knex {
}

export = knex;
>knex : typeof import("index.ts")
>knex : typeof import("knex")

=== index.ts ===
import "knex";
declare module "knex" {
>"knex" : typeof import("node_modules/knex/index")
>"knex" : typeof import("knex")

namespace Knex {
>Knex : typeof Knex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { BindingKey } from '@loopback/context';
export const CONTROLLER_CLASS = BindingKey.create<ControllerClass>(null as any); // line in question
>CONTROLLER_CLASS : BindingKey<ControllerClass>
>BindingKey.create<ControllerClass>(null as any) : BindingKey<ControllerClass>
>BindingKey.create : <T extends import("/monorepo/context/index").Constructor<any>>(ctor: T) => BindingKey<T>
>BindingKey.create : <T extends import("@loopback/context").Constructor<any>>(ctor: T) => BindingKey<T>
>BindingKey : typeof BindingKey
>create : <T extends import("/monorepo/context/index").Constructor<any>>(ctor: T) => BindingKey<T>
>create : <T extends import("@loopback/context").Constructor<any>>(ctor: T) => BindingKey<T>
>null as any : any

=== /monorepo/context/src/value-promise.d.ts ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export declare function styled(): Color;

=== file2.ts ===
import { styled } from "./file1";
>styled : () => import("color").default
>styled : () => import("./color").default

export const A = styled();
>A : import("color").default
>styled() : import("color").default
>styled : () => import("color").default
>A : import("./color").default
>styled() : import("./color").default
>styled : () => import("./color").default

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- old.allowSyntheticDefaultImportsCanPaintCrossModuleDeclaration.types
+++ new.allowSyntheticDefaultImportsCanPaintCrossModuleDeclaration.types
@@= skipped -16, +16 lines =@@

=== file2.ts ===
import { styled } from "./file1";
->styled : () => import("color").default
+>styled : () => import("./color").default

export const A = styled();
->A : import("color").default
->styled() : import("color").default
->styled : () => import("color").default
+>A : import("./color").default
+>styled() : import("./color").default
+>styled : () => import("./color").default
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default 2 + 2;
>2 : 2

export as namespace Foo;
>Foo : typeof import("foo")
>Foo : typeof import("./foo")

=== foo2.d.ts ===
export = 2 + 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- old.ambientExportDefaultErrors.types
+++ new.ambientExportDefaultErrors.types
@@= skipped -14, +14 lines =@@
>2 : 2

export as namespace Foo;
->Foo : typeof import("foo")
+>Foo : typeof import("./foo")

=== foo2.d.ts ===
export = 2 + 2;
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function Timestamped<TBase extends Constructor>(Base: TBase) {
=== index.ts ===
import { wrapClass, Timestamped } from "./wrapClass";
>wrapClass : (param: any) => typeof Wrapped
>Timestamped : <TBase extends import("wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped.(Anonymous class); } & TBase
>Timestamped : <TBase extends import("./wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped.(Anonymous class); } & TBase

export default wrapClass(0);
>wrapClass(0) : typeof Wrapped
Expand All @@ -63,7 +63,7 @@ export class User {
export class TimestampedUser extends Timestamped(User) {
>TimestampedUser : TimestampedUser
>Timestamped(User) : Timestamped.(Anonymous class) & User
>Timestamped : <TBase extends import("wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped.(Anonymous class); } & TBase
>Timestamped : <TBase extends import("./wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped.(Anonymous class); } & TBase
>User : typeof User

constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { wrapClass, Timestamped } from "./wrapClass";
>wrapClass : (param: any) => typeof Wrapped
->Timestamped : <TBase extends import("wrapClass").Constructor>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped<any>.(Anonymous class); } & TBase
+>Timestamped : <TBase extends import("wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped.(Anonymous class); } & TBase
+>Timestamped : <TBase extends import("./wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped.(Anonymous class); } & TBase

export default wrapClass(0);
>wrapClass(0) : typeof Wrapped
Expand All @@ -30,7 +30,7 @@
->Timestamped(User) : Timestamped<typeof User>.(Anonymous class) & User
->Timestamped : <TBase extends import("wrapClass").Constructor>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped<any>.(Anonymous class); } & TBase
+>Timestamped(User) : Timestamped.(Anonymous class) & User
+>Timestamped : <TBase extends import("wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped.(Anonymous class); } & TBase
+>Timestamped : <TBase extends import("./wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped.(Anonymous class); } & TBase
>User : typeof User

constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export function f() {

=== nested/derived.ts ===
import { f } from "./base";
>f : () => import("index").B
>f : () => import("./shared").B

export function g() {
>g : () => import("index").B
>g : () => import("./shared").B

return f();
>f() : import("index").B
>f : () => import("index").B
>f() : import("./shared").B
>f : () => import("./shared").B
}

=== nested/index.ts ===
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--- old.bundledDtsLateExportRenaming.types
+++ new.bundledDtsLateExportRenaming.types
@@= skipped -17, +17 lines =@@

=== nested/derived.ts ===
import { f } from "./base";
->f : () => import("index").B
+>f : () => import("./shared").B

export function g() {
->g : () => import("index").B
+>g : () => import("./shared").B

return f();
->f() : import("index").B
->f : () => import("index").B
+>f() : import("./shared").B
+>f : () => import("./shared").B
}

=== nested/index.ts ===
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export default { c: false };

=== b.js ===
import a from "./a";
>a : import("a").Foo
>a : import("./a").Foo

a;
>a : import("a").Foo
>a : import("./a").Foo

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default { c: false };

=== c.js ===
import b from "./b";
>b : import("a").Foo
>b : import("./a").Foo

b;
>b : import("a").Foo
>b : import("./a").Foo

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default { a: 1, b: 1 };

=== b.js ===
import a from "./a";
>a : import("a").Foo
>a : import("./a").Foo

a;
>a : import("a").Foo
>a : import("./a").Foo

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default { a: 1, b: 1, c: 1 };

=== b.js ===
import a from "./a";
>a : import("a").Foo
>a : import("./a").Foo

a;
>a : import("a").Foo
>a : import("./a").Foo

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as _three from './three';
>_three : typeof _three

export as namespace THREE;
>THREE : typeof import("global")
>THREE : typeof import("./global")

declare global {
>global : typeof global
Expand All @@ -25,6 +25,6 @@ declare global {

=== test.ts ===
const m = THREE
>m : typeof import("three")
>THREE : typeof import("three")
>m : typeof import("./three")
>THREE : typeof import("./three")

Loading
Loading