Skip to content

Commit 6065110

Browse files
authored
Fix multiple issues uncovered by test suites (#641)
1 parent 96d74d8 commit 6065110

File tree

859 files changed

+3718
-15316
lines changed

Some content is hidden

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

859 files changed

+3718
-15316
lines changed

internal/ast/ast.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ func (n *Node) ModifierFlags() ModifierFlags {
442442
if modifiers != nil {
443443
return modifiers.ModifierFlags
444444
}
445+
if n.Flags&NodeFlagsNestedNamespace != 0 {
446+
return ModifierFlagsExport
447+
}
445448
return ModifierFlagsNone
446449
}
447450

internal/binder/binder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func bindSourceFile(file *ast.SourceFile, options *core.CompilerOptions) {
117117
b.file = file
118118
b.options = options
119119
b.languageVersion = options.GetEmitScriptTarget()
120-
b.inStrictMode = options.AlwaysStrict.IsTrue()
120+
b.inStrictMode = (options.AlwaysStrict.IsTrue() || options.Strict.IsTrue()) && !file.IsDeclarationFile || ast.IsExternalModule(file)
121121
b.unreachableFlow = b.newFlowNode(ast.FlowFlagsUnreachable)
122122
b.reportedUnreachableFlow = b.newFlowNode(ast.FlowFlagsUnreachable)
123123
b.bind(file.AsNode())

internal/checker/checker.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ type Checker struct {
741741
reverseMappedSourceStack []*Type
742742
reverseMappedTargetStack []*Type
743743
reverseExpandingFlags ExpandingFlags
744-
relaters []Relater
744+
freeRelater *Relater
745745
subtypeRelation *Relation
746746
strictSubtypeRelation *Relation
747747
assignableRelation *Relation
@@ -791,6 +791,7 @@ type Checker struct {
791791
getGlobalClassMethodDecoratorContextType func() *Type
792792
getGlobalClassGetterDecoratorContextType func() *Type
793793
getGlobalClassSetterDecoratorContextType func() *Type
794+
getGlobalClassAccessorDecoratorContxtType func() *Type
794795
getGlobalClassAccessorDecoratorContextType func() *Type
795796
getGlobalClassAccessorDecoratorTargetType func() *Type
796797
getGlobalClassAccessorDecoratorResultType func() *Type
@@ -2171,6 +2172,8 @@ func (c *Checker) checkSourceElementWorker(node *ast.Node) {
21712172
c.checkTypeAliasDeclaration(node)
21722173
case ast.KindEnumDeclaration:
21732174
c.checkEnumDeclaration(node)
2175+
case ast.KindEnumMember:
2176+
c.checkEnumMember(node)
21742177
case ast.KindModuleDeclaration:
21752178
c.checkModuleDeclaration(node)
21762179
case ast.KindImportDeclaration:
@@ -4270,7 +4273,7 @@ basePropertyCheck:
42704273
for errorNode, memberInfo := range notImplementedInfo {
42714274
switch {
42724275
case len(memberInfo.missedProperties) == 1:
4273-
missedProperty := "'" + memberInfo.missedProperties[0] + "'"
4276+
missedProperty := memberInfo.missedProperties[0]
42744277
if ast.IsClassExpression(errorNode) {
42754278
c.error(errorNode, diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1, missedProperty, memberInfo.baseTypeName)
42764279
} else {
@@ -4711,6 +4714,15 @@ func (c *Checker) checkEnumDeclaration(node *ast.Node) {
47114714
}
47124715
}
47134716

4717+
func (c *Checker) checkEnumMember(node *ast.Node) {
4718+
if ast.IsPrivateIdentifier(node.Name()) {
4719+
c.error(node, diagnostics.An_enum_member_cannot_be_named_with_a_private_identifier)
4720+
}
4721+
if node.Initializer() != nil {
4722+
c.checkExpression(node.Initializer())
4723+
}
4724+
}
4725+
47144726
func (c *Checker) checkModuleDeclaration(node *ast.Node) {
47154727
if body := node.Body(); body != nil {
47164728
c.checkSourceElement(body)
@@ -8025,7 +8037,7 @@ func (c *Checker) isConstructorAccessible(node *ast.Node, signature *Signature)
80258037
declaration := signature.declaration
80268038
modifiers := getSelectedEffectiveModifierFlags(declaration, ast.ModifierFlagsNonPublicAccessibilityModifier)
80278039
// (1) Public constructors and (2) constructor functions are always accessible.
8028-
if modifiers == 0 || ast.IsConstructorDeclaration(declaration) {
8040+
if modifiers == 0 || !ast.IsConstructorDeclaration(declaration) {
80298041
return true
80308042
}
80318043
declaringClassDeclaration := getClassLikeDeclarationOfSymbol(declaration.Parent.Symbol())
@@ -10449,7 +10461,7 @@ func (c *Checker) checkPropertyAccessChain(node *ast.Node, checkMode CheckMode)
1044910461
}
1045010462

1045110463
func (c *Checker) checkPropertyAccessExpressionOrQualifiedName(node *ast.Node, left *ast.Node, leftType *Type, right *ast.Node, checkMode CheckMode, writeOnly bool) *Type {
10452-
parentSymbol := c.typeNodeLinks.Get(node).resolvedSymbol
10464+
parentSymbol := c.typeNodeLinks.Get(left).resolvedSymbol
1045310465
assignmentKind := getAssignmentTargetKind(node)
1045410466
widenedType := leftType
1045510467
if assignmentKind != AssignmentKindNone || c.isMethodAccessForCall(node) {
@@ -11521,7 +11533,8 @@ func (c *Checker) checkBinaryLikeExpression(left *ast.Node, operatorToken *ast.N
1152111533
ast.KindGreaterThanGreaterThanGreaterThanEqualsToken:
1152211534
rhsEval := c.evaluate(right, right)
1152311535
if numValue, ok := rhsEval.Value.(jsnum.Number); ok && numValue.Abs() >= 32 {
11524-
c.errorOrSuggestion(ast.IsEnumMember(ast.WalkUpParenthesizedExpressions(right.Parent.Parent)), errorNode, diagnostics.This_operation_can_be_simplified_This_shift_is_identical_to_0_1_2, scanner.GetTextOfNode(left), scanner.TokenToString(operator), (numValue / 32).Floor())
11536+
// Elevate from suggestion to error within an enum member
11537+
c.errorOrSuggestion(ast.IsEnumMember(ast.WalkUpParenthesizedExpressions(right.Parent.Parent)), errorNode, diagnostics.This_operation_can_be_simplified_This_shift_is_identical_to_0_1_2, scanner.GetTextOfNode(left), scanner.TokenToString(operator), numValue.Remainder(32))
1152511538
}
1152611539
}
1152711540
}
@@ -21899,6 +21912,7 @@ func (c *Checker) createComputedEnumType(symbol *ast.Symbol) *Type {
2189921912
freshType := c.newLiteralType(TypeFlagsEnum, nil, regularType)
2190021913
freshType.symbol = symbol
2190121914
regularType.AsLiteralType().freshType = freshType
21915+
freshType.AsLiteralType().freshType = freshType
2190221916
return regularType
2190321917
}
2190421918

internal/checker/grammarchecks.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,24 +1907,22 @@ func (c *Checker) checkGrammarProperty(node *ast.Node /*Union[PropertyDeclaratio
19071907
if ast.IsAutoAccessorPropertyDeclaration(node) && c.checkGrammarForInvalidQuestionMark(node.AsPropertyDeclaration().PostfixToken, diagnostics.An_accessor_property_cannot_be_declared_optional) {
19081908
return true
19091909
}
1910-
} else if node.Parent.Kind == ast.KindInterfaceDeclaration {
1910+
} else if ast.IsInterfaceDeclaration(node.Parent) {
19111911
if c.checkGrammarForInvalidDynamicName(propertyName, diagnostics.A_computed_property_name_in_an_interface_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type) {
19121912
return true
19131913
}
1914-
19151914
if !ast.IsPropertySignatureDeclaration(node) {
19161915
// Interfaces cannot contain property declarations
19171916
panic(fmt.Sprintf("Unexpected node kind %q", node.Kind))
19181917
}
1919-
19201918
if initializer := node.AsPropertySignatureDeclaration().Initializer; initializer != nil {
19211919
return c.grammarErrorOnNode(initializer, diagnostics.An_interface_property_cannot_have_an_initializer)
19221920
}
1923-
} else if ast.IsTypeAliasDeclaration(node.Parent) {
1921+
} else if ast.IsTypeLiteralNode(node.Parent) {
19241922
if c.checkGrammarForInvalidDynamicName(node.Name(), diagnostics.A_computed_property_name_in_a_type_literal_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type) {
19251923
return true
19261924
}
1927-
if ast.IsPropertySignatureDeclaration(node) {
1925+
if !ast.IsPropertySignatureDeclaration(node) {
19281926
// Type literals cannot contain property declarations
19291927
panic(fmt.Sprintf("Unexpected node kind %q", node.Kind))
19301928
}

internal/checker/printer.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ func (p *Printer) printName(symbol *ast.Symbol) {
122122
p.print(p.c.symbolToString(symbol))
123123
}
124124

125+
func (p *Printer) printQualifiedName(symbol *ast.Symbol) {
126+
if p.flags&TypeFormatFlagsUseFullyQualifiedType != 0 && symbol.Parent != nil {
127+
p.printQualifiedName(symbol.Parent)
128+
p.print(".")
129+
}
130+
if symbol.Flags&ast.SymbolFlagsModule != 0 && strings.HasPrefix(symbol.Name, "\"") {
131+
p.print("import(")
132+
p.print(symbol.Name)
133+
p.print(")")
134+
return
135+
}
136+
p.printName(symbol)
137+
}
138+
125139
func (p *Printer) printTypeEx(t *Type, precedence ast.TypePrecedence) {
126140
if p.c.getTypePrecedence(t) < precedence {
127141
p.print("(")
@@ -139,7 +153,7 @@ func (p *Printer) printType(t *Type) {
139153
}
140154

141155
if t.alias != nil && (p.flags&TypeFormatFlagsInTypeAlias == 0 || p.depth > 0) {
142-
p.printName(t.alias.symbol)
156+
p.printQualifiedName(t.alias.symbol)
143157
p.printTypeArguments(t.alias.typeArguments)
144158
} else {
145159
p.printTypeNoAlias(t)
@@ -255,18 +269,22 @@ func (p *Printer) printStringMappingType(t *Type) {
255269

256270
func (p *Printer) printEnumLiteral(t *Type) {
257271
if parent := p.c.getParentOfSymbol(t.symbol); parent != nil {
258-
p.printName(parent)
259-
p.print(".")
272+
p.printQualifiedName(parent)
273+
if p.c.getDeclaredTypeOfSymbol(parent) != t {
274+
p.print(".")
275+
p.printName(t.symbol)
276+
}
277+
return
260278
}
261-
p.printName(t.symbol)
279+
p.printQualifiedName(t.symbol)
262280
}
263281

264282
func (p *Printer) printObjectType(t *Type) {
265283
switch {
266284
case t.objectFlags&ObjectFlagsReference != 0:
267285
p.printParameterizedType(t)
268286
case t.objectFlags&ObjectFlagsClassOrInterface != 0:
269-
p.printName(t.symbol)
287+
p.printQualifiedName(t.symbol)
270288
case p.c.isGenericMappedType(t) || t.objectFlags&ObjectFlagsMapped != 0 && t.AsMappedType().containsError:
271289
p.printMappedType(t)
272290
default:
@@ -286,7 +304,7 @@ func (p *Printer) printParameterizedType(t *Type) {
286304
}
287305

288306
func (p *Printer) printTypeReference(t *Type) {
289-
p.printName(t.symbol)
307+
p.printQualifiedName(t.symbol)
290308
p.printTypeArguments(p.c.getTypeArguments(t)[:p.c.getTypeReferenceArity(t)])
291309
}
292310

@@ -360,13 +378,7 @@ func (p *Printer) printAnonymousType(t *Type) {
360378
if t.symbol.Flags&(ast.SymbolFlagsClass|ast.SymbolFlagsEnum|ast.SymbolFlagsValueModule) != 0 {
361379
if t == p.c.getTypeOfSymbol(t.symbol) {
362380
p.print("typeof ")
363-
if t.symbol.Flags&ast.SymbolFlagsValueModule != 0 && t.symbol.Name[0] == '"' {
364-
p.print("import(")
365-
p.print(t.symbol.Name)
366-
p.print(")")
367-
} else {
368-
p.printName(t.symbol)
369-
}
381+
p.printQualifiedName(t.symbol)
370382
return
371383
}
372384
}
@@ -515,7 +527,7 @@ func (p *Printer) printUnionType(t *Type) {
515527
case t.flags&TypeFlagsBoolean != 0:
516528
p.print("boolean")
517529
case t.flags&TypeFlagsEnumLiteral != 0:
518-
p.printName(t.symbol)
530+
p.printQualifiedName(t.symbol)
519531
default:
520532
u := t.AsUnionType()
521533
if u.origin != nil {

internal/checker/relater.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,12 @@ func (c *Checker) checkTypeRelatedToEx(
365365
headMessage *diagnostics.Message,
366366
diagnosticOutput *[]*ast.Diagnostic,
367367
) bool {
368-
relaterCount := len(c.relaters)
369-
c.relaters = slices.Grow(c.relaters, 1)[:relaterCount+1]
370-
r := &c.relaters[relaterCount]
371-
r.c = c
368+
r := c.freeRelater
369+
if r == nil {
370+
r = &Relater{c: c}
371+
} else {
372+
c.freeRelater = r.next
373+
}
372374
r.relation = relation
373375
r.errorNode = errorNode
374376
r.relationCount = (16_000_000 - relation.size()) / 8
@@ -396,14 +398,16 @@ func (c *Checker) checkTypeRelatedToEx(
396398
}
397399
c.reportDiagnostic(createDiagnosticChainFromErrorChain(r.errorChain, r.errorNode, r.relatedInfo), diagnosticOutput)
398400
}
399-
c.relaters = c.relaters[:relaterCount]
400401
r.maybeKeysSet.Clear()
401402
*r = Relater{
403+
c: c,
402404
maybeKeys: r.maybeKeys[:0],
403405
maybeKeysSet: r.maybeKeysSet,
404406
sourceStack: r.sourceStack[:0],
405407
targetStack: r.targetStack[:0],
408+
next: c.freeRelater,
406409
}
410+
c.freeRelater = r
407411
return result != TernaryFalse
408412
}
409413

@@ -2521,6 +2525,7 @@ type Relater struct {
25212525
expandingFlags ExpandingFlags
25222526
overflow bool
25232527
relationCount int
2528+
next *Relater
25242529
}
25252530

25262531
func (r *Relater) isRelatedToSimple(source *Type, target *Type) Ternary {
@@ -4265,18 +4270,20 @@ func (r *Relater) reportUnmatchedProperty(source *Type, target *Type, unmatchedP
42654270
}
42664271
props := r.c.getUnmatchedProperties(source, target, requireOptionalProperties, false /*matchDiscriminantProperties*/)
42674272
if len(props) == 1 {
4273+
sourceType, targetType := r.c.getTypeNamesForErrorDisplay(source, target)
42684274
propName := r.c.symbolToString(unmatchedProperty)
4269-
r.reportError(diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2, propName, r.c.TypeToString(source), r.c.TypeToString(target))
4275+
r.reportError(diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2, propName, sourceType, targetType)
42704276
if len(unmatchedProperty.Declarations) != 0 {
42714277
r.relatedInfo = append(r.relatedInfo, createDiagnosticForNode(unmatchedProperty.Declarations[0], diagnostics.X_0_is_declared_here, propName))
42724278
}
42734279
} else if r.tryElaborateArrayLikeErrors(source, target, false /*reportErrors*/) {
4280+
sourceType, targetType := r.c.getTypeNamesForErrorDisplay(source, target)
42744281
if len(props) > 5 {
42754282
propNames := strings.Join(core.Map(props[:4], r.c.symbolToString), ", ")
4276-
r.reportError(diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2_and_3_more, r.c.TypeToString(source), r.c.TypeToString(target), propNames, len(props)-4)
4283+
r.reportError(diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2_and_3_more, sourceType, targetType, propNames, len(props)-4)
42774284
} else {
42784285
propNames := strings.Join(core.Map(props, r.c.symbolToString), ", ")
4279-
r.reportError(diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, r.c.TypeToString(source), r.c.TypeToString(target), propNames)
4286+
r.reportError(diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, sourceType, targetType, propNames)
42804287
}
42814288
}
42824289
}
@@ -4677,6 +4684,7 @@ func (r *Relater) reportRelationError(message *diagnostics.Message, source *Type
46774684
case constraint != nil && r.c.isTypeAssignableTo(source, constraint):
46784685
r.reportError(diagnostics.X_0_is_assignable_to_the_constraint_of_type_1_but_1_could_be_instantiated_with_a_different_subtype_of_constraint_2, sourceType, targetType, r.c.TypeToString(constraint))
46794686
default:
4687+
r.errorChain = nil // Only report this error once
46804688
r.reportError(diagnostics.X_0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1, targetType, generalizedSourceType)
46814689
}
46824690
}
@@ -4755,12 +4763,7 @@ func (r *Relater) reportError(message *diagnostics.Message, args ...any) {
47554763
diagnostics.The_types_returned_by_0_are_incompatible_between_these_types:
47564764
head := getPropertyNameArg(args[0])
47574765
tail := getPropertyNameArg(r.errorChain.next.args[0])
4758-
var arg string
4759-
if len(tail) != 0 && tail[0] == '[' {
4760-
arg = head + tail
4761-
} else {
4762-
arg = head + "." + tail
4763-
}
4766+
arg := addToDottedName(head, tail)
47644767
r.errorChain = r.errorChain.next.next
47654768
if message == diagnostics.Types_of_property_0_are_incompatible {
47664769
message = diagnostics.The_types_of_0_are_incompatible_between_these_types
@@ -4772,6 +4775,28 @@ func (r *Relater) reportError(message *diagnostics.Message, args ...any) {
47724775
r.errorChain = &ErrorChain{next: r.errorChain, message: message, args: args}
47734776
}
47744777

4778+
func addToDottedName(head string, tail string) string {
4779+
if strings.HasPrefix(head, "new ") {
4780+
head = "(" + head + ")"
4781+
}
4782+
pos := 0
4783+
for {
4784+
if strings.HasPrefix(tail[pos:], "(") {
4785+
pos++
4786+
} else if strings.HasPrefix(tail[pos:], "new ") {
4787+
pos += 4
4788+
} else {
4789+
break
4790+
}
4791+
}
4792+
prefix := tail[:pos]
4793+
suffix := tail[pos:]
4794+
if strings.HasPrefix(suffix, "[") {
4795+
return prefix + head + suffix
4796+
}
4797+
return prefix + head + "." + suffix
4798+
}
4799+
47754800
func (r *Relater) getChainMessage(index int) *diagnostics.Message {
47764801
e := r.errorChain
47774802
for {

internal/jsnum/jsnum.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,10 @@ func (n Number) Remainder(d Number) Number {
137137
case n == 0:
138138
return n
139139
}
140-
141140
r := n - d*(n/d).trunc()
142-
if r == 0 || n < 0 {
141+
if r == 0 && n < 0 {
143142
return negativeZero
144143
}
145-
146144
return r
147145
}
148146

0 commit comments

Comments
 (0)