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
72 changes: 72 additions & 0 deletions tsc/internal/checker/alias_terminal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package checker_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/bundled"
"github.com/microsoft/TypeScript/tsc/internal/compiler"
"github.com/microsoft/TypeScript/tsc/internal/core"
"github.com/microsoft/TypeScript/tsc/internal/tsoptions"
"github.com/microsoft/TypeScript/tsc/internal/tspath"
"github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest"
"gotest.tools/v3/assert"
)

func TestDeclaredAliasDeprecationAndTypeError(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically do not add unit tests like this (and definitely not one off files).

Is this not testable with a plain compiler test? You added three, so what are they?

t.Parallel()

fs := bundled.WrapFS(vfstest.FromMap(map[string]string{
"/origin.ts": `/** @deprecated old value */
export const value = 42;
/** @deprecated old function */
export function old(): number { return 42 }`,
"/middle.ts": `export { value as mid, old } from './origin'`,
"/last.ts": `export { mid as final, old } from './middle'`,
"/main.ts": `import { final as selected, old } from './last'; export const actual: number = selected + old(); export const invalid: number = 'bad'`,
"/tsconfig.json": `{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"noImplicitReturns": true,
"noImplicitOverride": true,
"allowUnreachableCode": false,
"allowImportingTsExtensions": true,
"allowJs": true,
"maxNodeModuleJsDepth": 8,
"types": [],
"skipLibCheck": true,
"noEmit": true,
"lib": [
"ES2022"
]
},
"files": [
"main.ts"
]
}`,
}, tspath.CaseSensitive))
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, nil, tsoptions.NewDefaultParseConfigHost(fs), nil)
assert.Equal(t, len(errors), 0)
program := compiler.NewProgram(compiler.ProgramOptions{
Config: parsed,
Host: compiler.NewCompilerHost(fs, bundled.LibPath(), nil, nil, nil),
})
program.BindSourceFiles()
c, done := program.GetTypeChecker(t.Context())
defer done()
file := program.GetSourceFile("/main.ts")
assert.Equal(t, len(c.GetGlobalDiagnostics()), 0)
diagnostics := c.GetDiagnostics(t.Context(), file)
assert.Equal(t, len(diagnostics), 1)
assert.Equal(t, diagnostics[0].Code(), int32(2322))
deprecated := 0
for _, suggestion := range c.GetSuggestionDiagnostics(t.Context(), file) {
if suggestion.ReportsDeprecated() {
assert.Assert(t, suggestion.Code() == 6385 || suggestion.Code() == 6387)
deprecated++
}
}
assert.Equal(t, deprecated, 4)
}
16 changes: 7 additions & 9 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16806,20 +16806,18 @@ func (c *Checker) resolveAliasWithDeprecationCheck(symbol *ast.Symbol, location
}
for symbol.Flags()&ast.SymbolFlagsAlias != 0 {
target := c.getImmediateAliasedSymbol(symbol)
if target != nil {
if target != nil && len(target.Declarations()) != 0 {
if target == targetSymbol {
break
}
if len(target.Declarations()) != 0 {
if c.isDeprecatedSymbol(target) {
c.addDeprecatedSuggestion(location, target.Declarations(), target.Name())
if c.isDeprecatedSymbol(target) {
c.addDeprecatedSuggestion(location, target.Declarations(), target.Name())
break
} else {
if symbol == targetSymbol {
break
} else {
if symbol == targetSymbol {
break
}
symbol = target
}
symbol = target
}
} else {
break
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/aliasToDeclarationFreeUndefined.ts] ////

=== main.ts ===
import { read } from './bridge.mjs'; read();
>read : Symbol(read, Decl(main.ts, 0, 8))
>read : Symbol(read, Decl(main.ts, 0, 8))

=== target.cjs ===
module.exports=undefined
>module.exports : Symbol(exports, Decl(target.cjs, 0, 0))
>module : Symbol(module, Decl(target.cjs, 0, 0))
>exports : Symbol(exports, Decl(target.cjs, 0, 0))
>undefined : Symbol(undefined)

=== bridge.mjs ===
import * as ns from './target.cjs'; export function read() { return ns }
>ns : Symbol(ns, Decl(bridge.mjs, 0, 6))
>read : Symbol(read, Decl(bridge.mjs, 0, 35))
>ns : Symbol(ns, Decl(bridge.mjs, 0, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/aliasToDeclarationFreeUndefined.ts] ////

=== main.ts ===
import { read } from './bridge.mjs'; read();
>read : () => { default: undefined; }
>read() : { default: undefined; }
>read : () => { default: undefined; }

=== target.cjs ===
module.exports=undefined
>module.exports=undefined : undefined
>module.exports : undefined
>module : { exports: undefined; }
>exports : undefined
>undefined : undefined

=== bridge.mjs ===
import * as ns from './target.cjs'; export function read() { return ns }
>ns : { default: undefined; }
>read : () => { default: undefined; }
>ns : { default: undefined; }

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//// [tests/cases/compiler/aliasToDeclarationFreeUndefinedCycle.ts] ////

=== src/host.ts ===
import { report } from "mid"; export function run(): string { return report() }
>report : Symbol(report, Decl(host.ts, 0, 8))
>run : Symbol(run, Decl(host.ts, 0, 29))
>report : Symbol(report, Decl(host.ts, 0, 8))

=== node_modules/target/index.cjs ===
module.exports=undefined
>module.exports : Symbol(exports, Decl(index.cjs, 0, 0))
>module : Symbol(module, Decl(index.cjs, 0, 0))
>exports : Symbol(exports, Decl(index.cjs, 0, 0))
>undefined : Symbol(undefined)

=== node_modules/mid/index.mjs ===
import {initial,early} from './reader.mjs';import * as ns from 'target';export function read(){return ns};export function report(){return early+':'+(initial===ns)+':'+String(ns.default)+':'+Object.keys(ns).join(',')}
>initial : Symbol(initial, Decl(index.mjs, 0, 8))
>early : Symbol(early, Decl(index.mjs, 0, 16))
>ns : Symbol(ns, Decl(index.mjs, 0, 49))
>read : Symbol(read, Decl(index.mjs, 0, 72))
>ns : Symbol(ns, Decl(index.mjs, 0, 49))
>report : Symbol(report, Decl(index.mjs, 0, 106))
>early : Symbol(early, Decl(index.mjs, 0, 16))
>initial : Symbol(initial, Decl(index.mjs, 0, 8))
>ns : Symbol(ns, Decl(index.mjs, 0, 49))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 6 more)
>ns.default : Symbol(default)
>ns : Symbol(ns, Decl(index.mjs, 0, 49))
>default : Symbol(default)
>Object.keys(ns).join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
>Object.keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>ns : Symbol(ns, Decl(index.mjs, 0, 49))
>join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))

=== node_modules/mid/reader.mjs ===
import{read}from './index.mjs';export var initial;export var early;try{initial=read();early=typeof initial+':'+String(initial.default)+':'+Object.keys(initial).join(',')}catch(e){early=e.name}
>read : Symbol(read, Decl(reader.mjs, 0, 7))
>initial : Symbol(initial, Decl(reader.mjs, 0, 41))
>early : Symbol(early, Decl(reader.mjs, 0, 60))
>initial : Symbol(initial, Decl(reader.mjs, 0, 41))
>read : Symbol(read, Decl(reader.mjs, 0, 7))
>early : Symbol(early, Decl(reader.mjs, 0, 60))
>initial : Symbol(initial, Decl(reader.mjs, 0, 41))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 6 more)
>initial : Symbol(initial, Decl(reader.mjs, 0, 41))
>Object.keys(initial).join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
>Object.keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>initial : Symbol(initial, Decl(reader.mjs, 0, 41))
>join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
>e : Symbol(e, Decl(reader.mjs, 0, 176))
>early : Symbol(early, Decl(reader.mjs, 0, 60))
>e : Symbol(e, Decl(reader.mjs, 0, 176))

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//// [tests/cases/compiler/aliasToDeclarationFreeUndefinedCycle.ts] ////

=== src/host.ts ===
import { report } from "mid"; export function run(): string { return report() }
>report : () => string
>run : () => string
>report() : string
>report : () => string

=== node_modules/target/index.cjs ===
module.exports=undefined
>module.exports=undefined : undefined
>module.exports : undefined
>module : { exports: undefined; }
>exports : undefined
>undefined : undefined

=== node_modules/mid/index.mjs ===
import {initial,early} from './reader.mjs';import * as ns from 'target';export function read(){return ns};export function report(){return early+':'+(initial===ns)+':'+String(ns.default)+':'+Object.keys(ns).join(',')}
>initial : any
>early : any
>ns : { default: undefined; }
>read : () => { default: undefined; }
>ns : { default: undefined; }
>report : () => string
>early+':'+(initial===ns)+':'+String(ns.default)+':'+Object.keys(ns).join(',') : string
>early+':'+(initial===ns)+':'+String(ns.default)+':' : string
>early+':'+(initial===ns)+':'+String(ns.default) : string
>early+':'+(initial===ns)+':' : string
>early+':'+(initial===ns) : string
>early+':' : string
>early : any
>':' : ":"
>(initial===ns) : boolean
>initial===ns : boolean
>initial : any
>ns : { default: undefined; }
>':' : ":"
>String(ns.default) : string
>String : StringConstructor
>ns.default : undefined
>ns : { default: undefined; }
>default : undefined
>':' : ":"
>Object.keys(ns).join(',') : string
>Object.keys(ns).join : (separator?: string) => string
>Object.keys(ns) : string[]
>Object.keys : { (o: object): string[]; (o: {}): string[]; }
>Object : ObjectConstructor
>keys : { (o: object): string[]; (o: {}): string[]; }
>ns : { default: undefined; }
>join : (separator?: string) => string
>',' : ","

=== node_modules/mid/reader.mjs ===
import{read}from './index.mjs';export var initial;export var early;try{initial=read();early=typeof initial+':'+String(initial.default)+':'+Object.keys(initial).join(',')}catch(e){early=e.name}
>read : () => { default: undefined; }
>initial : any
>early : any
>initial=read() : { default: undefined; }
>initial : any
>read() : { default: undefined; }
>read : () => { default: undefined; }
>early=typeof initial+':'+String(initial.default)+':'+Object.keys(initial).join(',') : string
>early : any
>typeof initial+':'+String(initial.default)+':'+Object.keys(initial).join(',') : string
>typeof initial+':'+String(initial.default)+':' : string
>typeof initial+':'+String(initial.default) : string
>typeof initial+':' : string
>typeof initial : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"
>initial : any
>':' : ":"
>String(initial.default) : string
>String : StringConstructor
>initial.default : any
>initial : any
>default : any
>':' : ":"
>Object.keys(initial).join(',') : string
>Object.keys(initial).join : (separator?: string) => string
>Object.keys(initial) : string[]
>Object.keys : { (o: object): string[]; (o: {}): string[]; }
>Object : ObjectConstructor
>keys : { (o: object): string[]; (o: {}): string[]; }
>initial : any
>join : (separator?: string) => string
>',' : ","
>e : unknown
>early=e.name : error
>early : any
>e.name : error
>e : unknown
>name : any

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/aliasToDeclarationFreeUndefinedUnused.ts] ////

