Skip to content

Commit b686dd4

Browse files
committed
Merge branch 'main' of https://github.com/microsoft/typescript-go into feat/completion-snippets
2 parents 24caea3 + f20e919 commit b686dd4

13 files changed

Lines changed: 99 additions & 52 deletions

internal/ls/hover.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, sy
190190
}
191191
comments := tag.Comments()
192192
if tag.Kind == ast.KindJSDocUnknownTag && tag.TagName().Text() == "example" {
193-
commentText := strings.TrimRight(getCommentText(comments), " \t\r\n")
193+
commentText := scanner.GetTextOfJSDocComment(tag.CommentList())
194194
if strings.HasPrefix(commentText, "<caption>") {
195195
if captionEnd := strings.Index(commentText, "</caption>"); captionEnd > 0 {
196196
b.WriteString(" — ")
@@ -242,19 +242,6 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, sy
242242
return b.String()
243243
}
244244

245-
func getCommentText(comments []*ast.Node) string {
246-
var b strings.Builder
247-
for _, comment := range comments {
248-
switch comment.Kind {
249-
case ast.KindJSDocText:
250-
b.WriteString(comment.Text())
251-
case ast.KindJSDocLink, ast.KindJSDocLinkCode, ast.KindJSDocLinkPlain:
252-
b.WriteString(scanner.GetTextOfNode(comment))
253-
}
254-
}
255-
return b.String()
256-
}
257-
258245
func formatQuickInfo(quickInfo string) string {
259246
var b strings.Builder
260247
b.Grow(32)

internal/scanner/utilities.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scanner
22

33
import (
44
"strings"
5+
"unicode"
56
"unicode/utf8"
67

78
"github.com/microsoft/typescript-go/internal/ast"
@@ -81,6 +82,22 @@ func GetTextOfNode(node *ast.Node) string {
8182
return GetSourceTextOfNodeFromSourceFile(ast.GetSourceFileOfNode(node), node, false /*includeTrivia*/)
8283
}
8384

85+
func GetTextOfJSDocComment(comment *ast.NodeList) string {
86+
if comment == nil {
87+
return ""
88+
}
89+
var b strings.Builder
90+
for _, n := range comment.Nodes {
91+
switch n.Kind {
92+
case ast.KindJSDocText:
93+
b.WriteString(n.Text())
94+
case ast.KindJSDocLink, ast.KindJSDocLinkCode, ast.KindJSDocLinkPlain:
95+
b.WriteString(GetTextOfNode(n))
96+
}
97+
}
98+
return strings.TrimRightFunc(b.String(), unicode.IsSpace)
99+
}
100+
84101
func DeclarationNameToString(name *ast.Node) string {
85102
if name == nil || name.Pos() == name.End() {
86103
return "(Missing)"

internal/transformers/declarations/transform.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,14 +864,16 @@ func (tx *DeclarationTransformer) transformPropertySignatureDeclaration(input *a
864864
if ast.IsPrivateIdentifier(input.Name()) {
865865
return nil
866866
}
867-
return tx.Factory().UpdatePropertySignatureDeclaration(
867+
result := tx.Factory().UpdatePropertySignatureDeclaration(
868868
input,
869869
tx.ensureModifiers(input.AsNode()),
870870
input.Name(),
871871
input.PostfixToken,
872872
tx.ensureType(input.AsNode(), false),
873873
tx.ensureNoInitializer(input.AsNode()), // TODO: possible strada bug (fixed here) - const property signatures never initialized
874874
)
875+
tx.preservePartialJsDoc(result, input.AsNode())
876+
return result
875877
}
876878

877879
func (tx *DeclarationTransformer) transformPropertyDeclaration(input *ast.PropertyDeclaration) *ast.Node {
@@ -1226,6 +1228,22 @@ func (tx *DeclarationTransformer) preserveJsDoc(updated *ast.Node, original *ast
12261228
tx.EmitContext().AssignCommentRange(updated, original)
12271229
}
12281230

1231+
func (tx *DeclarationTransformer) preservePartialJsDoc(updated *ast.Node, original *ast.Node) {
1232+
if original.Flags&ast.NodeFlagsReparsed == 0 {
1233+
return
1234+
}
1235+
jsdoc := core.FirstOrNil(original.EagerJSDoc(ast.GetSourceFileOfNode(original)))
1236+
if jsdoc == nil {
1237+
return
1238+
}
1239+
description := scanner.GetTextOfJSDocComment(jsdoc.AsJSDoc().Comment)
1240+
if description == "" {
1241+
return
1242+
}
1243+
comment := "*\n * " + strings.ReplaceAll(description, "\n", "\n * ") + "\n "
1244+
tx.EmitContext().AddSyntheticLeadingComment(updated, ast.KindMultiLineCommentTrivia, comment, true /*hasTrailingNewLine*/)
1245+
}
1246+
12291247
func (tx *DeclarationTransformer) removeAllComments(node *ast.Node) {
12301248
tx.EmitContext().AddEmitFlags(node, printer.EFNoComments)
12311249
// !!! TODO: Also remove synthetic trailing/leading comments added by transforms

testdata/baselines/reference/submodule/conformance/jsDeclarationsClassStatic(target=es2015).js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ declare const Strings: {
6363
export = Handler;
6464
export { Strings };
6565
export type HandlerOptions = {
66+
/**
67+
* Should be able to export a type alias at the same time.
68+
*/
6669
name: string;
6770
};
6871
/**

testdata/baselines/reference/submodule/conformance/jsDeclarationsImportAliasExposedWithinNamespace.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ export declare namespace myTypes {
6565
}
6666
export declare namespace myTypes {
6767
export type typeB = {
68+
/**
69+
* - Prop 1.
70+
*/
6871
prop1: myTypes.typeA;
72+
/**
73+
* - Prop 2.
74+
*/
6975
prop2: string;
7076
};
7177
}

testdata/baselines/reference/submodule/conformance/jsDeclarationsImportAliasExposedWithinNamespaceCjs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ export declare namespace myTypes {
6565
}
6666
export declare namespace myTypes {
6767
export type typeB = {
68+
/**
69+
* - Prop 1.
70+
*/
6871
prop1: myTypes.typeA;
72+
/**
73+
* - Prop 2.
74+
*/
6975
prop2: string;
7076
};
7177
}

testdata/baselines/reference/submodule/conformance/jsDeclarationsTypedefDescriptionsPreserved.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,23 @@
4444
* @property {string} baz - Sylvester McMonkey McBean
4545
*/
4646
type FooOptions = {
47+
/**
48+
* - Marvin K Mooney
49+
*/
4750
bar: boolean;
51+
/**
52+
* - Sylvester McMonkey McBean
53+
*/
4854
baz: string;
4955
};
5056
type BarOptions = {
57+
/**
58+
* - Marvin K Mooney
59+
*/
5160
bar: boolean;
61+
/**
62+
* - Sylvester McMonkey McBean
63+
*/
5264
baz: string;
5365
};
5466
/**

testdata/baselines/reference/submodule/conformance/linkTagEmit1.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ var see3 = true;
5656
*/
5757
type N = number;
5858
type D1 = {
59+
/**
60+
* Just link to {@link NS.R} this time
61+
*/
5962
e: 1;
63+
/**
64+
* Wyatt Earp loved {@link N integers} I bet.
65+
*/
6066
m: 1;
6167
};
6268
type Z = number;

testdata/baselines/reference/submoduleAccepted/conformance/jsDeclarationsClassStatic(target=es2015).js.diff

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
- let b: string;
1919
-}
2020
-type HandlerOptions = {
21-
- /**
22-
- * Should be able to export a type alias at the same time.
23-
- */
2421
+ var statische: () => void;
2522
+}
2623
+declare const Strings: {
@@ -30,6 +27,9 @@
3027
+export = Handler;
3128
+export { Strings };
3229
+export type HandlerOptions = {
30+
/**
31+
* Should be able to export a type alias at the same time.
32+
*/
3333
name: string;
3434
};
3535
+/**

testdata/baselines/reference/submoduleAccepted/conformance/jsDeclarationsImportAliasExposedWithinNamespace.js.diff

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
-export namespace myTypes {
88
- type typeA = string | RegExp | Array<string | RegExp>;
99
- type typeB = {
10-
- /**
11-
- * - Prop 1.
12-
- */
1310
+/**
1411
+ * @namespace myTypes
1512
+ * @global
@@ -21,10 +18,11 @@
2118
+}
2219
+export declare namespace myTypes {
2320
+ export type typeB = {
24-
prop1: myTypes.typeA;
25-
- /**
26-
- * - Prop 2.
27-
- */
21+
/**
22+
* - Prop 1.
23+
*/
24+
@@= skipped -12, +20 lines =@@
25+
*/
2826
prop2: string;
2927
};
3028
- type typeC = myTypes.typeB | Function;
@@ -62,7 +60,7 @@
6260
}
6361
/** @typedef {boolean|myTypes.typeC} testFnTypes.input */
6462
/**
65-
@@= skipped -33, +44 lines =@@
63+
@@= skipped -21, +30 lines =@@
6664
* @param {testFnTypes.input} input - Input.
6765
* @returns {number|null} Result.
6866
*/

0 commit comments

Comments
 (0)