Skip to content
Open
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
4 changes: 3 additions & 1 deletion tsc/internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5093,7 +5093,9 @@ func (p *Parser) parseJsxAttributeValue() *ast.Expression {
return p.parseJsxExpression( /*inExpressionContext*/ true)
}
if p.token == ast.KindLessThanToken {
return p.parseJsxElementOrSelfClosingElementOrFragment(true /*inExpressionContext*/, -1, nil, false)
// An attribute value must be a single JsxAttributeValue, so don't allow the sibling-element
// recovery to wrap it in a synthetic binary expression.
return p.parseJsxElementOrSelfClosingElementOrFragment(true /*inExpressionContext*/, -1 /*topInvalidNodePosition*/, nil /*openingTag*/, true /*mustBeUnary*/)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to handle this here - where the bad data was created. All of the other code just doesn't expect BinaryExpression here given:

type JsxAttributeValue = StringLiteral | JsxExpression | JsxElement | JsxSelfClosingElement | JsxFragment

}
p.parseErrorAtCurrentToken(diagnostics.X_or_JSX_element_expected)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,11): error TS2657: JSX expressions must have one parent element.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,29): error TS1003: Identifier expected.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,39): error TS1109: Expression expected.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,40): error TS1109: Expression expected.


==== jsxAttributeInitializerMultipleElementsNoCrash.tsx (4 errors) ====
declare namespace JSX { interface IntrinsicElements { [x: string]: any } }
declare const React: any;

const a = <div attr=<span /><span /> />;
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2657: JSX expressions must have one parent element.
~
!!! error TS1003: Identifier expected.
~
!!! error TS1109: Expression expected.
~
!!! error TS1109: Expression expected.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [tests/cases/compiler/jsxAttributeInitializerMultipleElementsNoCrash.tsx] ////

//// [jsxAttributeInitializerMultipleElementsNoCrash.tsx]
declare namespace JSX { interface IntrinsicElements { [x: string]: any } }
declare const React: any;

const a = <div attr=<span /><span /> />;


//// [jsxAttributeInitializerMultipleElementsNoCrash.js]
"use strict";
const a = (React.createElement("div", { attr: React.createElement("span", null) }), React.createElement("span", null)) / > ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,11): error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,11): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,11): error TS2657: JSX expressions must have one parent element.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,21): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,29): error TS1003: Identifier expected.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,29): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,39): error TS1109: Expression expected.
jsxAttributeInitializerMultipleElementsNoCrash.tsx(4,40): error TS1109: Expression expected.


==== jsxAttributeInitializerMultipleElementsNoCrash.tsx (8 errors) ====
declare namespace JSX { interface IntrinsicElements { [x: string]: any } }
declare const React: any;

const a = <div attr=<span /><span /> />;
~~~~~~~~~~~~~~~~~~
!!! error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
~~~~~~~~~~~~~~~~~~
!!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2657: JSX expressions must have one parent element.
~~~~~~~~
!!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
~
!!! error TS1003: Identifier expected.
~~~~~~~~
!!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
~
!!! error TS1109: Expression expected.
~
!!! error TS1109: Expression expected.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [tests/cases/compiler/jsxAttributeInitializerMultipleElementsNoCrash.tsx] ////

//// [jsxAttributeInitializerMultipleElementsNoCrash.tsx]
declare namespace JSX { interface IntrinsicElements { [x: string]: any } }
declare const React: any;

const a = <div attr=<span /><span /> />;


//// [jsxAttributeInitializerMultipleElementsNoCrash.js]
import { jsx as _jsx } from "react/jsx-runtime";
const a = (_jsx("div", { attr: _jsx("span", {}) }), _jsx("span", {})) / > ;
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
jsxAttributeValueBinaryExpression.tsx(1,1): error TS2657: JSX expressions must have one parent element.
jsxAttributeValueBinaryExpression.tsx(1,2): error TS2304: Cannot find name 'X'.
jsxAttributeValueBinaryExpression.tsx(1,6): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
jsxAttributeValueBinaryExpression.tsx(1,6): error TS2657: JSX expressions must have one parent element.
jsxAttributeValueBinaryExpression.tsx(1,10): error TS1003: Identifier expected.
jsxAttributeValueBinaryExpression.tsx(1,10): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
jsxAttributeValueBinaryExpression.tsx(1,16): error TS1109: Expression expected.
jsxAttributeValueBinaryExpression.tsx(1,17): error TS1109: Expression expected.


==== jsxAttributeValueBinaryExpression.tsx (4 errors) ====
==== jsxAttributeValueBinaryExpression.tsx (7 errors) ====
<X a=<b/><c/> />
~~~~~~~~~~~~~
!!! error TS2657: JSX expressions must have one parent element.
~
!!! error TS2304: Cannot find name 'X'.
~~~~
!!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
~~~~~~~~
!!! error TS2657: JSX expressions must have one parent element.
~
!!! error TS1003: Identifier expected.
~~~~
!!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
~
!!! error TS1109: Expression expected.

!!! error TS1109: Expression expected.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

//// [jsxAttributeValueBinaryExpression.jsx]
"use strict";
<X a=<b />, <c />/>;
(<X a=<b />/>, <c />) / >
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @jsx: react,react-jsx
// @noTypesAndSymbols: true

declare namespace JSX { interface IntrinsicElements { [x: string]: any } }
declare const React: any;

const a = <div attr=<span /><span /> />;