=== src/host.ts ===
import { report } from "mid"; export function run(): string { return report() }
>report : Symbol(report, Decl(host.ts, 0, 8))
>run : Symbol(run, Decl(host.ts, 0, 29))
>report : Symbol(report, Decl(host.ts, 0, 8))

=== node_modules/target/index.cjs ===
module.exports=undefined
>module.exports : Symbol(exports, Decl(index.cjs, 0, 0))
>module : Symbol(module, Decl(index.cjs, 0, 0))
>exports : Symbol(exports, Decl(index.cjs, 0, 0))
>undefined : Symbol(undefined)

=== node_modules/mid/index.mjs ===
import * as ns from 'target'; export function report(){return 'ok'}
>ns : Symbol(ns, Decl(index.mjs, 0, 6))
>report : Symbol(report, Decl(index.mjs, 0, 29))

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/aliasToDeclarationFreeUndefinedUnused.ts] ////

=== src/host.ts ===
import { report } from "mid"; export function run(): string { return report() }
>report : () => string
>run : () => string
>report() : string
>report : () => string

=== node_modules/target/index.cjs ===
module.exports=undefined
>module.exports=undefined : undefined
>module.exports : undefined
>module : { exports: undefined; }
>exports : undefined
>undefined : undefined

=== node_modules/mid/index.mjs ===
import * as ns from 'target'; export function report(){return 'ok'}
>ns : { default: undefined; }
>report : () => string
>'ok' : "ok"

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @filename: target.cjs
module.exports=undefined
// @filename: bridge.mjs
import * as ns from './target.cjs'; export function read() { return ns }
// @filename: main.ts
import { read } from './bridge.mjs'; read();
// @filename: tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"noImplicitReturns": true,
"noImplicitOverride": true,
"allowUnreachableCode": false,
"allowImportingTsExtensions": true,
"allowJs": true,
"maxNodeModuleJsDepth": 8,
"types": [],
"skipLibCheck": true,
"noEmit": true,
"lib": [
"ES2022"
]
},
"files": [
"main.ts"
]
}
Loading