Skip to content

Commit 1754934

Browse files
committed
Merge pull request #6812 from Microsoft/port-6798
Ports #6798 into release-1.8
2 parents 738dde1 + b14685d commit 1754934

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

src/services/services.ts

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6856,21 +6856,58 @@ namespace ts {
68566856
}
68576857
}
68586858

6859-
function classifyTokenOrJsxText(token: Node): void {
6860-
if (nodeIsMissing(token)) {
6861-
return;
6859+
/**
6860+
* Returns true if node should be treated as classified and no further processing is required.
6861+
* False will mean that node is not classified and traverse routine should recurse into node contents.
6862+
*/
6863+
function tryClassifyNode(node: Node): boolean {
6864+
if (nodeIsMissing(node)) {
6865+
return true;
68626866
}
68636867

6864-
const tokenStart = token.kind === SyntaxKind.JsxText ? token.pos : classifyLeadingTriviaAndGetTokenStart(token);
6868+
const classifiedElementName = tryClassifyJsxElementName(node);
6869+
if (!isToken(node) && node.kind !== SyntaxKind.JsxText && classifiedElementName === undefined) {
6870+
return false;
6871+
}
6872+
6873+
const tokenStart = node.kind === SyntaxKind.JsxText ? node.pos : classifyLeadingTriviaAndGetTokenStart(node);
68656874

6866-
const tokenWidth = token.end - tokenStart;
6875+
const tokenWidth = node.end - tokenStart;
68676876
Debug.assert(tokenWidth >= 0);
68686877
if (tokenWidth > 0) {
6869-
const type = classifyTokenType(token.kind, token);
6878+
const type = classifiedElementName || classifyTokenType(node.kind, node);
68706879
if (type) {
68716880
pushClassification(tokenStart, tokenWidth, type);
68726881
}
68736882
}
6883+
6884+
return true;
6885+
}
6886+
6887+
function tryClassifyJsxElementName(token: Node): ClassificationType {
6888+
switch (token.parent && token.parent.kind) {
6889+
case SyntaxKind.JsxOpeningElement:
6890+
if ((<JsxOpeningElement>token.parent).tagName === token) {
6891+
return ClassificationType.jsxOpenTagName;
6892+
}
6893+
break;
6894+
case SyntaxKind.JsxClosingElement:
6895+
if ((<JsxClosingElement>token.parent).tagName === token) {
6896+
return ClassificationType.jsxCloseTagName;
6897+
}
6898+
break;
6899+
case SyntaxKind.JsxSelfClosingElement:
6900+
if ((<JsxSelfClosingElement>token.parent).tagName === token) {
6901+
return ClassificationType.jsxSelfClosingTagName;
6902+
}
6903+
break;
6904+
case SyntaxKind.JsxAttribute:
6905+
if ((<JsxAttribute>token.parent).name === token) {
6906+
return ClassificationType.jsxAttribute;
6907+
}
6908+
break;
6909+
}
6910+
return undefined;
68746911
}
68756912

68766913
// for accurate classification, the actual token should be passed in. however, for
@@ -6963,28 +7000,6 @@ namespace ts {
69637000
return ClassificationType.parameterName;
69647001
}
69657002
return;
6966-
6967-
case SyntaxKind.JsxOpeningElement:
6968-
if ((<JsxOpeningElement>token.parent).tagName === token) {
6969-
return ClassificationType.jsxOpenTagName;
6970-
}
6971-
return;
6972-
6973-
case SyntaxKind.JsxClosingElement:
6974-
if ((<JsxClosingElement>token.parent).tagName === token) {
6975-
return ClassificationType.jsxCloseTagName;
6976-
}
6977-
return;
6978-
6979-
case SyntaxKind.JsxSelfClosingElement:
6980-
if ((<JsxSelfClosingElement>token.parent).tagName === token) {
6981-
return ClassificationType.jsxSelfClosingTagName;
6982-
}
6983-
return;
6984-
case SyntaxKind.JsxAttribute:
6985-
if ((<JsxAttribute>token.parent).name === token) {
6986-
return ClassificationType.jsxAttribute;
6987-
}
69887003
}
69897004
}
69907005
return ClassificationType.identifier;
@@ -7003,10 +7018,7 @@ namespace ts {
70037018
const children = element.getChildren(sourceFile);
70047019
for (let i = 0, n = children.length; i < n; i++) {
70057020
const child = children[i];
7006-
if (isToken(child) || child.kind === SyntaxKind.JsxText) {
7007-
classifyTokenOrJsxText(child);
7008-
}
7009-
else {
7021+
if (!tryClassifyNode(child)) {
70107022
// Recurse into our child nodes.
70117023
processElement(child);
70127024
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
// @Filename: file1.tsx
4+
////let x = <div.name b = "some-value" c = {1}>
5+
//// some jsx text
6+
////</div.name>;
7+
////
8+
////let y = <element.name attr="123"/>
9+
10+
const c = classification;
11+
verify.syntacticClassificationsAre(
12+
c.keyword("let"), c.identifier("x"), c.operator("="),
13+
c.punctuation("<"),
14+
c.jsxOpenTagName("div.name"),
15+
c.jsxAttribute("b"), c.operator("="), c.jsxAttributeStringLiteralValue(`"some-value"`),
16+
c.jsxAttribute("c"), c.operator("="), c.punctuation("{"), c.numericLiteral("1"), c.punctuation("}"),
17+
c.punctuation(">"),
18+
c.jsxText(`
19+
some jsx text
20+
`),
21+
c.punctuation("<"), c.punctuation("/"), c.jsxCloseTagName("div.name"), c.punctuation(">"), c.punctuation(";"),
22+
c.keyword("let"), c.identifier("y"), c.operator("="),
23+
c.punctuation("<"),
24+
c.jsxSelfClosingTagName("element.name"),
25+
c.jsxAttribute("attr"), c.operator("="), c.jsxAttributeStringLiteralValue(`"123"`),
26+
c.punctuation("/"), c.punctuation(">")
27+
)

0 commit comments

Comments
 (0)