Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
27 changes: 27 additions & 0 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4324,6 +4324,7 @@ func (c *Checker) checkClassLikeDeclaration(node *ast.Node) {
baseTypes := c.getBaseTypes(classType)
if len(baseTypes) != 0 {
baseType := baseTypes[0]
c.checkJSDocAugmentsTagMatchesExtends(node, baseTypeNode, baseType)
baseConstructorType := c.getBaseConstructorTypeOfClass(classType)
staticBaseType := c.getApparentType(baseConstructorType)
c.checkBaseTypeAccessibility(staticBaseType, baseTypeNode)
Expand Down Expand Up @@ -4399,6 +4400,32 @@ 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
Expand Down
18 changes: 0 additions & 18 deletions internal/checker/grammarchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,24 +912,6 @@ func (c *Checker) checkGrammarClassDeclarationHeritageClauses(node *ast.ClassLik
return c.grammarErrorOnFirstToken(typeNodes[1], diagnostics.Classes_can_only_extend_a_single_class)
}

if len(typeNodes) > 0 {
for _, j := range node.EagerJSDoc(file) {
if j.AsJSDoc().Tags == nil {
continue
}
for _, tag := range j.AsJSDoc().Tags.Nodes {
if tag.Kind == ast.KindJSDocAugmentsTag {
target := typeNodes[0].AsExpressionWithTypeArguments()
source := tag.ClassName().AsExpressionWithTypeArguments()
targetName := getIdentifierFromEntityNameExpression(target.Expression)
sourceName := getIdentifierFromEntityNameExpression(source.Expression)
if targetName != nil && sourceName != nil && targetName.Text() != sourceName.Text() {
return c.grammarErrorOnNode(sourceName, diagnostics.JSDoc_0_1_does_not_match_the_extends_2_clause, tag.TagName().Text(), sourceName.Text(), targetName.Text())
}
}
}
}
}
seenExtendsClause = true
} else {
if heritageClause.Token != ast.KindImplementsKeyword {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/jsdocAugmentsAliasExtends.ts] ////

=== jsdocAugmentsAliasExtends.ts ===
declare class Base {
>Base : Symbol(Base, Decl(jsdocAugmentsAliasExtends.ts, 0, 0))
}
declare class Other {
>Other : Symbol(Other, Decl(jsdocAugmentsAliasExtends.ts, 1, 1))
}
declare const StateDependencies_base: typeof Base;
>StateDependencies_base : Symbol(StateDependencies_base, Decl(jsdocAugmentsAliasExtends.ts, 4, 13))
>Base : Symbol(Base, Decl(jsdocAugmentsAliasExtends.ts, 0, 0))

/**
* @augments {Base}
*/
export declare class StateDependencies extends StateDependencies_base {
>StateDependencies : Symbol(StateDependencies, Decl(jsdocAugmentsAliasExtends.ts, 4, 50))
>StateDependencies_base : Symbol(StateDependencies_base, Decl(jsdocAugmentsAliasExtends.ts, 4, 13))
}
/**
* @augments {Base}
*/
export declare class TypeScriptStateDependencies extends Other {
>TypeScriptStateDependencies : Symbol(TypeScriptStateDependencies, Decl(jsdocAugmentsAliasExtends.ts, 9, 1))
>Other : Symbol(Other, Decl(jsdocAugmentsAliasExtends.ts, 1, 1))
}

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

=== jsdocAugmentsAliasExtends.ts ===
declare class Base {
>Base : Base
}
declare class Other {
>Other : Other
}
declare const StateDependencies_base: typeof Base;
>StateDependencies_base : typeof Base
>Base : typeof Base

/**
* @augments {Base}
*/
export declare class StateDependencies extends StateDependencies_base {
>StateDependencies : StateDependencies
>StateDependencies_base : Base
}
/**
* @augments {Base}
*/
export declare class TypeScriptStateDependencies extends Other {
>TypeScriptStateDependencies : TypeScriptStateDependencies
>Other : Other
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ main.js(2,20): error TS8023: JSDoc '@extends Component' does not match the 'exte

==== react.d.ts (0 errors) ====
declare namespace React {
class Component {}
class PureComponent {}
class Component { component: string }
class PureComponent { pure: string }
}

==== main.js (1 errors) ====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
declare namespace React {
>React : Symbol(React, Decl(react.d.ts, 0, 0))

class Component {}
class Component { component: string }
>Component : Symbol(Component, Decl(react.d.ts, 0, 25))
>component : Symbol(Component.component, Decl(react.d.ts, 1, 21))

class PureComponent {}
>PureComponent : Symbol(PureComponent, Decl(react.d.ts, 1, 22))
class PureComponent { pure: string }
>PureComponent : Symbol(PureComponent, Decl(react.d.ts, 1, 41))
>pure : Symbol(PureComponent.pure, Decl(react.d.ts, 2, 25))
}

=== main.js ===
Expand All @@ -17,9 +19,9 @@ declare namespace React {
*/
class C extends React.PureComponent {}
>C : Symbol(C, Decl(main.js, 0, 0))
>React.PureComponent : Symbol(React.PureComponent, Decl(react.d.ts, 1, 22))
>React.PureComponent : Symbol(React.PureComponent, Decl(react.d.ts, 1, 41))
>React : Symbol(React, Decl(react.d.ts, 0, 0))
>PureComponent : Symbol(React.PureComponent, Decl(react.d.ts, 1, 22))
>PureComponent : Symbol(React.PureComponent, Decl(react.d.ts, 1, 41))

/**
* @extends {React.Component}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
declare namespace React {
>React : typeof React

class Component {}
class Component { component: string }
>Component : Component
>component : string

class PureComponent {}
class PureComponent { pure: string }
>PureComponent : PureComponent
>pure : string
}

=== main.js ===
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error TS5055: Cannot write file 'b.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
b.js(3,15): error TS2314: Generic type 'A<T>' requires 1 type argument(s).
b.js(4,25): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
b.js(17,15): error TS2314: Generic type 'A<T>' requires 1 type argument(s).
b.js(18,25): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.


Expand All @@ -9,10 +11,12 @@ b.js(18,25): error TS8026: Expected A<T> type arguments; provide these with an '
==== a.ts (0 errors) ====
export class A<T> {}

==== b.js (2 errors) ====
==== b.js (4 errors) ====
import { A } from './a.js';

/** @extends {A} */
~
!!! error TS2314: Generic type 'A<T>' requires 1 type argument(s).
export class B1 extends A {
~
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
Expand All @@ -29,6 +33,8 @@ b.js(18,25): error TS8026: Expected A<T> type arguments; provide these with an '
}

/** @extends {A<string, string>} */
~~~~~~~~~~~~~~~~~
!!! error TS2314: Generic type 'A<T>' requires 1 type argument(s).
export class B3 extends A {
~
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
/b.js(1,17): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
/b.js(4,15): error TS2314: Generic type 'A<T>' requires 1 type argument(s).
/b.js(5,17): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
/b.js(8,15): error TS2314: Generic type 'A<T>' requires 1 type argument(s).
/b.js(9,17): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.


==== /a.d.ts (0 errors) ====
declare class A<T> { x: T; }

==== /b.js (3 errors) ====
==== /b.js (5 errors) ====
class B extends A {}
~
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
new B().x;

/** @augments A */
~
!!! error TS2314: Generic type 'A<T>' requires 1 type argument(s).
class C extends A { }
~
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
new C().x;

/** @augments A<number, number, number> */
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2314: Generic type 'A<T>' requires 1 type argument(s).
class D extends A {}
~
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@
+++ new.jsExtendsImplicitAny.errors.txt
@@= skipped -0, +0 lines =@@
/b.js(1,17): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
-/b.js(4,15): error TS2314: Generic type 'A<T>' requires 1 type argument(s).
-/b.js(8,15): error TS2314: Generic type 'A<T>' requires 1 type argument(s).
/b.js(4,15): error TS2314: Generic type 'A<T>' requires 1 type argument(s).
+/b.js(5,17): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
/b.js(8,15): error TS2314: Generic type 'A<T>' requires 1 type argument(s).
+/b.js(9,17): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.


==== /a.d.ts (0 errors) ====
@@= skipped -12, +12 lines =@@
new B().x;
declare class A<T> { x: T; }

/** @augments A */
- ~
-!!! error TS2314: Generic type 'A<T>' requires 1 type argument(s).
-==== /b.js (3 errors) ====
+==== /b.js (5 errors) ====
class B extends A {}
~
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
@@= skipped -15, +17 lines =@@
~
!!! error TS2314: Generic type 'A<T>' requires 1 type argument(s).
class C extends A { }
+ ~
+!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
new C().x;

/** @augments A<number, number, number> */
- ~~~~~~~~~~~~~~~~~~~~~~~~~
-!!! error TS2314: Generic type 'A<T>' requires 1 type argument(s).
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2314: Generic type 'A<T>' requires 1 type argument(s).
class D extends A {}
+ ~
+!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
main.js(2,15): error TS2304: Cannot find name 'Mismatch'.
main.js(2,15): error TS8023: JSDoc '@extends Mismatch' does not match the 'extends B' clause.


==== super.js (0 errors) ====
export class B { }

==== main.js (1 errors) ====
==== main.js (2 errors) ====
import { B } from './super'
/** @extends {Mismatch} */
~~~~~~~~
!!! error TS2304: Cannot find name 'Mismatch'.
~~~~~~~~
!!! error TS8023: JSDoc '@extends Mismatch' does not match the 'extends B' clause.
class C extends B { }

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- old.jsdocAugments_nameMismatch.errors.txt
+++ new.jsdocAugments_nameMismatch.errors.txt
@@= skipped -0, +0 lines =@@
-/b.js(4,15): error TS8023: JSDoc '@augments A' does not match the 'extends B' clause.
-
-
-==== /b.js (1 errors) ====
- class A {}
- class B {}
-
- /** @augments A */
- ~
-!!! error TS8023: JSDoc '@augments A' does not match the 'extends B' clause.
- class C extends B {}
-
+<no content>
17 changes: 17 additions & 0 deletions testdata/tests/cases/compiler/jsdocAugmentsAliasExtends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @noEmit: true

declare class Base {
}
declare class Other {
}
declare const StateDependencies_base: typeof Base;
/**
* @augments {Base}
*/
export declare class StateDependencies extends StateDependencies_base {
}
/**
* @augments {Base}
*/
export declare class TypeScriptStateDependencies extends Other {
}
4 changes: 2 additions & 2 deletions testdata/tests/cases/compiler/jsdocExtendsClauseMismatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

// @filename: react.d.ts
declare namespace React {
class Component {}
class PureComponent {}
class Component { component: string }
class PureComponent { pure: string }
}

// @filename: main.js
Expand Down