Skip to content

Commit 02672d2

Browse files
authored
Use jsx language variant for jsx file scanning in getChildren (#61928)
1 parent f3a6d31 commit 02672d2

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4288,6 +4288,7 @@ export interface SourceFileLike {
42884288
lineMap?: readonly number[];
42894289
/** @internal */
42904290
getPositionOfLineAndCharacter?(line: number, character: number, allowEdits?: true): number;
4291+
languageVariant?: LanguageVariant;
42914292
}
42924293

42934294
/** @internal */

src/services/completions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ function getJsxClosingTagCompletion(location: Node | undefined, sourceFile: Sour
15951595
switch (node.kind) {
15961596
case SyntaxKind.JsxClosingElement:
15971597
return true;
1598-
case SyntaxKind.SlashToken:
1598+
case SyntaxKind.LessThanSlashToken:
15991599
case SyntaxKind.GreaterThanToken:
16001600
case SyntaxKind.Identifier:
16011601
case SyntaxKind.PropertyAccessExpression:
@@ -3508,7 +3508,7 @@ function getCompletionData(
35083508
}
35093509
break;
35103510

3511-
case SyntaxKind.SlashToken:
3511+
case SyntaxKind.LessThanSlashToken:
35123512
if (currentToken.parent.kind === SyntaxKind.JsxSelfClosingElement) {
35133513
location = currentToken;
35143514
}
@@ -3518,7 +3518,7 @@ function getCompletionData(
35183518

35193519
switch (parent.kind) {
35203520
case SyntaxKind.JsxClosingElement:
3521-
if (contextToken.kind === SyntaxKind.SlashToken) {
3521+
if (contextToken.kind === SyntaxKind.LessThanSlashToken) {
35223522
isStartingCloseTag = true;
35233523
location = contextToken;
35243524
}
@@ -5805,7 +5805,7 @@ function isValidTrigger(sourceFile: SourceFile, triggerCharacter: CompletionsTri
58055805
case "/":
58065806
return !!contextToken && (isStringLiteralLike(contextToken)
58075807
? !!tryGetImportFromModuleSpecifier(contextToken)
5808-
: contextToken.kind === SyntaxKind.SlashToken && isJsxClosingElement(contextToken.parent));
5808+
: contextToken.kind === SyntaxKind.LessThanSlashToken && isJsxClosingElement(contextToken.parent));
58095809
case " ":
58105810
return !!contextToken && isImportKeyword(contextToken) && contextToken.parent.kind === SyntaxKind.SourceFile;
58115811
default:

src/services/services.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,9 @@ function createChildren(node: Node, sourceFile: SourceFileLike | undefined): rea
504504
});
505505
return children;
506506
}
507-
507+
const languageVariant = sourceFile?.languageVariant ?? LanguageVariant.Standard;
508508
scanner.setText((sourceFile || node.getSourceFile()).text);
509+
scanner.setLanguageVariant(languageVariant);
509510
let pos = node.pos;
510511
const processNode = (child: Node) => {
511512
addSyntheticNodes(children, pos, child.pos, node);
@@ -526,6 +527,7 @@ function createChildren(node: Node, sourceFile: SourceFileLike | undefined): rea
526527
node.forEachChild(processNode, processNodes);
527528
addSyntheticNodes(children, pos, node.end, node);
528529
scanner.setText(undefined);
530+
scanner.setLanguageVariant(LanguageVariant.Standard);
529531
return children;
530532
}
531533

src/services/utilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ export function isInsideJsxElementOrAttribute(sourceFile: SourceFile, position:
18891889
}
18901890

18911891
// <div>|</div>
1892-
if (token.kind === SyntaxKind.LessThanToken && token.parent.kind === SyntaxKind.JsxClosingElement) {
1892+
if (token.kind === SyntaxKind.LessThanSlashToken && token.parent.kind === SyntaxKind.JsxClosingElement) {
18931893
return true;
18941894
}
18951895

@@ -1934,6 +1934,7 @@ export function isInsideJsxElement(sourceFile: SourceFile, position: number): bo
19341934
|| node.kind === SyntaxKind.CloseBraceToken
19351935
|| node.kind === SyntaxKind.OpenBraceToken
19361936
|| node.kind === SyntaxKind.SlashToken
1937+
|| node.kind === SyntaxKind.LessThanSlashToken
19371938
) {
19381939
node = node.parent;
19391940
}

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5907,6 +5907,7 @@ declare namespace ts {
59075907
*/
59085908
interface SourceFileLike {
59095909
readonly text: string;
5910+
languageVariant?: LanguageVariant;
59105911
}
59115912
interface SourceFileLike {
59125913
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;

tests/cases/fourslash/syntacticClassificationsJsx1.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ verify.syntacticClassificationsAre(
1818
c.jsxText(`
1919
some jsx text
2020
`),
21-
c.punctuation("<"), c.punctuation("/"), c.jsxCloseTagName("div"), c.punctuation(">"), c.punctuation(";"),
21+
c.punctuation("</"), c.jsxCloseTagName("div"), c.punctuation(">"), c.punctuation(";"),
2222
c.keyword("let"), c.identifier("y"), c.operator("="),
2323
c.punctuation("<"),
2424
c.jsxSelfClosingTagName("element"),
2525
c.jsxAttribute("attr"), c.operator("="), c.jsxAttributeStringLiteralValue(`"123"`),
2626
c.punctuation("/"), c.punctuation(">")
2727
)
2828

29-
const c2 = classification("2020");
30-
verify.semanticClassificationsAre("2020",
31-
c2.semanticToken("variable.declaration", "x"),
32-
c2.semanticToken("variable.declaration", "y"),
29+
const c2 = classification("2020");
30+
verify.semanticClassificationsAre("2020",
31+
c2.semanticToken("variable.declaration", "x"),
32+
c2.semanticToken("variable.declaration", "y"),
3333
);

tests/cases/fourslash/syntacticClassificationsJsx2.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ verify.syntacticClassificationsAre(
1818
c.jsxText(`
1919
some jsx text
2020
`),
21-
c.punctuation("<"), c.punctuation("/"), c.jsxCloseTagName("div.name"), c.punctuation(">"), c.punctuation(";"),
21+
c.punctuation("</"), c.jsxCloseTagName("div.name"), c.punctuation(">"), c.punctuation(";"),
2222
c.keyword("let"), c.identifier("y"), c.operator("="),
2323
c.punctuation("<"),
2424
c.jsxSelfClosingTagName("element.name"),
2525
c.jsxAttribute("attr"), c.operator("="), c.jsxAttributeStringLiteralValue(`"123"`),
2626
c.punctuation("/"), c.punctuation(">")
2727
)
2828

29-
const c2 = classification("2020");
30-
verify.semanticClassificationsAre("2020",
31-
c2.semanticToken("variable.declaration", "x"),
32-
c2.semanticToken("variable.declaration", "y"),
29+
const c2 = classification("2020");
30+
verify.semanticClassificationsAre("2020",
31+
c2.semanticToken("variable.declaration", "x"),
32+
c2.semanticToken("variable.declaration", "y"),
3333
);

0 commit comments

Comments
 (0)