Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions tsc/internal/ast/nodeflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tsc/internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 12 additions & 3 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Comment on lines +32841 to +32842
}
}
if links.resolvedSymbol == nil && isJSDoc && ast.IsQualifiedName(name) {
return c.resolveJSDocMemberName(name)
Expand Down
282 changes: 282 additions & 0 deletions tsc/internal/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
})
}
}
4 changes: 4 additions & 0 deletions tsc/internal/parser/jsdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 25 additions & 0 deletions tsc/testdata/baselines/reference/conformance/jsdocLinkTag10.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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))
}

Loading