-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Under jsx: preserve, actually preserve expressions which contain only comments #41757
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2824,7 +2824,8 @@ namespace ts { | |
} | ||
pos = writeTokenText(token, writer, pos); | ||
if (isSimilarNode && contextNode.end !== pos) { | ||
emitTrailingCommentsOfPosition(pos, /*prefixSpace*/ true); | ||
const isJsxExprContex = contextNode.kind === SyntaxKind.JsxExpression; | ||
emitTrailingCommentsOfPosition(pos, /*prefixSpace*/ !isJsxExprContex, /*forceNoNewline*/ isJsxExprContex); | ||
} | ||
return pos; | ||
} | ||
|
@@ -3378,8 +3379,8 @@ namespace ts { | |
} | ||
|
||
function emitJsxExpression(node: JsxExpression) { | ||
if (node.expression) { | ||
writePunctuation("{"); | ||
if (node.expression || (!commentsDisabled && !nodeIsSynthesized(node) && getTextOfNode(node).length > 2 && getTextOfNode(node).indexOf("/*") > -1)) { // preserve empty expressions if they contain comments! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
const x = <div>
{ // first line
/*last line*/}
</div>; Maybe it's worth doing a full check with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we care about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. That's why I'm asking if it's worth doing a full comment range iteration to check if there are any comments attached to the position. |
||
emitTokenWithComment(SyntaxKind.OpenBraceToken, node.pos, writePunctuation, node); | ||
emit(node.dotDotDotToken); | ||
emitExpression(node.expression); | ||
writePunctuation("}"); | ||
|
@@ -5230,21 +5231,25 @@ namespace ts { | |
} | ||
} | ||
|
||
function emitTrailingCommentsOfPosition(pos: number, prefixSpace?: boolean) { | ||
function emitTrailingCommentsOfPosition(pos: number, prefixSpace?: boolean, forceNoNewline?: boolean) { | ||
if (commentsDisabled) { | ||
return; | ||
} | ||
enterComment(); | ||
forEachTrailingCommentToEmit(pos, prefixSpace ? emitTrailingComment : emitTrailingCommentOfPosition); | ||
forEachTrailingCommentToEmit(pos, prefixSpace ? emitTrailingComment : forceNoNewline ? emitTrailingCommentOfPositionNoNewline : emitTrailingCommentOfPosition); | ||
exitComment(); | ||
} | ||
|
||
function emitTrailingCommentOfPosition(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) { | ||
function emitTrailingCommentOfPositionNoNewline(commentPos: number, commentEnd: number) { | ||
// trailing comments of a position are emitted at /*trailing comment1 */space/*trailing comment*/space | ||
|
||
emitPos(commentPos); | ||
writeCommentRange(currentSourceFile!.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine); | ||
emitPos(commentEnd); | ||
} | ||
|
||
function emitTrailingCommentOfPosition(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) { | ||
emitTrailingCommentOfPositionNoNewline(commentPos, commentEnd); | ||
|
||
if (hasTrailingNewLine) { | ||
writer.writeLine(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1790,7 +1790,7 @@ namespace ts { | |
const name = importDeclaration.propertyName || importDeclaration.name; | ||
return setTextRange( | ||
factory.createPropertyAccessExpression( | ||
factory.getGeneratedNameForNode(importDeclaration.parent.parent.parent), | ||
factory.getGeneratedNameForNode(importDeclaration.parent?.parent?.parent || importDeclaration), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change (and the similar ones in Since this part fixes a crash, I'm wondering if I should (partially?) port this to the 4.1 branch or no? @DanielRosenwasser ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment may actually be referring to the crash fixed here (assuming the input file is exactly what the user in that comment says, odd as a top-level |
||
factory.cloneNode(name) | ||
), | ||
/*location*/ node | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//// [commentsOnJSXExpressionsArePreserved.tsx] | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
weswigham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type JSXElement = any; | ||
} | ||
class Component { | ||
render() { | ||
return <div> | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} | ||
|
||
//// [commentsOnJSXExpressionsArePreserved.jsx] | ||
var Component = /** @class */ (function () { | ||
function Component() { | ||
} | ||
Component.prototype.render = function () { | ||
return <div> | ||
{/* missing */} | ||
{null /* preserved */} | ||
</div>; | ||
}; | ||
return Component; | ||
}()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx === | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
>JSX : Symbol(JSX, Decl(commentsOnJSXExpressionsArePreserved.tsx, 0, 0)) | ||
|
||
interface IntrsinsicElements { [index: string]: any } | ||
>IntrsinsicElements : Symbol(IntrsinsicElements, Decl(commentsOnJSXExpressionsArePreserved.tsx, 1, 15)) | ||
>index : Symbol(index, Decl(commentsOnJSXExpressionsArePreserved.tsx, 2, 36)) | ||
|
||
type JSXElement = any; | ||
>JSXElement : Symbol(JSXElement, Decl(commentsOnJSXExpressionsArePreserved.tsx, 2, 57)) | ||
} | ||
class Component { | ||
>Component : Symbol(Component, Decl(commentsOnJSXExpressionsArePreserved.tsx, 4, 1)) | ||
|
||
render() { | ||
>render : Symbol(Component.render, Decl(commentsOnJSXExpressionsArePreserved.tsx, 5, 17)) | ||
|
||
return <div> | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx === | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
>index : string | ||
|
||
type JSXElement = any; | ||
>JSXElement : any | ||
} | ||
class Component { | ||
>Component : Component | ||
|
||
render() { | ||
>render : () => any | ||
|
||
return <div> | ||
><div> {/* missing */} {null/* preserved */} </div> : error | ||
>div : any | ||
|
||
{/* missing */} | ||
{null/* preserved */} | ||
>null : null | ||
|
||
</div>; | ||
>div : any | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//// [commentsOnJSXExpressionsArePreserved.tsx] | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
type JSXElement = any; | ||
} | ||
class Component { | ||
render() { | ||
return <div> | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} | ||
|
||
//// [commentsOnJSXExpressionsArePreserved.jsx] | ||
var Component = /** @class */ (function () { | ||
function Component() { | ||
} | ||
Component.prototype.render = function () { | ||
return <div> | ||
{/* missing */} | ||
{null /* preserved */} | ||
</div>; | ||
}; | ||
return Component; | ||
}()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx === | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
>JSX : Symbol(JSX, Decl(commentsOnJSXExpressionsArePreserved.tsx, 0, 0)) | ||
|
||
interface IntrsinsicElements { [index: string]: any } | ||
>IntrsinsicElements : Symbol(IntrsinsicElements, Decl(commentsOnJSXExpressionsArePreserved.tsx, 1, 15)) | ||
>index : Symbol(index, Decl(commentsOnJSXExpressionsArePreserved.tsx, 2, 36)) | ||
|
||
type JSXElement = any; | ||
>JSXElement : Symbol(JSXElement, Decl(commentsOnJSXExpressionsArePreserved.tsx, 2, 57)) | ||
} | ||
class Component { | ||
>Component : Symbol(Component, Decl(commentsOnJSXExpressionsArePreserved.tsx, 4, 1)) | ||
|
||
render() { | ||
>render : Symbol(Component.render, Decl(commentsOnJSXExpressionsArePreserved.tsx, 5, 17)) | ||
|
||
return <div> | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx === | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
>index : string | ||
|
||
type JSXElement = any; | ||
>JSXElement : any | ||
} | ||
class Component { | ||
>Component : Component | ||
|
||
render() { | ||
>render : () => any | ||
|
||
return <div> | ||
><div> {/* missing */} {null/* preserved */} </div> : error | ||
>div : any | ||
|
||
{/* missing */} | ||
{null/* preserved */} | ||
>null : null | ||
|
||
</div>; | ||
>div : any | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx(8,17): error TS2304: Cannot find name 'React'. | ||
|
||
|
||
==== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx (1 errors) ==== | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
type JSXElement = any; | ||
} | ||
class Component { | ||
render() { | ||
return <div> | ||
~~~ | ||
!!! error TS2304: Cannot find name 'React'. | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//// [commentsOnJSXExpressionsArePreserved.tsx] | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
type JSXElement = any; | ||
} | ||
class Component { | ||
render() { | ||
return <div> | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} | ||
|
||
//// [commentsOnJSXExpressionsArePreserved.js] | ||
var Component = /** @class */ (function () { | ||
function Component() { | ||
} | ||
Component.prototype.render = function () { | ||
return React.createElement("div", null, null /* preserved */); | ||
}; | ||
return Component; | ||
}()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx === | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
>JSX : Symbol(JSX, Decl(commentsOnJSXExpressionsArePreserved.tsx, 0, 0)) | ||
|
||
interface IntrsinsicElements { [index: string]: any } | ||
>IntrsinsicElements : Symbol(IntrsinsicElements, Decl(commentsOnJSXExpressionsArePreserved.tsx, 1, 15)) | ||
>index : Symbol(index, Decl(commentsOnJSXExpressionsArePreserved.tsx, 2, 36)) | ||
|
||
type JSXElement = any; | ||
>JSXElement : Symbol(JSXElement, Decl(commentsOnJSXExpressionsArePreserved.tsx, 2, 57)) | ||
} | ||
class Component { | ||
>Component : Symbol(Component, Decl(commentsOnJSXExpressionsArePreserved.tsx, 4, 1)) | ||
|
||
render() { | ||
>render : Symbol(Component.render, Decl(commentsOnJSXExpressionsArePreserved.tsx, 5, 17)) | ||
|
||
return <div> | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx === | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
>index : string | ||
|
||
type JSXElement = any; | ||
>JSXElement : any | ||
} | ||
class Component { | ||
>Component : Component | ||
|
||
render() { | ||
>render : () => any | ||
|
||
return <div> | ||
><div> {/* missing */} {null/* preserved */} </div> : any | ||
>div : any | ||
|
||
{/* missing */} | ||
{null/* preserved */} | ||
>null : null | ||
|
||
</div>; | ||
>div : any | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx(8,17): error TS2304: Cannot find name 'React'. | ||
|
||
|
||
==== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx (1 errors) ==== | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
type JSXElement = any; | ||
} | ||
class Component { | ||
render() { | ||
return <div> | ||
~~~ | ||
!!! error TS2304: Cannot find name 'React'. | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//// [commentsOnJSXExpressionsArePreserved.tsx] | ||
// file is interntionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs | ||
namespace JSX { | ||
interface IntrsinsicElements { [index: string]: any } | ||
type JSXElement = any; | ||
} | ||
class Component { | ||
render() { | ||
return <div> | ||
{/* missing */} | ||
{null/* preserved */} | ||
</div>; | ||
} | ||
} | ||
|
||
//// [commentsOnJSXExpressionsArePreserved.js] | ||
var Component = /** @class */ (function () { | ||
function Component() { | ||
} | ||
Component.prototype.render = function () { | ||
return React.createElement("div", null, null /* preserved */); | ||
}; | ||
return Component; | ||
}()); |
Uh oh!
There was an error while loading. Please reload this page.