Skip to content

Commit 7ea1f5d

Browse files
Merge branch 'main' into fix/typos
2 parents 220b764 + 53ee499 commit 7ea1f5d

File tree

234 files changed

+1124
-3639
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+1124
-3639
lines changed

Diff for: .vscode/launch.template.json

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
],
1818
"autoAttachChildProcesses": true,
1919
"preLaunchTask": "Watch for extension run"
20+
},
21+
{
22+
"name": "Launch submodule test",
23+
"type": "go",
24+
"request": "launch",
25+
"mode": "test",
26+
"program": "${workspaceFolder}/internal/testrunner",
27+
"args": [
28+
"-test.run",
29+
"TestSubmodule/${fileBasename}"
30+
]
2031
}
2132
]
2233
}

Diff for: internal/checker/checker.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ func NewChecker(program Program) *Checker {
878878
c.autoType = c.newIntrinsicTypeEx(TypeFlagsAny, "any", ObjectFlagsNonInferrableType)
879879
c.wildcardType = c.newIntrinsicType(TypeFlagsAny, "any")
880880
c.blockedStringType = c.newIntrinsicType(TypeFlagsAny, "any")
881-
c.errorType = c.newIntrinsicType(TypeFlagsAny, "error")
881+
c.errorType = c.newIntrinsicType(TypeFlagsAny, "any")
882882
c.unresolvedType = c.newIntrinsicType(TypeFlagsAny, "unresolved")
883883
c.nonInferrableAnyType = c.newIntrinsicTypeEx(TypeFlagsAny, "any", ObjectFlagsContainsWideningType)
884884
c.intrinsicMarkerType = c.newIntrinsicType(TypeFlagsAny, "intrinsic")
@@ -6531,7 +6531,7 @@ func (c *Checker) reportUnusedVariableDeclarations(declarations []*ast.Node) {
65316531
if ast.IsBindingPattern(name) {
65326532
c.reportUnusedBindingElements(name)
65336533
} else if c.isUnreferencedVariableDeclaration(declaration) {
6534-
c.reportUnusedVariable(declaration, NewDiagnosticForNode(declaration, diagnostics.X_0_is_declared_but_its_value_is_never_read, name.Text()))
6534+
c.reportUnusedVariable(declaration, NewDiagnosticForNode(name, diagnostics.X_0_is_declared_but_its_value_is_never_read, name.Text()))
65356535
}
65366536
}
65376537
}
@@ -6594,7 +6594,7 @@ func importClauseFromImported(node *ast.Node) *ast.Node {
65946594
func (c *Checker) checkUnusedInferTypeParameter(node *ast.Node) {
65956595
typeParameter := node.AsInferTypeNode().TypeParameter
65966596
if c.isUnreferencedTypeParameter(typeParameter) {
6597-
c.reportUnused(node, UnusedKindParameter, NewDiagnosticForNode(typeParameter, diagnostics.X_0_is_declared_but_never_used, typeParameter.Name().Text()))
6597+
c.reportUnused(node, UnusedKindParameter, NewDiagnosticForNode(typeParameter.Name(), diagnostics.X_0_is_declared_but_never_used, typeParameter.Name().Text()))
65986598
}
65996599
}
66006600

Diff for: internal/checker/printer.go

+41-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (c *Checker) getTypePrecedence(t *Type) ast.TypePrecedence {
1818
return ast.TypePrecedenceIntersection
1919
case t.flags&TypeFlagsUnion != 0 && t.flags&TypeFlagsBoolean == 0:
2020
return ast.TypePrecedenceUnion
21-
case t.flags&TypeFlagsIndex != 0:
21+
case t.flags&TypeFlagsIndex != 0 || isInferTypeParameter(t):
2222
return ast.TypePrecedenceTypeOperator
2323
case c.isArrayType(t):
2424
return ast.TypePrecedencePostfix
@@ -98,11 +98,12 @@ func (c *Checker) valueToString(value any) string {
9898
}
9999

100100
type Printer struct {
101-
c *Checker
102-
flags TypeFormatFlags
103-
sb strings.Builder
104-
printing core.Set[*Type]
105-
depth int
101+
c *Checker
102+
flags TypeFormatFlags
103+
sb strings.Builder
104+
printing core.Set[*Type]
105+
depth int32
106+
extendsTypeDepth int32
106107
}
107108

108109
func (c *Checker) newPrinter(flags TypeFormatFlags) *Printer {
@@ -161,6 +162,8 @@ func (p *Printer) printTypeNoAlias(t *Type) {
161162
p.printRecursive(t, (*Printer).printIndexType)
162163
case t.flags&TypeFlagsIndexedAccess != 0:
163164
p.printRecursive(t, (*Printer).printIndexedAccessType)
165+
case t.flags&TypeFlagsConditional != 0:
166+
p.printRecursive(t, (*Printer).printConditionalType)
164167
case t.flags&TypeFlagsTemplateLiteral != 0:
165168
p.printTemplateLiteralType(t)
166169
case t.flags&TypeFlagsStringMapping != 0:
@@ -364,7 +367,6 @@ func (p *Printer) printAnonymousType(t *Type) {
364367
}
365368
}
366369
}
367-
368370
props := p.c.getPropertiesOfObjectType(t)
369371
callSignatures := p.c.getSignaturesOfType(t, SignatureKindCall)
370372
constructSignatures := p.c.getSignaturesOfType(t, SignatureKindConstruct)
@@ -434,7 +436,7 @@ func (p *Printer) printSignature(sig *Signature, returnSeparator string) {
434436
if tail {
435437
p.print(", ")
436438
}
437-
p.printName(tp.symbol)
439+
p.printTypeParameterAndConstraint(tp)
438440
tail = true
439441
}
440442
p.print(">")
@@ -483,15 +485,27 @@ func (p *Printer) printTypePredicate(pred *TypePredicate) {
483485
}
484486

485487
func (p *Printer) printTypeParameter(t *Type) {
486-
if t.AsTypeParameter().isThisType {
488+
switch {
489+
case t.AsTypeParameter().isThisType:
487490
p.print("this")
488-
} else if t.symbol != nil {
491+
case p.extendsTypeDepth > 0 && isInferTypeParameter(t):
492+
p.print("infer ")
493+
p.printTypeParameterAndConstraint(t)
494+
case t.symbol != nil:
489495
p.printName(t.symbol)
490-
} else {
496+
default:
491497
p.print("???")
492498
}
493499
}
494500

501+
func (p *Printer) printTypeParameterAndConstraint(t *Type) {
502+
p.printName(t.symbol)
503+
if constraint := p.c.getConstraintOfTypeParameter(t); constraint != nil {
504+
p.print(" extends ")
505+
p.printType(constraint)
506+
}
507+
}
508+
495509
func (p *Printer) printUnionType(t *Type) {
496510
switch {
497511
case t.flags&TypeFlagsBoolean != 0:
@@ -538,6 +552,18 @@ func (p *Printer) printIndexedAccessType(t *Type) {
538552
p.print("]")
539553
}
540554

555+
func (p *Printer) printConditionalType(t *Type) {
556+
p.printType(t.AsConditionalType().checkType)
557+
p.print(" extends ")
558+
p.extendsTypeDepth++
559+
p.printType(t.AsConditionalType().extendsType)
560+
p.extendsTypeDepth--
561+
p.print(" ? ")
562+
p.printType(p.c.getTrueTypeFromConditionalType(t))
563+
p.print(" : ")
564+
p.printType(p.c.getFalseTypeFromConditionalType(t))
565+
}
566+
541567
func (p *Printer) printMappedType(t *Type) {
542568
d := t.AsMappedType().declaration
543569
p.print("{ ")
@@ -663,3 +689,7 @@ func (c *Checker) formatUnionTypes(types []*Type) []*Type {
663689
}
664690
return result
665691
}
692+
693+
func isInferTypeParameter(t *Type) bool {
694+
return t.flags&TypeFlagsTypeParameter != 0 && t.symbol != nil && core.Some(t.symbol.Declarations, func(d *ast.Node) bool { return ast.IsInferTypeNode(d.Parent) })
695+
}

Diff for: internal/core/binarysearch.go

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package core
77
// compared, instead of the target element.
88
func BinarySearchUniqueFunc[S ~[]E, E any](x S, cmp func(int, E) int) (int, bool) {
99
n := len(x)
10+
if n == 0 {
11+
return 0, false
12+
}
1013
low, high := 0, n-1
1114
for low <= high {
1215
middle := low + ((high - low) >> 1)

Diff for: internal/scanner/scanner.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1660,9 +1660,14 @@ func (s *Scanner) scanNumber() ast.Kind {
16601660
s.tokenFlags |= ast.TokenFlagsContainsLeadingZero
16611661
fixedPart = digits
16621662
} else {
1663+
s.tokenValue = jsnum.FromString(digits).String()
16631664
s.tokenFlags |= ast.TokenFlagsOctal
1664-
s.tokenValue = "0o" + digits
1665-
s.errorAt(diagnostics.Octal_literals_are_not_allowed_Use_the_syntax_0, start, s.pos-start, digits)
1665+
withMinus := s.token == ast.KindMinusToken
1666+
literal := core.IfElse(withMinus, "-", "") + "0o" + s.tokenValue
1667+
if withMinus {
1668+
start--
1669+
}
1670+
s.errorAt(diagnostics.Octal_literals_are_not_allowed_Use_the_syntax_0, start, s.pos-start, literal)
16661671
return ast.KindNumericLiteral
16671672
}
16681673
}
File renamed without changes.

Diff for: internal/testrunner/compiler_runner_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package runner
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/bundled"
7+
"github.com/microsoft/typescript-go/internal/core"
8+
"github.com/microsoft/typescript-go/internal/repo"
9+
"github.com/microsoft/typescript-go/internal/tspath"
10+
"gotest.tools/v3/assert"
11+
)
12+
13+
// Runs the new compiler tests and produces baselines (e.g. `test1.symbols`).
14+
func TestLocal(t *testing.T) { runCompilerTests(t, false) } //nolint:paralleltest
15+
16+
// Runs the old compiler tests, and produces new baselines (e.g. `test1.symbols`)
17+
// and a diff between the new and old baselines (e.g. `test1.symbols.diff`).
18+
func TestSubmodule(t *testing.T) { runCompilerTests(t, true) } //nolint:paralleltest
19+
20+
func runCompilerTests(t *testing.T, isSubmodule bool) {
21+
t.Parallel()
22+
23+
if isSubmodule {
24+
repo.SkipIfNoTypeScriptSubmodule(t)
25+
}
26+
27+
if !bundled.Embedded {
28+
// Without embedding, we'd need to read all of the lib files out from disk into the MapFS.
29+
// Just skip this for now.
30+
t.Skip("bundled files are not embedded")
31+
}
32+
33+
runners := []*CompilerBaselineRunner{
34+
NewCompilerBaselineRunner(TestTypeRegression, isSubmodule),
35+
NewCompilerBaselineRunner(TestTypeConformance, isSubmodule),
36+
}
37+
38+
var seenTests core.Set[string]
39+
for _, runner := range runners {
40+
for _, test := range runner.EnumerateTestFiles() {
41+
test = tspath.GetBaseFileName(test)
42+
assert.Assert(t, !seenTests.Has(test), "Duplicate test file: %s", test)
43+
seenTests.Add(test)
44+
}
45+
}
46+
47+
for _, runner := range runners {
48+
runner.RunTests(t)
49+
}
50+
}
File renamed without changes.

Diff for: internal/testutil/runner/compiler_runner_test.go

-50
This file was deleted.

Diff for: internal/tsoptions/tsconfigparsing.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ func parseOwnConfigOfJsonSourceFile(
150150
}
151151
} else if keyText != "" {
152152
if parentOption.ElementOptions != nil {
153-
propertySetErrors = append(propertySetErrors, ast.NewCompilerDiagnostic(diagnostics.Option_build_must_be_the_first_command_line_argument, keyText))
153+
// !!! TODO: support suggestion
154+
propertySetErrors = append(propertySetErrors, createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, propertyAssignment.Name(), diagnostics.Unknown_compiler_option_0, keyText))
154155
} else {
155156
// errors = append(errors, ast.NewCompilerDiagnostic(diagnostics.Unknown_compiler_option_0_Did_you_mean_1, keyText, core.FindKey(parentOption.ElementOptions, keyText)))
156157
}
@@ -516,21 +517,24 @@ func convertOptionsFromJson[O optionParser](optionsNameMap map[string]*CommandLi
516517
var errors []*ast.Diagnostic
517518
for key, value := range jsonMap.Entries() {
518519
opt, ok := optionsNameMap[key]
520+
if !ok {
521+
// !!! TODO?: support suggestion
522+
errors = append(errors, ast.NewCompilerDiagnostic(diagnostics.Unknown_compiler_option_0, key))
523+
continue
524+
}
525+
519526
commandLineOptionEnumMapVal := opt.EnumMap()
520527
if commandLineOptionEnumMapVal != nil {
521528
val, ok := commandLineOptionEnumMapVal.Get(strings.ToLower(value.(string)))
522529
if ok {
523530
errors = result.ParseOption(key, val)
524531
}
525-
} else if ok {
532+
} else {
526533
convertJson, err := convertJsonOption(opt, value, basePath, nil, nil, nil)
527534
errors = append(errors, err...)
528535
compilerOptionsErr := result.ParseOption(key, convertJson)
529536
errors = append(errors, compilerOptionsErr...)
530537
}
531-
// else {
532-
// errors.push(createUnknownOptionError(id, diagnostics));
533-
// }
534538
}
535539
return result, errors
536540
}

Diff for: internal/tsoptions/tsconfigparsing_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,19 @@ var parseJsonConfigFileTests = []struct {
471471
allFileList: map[string]string{"/tsconfig.base.json": tsconfigWithExtendsAndConfigDir, "/src/index.ts": "", "/src/app.ts": "", "/node_modules/module.ts": "", "/dist/output.js": ""},
472472
}},
473473
},
474+
{
475+
title: "reports error for an unknown option",
476+
input: []testConfig{{
477+
jsonText: `{
478+
"compilerOptions": {
479+
"unknown": true
480+
}
481+
}`,
482+
configFileName: "tsconfig.json",
483+
basePath: "/",
484+
allFileList: map[string]string{"/app.ts": ""},
485+
}},
486+
},
474487
}
475488

476489
var tsconfigWithExtends = `{

Diff for: testdata/baselines/reference/compiler/symbolLinkDeclarationEmitModuleNamesRootDir.types

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
=== /monorepo/core/src/application.ts ===
44
import { Constructor } from "@loopback/context";
5-
>Constructor : error
5+
>Constructor : any
66

77
export type ControllerClass = Constructor<any>;
88
>ControllerClass : ControllerClass
99

1010
=== /monorepo/core/src/usage.ts ===
1111
import { ControllerClass } from './application';
12-
>ControllerClass : error
12+
>ControllerClass : any
1313

1414
import { BindingKey } from '@loopback/context';
1515
>BindingKey : typeof BindingKey
1616

1717
export const CONTROLLER_CLASS = BindingKey.create<ControllerClass>(null as any); // line in question
1818
>CONTROLLER_CLASS : BindingKey<ControllerClass>
1919
>BindingKey.create<ControllerClass>(null as any) : BindingKey<ControllerClass>
20-
>BindingKey.create : <T>(ctor: T) => BindingKey<T>
20+
>BindingKey.create : <T extends Constructor<any>>(ctor: T) => BindingKey<T>
2121
>BindingKey : typeof BindingKey
22-
>create : <T>(ctor: T) => BindingKey<T>
22+
>create : <T extends Constructor<any>>(ctor: T) => BindingKey<T>
2323
>null as any : any
2424

2525
=== /monorepo/context/src/value-promise.d.ts ===
@@ -30,7 +30,7 @@ export type Constructor<T> = (...args: any[]) => T;
3030

3131
=== /monorepo/context/src/bindingkey.d.ts ===
3232
import { Constructor } from "./value-promise"
33-
>Constructor : error
33+
>Constructor : any
3434

3535
export declare class BindingKey<T> {
3636
>BindingKey : BindingKey<T>
@@ -40,7 +40,7 @@ export declare class BindingKey<T> {
4040
>__type : T
4141

4242
static create<T extends Constructor<any>>(ctor: T): BindingKey<T>;
43-
>create : <T>(ctor: T) => BindingKey<T>
43+
>create : <T extends Constructor<any>>(ctor: T) => BindingKey<T>
4444
>T : T
4545
>ctor : T
4646
}

0 commit comments

Comments
 (0)