-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Throw syntax error for `}` and `>` in JSX text Fixes #36341 * Add codefix for error
- Loading branch information
1 parent
ad8c209
commit 348c4dd
Showing
19 changed files
with
487 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixIdHtmlEntity = "invalidJsxCharactersConvertToHtmlEntity"; | ||
const fixIdExpression = "invalidJsxCharactersConvertToExpression"; | ||
|
||
const errorCodes = [Diagnostics.Unexpected_token_Did_you_mean_or_gt.code, Diagnostics.Unexpected_token_Did_you_mean_or_rbrace.code]; | ||
|
||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions: context => { | ||
const { sourceFile, span } = context; | ||
const changeToExpression = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, span.start, /* useHtmlEntity */ false)); | ||
const changeToHtmlEntity = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, span.start, /* useHtmlEntity */ true)); | ||
return [ | ||
createCodeFixActionWithoutFixAll(fixIdExpression, changeToExpression, Diagnostics.Wrap_invalid_character_in_an_expression_container), | ||
createCodeFixAction(fixIdHtmlEntity, changeToHtmlEntity, Diagnostics.Convert_invalid_character_to_its_html_entity_code, fixIdHtmlEntity, Diagnostics.Convert_invalid_character_to_its_html_entity_code), | ||
]; | ||
}, | ||
fixIds: [fixIdExpression, fixIdHtmlEntity], | ||
}); | ||
|
||
const htmlEntity = { | ||
">": ">", | ||
"}": "}", | ||
}; | ||
function isValidCharacter(character: string): character is keyof typeof htmlEntity { | ||
return hasProperty(htmlEntity, character); | ||
} | ||
|
||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, start: number, useHtmlEntity: boolean) { | ||
const character = sourceFile.getText()[start]; | ||
// sanity check | ||
if (!isValidCharacter(character)) { | ||
return; | ||
} | ||
|
||
const replacement = useHtmlEntity | ||
? htmlEntity[character] | ||
: `{'${character}'}`; | ||
changes.replaceRangeWithText(sourceFile, { pos: start, end: start + 1 }, replacement); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
tests/cases/conformance/jsx/Error1.tsx(1,15): error TS1381: Unexpected token. Did you mean `{'}'}` or `}`? | ||
tests/cases/conformance/jsx/Error2.tsx(1,15): error TS1382: Unexpected token. Did you mean `{'>'}` or `>`? | ||
tests/cases/conformance/jsx/Error3.tsx(1,22): error TS1381: Unexpected token. Did you mean `{'}'}` or `}`? | ||
tests/cases/conformance/jsx/Error4.tsx(1,22): error TS1382: Unexpected token. Did you mean `{'>'}` or `>`? | ||
tests/cases/conformance/jsx/Error5.tsx(1,15): error TS1381: Unexpected token. Did you mean `{'}'}` or `}`? | ||
tests/cases/conformance/jsx/Error6.tsx(1,15): error TS1382: Unexpected token. Did you mean `{'>'}` or `>`? | ||
|
||
|
||
==== tests/cases/conformance/jsx/file.tsx (0 errors) ==== | ||
declare module JSX { | ||
interface Element {} | ||
interface IntrinsicElements { | ||
[s: string]: any; | ||
} | ||
} | ||
|
||
==== tests/cases/conformance/jsx/Error1.tsx (1 errors) ==== | ||
let x1 = <div>}</div>; | ||
~ | ||
!!! error TS1381: Unexpected token. Did you mean `{'}'}` or `}`? | ||
|
||
==== tests/cases/conformance/jsx/Error2.tsx (1 errors) ==== | ||
let x2 = <div>></div>; | ||
~ | ||
!!! error TS1382: Unexpected token. Did you mean `{'>'}` or `>`? | ||
|
||
==== tests/cases/conformance/jsx/Error3.tsx (1 errors) ==== | ||
let x3 = <div>{"foo"}}</div>; | ||
~ | ||
!!! error TS1381: Unexpected token. Did you mean `{'}'}` or `}`? | ||
|
||
==== tests/cases/conformance/jsx/Error4.tsx (1 errors) ==== | ||
let x4 = <div>{"foo"}></div>; | ||
~ | ||
!!! error TS1382: Unexpected token. Did you mean `{'>'}` or `>`? | ||
|
||
==== tests/cases/conformance/jsx/Error5.tsx (1 errors) ==== | ||
let x5 = <div>}{"foo"}</div>; | ||
~ | ||
!!! error TS1381: Unexpected token. Did you mean `{'}'}` or `}`? | ||
|
||
==== tests/cases/conformance/jsx/Error6.tsx (1 errors) ==== | ||
let x6 = <div>>{"foo"}</div>; | ||
~ | ||
!!! error TS1382: Unexpected token. Did you mean `{'>'}` or `>`? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//// [tests/cases/conformance/jsx/jsxParsingError3.tsx] //// | ||
|
||
//// [file.tsx] | ||
declare module JSX { | ||
interface Element {} | ||
interface IntrinsicElements { | ||
[s: string]: any; | ||
} | ||
} | ||
|
||
//// [Error1.tsx] | ||
let x1 = <div>}</div>; | ||
|
||
//// [Error2.tsx] | ||
let x2 = <div>></div>; | ||
|
||
//// [Error3.tsx] | ||
let x3 = <div>{"foo"}}</div>; | ||
|
||
//// [Error4.tsx] | ||
let x4 = <div>{"foo"}></div>; | ||
|
||
//// [Error5.tsx] | ||
let x5 = <div>}{"foo"}</div>; | ||
|
||
//// [Error6.tsx] | ||
let x6 = <div>>{"foo"}</div>; | ||
|
||
|
||
//// [file.jsx] | ||
//// [Error1.jsx] | ||
var x1 = <div>}</div>; | ||
//// [Error2.jsx] | ||
var x2 = <div>></div>; | ||
//// [Error3.jsx] | ||
var x3 = <div>{"foo"}}</div>; | ||
//// [Error4.jsx] | ||
var x4 = <div>{"foo"}></div>; | ||
//// [Error5.jsx] | ||
var x5 = <div>}{"foo"}</div>; | ||
//// [Error6.jsx] | ||
var x6 = <div>>{"foo"}</div>; |
Oops, something went wrong.