From 0e96c5eaf134c5317febf417044a638b4ce6d70e Mon Sep 17 00:00:00 2001 From: zhengbli Date: Wed, 1 Jun 2016 22:57:25 -0700 Subject: [PATCH 1/3] Run fixupParentReferences when parsing isolated jsDocComment --- src/compiler/parser.ts | 21 +++++++++++++++++- ...tacticClassificationForJSDocTemplateTag.ts | 22 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/syntacticClassificationForJSDocTemplateTag.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 6f042627c6ba4..13f16f3848acd 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -440,7 +440,26 @@ namespace ts { /* @internal */ export function parseIsolatedJSDocComment(content: string, start?: number, length?: number) { - return Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); + const result = Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); + if (result.jsDocComment) { + // because the jsDocComment was parsed out of the source file, it might + // not be covered by the fixupParentReferences. + let parentNode: Node = result.jsDocComment; + forEachChild(result.jsDocComment, visitNode); + + function visitNode(n: Node): void { + if (n.parent !== parentNode) { + n.parent = parentNode; + + const saveParent = parentNode; + parentNode = n; + forEachChild(n, visitNode); + parentNode = saveParent; + } + } + } + + return result; } /* @internal */ diff --git a/tests/cases/fourslash/syntacticClassificationForJSDocTemplateTag.ts b/tests/cases/fourslash/syntacticClassificationForJSDocTemplateTag.ts new file mode 100644 index 0000000000000..c3368207d2cc2 --- /dev/null +++ b/tests/cases/fourslash/syntacticClassificationForJSDocTemplateTag.ts @@ -0,0 +1,22 @@ +/// + +/////** @template T */ +////function ident: T { +////} + +var c = classification; +verify.syntacticClassificationsAre( + c.comment("/** "), + c.punctuation("@"), + c.docCommentTagName("template"), + c.typeParameterName("T"), + c.comment(" */"), + c.keyword("function"), + c.identifier("ident"), + c.punctuation("<"), + c.typeParameterName("T"), + c.punctuation(">"), + c.punctuation(":"), + c.identifier("T"), + c.punctuation("{"), + c.punctuation("}")); From d41ac8aa9a85203f413a52842975af4a4d4e09ec Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 2 Jun 2016 11:12:38 -0700 Subject: [PATCH 2/3] Add null check and CR feedback --- src/compiler/parser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 13f16f3848acd..e377f988d5533 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -441,14 +441,14 @@ namespace ts { /* @internal */ export function parseIsolatedJSDocComment(content: string, start?: number, length?: number) { const result = Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); - if (result.jsDocComment) { + if (result && result.jsDocComment) { // because the jsDocComment was parsed out of the source file, it might // not be covered by the fixupParentReferences. let parentNode: Node = result.jsDocComment; forEachChild(result.jsDocComment, visitNode); function visitNode(n: Node): void { - if (n.parent !== parentNode) { + if (n.parent === undefined) { n.parent = parentNode; const saveParent = parentNode; From e2a1a78dd30ea9f025b3990b73d68ecbb3eec160 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 2 Jun 2016 13:26:15 -0700 Subject: [PATCH 3/3] reuse the fixupParentReferences function --- src/compiler/parser.ts | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index e377f988d5533..b24dd85f2f0dd 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -444,19 +444,7 @@ namespace ts { if (result && result.jsDocComment) { // because the jsDocComment was parsed out of the source file, it might // not be covered by the fixupParentReferences. - let parentNode: Node = result.jsDocComment; - forEachChild(result.jsDocComment, visitNode); - - function visitNode(n: Node): void { - if (n.parent === undefined) { - n.parent = parentNode; - - const saveParent = parentNode; - parentNode = n; - forEachChild(n, visitNode); - parentNode = saveParent; - } - } + Parser.fixupParentReferences(result.jsDocComment); } return result; @@ -671,14 +659,14 @@ namespace ts { return node; } - export function fixupParentReferences(sourceFile: Node) { + export function fixupParentReferences(rootNode: Node) { // normally parent references are set during binding. However, for clients that only need // a syntax tree, and no semantic features, then the binding process is an unnecessary // overhead. This functions allows us to set all the parents, without all the expense of // binding. - let parent: Node = sourceFile; - forEachChild(sourceFile, visitNode); + let parent: Node = rootNode; + forEachChild(rootNode, visitNode); return; function visitNode(n: Node): void {