Skip to content

Support classification of triple-slash references #32565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2019
Merged
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
83 changes: 83 additions & 0 deletions src/services/classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,11 @@ namespace ts {
return;
}
}
else if (kind === SyntaxKind.SingleLineCommentTrivia) {
if (tryClassifyTripleSlashComment(start, width)) {
return;
}
}

// Simple comment. Just add as is.
pushCommentRange(start, width);
Expand Down Expand Up @@ -755,6 +760,84 @@ namespace ts {
}
}

function tryClassifyTripleSlashComment(start: number, width: number): boolean {
const tripleSlashXMLCommentRegEx = /^(\/\/\/\s*)(<)(?:(\S+)((?:[^/]|\/[^>])*)(\/>)?)?/im;
const attributeRegex = /(\S+)(\s*)(=)(\s*)('[^']+'|"[^"]+")/img;

const text = sourceFile.text.substr(start, width);
const match = tripleSlashXMLCommentRegEx.exec(text);
if (!match) {
return false;
}

let pos = start;

pushCommentRange(pos, match[1].length); // ///
pos += match[1].length;

pushClassification(pos, match[2].length, ClassificationType.punctuation); // <
pos += match[2].length;

if (!match[3]) {
return true;
}

pushClassification(pos, match[3].length, ClassificationType.jsxSelfClosingTagName); // element name
pos += match[3].length;

const attrText = match[4];
let attrPos = pos;
while (true) {
const attrMatch = attributeRegex.exec(attrText);
if (!attrMatch) {
break;
}

const newAttrPos = pos + attrMatch.index;
if (newAttrPos > attrPos) {
pushCommentRange(attrPos, newAttrPos - attrPos);
attrPos = newAttrPos;
}

pushClassification(attrPos, attrMatch[1].length, ClassificationType.jsxAttribute); // attribute name
attrPos += attrMatch[1].length;

if (attrMatch[2].length) {
pushCommentRange(attrPos, attrMatch[2].length); // whitespace
attrPos += attrMatch[2].length;
}

pushClassification(attrPos, attrMatch[3].length, ClassificationType.operator); // =
attrPos += attrMatch[3].length;

if (attrMatch[4].length) {
pushCommentRange(attrPos, attrMatch[4].length); // whitespace
attrPos += attrMatch[4].length;
}

pushClassification(attrPos, attrMatch[5].length, ClassificationType.jsxAttributeStringLiteralValue); // attribute value
attrPos += attrMatch[5].length;
}

pos += match[4].length;

if (pos > attrPos) {
pushCommentRange(attrPos, pos - attrPos);
}

if (match[5]) {
pushClassification(pos, match[5].length, ClassificationType.punctuation); // />
pos += match[5].length;
}

const end = start + width;
if (pos < end) {
pushCommentRange(pos, end - pos);
}

return true;
}

function processJSDocTemplateTag(tag: JSDocTemplateTag) {
for (const child of tag.getChildren()) {
processElement(child);
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module.ts" />

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module.ts\""),
c.comment(" "),
c.punctuation("/>"));
13 changes: 13 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module.ts"

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module.ts\""));
15 changes: 15 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module.ts" /

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module.ts\""),
c.comment(" "),
c.comment("/"));
19 changes: 19 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash12.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module.ts" bad types="node" />

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module.ts\""),
c.comment(" bad "),
c.jsxAttribute("types"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"node\""),
c.comment(" "),
c.punctuation("/>"));
16 changes: 16 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module.ts" /> trailing

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module.ts\""),
c.comment(" "),
c.punctuation("/>"),
c.comment(" trailing"));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts"/>

//// /// nonElement

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// nonElement"));
25 changes: 25 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash15.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module1.ts" />
//// /// <reference path="./module2.ts" />

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module1.ts\""),
c.comment(" "),
c.punctuation("/>"),
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module2.ts\""),
c.comment(" "),
c.punctuation("/>"));
17 changes: 17 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash16.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module.ts" />
//// 1

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module.ts\""),
c.comment(" "),
c.punctuation("/>"),
c.numericLiteral("1"));
16 changes: 16 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path="fourslash.ts"/>

//// ///<reference path = "./module.ts"/>

var c = classification;
verify.syntacticClassificationsAre(
c.comment("///"),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.comment(" "),
c.operator("="),
c.comment(" "),
c.jsxAttributeStringLiteralValue("\"./module.ts\""),
c.punctuation("/>"));
19 changes: 19 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module.ts" types="node" />

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" "),
c.jsxAttribute("path"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"./module.ts\""),
c.comment(" "),
c.jsxAttribute("types"),
c.operator("="),
c.jsxAttributeStringLiteralValue("\"node\""),
c.comment(" "),
c.punctuation("/>"));
8 changes: 8 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts"/>

//// /// <

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"));
9 changes: 9 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts"/>

//// /// <reference

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"));
10 changes: 10 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" path"));
10 changes: 10 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path=

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" path="));
10 changes: 10 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" path=\""));
10 changes: 10 additions & 0 deletions tests/cases/fourslash/syntacticClassificationsTripleSlash9.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts"/>

//// /// <reference path="./module.ts

var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"),
c.comment(" path=\"./module.ts"));