Skip to content

Commit 76addd7

Browse files
authored
Add JSXText check into isValidLocationToAddComment (#27653)
* Add JSXText check into isValidLocationToAddComment * Small simplification
1 parent 6e5e09c commit 76addd7

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

src/services/textChanges.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ namespace ts.textChanges {
10751075
}
10761076

10771077
export function isValidLocationToAddComment(sourceFile: SourceFile, position: number) {
1078-
return !isInComment(sourceFile, position) && !isInString(sourceFile, position) && !isInTemplateString(sourceFile, position);
1078+
return !isInComment(sourceFile, position) && !isInString(sourceFile, position) && !isInTemplateString(sourceFile, position) && !isInJSXText(sourceFile, position);
10791079
}
10801080

10811081
function needSemicolonBetween(a: Node, b: Node): boolean {

src/services/utilities.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,20 @@ namespace ts {
909909
return isTemplateLiteralKind(token.kind) && position > token.getStart(sourceFile);
910910
}
911911

912+
export function isInJSXText(sourceFile: SourceFile, position: number) {
913+
const token = getTokenAtPosition(sourceFile, position);
914+
if (isJsxText(token)) {
915+
return true;
916+
}
917+
if (token.kind === SyntaxKind.OpenBraceToken && isJsxExpression(token.parent) && isJsxElement(token.parent.parent)) {
918+
return true;
919+
}
920+
if (token.kind === SyntaxKind.LessThanToken && isJsxOpeningLikeElement(token.parent) && isJsxElement(token.parent.parent)) {
921+
return true;
922+
}
923+
return false;
924+
}
925+
912926
export function findPrecedingMatchingToken(token: Node, matchingTokenKind: SyntaxKind, sourceFile: SourceFile) {
913927
const tokenKind = token.kind;
914928
let remainingMatchingTokens = 0;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
// @checkJs: true
5+
// @noEmit: true
6+
// @jsx: react
7+
// @esModuleInterop: true
8+
// @skipLibCheck: true
9+
// @Filename: declarations.d.ts
10+
////declare namespace JSX {
11+
//// interface Element {}
12+
//// interface IntrinsicElements { [index: string]: {} }
13+
////}
14+
////declare var React: any;
15+
// @Filename: MyComponent.jsx
16+
////class MyComponent extends React.Component {
17+
//// render() {
18+
//// return (
19+
//// <div>
20+
//// {[|/*1*/doesNotExist|]}
21+
//// </div>
22+
//// );
23+
//// }
24+
////}
25+
// @Filename: MyComponent2.jsx
26+
////class MyComponent2 extends React.Component {
27+
//// render() {
28+
//// return (
29+
//// <div>
30+
//// Aleph{[|/*2*/doesNotExist|]}Bet
31+
//// </div>
32+
//// );
33+
//// }
34+
////}
35+
// @Filename: MyComponent3.jsx
36+
////class MyComponent3 extends React.Component {
37+
//// render() {
38+
//// return (
39+
//// <div>
40+
//// <[|/*3*/DoesNotExist|] />
41+
//// </div>
42+
//// );
43+
//// }
44+
////}
45+
46+
goTo.file(1);
47+
verify.getSyntacticDiagnostics([]);
48+
verify.getSemanticDiagnostics([{ code: 2304, message: "Cannot find name 'doesNotExist'." }]);
49+
verify.codeFix({
50+
index: 0,
51+
description: "Ignore this error message",
52+
newFileContent: `class MyComponent extends React.Component {
53+
render() {
54+
return (
55+
<div>
56+
{
57+
// @ts-ignore
58+
doesNotExist}
59+
</div>
60+
);
61+
}
62+
}`
63+
});
64+
goTo.file(2);
65+
verify.codeFix({
66+
index: 0,
67+
description: "Ignore this error message",
68+
newFileContent: `class MyComponent2 extends React.Component {
69+
render() {
70+
return (
71+
<div>
72+
Aleph{
73+
// @ts-ignore
74+
doesNotExist}Bet
75+
</div>
76+
);
77+
}
78+
}`
79+
});
80+
goTo.file(3);
81+
verify.codeFix({
82+
index: 0,
83+
description: "Ignore this error message",
84+
newFileContent: `class MyComponent3 extends React.Component {
85+
render() {
86+
return (
87+
<div>
88+
<
89+
// @ts-ignore
90+
DoesNotExist />
91+
</div>
92+
);
93+
}
94+
}`
95+
});

0 commit comments

Comments
 (0)