Merge pull request #32565 from amcasey/TripleSlashClassification

Support classification of triple-slash references
This commit is contained in:
Andrew Casey 2019-07-29 12:27:55 -07:00 committed by GitHub
commit dbe9e3d237
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 302 additions and 0 deletions

View file

@ -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);
@ -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);

View file

@ -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("/>"));

View file

@ -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\""));

View file

@ -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("/"));

View file

@ -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("/>"));

View file

@ -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"));

View file

@ -0,0 +1,7 @@
/// <reference path="fourslash.ts"/>
//// /// nonElement
var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// nonElement"));

View file

@ -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("/>"));

View file

@ -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"));

View file

@ -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("/>"));

View file

@ -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("/>"));

View file

@ -0,0 +1,8 @@
/// <reference path="fourslash.ts"/>
//// /// <
var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"));

View file

@ -0,0 +1,9 @@
/// <reference path="fourslash.ts"/>
//// /// <reference
var c = classification;
verify.syntacticClassificationsAre(
c.comment("/// "),
c.punctuation("<"),
c.jsxSelfClosingTagName("reference"));

View file

@ -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"));

View file

@ -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="));

View file

@ -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=\""));

View file

@ -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"));