-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Improve error range for ts2657 (jsx expr must have parent element), add code fix for it #37917
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a47eb2c
fix: range of ts2657 (jsx expr must have parent) and remove 2695 (LHS…
Jack-Works 1a93e72
feat: add code fix for 2657
Jack-Works 446749b
fix: resolve review
Jack-Works fae9476
chore: hoist a var
Jack-Works 389efeb
chore: add test for skipTrivia
Jack-Works 2548581
fix: rebase error
Jack-Works 472fdf7
Merge branch 'master' into codefix/jsx-2695
andrewbranch 6a1f004
Update src/compiler/diagnosticMessages.json
Jack-Works cb1fdb7
Update src/services/codefixes/wrapJsxInFragment.ts
Jack-Works File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixID = "wrapJsxInFragment"; | ||
const errorCodes = [Diagnostics.JSX_expressions_must_have_one_parent_element.code]; | ||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions: context => { | ||
const { jsx } = context.program.getCompilerOptions(); | ||
if (jsx !== JsxEmit.React && jsx !== JsxEmit.ReactNative) { | ||
return undefined; | ||
} | ||
const { sourceFile, span } = context; | ||
const node = findNodeToFix(sourceFile, span.start); | ||
if (!node) return undefined; | ||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node)); | ||
return [createCodeFixAction(fixID, changes, Diagnostics.Wrap_in_JSX_fragment, fixID, Diagnostics.Wrap_all_unparented_JSX_in_JSX_fragment)]; | ||
}, | ||
fixIds: [fixID], | ||
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { | ||
const node = findNodeToFix(context.sourceFile, diag.start); | ||
if (!node) return undefined; | ||
doChange(changes, context.sourceFile, node); | ||
}), | ||
}); | ||
|
||
function findNodeToFix(sourceFile: SourceFile, pos: number): BinaryExpression | undefined { | ||
// The error always at 1st token that is "<" in "<a /><a />" | ||
const lessThanToken = getTokenAtPosition(sourceFile, pos); | ||
const firstJsxElementOrOpenElement = lessThanToken.parent; | ||
let binaryExpr = firstJsxElementOrOpenElement.parent; | ||
if (!isBinaryExpression(binaryExpr)) { | ||
// In case the start element is a JsxSelfClosingElement, it the end. | ||
// For JsxOpenElement, find one more parent | ||
binaryExpr = binaryExpr.parent; | ||
if (!isBinaryExpression(binaryExpr)) return undefined; | ||
} | ||
if (!nodeIsMissing(binaryExpr.operatorToken)) return undefined; | ||
return binaryExpr; | ||
} | ||
|
||
function doChange(changeTracker: textChanges.ChangeTracker, sf: SourceFile, node: Node) { | ||
const jsx = flattenInvalidBinaryExpr(node); | ||
if (jsx) changeTracker.replaceNode(sf, node, createJsxFragment(createJsxOpeningFragment(), jsx, createJsxJsxClosingFragment())); | ||
} | ||
// The invalid syntax is constructed as | ||
// InvalidJsxTree :: One of | ||
// JsxElement CommaToken InvalidJsxTree | ||
// JsxElement CommaToken JsxElement | ||
function flattenInvalidBinaryExpr(node: Node): JsxChild[] | undefined { | ||
const children: JsxChild[] = []; | ||
let current = node; | ||
while (true) { | ||
if (isBinaryExpression(current) && nodeIsMissing(current.operatorToken) && current.operatorToken.kind === SyntaxKind.CommaToken) { | ||
children.push(<JsxChild>current.left); | ||
if (isJsxChild(current.right)) { | ||
children.push(current.right); | ||
// Indicates the tree has go to the bottom | ||
return children; | ||
} | ||
else if (isBinaryExpression(current.right)) { | ||
current = current.right; | ||
continue; | ||
} | ||
// Unreachable case | ||
else return undefined; | ||
} | ||
// Unreachable case | ||
else return undefined; | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,23 +1,18 @@ | ||
tests/cases/conformance/jsx/file1.tsx(3,1): error TS2695: Left side of comma operator is unused and has no side effects. | ||
tests/cases/conformance/jsx/file1.tsx(5,1): error TS2657: JSX expressions must have one parent element. | ||
tests/cases/conformance/jsx/file2.tsx(1,9): error TS2695: Left side of comma operator is unused and has no side effects. | ||
tests/cases/conformance/jsx/file2.tsx(2,1): error TS2657: JSX expressions must have one parent element. | ||
tests/cases/conformance/jsx/file1.tsx(3,1): error TS2657: JSX expressions must have one parent element. | ||
tests/cases/conformance/jsx/file2.tsx(1,9): error TS2657: JSX expressions must have one parent element. | ||
|
||
|
||
==== tests/cases/conformance/jsx/file1.tsx (2 errors) ==== | ||
==== tests/cases/conformance/jsx/file1.tsx (1 errors) ==== | ||
declare namespace JSX { interface Element { } } | ||
|
||
<div></div> | ||
~~~~~~~~~~~ | ||
!!! error TS2695: Left side of comma operator is unused and has no side effects. | ||
<div></div> | ||
|
||
|
||
~~~~~~~~~~~ | ||
!!! error TS2657: JSX expressions must have one parent element. | ||
==== tests/cases/conformance/jsx/file2.tsx (2 errors) ==== | ||
var x = <div></div><div></div> | ||
~~~~~~~~~~~ | ||
!!! error TS2695: Left side of comma operator is unused and has no side effects. | ||
|
||
|
||
!!! error TS2657: JSX expressions must have one parent element. | ||
==== tests/cases/conformance/jsx/file2.tsx (1 errors) ==== | ||
var x = <div></div><div></div> | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2657: JSX expressions must have one parent element. | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -1,35 +1,30 @@ | ||
tests/cases/conformance/jsx/file1.tsx(3,1): error TS2695: Left side of comma operator is unused and has no side effects. | ||
tests/cases/conformance/jsx/file1.tsx(3,1): error TS2657: JSX expressions must have one parent element. | ||
tests/cases/conformance/jsx/file1.tsx(3,2): error TS2304: Cannot find name 'React'. | ||
tests/cases/conformance/jsx/file1.tsx(4,2): error TS2304: Cannot find name 'React'. | ||
tests/cases/conformance/jsx/file1.tsx(5,1): error TS2657: JSX expressions must have one parent element. | ||
tests/cases/conformance/jsx/file2.tsx(1,9): error TS2695: Left side of comma operator is unused and has no side effects. | ||
tests/cases/conformance/jsx/file2.tsx(1,9): error TS2657: JSX expressions must have one parent element. | ||
tests/cases/conformance/jsx/file2.tsx(1,10): error TS2304: Cannot find name 'React'. | ||
tests/cases/conformance/jsx/file2.tsx(1,21): error TS2304: Cannot find name 'React'. | ||
tests/cases/conformance/jsx/file2.tsx(2,1): error TS2657: JSX expressions must have one parent element. | ||
|
||
|
||
==== tests/cases/conformance/jsx/file1.tsx (4 errors) ==== | ||
==== tests/cases/conformance/jsx/file1.tsx (3 errors) ==== | ||
declare namespace JSX { interface Element { } } | ||
|
||
<div></div> | ||
~~~~~~~~~~~ | ||
!!! error TS2695: Left side of comma operator is unused and has no side effects. | ||
~~~ | ||
!!! error TS2304: Cannot find name 'React'. | ||
<div></div> | ||
~~~~~~~~~~~ | ||
!!! error TS2657: JSX expressions must have one parent element. | ||
~~~ | ||
!!! error TS2304: Cannot find name 'React'. | ||
|
||
|
||
!!! error TS2657: JSX expressions must have one parent element. | ||
==== tests/cases/conformance/jsx/file2.tsx (4 errors) ==== | ||
==== tests/cases/conformance/jsx/file2.tsx (3 errors) ==== | ||
var x = <div></div><div></div> | ||
~~~~~~~~~~~ | ||
!!! error TS2695: Left side of comma operator is unused and has no side effects. | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2657: JSX expressions must have one parent element. | ||
~~~ | ||
!!! error TS2304: Cannot find name 'React'. | ||
~~~ | ||
!!! error TS2304: Cannot find name 'React'. | ||
|
||
|
||
!!! error TS2657: JSX expressions must have one parent element. | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @jsx: react | ||
// @Filename: /a.tsx | ||
////[|<a /><a />|] | ||
|
||
verify.rangeAfterCodeFix(`<><a /><a /></>`, /*includeWhiteSpace*/false, /*errorCode*/ undefined, /*index*/ 0); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.