Skip to content

Use jsx language variant for jsx file scanning in getChildren #61928

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 3 commits into from
Jun 25, 2025
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
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4288,6 +4288,7 @@ export interface SourceFileLike {
lineMap?: readonly number[];
/** @internal */
getPositionOfLineAndCharacter?(line: number, character: number, allowEdits?: true): number;
languageVariant?: LanguageVariant;
}

/** @internal */
Expand Down
8 changes: 4 additions & 4 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,7 @@ function getJsxClosingTagCompletion(location: Node | undefined, sourceFile: Sour
switch (node.kind) {
case SyntaxKind.JsxClosingElement:
return true;
case SyntaxKind.SlashToken:
case SyntaxKind.LessThanSlashToken:
case SyntaxKind.GreaterThanToken:
case SyntaxKind.Identifier:
case SyntaxKind.PropertyAccessExpression:
Expand Down Expand Up @@ -3508,7 +3508,7 @@ function getCompletionData(
}
break;

case SyntaxKind.SlashToken:
case SyntaxKind.LessThanSlashToken:
if (currentToken.parent.kind === SyntaxKind.JsxSelfClosingElement) {
location = currentToken;
}
Expand All @@ -3518,7 +3518,7 @@ function getCompletionData(

switch (parent.kind) {
case SyntaxKind.JsxClosingElement:
if (contextToken.kind === SyntaxKind.SlashToken) {
if (contextToken.kind === SyntaxKind.LessThanSlashToken) {
isStartingCloseTag = true;
location = contextToken;
}
Expand Down Expand Up @@ -5805,7 +5805,7 @@ function isValidTrigger(sourceFile: SourceFile, triggerCharacter: CompletionsTri
case "/":
return !!contextToken && (isStringLiteralLike(contextToken)
? !!tryGetImportFromModuleSpecifier(contextToken)
: contextToken.kind === SyntaxKind.SlashToken && isJsxClosingElement(contextToken.parent));
: contextToken.kind === SyntaxKind.LessThanSlashToken && isJsxClosingElement(contextToken.parent));
case " ":
return !!contextToken && isImportKeyword(contextToken) && contextToken.parent.kind === SyntaxKind.SourceFile;
default:
Expand Down
4 changes: 3 additions & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,9 @@ function createChildren(node: Node, sourceFile: SourceFileLike | undefined): rea
});
return children;
}

const languageVariant = sourceFile?.languageVariant ?? LanguageVariant.Standard;
scanner.setText((sourceFile || node.getSourceFile()).text);
scanner.setLanguageVariant(languageVariant);
let pos = node.pos;
const processNode = (child: Node) => {
addSyntheticNodes(children, pos, child.pos, node);
Expand All @@ -526,6 +527,7 @@ function createChildren(node: Node, sourceFile: SourceFileLike | undefined): rea
node.forEachChild(processNode, processNodes);
addSyntheticNodes(children, pos, node.end, node);
scanner.setText(undefined);
scanner.setLanguageVariant(LanguageVariant.Standard);
return children;
}

Expand Down
3 changes: 2 additions & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,7 @@ export function isInsideJsxElementOrAttribute(sourceFile: SourceFile, position:
}

// <div>|</div>
if (token.kind === SyntaxKind.LessThanToken && token.parent.kind === SyntaxKind.JsxClosingElement) {
if (token.kind === SyntaxKind.LessThanSlashToken && token.parent.kind === SyntaxKind.JsxClosingElement) {
return true;
}

Expand Down Expand Up @@ -1934,6 +1934,7 @@ export function isInsideJsxElement(sourceFile: SourceFile, position: number): bo
|| node.kind === SyntaxKind.CloseBraceToken
|| node.kind === SyntaxKind.OpenBraceToken
|| node.kind === SyntaxKind.SlashToken
|| node.kind === SyntaxKind.LessThanSlashToken
) {
node = node.parent;
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5907,6 +5907,7 @@ declare namespace ts {
*/
interface SourceFileLike {
readonly text: string;
languageVariant?: LanguageVariant;
}
interface SourceFileLike {
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
Expand Down
10 changes: 5 additions & 5 deletions tests/cases/fourslash/syntacticClassificationsJsx1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ verify.syntacticClassificationsAre(
c.jsxText(`
some jsx text
`),
c.punctuation("<"), c.punctuation("/"), c.jsxCloseTagName("div"), c.punctuation(">"), c.punctuation(";"),
c.punctuation("</"), c.jsxCloseTagName("div"), c.punctuation(">"), c.punctuation(";"),
c.keyword("let"), c.identifier("y"), c.operator("="),
c.punctuation("<"),
c.jsxSelfClosingTagName("element"),
c.jsxAttribute("attr"), c.operator("="), c.jsxAttributeStringLiteralValue(`"123"`),
c.punctuation("/"), c.punctuation(">")
)

const c2 = classification("2020");
verify.semanticClassificationsAre("2020",
c2.semanticToken("variable.declaration", "x"),
c2.semanticToken("variable.declaration", "y"),
const c2 = classification("2020");
verify.semanticClassificationsAre("2020",
c2.semanticToken("variable.declaration", "x"),
c2.semanticToken("variable.declaration", "y"),
);
10 changes: 5 additions & 5 deletions tests/cases/fourslash/syntacticClassificationsJsx2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ verify.syntacticClassificationsAre(
c.jsxText(`
some jsx text
`),
c.punctuation("<"), c.punctuation("/"), c.jsxCloseTagName("div.name"), c.punctuation(">"), c.punctuation(";"),
c.punctuation("</"), c.jsxCloseTagName("div.name"), c.punctuation(">"), c.punctuation(";"),
c.keyword("let"), c.identifier("y"), c.operator("="),
c.punctuation("<"),
c.jsxSelfClosingTagName("element.name"),
c.jsxAttribute("attr"), c.operator("="), c.jsxAttributeStringLiteralValue(`"123"`),
c.punctuation("/"), c.punctuation(">")
)

const c2 = classification("2020");
verify.semanticClassificationsAre("2020",
c2.semanticToken("variable.declaration", "x"),
c2.semanticToken("variable.declaration", "y"),
const c2 = classification("2020");
verify.semanticClassificationsAre("2020",
c2.semanticToken("variable.declaration", "x"),
c2.semanticToken("variable.declaration", "y"),
);