diff --git a/tsc/internal/ast/utilities.go b/tsc/internal/ast/utilities.go index e2d464fa9746c..bbdebc7fbf67f 100644 --- a/tsc/internal/ast/utilities.go +++ b/tsc/internal/ast/utilities.go @@ -1219,6 +1219,24 @@ func IsVarUsing(node *Node) bool { return GetCombinedNodeFlags(node)&NodeFlagsBlockScoped == NodeFlagsUsing } +// GetJSDocAugmentsTag returns the first @augments JSDoc tag for the given node, or nil if none exists. +func GetJSDocAugmentsTag(node *Node) *Node { + if node == nil { + return nil + } + for _, jsdoc := range node.JSDoc(nil) { + tags := jsdoc.AsJSDoc().Tags + if tags != nil { + for _, tag := range tags.Nodes { + if IsJSDocAugmentsTag(tag) { + return tag + } + } + } + } + return nil +} + // GetJSDocDeprecatedTag returns the first @deprecated JSDoc tag for the given node, or nil if none exists. func GetJSDocDeprecatedTag(node *Node) *Node { for _, jsdoc := range node.JSDoc(nil) { diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index ca77e97af8547..620b588237453 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -4334,14 +4334,14 @@ func (c *Checker) checkClassLikeDeclaration(node *ast.Node) { c.checkClassForStaticPropertyNameConflicts(node) } - baseTypeNode := ast.GetClassExtendsHeritageElement(node) + baseTypeNode := c.getEffectiveBaseTypeNode(classType) if baseTypeNode != nil { c.checkSourceElements(baseTypeNode.TypeArguments()) baseTypes := c.getBaseTypes(classType) if len(baseTypes) != 0 { baseType := baseTypes[0] - c.checkJSDocAugmentsTagMatchesExtends(node, baseTypeNode, baseType) baseConstructorType := c.getBaseConstructorTypeOfClass(classType) + c.checkJSDocAugmentsTagMatchesExtends(node, baseTypeNode, baseType, baseConstructorType) staticBaseType := c.getApparentType(baseConstructorType) c.checkBaseTypeAccessibility(staticBaseType, baseTypeNode) c.checkSourceElement(baseTypeNode.Expression()) @@ -4416,32 +4416,6 @@ func (c *Checker) checkClassLikeDeclaration(node *ast.Node) { c.checkPropertyInitialization(node) } -func (c *Checker) checkJSDocAugmentsTagMatchesExtends(node *ast.Node, baseTypeNode *ast.ExpressionWithTypeArgumentsNode, baseType *Type) { - if !ast.IsInJSFile(node) { - return - } - file := ast.GetSourceFileOfNode(node) - for _, j := range node.EagerJSDoc(file) { - if j.AsJSDoc().Tags == nil { - continue - } - for _, tag := range j.AsJSDoc().Tags.Nodes { - if tag.Kind != ast.KindJSDocAugmentsTag { - continue - } - sourceTypeNode := tag.ClassName() - if c.isTypeIdenticalTo(c.getTypeFromTypeNode(sourceTypeNode), baseType) { - continue - } - targetName := getIdentifierFromEntityNameExpression(baseTypeNode.Expression()) - sourceName := getIdentifierFromEntityNameExpression(sourceTypeNode.Expression()) - if targetName != nil && sourceName != nil { - c.error(sourceName, diagnostics.JSDoc_0_1_does_not_match_the_extends_2_clause, tag.TagName().Text(), sourceName.Text(), targetName.Text()) - } - } - } -} - func (c *Checker) checkClassForStaticPropertyNameConflicts(node *ast.Node) { if c.compilerOptions.GetUseDefineForClassFields() { return @@ -8653,7 +8627,9 @@ func (c *Checker) resolveCallExpression(node *ast.Node, candidatesOutArray *[]*S if !c.isErrorType(superType) { // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated // with the type arguments specified in the extends clause. - baseTypeNode := ast.GetClassExtendsHeritageElement(ast.GetContainingClass(node)) + containingClass := ast.GetContainingClass(node) + classType := c.getDeclaredTypeOfSymbol(c.getSymbolOfDeclaration(containingClass)) + baseTypeNode := c.getEffectiveBaseTypeNode(classType) if baseTypeNode != nil { baseConstructors := c.getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.TypeArguments(), baseTypeNode) return c.resolveCall(node, baseConstructors, candidatesOutArray, checkMode, SignatureFlagsNone, nil) @@ -19537,7 +19513,7 @@ func (c *Checker) resolveBaseTypesOfClass(t *Type) { if baseConstructorType.flags&(TypeFlagsObject|TypeFlagsIntersection|TypeFlagsAny) == 0 { return } - baseTypeNode := getBaseTypeNodeOfClass(t) + baseTypeNode := c.getEffectiveBaseTypeNode(t) var baseType *Type var originalBaseType *Type if baseConstructorType.symbol != nil { @@ -21178,7 +21154,7 @@ func (c *Checker) getDefaultConstructSignatures(classType *Type) []*Signature { flags := core.IfElse(isAbstract, SignatureFlagsConstruct|SignatureFlagsAbstract, SignatureFlagsConstruct) return []*Signature{c.newSignature(flags, nil, classType.AsInterfaceType().LocalTypeParameters(), nil, nil, classType, nil, 0)} } - baseTypeNode := getBaseTypeNodeOfClass(classType) + baseTypeNode := c.getEffectiveBaseTypeNode(classType) isJavaScript := declaration != nil && ast.IsInJSFile(declaration) typeArguments := c.getTypeArgumentsFromNode(baseTypeNode) typeArgCount := len(typeArguments) diff --git a/tsc/internal/checker/emitresolver.go b/tsc/internal/checker/emitresolver.go index 9bc590fd179bf..659c7c0eea62b 100644 --- a/tsc/internal/checker/emitresolver.go +++ b/tsc/internal/checker/emitresolver.go @@ -1321,3 +1321,14 @@ func (r *EmitResolver) IsThisPropertyAssignmentDeclarationRedundant(node *ast.No } return false } + +func (r *EmitResolver) GetEffectiveBaseTypeNode(node *ast.Node) *ast.Node { + r.checkerMu.Lock() + defer r.checkerMu.Unlock() + symbol := r.checker.getSymbolOfDeclaration(node) + if symbol == nil { + return ast.GetClassExtendsHeritageElement(node) + } + classType := r.checker.getDeclaredTypeOfSymbol(symbol) + return r.checker.getEffectiveBaseTypeNode(classType) +} diff --git a/tsc/internal/checker/jsdoc.go b/tsc/internal/checker/jsdoc.go index c8b5fb1adba44..2d9090ba6c359 100644 --- a/tsc/internal/checker/jsdoc.go +++ b/tsc/internal/checker/jsdoc.go @@ -98,3 +98,65 @@ func getAllJSDocTags(node *ast.Node) []*ast.Node { } return nil } + +func (c *Checker) getEffectiveBaseTypeNode(t *Type) *ast.Node { + baseTypeNode := getBaseTypeNodeOfClass(t) + if tag := c.tryGetMatchingJSDocAugmentsTag(t, baseTypeNode); tag != nil { + return tag.ClassName() + } + return baseTypeNode +} + +func (c *Checker) tryGetMatchingJSDocAugmentsTag(t *Type, baseTypeNode *ast.Node) *ast.Node { + if baseTypeNode == nil || !ast.IsInJSFile(baseTypeNode) || len(baseTypeNode.TypeArguments()) >= 1 { + return nil + } + expression := ast.SkipParentheses(baseTypeNode.Expression()) + if ast.IsCallExpression(expression) { + tag := ast.GetJSDocAugmentsTag(ast.GetClassLikeDeclarationOfSymbol(t.symbol)) + if tag == nil || len(tag.ClassName().TypeArguments()) == 0 { + return nil + } + baseConstructorType := c.getBaseConstructorTypeOfClass(t) + if baseConstructorType.flags&TypeFlagsIntersection == 0 { + sourceType := c.getTypeFromTypeNode(tag.ClassName()) + sourceSymbol := c.getMergedSymbol(getTargetType(sourceType).symbol) + baseSymbol := c.getMergedSymbol(c.getApparentType(baseConstructorType).symbol) + if sourceSymbol != nil && sourceSymbol == baseSymbol { + return tag + } + } + } + return nil +} + +func (c *Checker) checkJSDocAugmentsTagMatchesExtends(node *ast.Node, baseTypeNode *ast.ExpressionWithTypeArgumentsNode, baseType *Type, baseConstructorType *Type) { + if !ast.IsInJSFile(node) { + return + } + tag := ast.GetJSDocAugmentsTag(node) + if tag == nil { + return + } + sourceTypeNode := tag.ClassName() + sourceType := c.getTypeFromTypeNode(sourceTypeNode) + sourceSymbol := c.getMergedSymbol(getTargetType(sourceType).symbol) + sourceName := getIdentifierFromEntityNameExpression(sourceTypeNode.Expression()) + if sourceName != nil && ast.IsCallExpression(ast.SkipParentheses(baseTypeNode.Expression())) { + targetSymbol := c.getMergedSymbol(c.getApparentType(baseConstructorType).symbol) + if sourceSymbol != nil && targetSymbol != nil && sourceSymbol != targetSymbol { + declarationName := getIdentifierNameOfSymbolDeclaration(targetSymbol) + if declarationName != nil { + c.error(sourceName, diagnostics.JSDoc_0_1_does_not_match_the_extends_2_clause, tag.TagName().Text(), sourceName.Text(), declarationName.Text()) + return + } + } + } + if c.isTypeIdenticalTo(sourceType, baseType) { + return + } + targetName := getIdentifierFromEntityNameExpression(baseTypeNode.Expression()) + if targetName != nil && sourceName != nil { + c.error(sourceName, diagnostics.JSDoc_0_1_does_not_match_the_extends_2_clause, tag.TagName().Text(), sourceName.Text(), targetName.Text()) + } +} diff --git a/tsc/internal/checker/utilities.go b/tsc/internal/checker/utilities.go index 132b3b98692c1..4695e80e68ba6 100644 --- a/tsc/internal/checker/utilities.go +++ b/tsc/internal/checker/utilities.go @@ -35,6 +35,21 @@ func NewDiagnosticChainForNode(chain *ast.Diagnostic, node *ast.Node, message *d return NewDiagnosticForNode(node, message, args...) } +func getIdentifierNameOfSymbolDeclaration(symbol *ast.Symbol) *ast.Node { + if symbol == nil { + return nil + } + if name := ast.GetNameOfDeclaration(symbol.ValueDeclaration); name != nil && ast.IsIdentifier(name) { + return name + } + for _, declaration := range symbol.Declarations { + if name := ast.GetNameOfDeclaration(declaration); name != nil && ast.IsIdentifier(name) { + return name + } + } + return nil +} + func findInMap[K comparable, V any](m map[K]V, predicate func(V) bool) V { for _, value := range m { if predicate(value) { diff --git a/tsc/internal/parser/parser_test.go b/tsc/internal/parser/parser_test.go index 200064160228b..1646f7b7799dc 100644 --- a/tsc/internal/parser/parser_test.go +++ b/tsc/internal/parser/parser_test.go @@ -308,6 +308,53 @@ function foo(options) {}` assert.Equal(t, scanner.GetTokenPosOfNode(typeNode, file, false /*includeJSDoc*/), strings.Index(sourceText, "{{")+1) } +func TestJSDocDoesNotAugmentCallHeritage(t *testing.T) { + t.Parallel() + sourceText := `/** @template T */ +class A { + static extend() { + return this; + } +} + +/** @extends {A} */ +class B extends A.extend() {}` + opts := ast.SourceFileParseOptions{ + FileName: "/index.js", + Path: "/index.js", + } + + file := parser.ParseSourceFile(opts, sourceText, core.ScriptKindJS) + statements := file.Statements.Nodes + assert.Equal(t, len(statements), 2) + + classB := statements[1] + assert.Assert(t, ast.IsClassDeclaration(classB)) + + baseType := ast.GetClassExtendsHeritageElement(classB) + assert.Assert(t, baseType != nil) + assert.Assert(t, ast.IsCallExpression(baseType.Expression())) + assert.Equal(t, scanner.GetTextOfNode(baseType.Expression()), "A.extend()") + + typeArguments := baseType.TypeArguments() + assert.Equal(t, len(typeArguments), 0) + + jsDocs := classB.JSDoc(file) + assert.Equal(t, len(jsDocs), 1) + + tags := jsDocs[0].AsJSDoc().Tags + assert.Assert(t, tags != nil) + assert.Equal(t, len(tags.Nodes), 1) + + tag := tags.Nodes[0] + assert.Assert(t, ast.IsJSDocAugmentsTag(tag)) + + sourceTypeArguments := tag.ClassName().TypeArguments() + assert.Equal(t, len(sourceTypeArguments), 1) + assert.Equal(t, sourceTypeArguments[0].Kind, ast.KindStringKeyword) + assert.Equal(t, ast.GetReparsedNodeForNode(sourceTypeArguments[0]), sourceTypeArguments[0]) +} + func TestSourceFilePositionMapWithNonASCIIStringLiteral(t *testing.T) { t.Parallel() sourceText := `const x = "─"; diff --git a/tsc/internal/parser/reparser.go b/tsc/internal/parser/reparser.go index bdebd40ade98d..ba5bdb7987d2a 100644 --- a/tsc/internal/parser/reparser.go +++ b/tsc/internal/parser/reparser.go @@ -597,21 +597,23 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node) }); extendsClause != nil && len(extendsClause.AsHeritageClause().Types.Nodes) == 1 { target := extendsClause.AsHeritageClause().Types.Nodes[0].AsExpressionWithTypeArguments() source := tag.ClassName().AsExpressionWithTypeArguments() - if ast.HasSamePropertyAccessName(target.Expression, source.Expression) { - if target.TypeArguments == nil && source.TypeArguments != nil { - newArguments := p.nodeSliceArena.NewSlice(len(source.TypeArguments.Nodes)) - for i, arg := range source.TypeArguments.Nodes { - newArguments[i] = p.addDeepCloneReparse(arg) - } - target.TypeArguments = p.newNodeList(source.TypeArguments.Loc, newArguments) - p.finishMutatedNode(target.AsNode()) - } + if target.TypeArguments == nil && source.TypeArguments != nil && ast.HasSamePropertyAccessName(target.Expression, source.Expression) { + p.setReparsedTypeArguments(target, source.TypeArguments) } } } } } +func (p *Parser) setReparsedTypeArguments(target *ast.ExpressionWithTypeArguments, source *ast.NodeList) { + typeArguments := p.nodeSliceArena.NewSlice(len(source.Nodes)) + for i, typeArgument := range source.Nodes { + typeArguments[i] = p.addDeepCloneReparse(typeArgument) + } + target.TypeArguments = p.newNodeList(source.Loc, typeArguments) + p.finishMutatedNode(target.AsNode()) +} + func (p *Parser) makeQuestionIfOptional(parameter *ast.JSDocParameterOrPropertyTag) *ast.Node { var questionToken *ast.Node if parameter.IsBracketed || parameter.TypeExpression != nil && parameter.TypeExpression.Type().Kind == ast.KindJSDocOptionalType { diff --git a/tsc/internal/printer/emitresolver.go b/tsc/internal/printer/emitresolver.go index f9b98dba994ce..11ecdc5f71e5c 100644 --- a/tsc/internal/printer/emitresolver.go +++ b/tsc/internal/printer/emitresolver.go @@ -110,6 +110,7 @@ type EmitResolver interface { IsLateBound(node *ast.Node) bool IsOptionalParameter(node *ast.Node) bool IsThisPropertyAssignmentDeclarationRedundant(node *ast.Node) bool + GetEffectiveBaseTypeNode(node *ast.Node) *ast.Node // isolatedDeclarations-specific declaration emit GetPropertiesOfContainerFunction(node *ast.Node) []*ast.Symbol diff --git a/tsc/internal/transformers/declarations/transform.go b/tsc/internal/transformers/declarations/transform.go index 32fa0201a9ef5..1aca2d50fbdf0 100644 --- a/tsc/internal/transformers/declarations/transform.go +++ b/tsc/internal/transformers/declarations/transform.go @@ -1996,9 +1996,14 @@ func (tx *DeclarationTransformer) transformClassDeclaration(input *ast.ClassDecl members := tx.buildClassMembers(input.AsNode(), extraMembers...) - extendsClause := getEffectiveBaseTypeNode(input.AsNode()) + extendsClause := ast.GetClassExtendsHeritageElement(input.AsNode()) if extendsClause != nil && !ast.IsEntityNameExpression(extendsClause.AsExpressionWithTypeArguments().Expression) && extendsClause.AsExpressionWithTypeArguments().Expression.Kind != ast.KindNullKeyword { + effectiveBaseType := tx.resolver.GetEffectiveBaseTypeNode(input.AsNode()) + typeArguments := extendsClause.AsExpressionWithTypeArguments().TypeArguments + if effectiveBaseType != nil { + typeArguments = effectiveBaseType.AsExpressionWithTypeArguments().TypeArguments + } tx.tracker.ReportInferenceFallback(extendsClause.AsExpressionWithTypeArguments().Expression) // Add an isolated declarations error on this extends clause oldId := "default" if ast.NodeIsPresent(input.Name()) && ast.IsIdentifier(input.Name()) && len(input.Name().Text()) > 0 { @@ -2034,7 +2039,7 @@ func (tx *DeclarationTransformer) transformClassDeclaration(input *ast.ClassDecl tx.Factory().UpdateExpressionWithTypeArguments( extendsClause.AsExpressionWithTypeArguments(), newId, - tx.Visitor().VisitNodes(extendsClause.AsExpressionWithTypeArguments().TypeArguments), + tx.Visitor().VisitNodes(typeArguments), ), }), ) diff --git a/tsc/internal/transformers/declarations/util.go b/tsc/internal/transformers/declarations/util.go index cfe66b26ba76c..b33e792d80b5b 100644 --- a/tsc/internal/transformers/declarations/util.go +++ b/tsc/internal/transformers/declarations/util.go @@ -161,19 +161,6 @@ func shouldEmitFunctionProperties(input *ast.FunctionDeclaration) bool { }) } -func getEffectiveBaseTypeNode(node *ast.Node) *ast.Node { - baseType := ast.GetClassExtendsHeritageElement(node) - // !!! TODO: JSDoc support - // if (baseType && isInJSFile(node)) { - // // Prefer an @augments tag because it may have type parameters. - // const tag = getJSDocAugmentsTag(node); - // if (tag) { - // return tag.class; - // } - // } - return baseType -} - func isScopeMarker(node *ast.Node) bool { return ast.IsExportAssignment(node) || ast.IsExportDeclaration(node) } diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag10.js b/tsc/testdata/baselines/reference/conformance/extendsTag10.js new file mode 100644 index 0000000000000..28ab2da1e2727 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag10.js @@ -0,0 +1,76 @@ +//// [tests/cases/conformance/jsdoc/extendsTag10.ts] //// + +//// [a.js] +/** @template T */ +class A { + /** @returns {T} */ + get value() { + throw new Error(); + } +} + +/** @param {any} Base */ +function mixin(Base) { + return class extends Base { + extra = 1; + }; +} + +/** @extends {A} */ +class B extends mixin(A) {} + +const value = new B().value; +const extra = new B().extra; + + +//// [a.js] +"use strict"; +/** @template T */ +class A { + /** @returns {T} */ + get value() { + throw new Error(); + } +} +/** @param {any} Base */ +function mixin(Base) { + return class extends Base { + constructor() { + super(...arguments); + this.extra = 1; + } + }; +} +/** @extends {A} */ +class B extends mixin(A) { +} +const value = new B().value; +const extra = new B().extra; + + +//// [a.d.ts] +/** @template T */ +declare class A { + /** @returns {T} */ + get value(): T; +} +/** @param {any} Base */ +declare function mixin(Base: any): { + new (): { + [x: string]: any; + extra: number; + }; + [x: string]: any; +}; +declare const B_base: { + new (): { + [x: string]: any; + extra: number; + }; + [x: string]: any; +}; +/** @extends {A} */ +declare class B extends B_base { +} +declare const value: any; +declare const extra: number; diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag10.symbols b/tsc/testdata/baselines/reference/conformance/extendsTag10.symbols new file mode 100644 index 0000000000000..2cb465b2039e2 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag10.symbols @@ -0,0 +1,46 @@ +//// [tests/cases/conformance/jsdoc/extendsTag10.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : Symbol(A, Decl(a.js, 0, 0)) + + /** @returns {T} */ + get value() { +>value : Symbol(A.value, Decl(a.js, 1, 9)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } +} + +/** @param {any} Base */ +function mixin(Base) { +>mixin : Symbol(mixin, Decl(a.js, 6, 1)) +>Base : Symbol(Base, Decl(a.js, 9, 15)) + + return class extends Base { +>Base : Symbol(Base, Decl(a.js, 9, 15)) + + extra = 1; +>extra : Symbol((Anonymous class).extra, Decl(a.js, 10, 31)) + + }; +} + +/** @extends {A} */ +class B extends mixin(A) {} +>B : Symbol(B, Decl(a.js, 13, 1)) +>mixin : Symbol(mixin, Decl(a.js, 6, 1)) +>A : Symbol(A, Decl(a.js, 0, 0)) + +const value = new B().value; +>value : Symbol(value, Decl(a.js, 18, 5)) +>B : Symbol(B, Decl(a.js, 13, 1)) + +const extra = new B().extra; +>extra : Symbol(extra, Decl(a.js, 19, 5)) +>new B().extra : Symbol((Anonymous class).extra, Decl(a.js, 10, 31)) +>B : Symbol(B, Decl(a.js, 13, 1)) +>extra : Symbol((Anonymous class).extra, Decl(a.js, 10, 31)) + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag10.types b/tsc/testdata/baselines/reference/conformance/extendsTag10.types new file mode 100644 index 0000000000000..5b9852dab2420 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag10.types @@ -0,0 +1,54 @@ +//// [tests/cases/conformance/jsdoc/extendsTag10.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : A + + /** @returns {T} */ + get value() { +>value : T + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor + } +} + +/** @param {any} Base */ +function mixin(Base) { +>mixin : (Base: any) => typeof (Anonymous class) +>Base : any + + return class extends Base { +>class extends Base { extra = 1; } : typeof (Anonymous class) +>Base : any + + extra = 1; +>extra : number +>1 : 1 + + }; +} + +/** @extends {A} */ +class B extends mixin(A) {} +>B : B +>mixin(A) : (Anonymous class) +>mixin : (Base: any) => typeof (Anonymous class) +>A : typeof A + +const value = new B().value; +>value : any +>new B().value : any +>new B() : B +>B : typeof B +>value : any + +const extra = new B().extra; +>extra : number +>new B().extra : number +>new B() : B +>B : typeof B +>extra : number + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag11.errors.txt b/tsc/testdata/baselines/reference/conformance/extendsTag11.errors.txt new file mode 100644 index 0000000000000..bd9db178f4833 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag11.errors.txt @@ -0,0 +1,49 @@ +a.js(18,7): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. +a.js(23,15): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. +a.js(36,22): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. + + +==== a.js (3 errors) ==== + /** @template T */ + class A { + /** @param {T} value */ + constructor(value) { + this.value = value; + } + + /** @returns {typeof A} */ + static extend() { + return this; + } + } + + /** @extends {A} */ + class B extends A.extend() {} + + new B("ok"); + new B(1); + ~ +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. + + /** @extends {A} */ + class C extends A.extend() { + constructor() { + super(1); + ~ +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. + } + } + + /** + * @param {number} required + * @returns {typeof A} + */ + function getA(required) { + return A; + } + + /** @extends {A} */ + class D extends getA("wrong") {} + ~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag11.symbols b/tsc/testdata/baselines/reference/conformance/extendsTag11.symbols new file mode 100644 index 0000000000000..886f9ac64cf2f --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag11.symbols @@ -0,0 +1,70 @@ +//// [tests/cases/conformance/jsdoc/extendsTag11.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : Symbol(A, Decl(a.js, 0, 0)) + + /** @param {T} value */ + constructor(value) { +>value : Symbol(value, Decl(a.js, 3, 16)) + + this.value = value; +>this.value : Symbol(A.value, Decl(a.js, 3, 24)) +>this : Symbol(A, Decl(a.js, 0, 0)) +>value : Symbol(A.value, Decl(a.js, 3, 24)) +>value : Symbol(value, Decl(a.js, 3, 16)) + } + + /** @returns {typeof A} */ + static extend() { +>extend : Symbol(A.extend, Decl(a.js, 5, 5)) + + return this; +>this : Symbol(A, Decl(a.js, 0, 0)) + } +} + +/** @extends {A} */ +class B extends A.extend() {} +>B : Symbol(B, Decl(a.js, 11, 1)) +>A.extend : Symbol(A.extend, Decl(a.js, 5, 5)) +>A : Symbol(A, Decl(a.js, 0, 0)) +>extend : Symbol(A.extend, Decl(a.js, 5, 5)) + +new B("ok"); +>B : Symbol(B, Decl(a.js, 11, 1)) + +new B(1); +>B : Symbol(B, Decl(a.js, 11, 1)) + +/** @extends {A} */ +class C extends A.extend() { +>C : Symbol(C, Decl(a.js, 17, 9)) +>A.extend : Symbol(A.extend, Decl(a.js, 5, 5)) +>A : Symbol(A, Decl(a.js, 0, 0)) +>extend : Symbol(A.extend, Decl(a.js, 5, 5)) + + constructor() { + super(1); +>super : Symbol(A, Decl(a.js, 0, 0)) + } +} + +/** + * @param {number} required + * @returns {typeof A} + */ +function getA(required) { +>getA : Symbol(getA, Decl(a.js, 24, 1)) +>required : Symbol(required, Decl(a.js, 30, 14)) + + return A; +>A : Symbol(A, Decl(a.js, 0, 0)) +} + +/** @extends {A} */ +class D extends getA("wrong") {} +>D : Symbol(D, Decl(a.js, 32, 1)) +>getA : Symbol(getA, Decl(a.js, 24, 1)) + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag11.types b/tsc/testdata/baselines/reference/conformance/extendsTag11.types new file mode 100644 index 0000000000000..e0fb705e38eee --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag11.types @@ -0,0 +1,81 @@ +//// [tests/cases/conformance/jsdoc/extendsTag11.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : A + + /** @param {T} value */ + constructor(value) { +>value : T + + this.value = value; +>this.value = value : T +>this.value : any +>this : this +>value : any +>value : T + } + + /** @returns {typeof A} */ + static extend() { +>extend : () => typeof A + + return this; +>this : typeof A + } +} + +/** @extends {A} */ +class B extends A.extend() {} +>B : B +>A.extend() : A +>A.extend : () => typeof A +>A : typeof A +>extend : () => typeof A + +new B("ok"); +>new B("ok") : B +>B : typeof B +>"ok" : "ok" + +new B(1); +>new B(1) : B +>B : typeof B +>1 : 1 + +/** @extends {A} */ +class C extends A.extend() { +>C : C +>A.extend() : A +>A.extend : () => typeof A +>A : typeof A +>extend : () => typeof A + + constructor() { + super(1); +>super(1) : void +>super : typeof A +>1 : 1 + } +} + +/** + * @param {number} required + * @returns {typeof A} + */ +function getA(required) { +>getA : (required: number) => typeof A +>required : number + + return A; +>A : typeof A +} + +/** @extends {A} */ +class D extends getA("wrong") {} +>D : D +>getA("wrong") : A +>getA : (required: number) => typeof A +>"wrong" : "wrong" + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag7.js b/tsc/testdata/baselines/reference/conformance/extendsTag7.js new file mode 100644 index 0000000000000..03a22db1d8266 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag7.js @@ -0,0 +1,54 @@ +//// [tests/cases/conformance/jsdoc/extendsTag7.ts] //// + +//// [a.js] +/** @template T */ +class A { + /** @returns {T} */ + get value() { + throw new Error(); + } + + /** @returns {typeof A} */ + static extend() { + return this; + } +} + +/** @extends {A} */ +class B extends A.extend() {} + +const value = new B().value; + + +//// [a.js] +"use strict"; +/** @template T */ +class A { + /** @returns {T} */ + get value() { + throw new Error(); + } + /** @returns {typeof A} */ + static extend() { + return this; + } +} +/** @extends {A} */ +class B extends A.extend() { +} +const value = new B().value; + + +//// [a.d.ts] +/** @template T */ +declare class A { + /** @returns {T} */ + get value(): T; + /** @returns {typeof A} */ + static extend(): typeof A; +} +declare const B_base: typeof A; +/** @extends {A} */ +declare class B extends B_base { +} +declare const value: string; diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag7.symbols b/tsc/testdata/baselines/reference/conformance/extendsTag7.symbols new file mode 100644 index 0000000000000..20349b715c45d --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag7.symbols @@ -0,0 +1,37 @@ +//// [tests/cases/conformance/jsdoc/extendsTag7.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : Symbol(A, Decl(a.js, 0, 0)) + + /** @returns {T} */ + get value() { +>value : Symbol(A.value, Decl(a.js, 1, 9)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } + + /** @returns {typeof A} */ + static extend() { +>extend : Symbol(A.extend, Decl(a.js, 5, 5)) + + return this; +>this : Symbol(A, Decl(a.js, 0, 0)) + } +} + +/** @extends {A} */ +class B extends A.extend() {} +>B : Symbol(B, Decl(a.js, 11, 1)) +>A.extend : Symbol(A.extend, Decl(a.js, 5, 5)) +>A : Symbol(A, Decl(a.js, 0, 0)) +>extend : Symbol(A.extend, Decl(a.js, 5, 5)) + +const value = new B().value; +>value : Symbol(value, Decl(a.js, 16, 5)) +>new B().value : Symbol(A.value, Decl(a.js, 1, 9)) +>B : Symbol(B, Decl(a.js, 11, 1)) +>value : Symbol(A.value, Decl(a.js, 1, 9)) + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag7.types b/tsc/testdata/baselines/reference/conformance/extendsTag7.types new file mode 100644 index 0000000000000..50b3b50060c01 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag7.types @@ -0,0 +1,40 @@ +//// [tests/cases/conformance/jsdoc/extendsTag7.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : A + + /** @returns {T} */ + get value() { +>value : T + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor + } + + /** @returns {typeof A} */ + static extend() { +>extend : () => typeof A + + return this; +>this : typeof A + } +} + +/** @extends {A} */ +class B extends A.extend() {} +>B : B +>A.extend() : A +>A.extend : () => typeof A +>A : typeof A +>extend : () => typeof A + +const value = new B().value; +>value : string +>new B().value : string +>new B() : B +>B : typeof B +>value : string + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag8.errors.txt b/tsc/testdata/baselines/reference/conformance/extendsTag8.errors.txt new file mode 100644 index 0000000000000..9512110af2b8c --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag8.errors.txt @@ -0,0 +1,23 @@ +a.js(12,15): error TS8023: JSDoc '@extends C' does not match the 'extends A' clause. +a.js(13,17): error TS8026: Expected A type arguments; provide these with an '@extends' tag. + + +==== a.js (2 errors) ==== + /** @template T */ + class A { + /** @returns {typeof A} */ + static extend() { + return this; + } + } + + /** @template T */ + class C {} + + /** @extends {C} */ + ~ +!!! error TS8023: JSDoc '@extends C' does not match the 'extends A' clause. + class B extends A.extend() {} + ~~~~~~~~~~ +!!! error TS8026: Expected A type arguments; provide these with an '@extends' tag. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag8.symbols b/tsc/testdata/baselines/reference/conformance/extendsTag8.symbols new file mode 100644 index 0000000000000..8b10f3499d474 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag8.symbols @@ -0,0 +1,27 @@ +//// [tests/cases/conformance/jsdoc/extendsTag8.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : Symbol(A, Decl(a.js, 0, 0)) + + /** @returns {typeof A} */ + static extend() { +>extend : Symbol(A.extend, Decl(a.js, 1, 9)) + + return this; +>this : Symbol(A, Decl(a.js, 0, 0)) + } +} + +/** @template T */ +class C {} +>C : Symbol(C, Decl(a.js, 6, 1)) + +/** @extends {C} */ +class B extends A.extend() {} +>B : Symbol(B, Decl(a.js, 9, 10)) +>A.extend : Symbol(A.extend, Decl(a.js, 1, 9)) +>A : Symbol(A, Decl(a.js, 0, 0)) +>extend : Symbol(A.extend, Decl(a.js, 1, 9)) + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag8.types b/tsc/testdata/baselines/reference/conformance/extendsTag8.types new file mode 100644 index 0000000000000..9c62961a7581e --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag8.types @@ -0,0 +1,28 @@ +//// [tests/cases/conformance/jsdoc/extendsTag8.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : A + + /** @returns {typeof A} */ + static extend() { +>extend : () => typeof A + + return this; +>this : typeof A + } +} + +/** @template T */ +class C {} +>C : C + +/** @extends {C} */ +class B extends A.extend() {} +>B : B +>A.extend() : A +>A.extend : () => typeof A +>A : typeof A +>extend : () => typeof A + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag9.errors.txt b/tsc/testdata/baselines/reference/conformance/extendsTag9.errors.txt new file mode 100644 index 0000000000000..6c4a1fd1494de --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag9.errors.txt @@ -0,0 +1,39 @@ +a.js(22,15): error TS8023: JSDoc '@extends A' does not match the 'extends C' clause. +a.js(23,17): error TS8026: Expected C type arguments; provide these with an '@extends' tag. +a.js(25,19): error TS2339: Property 'a' does not exist on type 'B'. + + +==== a.js (3 errors) ==== + /** @template T */ + class A { + /** @returns {T} */ + get a() { + throw new Error(); + } + + /** @returns {typeof C} */ + static extend() { + return C; + } + } + + /** @template T */ + class C { + /** @returns {T} */ + get c() { + throw new Error(); + } + } + + /** @extends {A} */ + ~ +!!! error TS8023: JSDoc '@extends A' does not match the 'extends C' clause. + class B extends A.extend() {} + ~~~~~~~~~~ +!!! error TS8026: Expected C type arguments; provide these with an '@extends' tag. + + const a = new B().a; + ~ +!!! error TS2339: Property 'a' does not exist on type 'B'. + const c = new B().c; + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag9.symbols b/tsc/testdata/baselines/reference/conformance/extendsTag9.symbols new file mode 100644 index 0000000000000..6ff2bd61bdd27 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag9.symbols @@ -0,0 +1,54 @@ +//// [tests/cases/conformance/jsdoc/extendsTag9.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : Symbol(A, Decl(a.js, 0, 0)) + + /** @returns {T} */ + get a() { +>a : Symbol(A.a, Decl(a.js, 1, 9)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } + + /** @returns {typeof C} */ + static extend() { +>extend : Symbol(A.extend, Decl(a.js, 5, 5)) + + return C; +>C : Symbol(C, Decl(a.js, 11, 1)) + } +} + +/** @template T */ +class C { +>C : Symbol(C, Decl(a.js, 11, 1)) + + /** @returns {T} */ + get c() { +>c : Symbol(C.c, Decl(a.js, 14, 9)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } +} + +/** @extends {A} */ +class B extends A.extend() {} +>B : Symbol(B, Decl(a.js, 19, 1)) +>A.extend : Symbol(A.extend, Decl(a.js, 5, 5)) +>A : Symbol(A, Decl(a.js, 0, 0)) +>extend : Symbol(A.extend, Decl(a.js, 5, 5)) + +const a = new B().a; +>a : Symbol(a, Decl(a.js, 24, 5)) +>B : Symbol(B, Decl(a.js, 19, 1)) + +const c = new B().c; +>c : Symbol(c, Decl(a.js, 25, 5)) +>new B().c : Symbol(C.c, Decl(a.js, 14, 9)) +>B : Symbol(B, Decl(a.js, 19, 1)) +>c : Symbol(C.c, Decl(a.js, 14, 9)) + diff --git a/tsc/testdata/baselines/reference/conformance/extendsTag9.types b/tsc/testdata/baselines/reference/conformance/extendsTag9.types new file mode 100644 index 0000000000000..bf6293901155a --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/extendsTag9.types @@ -0,0 +1,61 @@ +//// [tests/cases/conformance/jsdoc/extendsTag9.ts] //// + +=== a.js === +/** @template T */ +class A { +>A : A + + /** @returns {T} */ + get a() { +>a : T + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor + } + + /** @returns {typeof C} */ + static extend() { +>extend : () => typeof C + + return C; +>C : typeof C + } +} + +/** @template T */ +class C { +>C : C + + /** @returns {T} */ + get c() { +>c : T + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor + } +} + +/** @extends {A} */ +class B extends A.extend() {} +>B : B +>A.extend() : C +>A.extend : () => typeof C +>A : typeof A +>extend : () => typeof C + +const a = new B().a; +>a : any +>new B().a : any +>new B() : B +>B : typeof B +>a : any + +const c = new B().c; +>c : any +>new B().c : any +>new B() : B +>B : typeof B +>c : any + diff --git a/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag10.ts b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag10.ts new file mode 100644 index 0000000000000..93c7e3e57b711 --- /dev/null +++ b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag10.ts @@ -0,0 +1,27 @@ +// @target: es2015 +// @allowJs: true +// @checkJs: true +// @declaration: true +// @outDir: out + +// @filename: a.js +/** @template T */ +class A { + /** @returns {T} */ + get value() { + throw new Error(); + } +} + +/** @param {any} Base */ +function mixin(Base) { + return class extends Base { + extra = 1; + }; +} + +/** @extends {A} */ +class B extends mixin(A) {} + +const value = new B().value; +const extra = new B().extra; diff --git a/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag11.ts b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag11.ts new file mode 100644 index 0000000000000..2d7b0d7779701 --- /dev/null +++ b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag11.ts @@ -0,0 +1,42 @@ +// @target: es2015 +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @filename: a.js +/** @template T */ +class A { + /** @param {T} value */ + constructor(value) { + this.value = value; + } + + /** @returns {typeof A} */ + static extend() { + return this; + } +} + +/** @extends {A} */ +class B extends A.extend() {} + +new B("ok"); +new B(1); + +/** @extends {A} */ +class C extends A.extend() { + constructor() { + super(1); + } +} + +/** + * @param {number} required + * @returns {typeof A} + */ +function getA(required) { + return A; +} + +/** @extends {A} */ +class D extends getA("wrong") {} diff --git a/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag7.ts b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag7.ts new file mode 100644 index 0000000000000..3733efd21dbce --- /dev/null +++ b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag7.ts @@ -0,0 +1,24 @@ +// @target: es2015 +// @allowJs: true +// @checkJs: true +// @declaration: true +// @outDir: out + +// @filename: a.js +/** @template T */ +class A { + /** @returns {T} */ + get value() { + throw new Error(); + } + + /** @returns {typeof A} */ + static extend() { + return this; + } +} + +/** @extends {A} */ +class B extends A.extend() {} + +const value = new B().value; diff --git a/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag8.ts b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag8.ts new file mode 100644 index 0000000000000..d63c39d8f257c --- /dev/null +++ b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag8.ts @@ -0,0 +1,19 @@ +// @target: es2015 +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @filename: a.js +/** @template T */ +class A { + /** @returns {typeof A} */ + static extend() { + return this; + } +} + +/** @template T */ +class C {} + +/** @extends {C} */ +class B extends A.extend() {} diff --git a/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag9.ts b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag9.ts new file mode 100644 index 0000000000000..a5fe80b7e5fde --- /dev/null +++ b/tsc/testdata/tests/cases/conformance/jsdoc/extendsTag9.ts @@ -0,0 +1,32 @@ +// @target: es2015 +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @filename: a.js +/** @template T */ +class A { + /** @returns {T} */ + get a() { + throw new Error(); + } + + /** @returns {typeof C} */ + static extend() { + return C; + } +} + +/** @template T */ +class C { + /** @returns {T} */ + get c() { + throw new Error(); + } +} + +/** @extends {A} */ +class B extends A.extend() {} + +const a = new B().a; +const c = new B().c;