|
1 | 1 | /* @internal */ |
2 | 2 | namespace ts.codefix { |
3 | | - const fixIdHtmlEntity = "invalidJsxCharactersConvertToHtmlEntity"; |
4 | | - const fixIdExpression = "invalidJsxCharactersConvertToExpression"; |
| 3 | + const fixIdExpression = "fixInvalidJsxCharacters_expression"; |
| 4 | + const fixIdHtmlEntity = "fixInvalidJsxCharacters_htmlEntity"; |
5 | 5 |
|
6 | | - const errorCodes = [Diagnostics.Unexpected_token_Did_you_mean_or_gt.code, Diagnostics.Unexpected_token_Did_you_mean_or_rbrace.code]; |
| 6 | + const errorCodes = [ |
| 7 | + Diagnostics.Unexpected_token_Did_you_mean_or_gt.code, |
| 8 | + Diagnostics.Unexpected_token_Did_you_mean_or_rbrace.code |
| 9 | + ]; |
7 | 10 |
|
8 | 11 | registerCodeFix({ |
9 | 12 | errorCodes, |
10 | | - getCodeActions: context => { |
11 | | - const { sourceFile, span } = context; |
12 | | - const changeToExpression = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, span.start, /* useHtmlEntity */ false)); |
13 | | - const changeToHtmlEntity = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, span.start, /* useHtmlEntity */ true)); |
| 13 | + fixIds: [fixIdExpression, fixIdHtmlEntity], |
| 14 | + getCodeActions(context) { |
| 15 | + const { sourceFile, preferences, span } = context; |
| 16 | + const changeToExpression = textChanges.ChangeTracker.with(context, t => doChange(t, preferences, sourceFile, span.start, /* useHtmlEntity */ false)); |
| 17 | + const changeToHtmlEntity = textChanges.ChangeTracker.with(context, t => doChange(t, preferences, sourceFile, span.start, /* useHtmlEntity */ true)); |
| 18 | + |
14 | 19 | return [ |
15 | | - createCodeFixActionWithoutFixAll(fixIdExpression, changeToExpression, Diagnostics.Wrap_invalid_character_in_an_expression_container), |
16 | | - createCodeFixAction(fixIdHtmlEntity, changeToHtmlEntity, Diagnostics.Convert_invalid_character_to_its_html_entity_code, fixIdHtmlEntity, Diagnostics.Convert_invalid_character_to_its_html_entity_code), |
| 20 | + createCodeFixAction(fixIdExpression, changeToExpression, Diagnostics.Wrap_invalid_character_in_an_expression_container, fixIdExpression, Diagnostics.Wrap_all_invalid_characters_in_an_expression_container), |
| 21 | + createCodeFixAction(fixIdHtmlEntity, changeToHtmlEntity, Diagnostics.Convert_invalid_character_to_its_html_entity_code, fixIdHtmlEntity, Diagnostics.Convert_all_invalid_characters_to_HTML_entity_code) |
17 | 22 | ]; |
18 | 23 | }, |
19 | | - fixIds: [fixIdExpression, fixIdHtmlEntity], |
| 24 | + getAllCodeActions(context) { |
| 25 | + return codeFixAll(context, errorCodes, (changes, diagnostic) => doChange(changes, context.preferences, diagnostic.file, diagnostic.start, context.fixId === fixIdHtmlEntity)); |
| 26 | + } |
20 | 27 | }); |
21 | 28 |
|
22 | 29 | const htmlEntity = { |
23 | 30 | ">": ">", |
24 | 31 | "}": "}", |
25 | 32 | }; |
| 33 | + |
26 | 34 | function isValidCharacter(character: string): character is keyof typeof htmlEntity { |
27 | 35 | return hasProperty(htmlEntity, character); |
28 | 36 | } |
29 | 37 |
|
30 | | - function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, start: number, useHtmlEntity: boolean) { |
| 38 | + function doChange(changes: textChanges.ChangeTracker, preferences: UserPreferences, sourceFile: SourceFile, start: number, useHtmlEntity: boolean) { |
31 | 39 | const character = sourceFile.getText()[start]; |
32 | 40 | // sanity check |
33 | 41 | if (!isValidCharacter(character)) { |
34 | 42 | return; |
35 | 43 | } |
36 | 44 |
|
37 | | - const replacement = useHtmlEntity |
38 | | - ? htmlEntity[character] |
39 | | - : `{'${character}'}`; |
| 45 | + const replacement = useHtmlEntity ? htmlEntity[character] : `{${quote(character, preferences)}}`; |
40 | 46 | changes.replaceRangeWithText(sourceFile, { pos: start, end: start + 1 }, replacement); |
41 | 47 | } |
42 | 48 | } |
0 commit comments