Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
40 changes: 40 additions & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,32 @@ func (n *Node) QuestionDotToken() *Node {
panic("Unhandled case in Node.QuestionDotToken: " + n.Kind.String())
}

func (n *Node) TypeExpression() *Node {
switch n.Kind {
case KindJSDocPropertyTag, KindJSDocParameterTag:
return n.AsJSDocParameterOrPropertyTag().TypeExpression
case KindJSDocReturnTag:
return n.AsJSDocReturnTag().TypeExpression
case KindJSDocTypeTag:
return n.AsJSDocTypeTag().TypeExpression
case KindJSDocTypedefTag:
return n.AsJSDocTypedefTag().TypeExpression
case KindJSDocSatisfiesTag:
return n.AsJSDocSatisfiesTag().TypeExpression
}
panic("Unhandled case in Node.TypeExpression: " + n.Kind.String())
}

func (n *Node) ClassName() *Node {
switch n.Kind {
case KindJSDocAugmentsTag:
return n.AsJSDocAugmentsTag().ClassName
case KindJSDocImplementsTag:
return n.AsJSDocImplementsTag().ClassName
}
panic("Unhandled case in Node.ClassName: " + n.Kind.String())
}

// Determines if `n` contains `descendant` by walking up the `Parent` pointers from `descendant`. This method panics if
// `descendant` or one of its ancestors is not parented except when that node is a `SourceFile`.
func (n *Node) Contains(descendant *Node) bool {
Expand Down Expand Up @@ -2049,6 +2075,8 @@ type (
NamedExportsNode = Node
UnionType = Node
LiteralType = Node
JSDocNode = Node
BindingPatternNode = Node
)

type (
Expand Down Expand Up @@ -9535,6 +9563,10 @@ func (node *JSDocTemplateTag) Clone(f NodeFactoryCoercible) *Node {
return cloneNode(f.AsNodeFactory().NewJSDocTemplateTag(node.TagName, node.Constraint, node.TypeParameters, node.Comment), node.AsNode(), f.AsNodeFactory().hooks)
}

func IsJSDocTemplateTag(n *Node) bool {
return n.Kind == KindJSDocTemplateTag
}

// JSDocParameterOrPropertyTag
type JSDocParameterOrPropertyTag struct {
JSDocTagBase
Expand Down Expand Up @@ -9600,6 +9632,10 @@ func (node *JSDocParameterOrPropertyTag) Clone(f NodeFactoryCoercible) *Node {

func (node *JSDocParameterOrPropertyTag) Name() *EntityName { return node.name }

func IsJSDocParameterTag(node *Node) bool {
return node.Kind == KindJSDocParameterTag
}

// JSDocReturnTag
type JSDocReturnTag struct {
JSDocTagBase
Expand Down Expand Up @@ -9893,6 +9929,10 @@ func (node *JSDocImplementsTag) Clone(f NodeFactoryCoercible) *Node {
return cloneNode(f.AsNodeFactory().NewJSDocImplementsTag(node.TagName, node.ClassName, node.Comment), node.AsNode(), f.AsNodeFactory().hooks)
}

func IsJSDocImplementsTag(node *Node) bool {
return node.Kind == KindJSDocImplementsTag
}

// JSDocAugmentsTag
type JSDocAugmentsTag struct {
JSDocTagBase
Expand Down
5 changes: 5 additions & 0 deletions internal/checker/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,8 @@ func (c *Checker) formatUnionTypes(types []*Type) []*Type {
}
return result
}

func (c *Checker) TypeToTypeNode(t *Type, enclosingDeclaration *ast.Node, flags nodebuilder.Flags) *ast.TypeNode {
nodeBuilder := c.getNodeBuilder()
return nodeBuilder.TypeToTypeNode(t, enclosingDeclaration, flags, nodebuilder.InternalFlagsNone, nil)
}
6 changes: 3 additions & 3 deletions internal/format/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func formatNodeLines(ctx context.Context, sourceFile *ast.SourceFile, node *ast.
return nil
}
tokenStart := scanner.GetTokenPosOfNode(node, sourceFile, false)
lineStart := getLineStartPositionForPosition(tokenStart, sourceFile)
lineStart := GetLineStartPositionForPosition(tokenStart, sourceFile)
span := core.NewTextRange(lineStart, node.End())
return FormatSpan(ctx, span, sourceFile, requestKind)
}
Expand All @@ -90,7 +90,7 @@ func FormatDocument(ctx context.Context, sourceFile *ast.SourceFile) []core.Text
}

func FormatSelection(ctx context.Context, sourceFile *ast.SourceFile, start int, end int) []core.TextChange {
return FormatSpan(ctx, core.NewTextRange(getLineStartPositionForPosition(start, sourceFile), end), sourceFile, FormatRequestKindFormatSelection)
return FormatSpan(ctx, core.NewTextRange(GetLineStartPositionForPosition(start, sourceFile), end), sourceFile, FormatRequestKindFormatSelection)
}

func FormatOnOpeningCurly(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
Expand All @@ -112,7 +112,7 @@ func FormatOnOpeningCurly(ctx context.Context, sourceFile *ast.SourceFile, posit
* ```
* and we wouldn't want to move the closing brace.
*/
textRange := core.NewTextRange(getLineStartPositionForPosition(scanner.GetTokenPosOfNode(outermostNode, sourceFile, false), sourceFile), position)
textRange := core.NewTextRange(GetLineStartPositionForPosition(scanner.GetTokenPosOfNode(outermostNode, sourceFile, false), sourceFile), position)
return FormatSpan(ctx, textRange, sourceFile, FormatRequestKindFormatOnOpeningCurlyBrace)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/format/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func (w *formatSpanWorker) processChildNodes(
// }: {};
indentationOnListStartToken = w.indentationOnLastIndentedLine
} else {
startLinePosition := getLineStartPositionForPosition(tokenInfo.token.Loc.Pos(), w.sourceFile)
startLinePosition := GetLineStartPositionForPosition(tokenInfo.token.Loc.Pos(), w.sourceFile)
indentationOnListStartToken = findFirstNonWhitespaceColumn(startLinePosition, tokenInfo.token.Loc.Pos(), w.sourceFile, w.formattingContext.Options)
}

Expand Down Expand Up @@ -577,7 +577,7 @@ func (w *formatSpanWorker) tryComputeIndentationForListItem(startPos int, endPos
}
} else {
startLine, _ := scanner.GetLineAndCharacterOfPosition(w.sourceFile, startPos)
startLinePosition := getLineStartPositionForPosition(startPos, w.sourceFile)
startLinePosition := GetLineStartPositionForPosition(startPos, w.sourceFile)
column := findFirstNonWhitespaceColumn(startLinePosition, startPos, w.sourceFile, w.formattingContext.Options)
if startLine != parentStartLine || startPos == column {
// Use the base indent size if it is greater than
Expand Down
2 changes: 1 addition & 1 deletion internal/format/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func getCloseTokenForOpenToken(kind ast.Kind) ast.Kind {
return ast.KindUnknown
}

func getLineStartPositionForPosition(position int, sourceFile *ast.SourceFile) int {
func GetLineStartPositionForPosition(position int, sourceFile *ast.SourceFile) int {
lineStarts := scanner.GetLineStarts(sourceFile)
line, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, position)
return int(lineStarts[line])
Expand Down
17 changes: 0 additions & 17 deletions internal/fourslash/_scripts/failingTests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ TestCompletionImportModuleSpecifierEndingTsxReact
TestCompletionImportModuleSpecifierEndingUnsupportedExtension
TestCompletionInFunctionLikeBody_includesPrimitiveTypes
TestCompletionInJsDoc
TestCompletionInJsDocQualifiedNames
TestCompletionInNamedImportLocation
TestCompletionInUncheckedJSFile
TestCompletionJSDocNamePath
TestCompletionListBuilderLocations_VariableDeclarations
TestCompletionListForDerivedType1
TestCompletionListForTransitivelyExportedMembers04
Expand Down Expand Up @@ -131,9 +129,6 @@ TestCompletionsJSDocImportTagAttributesEmptyModuleSpecifier1
TestCompletionsJSDocImportTagAttributesErrorModuleSpecifier1
TestCompletionsJSDocImportTagEmptyModuleSpecifier1
TestCompletionsJSDocNoCrash1
TestCompletionsJSDocNoCrash2
TestCompletionsJSDocNoCrash3
TestCompletionsJsdocParamTypeBeforeName
TestCompletionsJsdocTag
TestCompletionsJsdocTypeTagCast
TestCompletionsJsxAttribute2
Expand Down Expand Up @@ -247,7 +242,6 @@ TestJsDocFunctionSignatures12
TestJsDocFunctionSignatures13
TestJsDocFunctionSignatures7
TestJsDocFunctionSignatures8
TestJsDocFunctionTypeCompletionsNoCrash
TestJsDocGenerics2
TestJsDocInheritDoc
TestJsDocPropertyDescription1
Expand All @@ -266,26 +260,15 @@ TestJsDocTagsWithHyphen
TestJsQuickInfoGenerallyAcceptableSize
TestJsRequireQuickInfo
TestJsdocCallbackTag
TestJsdocExtendsTagCompletion
TestJsdocImplementsTagCompletion
TestJsdocImportTagCompletion1
TestJsdocLink2
TestJsdocLink3
TestJsdocLink6
TestJsdocLink_findAllReferences1
TestJsdocOverloadTagCompletion
TestJsdocParameterNameCompletion
TestJsdocPropTagCompletion
TestJsdocPropertyTagCompletion
TestJsdocSatisfiesTagCompletion1
TestJsdocSatisfiesTagCompletion2
TestJsdocTemplatePrototypeCompletions
TestJsdocTemplateTagCompletion
TestJsdocThrowsTagCompletion
TestJsdocTypedefTag
TestJsdocTypedefTag2
TestJsdocTypedefTagNamespace
TestJsdocTypedefTagTypeExpressionCompletion
TestJsxFindAllReferencesOnRuntimeImportWithPaths1
TestLetQuickInfoAndCompletionList
TestLocalFunction
Expand Down
40 changes: 40 additions & 0 deletions internal/fourslash/tests/basicJSDocCompletions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
"github.com/microsoft/typescript-go/internal/testutil"
)

func TestBasicJSDocCompletions(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `
/**
* @/*1*/
*/
function foo(x) {
return x + 1;
}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyCompletions(t, "1", &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &DefaultCommitCharacters,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "link",
Kind: PtrTo(lsproto.CompletionItemKindKeyword),
Detail: PtrTo("link"),
},
"param",
"returns",
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestCompletionInJsDocQualifiedNames(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @allowJs: true
// @Filename: /node_modules/foo/index.d.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestCompletionJSDocNamePath(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @noLib: true
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestCompletionsJSDocNoCrash2(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @strict: true
// @filename: index.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestCompletionsJSDocNoCrash3(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @strict: true
// @filename: index.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestCompletionsJsdocParamTypeBeforeName(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `/** @param /*name1*/ {/*type*/} /*name2*/ */
function toString(obj) {}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsDocFunctionTypeCompletionsNoCrash(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `/**
* @returns {function/**/(): string}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocExtendsTagCompletion(t *testing.T) {
t.Parallel()
t.Skip()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original test is weird -- we expect to have A as a completion for A extends /**/ ? I guess it's the only class around, but it still doesn't make sense.

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `/** @extends {/**/} */
class A {}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocImplementsTagCompletion(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `/** @implements {/**/} */
class A {}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocImportTagCompletion1(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @allowJS: true
// @checkJs: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocOverloadTagCompletion(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @allowJS: true
// @checkJs: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocParameterNameCompletion(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `/**
* @param /*0*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocPropTagCompletion(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `/**
* @typedef Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocPropertyTagCompletion(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `/**
* @typedef {Object} Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocSatisfiesTagCompletion1(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @noEmit: true
// @allowJS: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocSatisfiesTagCompletion2(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @noEmit: true
// @allowJS: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsdocTemplateTagCompletion(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `/**
* @template {/**/} T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestJsdocTypedefTagTypeExpressionCompletion(t *testing.T) {
t.Parallel()
t.Skip()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `interface I {
age: number;
Expand Down
Loading
Loading