diff --git a/tsc/internal/ast/nodeflags.go b/tsc/internal/ast/nodeflags.go index 2c808601a2260..2ff96a4277049 100644 --- a/tsc/internal/ast/nodeflags.go +++ b/tsc/internal/ast/nodeflags.go @@ -45,6 +45,7 @@ const ( NodeFlagsPossiblyContainsDeprecatedTag NodeFlags = 1 << 26 // Set during parse if comment text contains '@deprecated'; must confirm via JSDoc lookup NodeFlagsUnreachable NodeFlags = 1 << 27 // If node is unreachable according to the binder NodeFlagsReparserTransformedLiteral NodeFlags = 1 << 28 // If node was transformed during parsing, making its' naive text source not match the AST + NodeFlagsJSDocMemberName NodeFlags = 1 << 29 // If a QualifiedName was parsed from a JSDoc link name using the '#' separator (Foo#member), marking it as an instance member reference rather than a static member reference NodeFlagsBlockScoped = NodeFlagsLet | NodeFlagsConst | NodeFlagsUsing NodeFlagsConstant = NodeFlagsConst | NodeFlagsUsing diff --git a/tsc/internal/ast/utilities.go b/tsc/internal/ast/utilities.go index c63ce7238c840..32f9c40adb660 100644 --- a/tsc/internal/ast/utilities.go +++ b/tsc/internal/ast/utilities.go @@ -325,6 +325,13 @@ func IsEntityName(node *Node) bool { return node.Kind == KindIdentifier || node.Kind == KindQualifiedName } +// IsJSDocMemberName reports whether a QualifiedName was parsed from a JSDoc link +// name using the '#' separator (Foo#member). Such a name refers to an instance +// member, unlike a '.' name, which resolves to a static member first. +func IsJSDocMemberName(node *Node) bool { + return node.Kind == KindQualifiedName && node.Flags&NodeFlagsJSDocMemberName != 0 +} + func IsPropertyName(node *Node) bool { switch node.Kind { case KindIdentifier, diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 694fc155b8e16..10b593fcc6671 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -2637,8 +2637,12 @@ func (c *Checker) checkJSDocComment(node *ast.Node) { func (c *Checker) resolveJSDocMemberName(name *ast.Node) *ast.Symbol { if name != nil && ast.IsEntityName(name) { meaning := ast.SymbolFlagsType | ast.SymbolFlagsNamespace | ast.SymbolFlagsValue - if symbol := c.resolveEntityName(name, meaning, true /*ignoreErrors*/, true /*dontResolveAlias*/, ast.GetHostSignatureFromJSDoc(name)); symbol != nil { - return symbol + // A '#' link (Foo#member) refers to an instance member, so it must not + // resolve through the static members the way a '.' link does. + if !ast.IsJSDocMemberName(name) { + if symbol := c.resolveEntityName(name, meaning, true /*ignoreErrors*/, true /*dontResolveAlias*/, ast.GetHostSignatureFromJSDoc(name)); symbol != nil { + return symbol + } } if ast.IsQualifiedName(name) { if symbol := c.resolveJSDocMemberName(name.AsQualifiedName().Left); symbol != nil { @@ -32831,7 +32835,12 @@ func (c *Checker) getSymbolOfNameOrPropertyAccessExpression(name *ast.Node) *ast ) } } else { - c.checkQualifiedName(name, CheckModeNormal) + // A '#' link (Foo#member) refers to an instance member. checkQualifiedName + // would resolve it to the static member, so resolve it as a JSDoc member + // name instead. + if !(isJSDoc && ast.IsJSDocMemberName(name)) { + c.checkQualifiedName(name, CheckModeNormal) + } } if links.resolvedSymbol == nil && isJSDoc && ast.IsQualifiedName(name) { return c.resolveJSDocMemberName(name) diff --git a/tsc/internal/checker/checker_test.go b/tsc/internal/checker/checker_test.go index f5b766607c5f7..bcd69df4574f9 100644 --- a/tsc/internal/checker/checker_test.go +++ b/tsc/internal/checker/checker_test.go @@ -214,3 +214,285 @@ export type Initial = Box<"first">; assert.Equal(t, c.TypeToString(initial), "Initial") assert.Equal(t, len(c.GetDiagnostics(t.Context(), file)), 0) } + +func TestGetSymbolAtLocationJSDocHashLink(t *testing.T) { + t.Parallel() + + content := `/** + * {@link Foo.member | static} + * {@link Foo#member | instance} + */ +class Foo { + static member = 123; + member = 456; +} +` + fs := vfstest.FromMap(map[string]string{ + "/foo.ts": content, + "/tsconfig.json": ` + { + "compilerOptions": {}, + "files": ["foo.ts"] + } + `, + }, tspath.CaseInsensitive /*caseSensitivity*/) + fs = bundled.WrapFS(fs) + + host := compiler.NewCompilerHost(fs, bundled.LibPath(), nil, nil, nil) + parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, nil, tsoptions.NewDefaultParseConfigHost(fs), nil) + assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line") + + p, err := compiler.NewProgram(t.Context(), compiler.ProgramOptions{ + Config: parsed, + Host: host, + }) + assert.NilError(t, err) + p.BindSourceFiles() + c, done := p.GetTypeChecker(t.Context()) + defer done() + file := p.GetSourceFile("/foo.ts") + classDecl := file.Statements.Nodes[0] + jsdocs := classDecl.JSDoc(file) + assert.Equal(t, len(jsdocs), 1, "Expected one JSDoc comment on the class") + var links []*ast.Node + for _, commentPart := range jsdocs[0].AsJSDoc().Comment.Nodes { + if commentPart.Kind == ast.KindJSDocLink { + links = append(links, commentPart) + } + } + assert.Equal(t, len(links), 2, "Expected two JSDoc links") + for i, link := range links { + name := link.AsJSDocLink().Name() + assert.Assert(t, ast.IsQualifiedName(name), "Expected link name to be a qualified name") + right := name.AsQualifiedName().Right + symbol := c.GetSymbolAtLocation(right) + assert.Assert(t, symbol != nil, "Expected link %d to resolve to a symbol", i) + assert.Equal(t, len(symbol.Declarations()), 1, "Expected link %d to resolve to a single declaration", i) + isStatic := ast.HasSyntacticModifier(symbol.Declarations()[0], ast.ModifierFlagsStatic) + if i == 0 { + assert.Assert(t, isStatic, "Expected {@link Foo.member} to resolve to the static member") + } else { + assert.Assert(t, !isStatic, "Expected {@link Foo#member} to resolve to the instance member") + } + } +} + +func TestGetSymbolAtLocationJSDocHashLinkEdges(t *testing.T) { + t.Parallel() + + // Each case is a full source file with one JSDoc link. resolve describes + // which member the link must resolve to: "static", "instance", "none", + // or "self" for a plain {@link Foo}. + cases := []struct { + name string + content string + resolve string + }{ + { + name: "instance only, dot link", + content: `/** {@link Foo.member} */ +class Foo { + member = 456; +} +`, + resolve: "instance", + }, + { + name: "instance only, hash link", + content: `/** {@link Foo#member} */ +class Foo { + member = 456; +} +`, + resolve: "instance", + }, + { + name: "static only, dot link", + content: `/** {@link Foo.member} */ +class Foo { + static member = 123; +} +`, + resolve: "static", + }, + { + name: "static only, hash link resolves to nothing", + content: `/** {@link Foo#member} */ +class Foo { + static member = 123; +} +`, + resolve: "none", + }, + { + name: "interface, dot link", + content: `/** {@link I.m} */ +interface I { + m: number; +} +`, + resolve: "instance", + }, + { + name: "interface, hash link", + content: `/** {@link I#m} */ +interface I { + m: number; +} +`, + resolve: "instance", + }, + { + name: "method, hash link resolves to instance method", + content: `/** {@link Foo#m} */ +class Foo { + static m() {} + m() {} +} +`, + resolve: "instance", + }, + { + name: "method, dot link resolves to static method", + content: `/** {@link Foo.m} */ +class Foo { + static m() {} + m() {} +} +`, + resolve: "static", + }, + { + name: "nonexistent member, hash link resolves to nothing", + content: `/** {@link Foo#nope} */ +class Foo { + static member = 123; + member = 456; +} +`, + resolve: "none", + }, + { + name: "plain link resolves to the class itself", + content: `/** {@link Foo} */ +class Foo { + static member = 123; + member = 456; +} +`, + resolve: "self", + }, + { + name: "namespace, hash link resolves to nothing, matching TS 6", + content: `/** {@link N#m} */ +namespace N { + export const m = 1; +} +`, + resolve: "none", + }, + { + name: "namespace qualified class, hash link resolves to instance member", + content: `/** {@link Ns.Foo#member} */ +namespace Ns { + export class Foo { + static member = 123; + member = 456; + } +} +`, + resolve: "instance", + }, + { + name: "chained hash link resolves to nothing, matching TS 6", + content: `/** {@link A#b#c} */ +class B { + c = 1; +} +class A { + b = new B(); +} +`, + resolve: "none", + }, + { + name: "see tag, hash link resolves to instance member", + content: `/** @see Foo#member */ +class Foo { + static member = 123; + member = 456; +} +`, + resolve: "instance", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + fs := vfstest.FromMap(map[string]string{ + "/foo.ts": tc.content, + "/tsconfig.json": ` + { + "compilerOptions": {}, + "files": ["foo.ts"] + } + `, + }, tspath.CaseInsensitive /*caseSensitivity*/) + fs = bundled.WrapFS(fs) + host := compiler.NewCompilerHost(fs, bundled.LibPath(), nil, nil, nil) + parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, nil, tsoptions.NewDefaultParseConfigHost(fs), nil) + assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line") + p, err := compiler.NewProgram(t.Context(), compiler.ProgramOptions{Config: parsed, Host: host}) + assert.NilError(t, err) + p.BindSourceFiles() + c, done := p.GetTypeChecker(t.Context()) + defer done() + file := p.GetSourceFile("/foo.ts") + decl := file.Statements.Nodes[0] + jsdocs := decl.JSDoc(file) + assert.Equal(t, len(jsdocs), 1, "Expected one JSDoc comment") + jsdoc := jsdocs[0].AsJSDoc() + // Find the link or name reference inside the JSDoc comment or tags. + var name *ast.Node + for _, part := range jsdoc.Comment.Nodes { + if part.Kind == ast.KindJSDocLink { + name = part.AsJSDocLink().Name() + } + } + if name == nil { + for _, tag := range jsdoc.Tags.Nodes { + if tag.Kind == ast.KindJSDocSeeTag { + if nameExpr := tag.AsJSDocSeeTag().NameExpression; nameExpr != nil { + name = nameExpr.AsJSDocNameReference().Name() + } + } + } + } + assert.Assert(t, name != nil, "Expected a link name to resolve") + var target *ast.Node + if ast.IsQualifiedName(name) { + target = name.AsQualifiedName().Right + } else { + target = name + } + symbol := c.GetSymbolAtLocation(target) + switch tc.resolve { + case "none": + assert.Assert(t, symbol == nil, "Expected the link to resolve to nothing, got %v", symbol) + case "self": + assert.Assert(t, symbol != nil, "Expected the link to resolve to the class") + assert.Equal(t, symbol, c.GetSymbolAtLocation(decl.Name()), "Expected the link to resolve to the class itself") + default: + assert.Assert(t, symbol != nil, "Expected the link to resolve to a symbol") + assert.Equal(t, len(symbol.Declarations()), 1, "Expected a single declaration") + isStatic := ast.HasSyntacticModifier(symbol.Declarations()[0], ast.ModifierFlagsStatic) + if tc.resolve == "static" { + assert.Assert(t, isStatic, "Expected the link to resolve to the static member") + } else { + assert.Assert(t, !isStatic, "Expected the link to resolve to the instance member") + } + } + }) + } +} diff --git a/tsc/internal/parser/jsdoc.go b/tsc/internal/parser/jsdoc.go index 8d14827dd669d..27500184f0364 100644 --- a/tsc/internal/parser/jsdoc.go +++ b/tsc/internal/parser/jsdoc.go @@ -756,6 +756,10 @@ func (p *Parser) parseJSDocLinkName() *ast.Node { p.scanner.ReScanHashToken() p.nextTokenJSDoc() name = p.finishNode(p.factory.NewQualifiedName(name, p.parseIdentifier()), pos) + // A '#' separator marks an instance member reference (Foo#member), unlike '.' + // which resolves to a static member first. Record the distinction here, since + // the checker cannot tell the two apart from a QualifiedName alone. + name.Flags |= ast.NodeFlagsJSDocMemberName } return name } diff --git a/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.js b/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.js new file mode 100644 index 0000000000000..6551ec59c0409 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.js @@ -0,0 +1,25 @@ +//// [tests/cases/conformance/jsdoc/jsdocLinkTag10.ts] //// + +//// [a.ts] +/** + * {@link Foo.member | static} + * {@link Foo#member | instance} + */ +class Foo { + static member = 123; + member = 456; +} + + +//// [a.js] +"use strict"; +/** + * {@link Foo.member | static} + * {@link Foo#member | instance} + */ +class Foo { + constructor() { + this.member = 456; + } +} +Foo.member = 123; diff --git a/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.symbols b/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.symbols new file mode 100644 index 0000000000000..5c7f50db49a08 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.symbols @@ -0,0 +1,17 @@ +//// [tests/cases/conformance/jsdoc/jsdocLinkTag10.ts] //// + +=== /a.ts === +/** + * {@link Foo.member | static} + * {@link Foo#member | instance} + */ +class Foo { +>Foo : Symbol(Foo, Decl(a.ts, 0, 0)) + + static member = 123; +>member : Symbol(Foo.member, Decl(a.ts, 4, 11)) + + member = 456; +>member : Symbol(Foo.member, Decl(a.ts, 5, 24)) +} + diff --git a/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.types b/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.types new file mode 100644 index 0000000000000..77e150036e14f --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.types @@ -0,0 +1,19 @@ +//// [tests/cases/conformance/jsdoc/jsdocLinkTag10.ts] //// + +=== /a.ts === +/** + * {@link Foo.member | static} + * {@link Foo#member | instance} + */ +class Foo { +>Foo : Foo + + static member = 123; +>member : number +>123 : 123 + + member = 456; +>member : number +>456 : 456 +} + diff --git a/tsc/testdata/tests/cases/conformance/jsdoc/jsdocLinkTag10.ts b/tsc/testdata/tests/cases/conformance/jsdoc/jsdocLinkTag10.ts new file mode 100644 index 0000000000000..1afb51bc5a94e --- /dev/null +++ b/tsc/testdata/tests/cases/conformance/jsdoc/jsdocLinkTag10.ts @@ -0,0 +1,10 @@ +// @target: es2015 +// @filename: /a.ts +/** + * {@link Foo.member | static} + * {@link Foo#member | instance} + */ +class Foo { + static member = 123; + member = 456; +}