Skip to content
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

Under jsx: preserve, actually preserve expressions which contain only comments #41757

Merged
merged 2 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
weswigham marked this conversation as resolved.
Show resolved Hide resolved
emitTrailingCommentsOfPosition(pos, /*prefixSpace*/ !isJsxExprContex, /*forceNoNewline*/ isJsxExprContex);
}
return pos;
}
Expand Down Expand Up @@ -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!
Copy link
Member Author

Choose a reason for hiding this comment

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

hasCommentAtPosition isn't a thing we currently have, but this does a, okay job of approximating it. Obviously doesn't handle something like

const x = <div>
{ // first line 
/*last line*/}
</div>;

Maybe it's worth doing a full check with forEachTrailingCommentRange? @rbuckton - your opinion?

Copy link
Member

Choose a reason for hiding this comment

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

do we care about // comments? The new check only handles /**/ comments, right?

Copy link
Member Author

Choose a reason for hiding this comment

The 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("}");
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Member Author

Choose a reason for hiding this comment

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

This change (and the similar ones in system) are what fix the crash mentioned in the OP. The non-module file has a reference to a synthetic import that, since it's never actually inserted into the file (since it's not a module), is never parented, and thus has no name. We emit a (admittedly type system, rather than grammar/program) error in these cases usually (module "react/jsx-runtime" not found), so the emitted JS being nonfunctional is probably fine, but this case might need a more descriptive error added. The emit simply uses an arbitrary name now (_a), rather than crashing.

Since this part fixes a crash, I'm wondering if I should (partially?) port this to the 4.1 branch or no? @DanielRosenwasser ?

Copy link
Member Author

@weswigham weswigham Dec 1, 2020

Choose a reason for hiding this comment

The 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 return is, since we wouldn't mark that as a module), though the stack is very different than the on the OP of that thread refers to (and is definitely a separate issue).

factory.cloneNode(name)
),
/*location*/ node
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/module/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ namespace ts {
factory.createPropertyAssignment(
factory.cloneNode(name),
factory.createPropertyAccessExpression(
factory.getGeneratedNameForNode(importDeclaration.parent.parent.parent),
factory.getGeneratedNameForNode(importDeclaration.parent?.parent?.parent || importDeclaration),
factory.cloneNode(importDeclaration.propertyName || importDeclaration.name)
),
),
Expand Down Expand Up @@ -1747,7 +1747,7 @@ namespace ts {
else if (isImportSpecifier(importDeclaration)) {
return setTextRange(
factory.createPropertyAccessExpression(
factory.getGeneratedNameForNode(importDeclaration.parent.parent.parent),
factory.getGeneratedNameForNode(importDeclaration.parent?.parent?.parent || importDeclaration),
factory.cloneNode(importDeclaration.propertyName || importDeclaration.name)
),
/*location*/ node
Expand Down
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;
}());