Skip to content

Commit 5c0d1a8

Browse files
committed
added jsx classification support to fourslash and tests
1 parent 946cf63 commit 5c0d1a8

4 files changed

Lines changed: 91 additions & 3 deletions

File tree

src/harness/fourslash.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3284,6 +3284,30 @@ namespace FourSlashInterface {
32843284
export function typeAliasName(text: string, position?: number): { classificationType: string; text: string; textSpan?: FourSlash.TextSpan } {
32853285
return getClassification("typeAliasName", text, position);
32863286
}
3287+
3288+
export function jsxOpenTagName(text: string, position?: number): { classificationType: string; text: string; textSpan?: FourSlash.TextSpan } {
3289+
return getClassification("jsxOpenTagName", text, position);
3290+
}
3291+
3292+
export function jsxCloseTagName(text: string, position?: number): { classificationType: string; text: string; textSpan?: FourSlash.TextSpan } {
3293+
return getClassification("jsxCloseTagName", text, position);
3294+
}
3295+
3296+
export function jsxSelfClosingTagName(text: string, position?: number): { classificationType: string; text: string; textSpan?: FourSlash.TextSpan } {
3297+
return getClassification("jsxSelfClosingTagName", text, position);
3298+
}
3299+
3300+
export function jsxAttribute(text: string, position?: number): { classificationType: string; text: string; textSpan?: FourSlash.TextSpan } {
3301+
return getClassification("jsxAttribute", text, position);
3302+
}
3303+
3304+
export function jsxText(text: string, position?: number): { classificationType: string; text: string; textSpan?: FourSlash.TextSpan } {
3305+
return getClassification("jsxText", text, position);
3306+
}
3307+
3308+
export function jsxAttributeStringLiteralValue(text: string, position?: number): { classificationType: string; text: string; textSpan?: FourSlash.TextSpan } {
3309+
return getClassification("jsxAttributeStringLiteralValue", text, position);
3310+
}
32873311

32883312
function getClassification(type: string, text: string, position?: number) {
32893313
return {

src/services/services.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,9 @@ namespace ts {
16161616
public static jsxOpenTagName = "jsx open tag name";
16171617
public static jsxCloseTagName = "jsx close tag name";
16181618
public static jsxSelfClosingTagName = "jsx self closing tag name";
1619+
public static jsxAttribute = "jsx attribute";
1620+
public static jsxText = "jsx text";
1621+
public static jsxAttributeStringLiteralValue = "jsx attribute string literal value";
16191622
}
16201623

16211624
export const enum ClassificationType {
@@ -1642,7 +1645,7 @@ namespace ts {
16421645
jsxSelfClosingTagName = 21,
16431646
jsxAttribute = 22,
16441647
jsxText = 23,
1645-
jsxAttributeStringValue = 24,
1648+
jsxAttributeStringLiteralValue = 24,
16461649
}
16471650

16481651
/// Language Service
@@ -6577,6 +6580,9 @@ namespace ts {
65776580
case ClassificationType.jsxOpenTagName: return ClassificationTypeNames.jsxOpenTagName;
65786581
case ClassificationType.jsxCloseTagName: return ClassificationTypeNames.jsxCloseTagName;
65796582
case ClassificationType.jsxSelfClosingTagName: return ClassificationTypeNames.jsxSelfClosingTagName;
6583+
case ClassificationType.jsxAttribute: return ClassificationTypeNames.jsxAttribute;
6584+
case ClassificationType.jsxText: return ClassificationTypeNames.jsxText;
6585+
case ClassificationType.jsxAttributeStringLiteralValue: return ClassificationTypeNames.jsxAttributeStringLiteralValue;
65806586
}
65816587
}
65826588

@@ -6826,7 +6832,8 @@ namespace ts {
68266832
// the '=' in a variable declaration is special cased here.
68276833
if (token.parent.kind === SyntaxKind.VariableDeclaration ||
68286834
token.parent.kind === SyntaxKind.PropertyDeclaration ||
6829-
token.parent.kind === SyntaxKind.Parameter) {
6835+
token.parent.kind === SyntaxKind.Parameter ||
6836+
token.parent.kind === SyntaxKind.JsxAttribute) {
68306837
return ClassificationType.operator;
68316838
}
68326839
}
@@ -6845,7 +6852,7 @@ namespace ts {
68456852
return ClassificationType.numericLiteral;
68466853
}
68476854
else if (tokenKind === SyntaxKind.StringLiteral || tokenKind === SyntaxKind.StringLiteralType) {
6848-
return token.parent.kind === SyntaxKind.JsxAttribute ? ClassificationType.jsxAttributeStringValue : ClassificationType.stringLiteral;
6855+
return token.parent.kind === SyntaxKind.JsxAttribute ? ClassificationType.jsxAttributeStringLiteralValue : ClassificationType.stringLiteral;
68496856
}
68506857
else if (tokenKind === SyntaxKind.RegularExpressionLiteral) {
68516858
// TODO: we should get another classification type for these literals.

tests/cases/fourslash/fourslash.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,36 @@ declare namespace FourSlashInterface {
338338
text: string;
339339
textSpan?: TextSpan;
340340
};
341+
function jsxOpenTagName(text: string, position?: number): {
342+
classificationType: string;
343+
text: string;
344+
textSpan?: TextSpan;
345+
};
346+
function jsxCloseTagName(text: string, position?: number): {
347+
classificationType: string;
348+
text: string;
349+
textSpan?: TextSpan;
350+
};
351+
function jsxSelfClosingTagName(text: string, position?: number): {
352+
classificationType: string;
353+
text: string;
354+
textSpan?: TextSpan;
355+
};
356+
function jsxAttribute(text: string, position?: number): {
357+
classificationType: string;
358+
text: string;
359+
textSpan?: TextSpan;
360+
};
361+
function jsxText(text: string, position?: number): {
362+
classificationType: string;
363+
text: string;
364+
textSpan?: TextSpan;
365+
};
366+
function jsxAttributeStringLiteralValue(text: string, position?: number): {
367+
classificationType: string;
368+
text: string;
369+
textSpan?: TextSpan;
370+
};
341371
}
342372
}
343373
declare function verifyOperationIsCancelled(f: any): void;
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 a = "some-value" b = {1}>
5+
//// some jsx text
6+
////</div>;
7+
////
8+
////let y = <element 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"),
15+
c.jsxAttribute("a"), c.operator("="), c.jsxAttributeStringLiteralValue(`"some-value"`),
16+
c.jsxAttribute("b"), 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"), c.punctuation(">"), c.punctuation(";"),
22+
c.keyword("let"), c.identifier("y"), c.operator("="),
23+
c.punctuation("<"),
24+
c.jsxSelfClosingTagName("element"),
25+
c.jsxAttribute("attr"), c.operator("="), c.jsxAttributeStringLiteralValue(`"123"`),
26+
c.punctuation("/"), c.punctuation(">")
27+
)

0 commit comments

Comments
 (0)