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 all commits
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
48 changes: 42 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 isJsxExprContext = contextNode.kind === SyntaxKind.JsxExpression;
emitTrailingCommentsOfPosition(pos, /*prefixSpace*/ !isJsxExprContext, /*forceNoNewline*/ isJsxExprContext);
}
return pos;
}
Expand Down Expand Up @@ -3377,12 +3378,35 @@ namespace ts {
writePunctuation("}");
}

function hasTrailingCommentsAtPosition(pos: number) {
let result = false;
forEachTrailingCommentRange(currentSourceFile?.text || "", pos + 1, () => result = true);
return result;
}

function hasLeadingCommentsAtPosition(pos: number) {
let result = false;
forEachLeadingCommentRange(currentSourceFile?.text || "", pos + 1, () => result = true);
return result;
}

function hasCommentsAtPosition(pos: number) {
return hasTrailingCommentsAtPosition(pos) || hasLeadingCommentsAtPosition(pos);
}

function emitJsxExpression(node: JsxExpression) {
if (node.expression) {
writePunctuation("{");
if (node.expression || (!commentsDisabled && !nodeIsSynthesized(node) && hasCommentsAtPosition(node.pos))) { // preserve empty expressions if they contain comments!
const isMultiline = currentSourceFile && !nodeIsSynthesized(node) && getLineAndCharacterOfPosition(currentSourceFile, node.pos).line !== getLineAndCharacterOfPosition(currentSourceFile, node.end).line;
if (isMultiline) {
writer.increaseIndent();
}
const end = emitTokenWithComment(SyntaxKind.OpenBraceToken, node.pos, writePunctuation, node);
emit(node.dotDotDotToken);
emitExpression(node.expression);
writePunctuation("}");
emitTokenWithComment(SyntaxKind.CloseBraceToken, node.expression?.end || end, writePunctuation, node);
if (isMultiline) {
writer.decreaseIndent();
}
}
}

Expand Down Expand Up @@ -5230,15 +5254,27 @@ 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 emitTrailingCommentOfPositionNoNewline(commentPos: number, commentEnd: number, kind: SyntaxKind) {
// 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);

if (kind === SyntaxKind.SingleLineCommentTrivia) {
writer.writeLine(); // still write a newline for single-line comments, so closing tokens aren't written on the same line
}
}

function emitTrailingCommentOfPosition(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) {
// trailing comments of a position are emitted at /*trailing comment1 */space/*trailing comment*/space

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
12 changes: 7 additions & 5 deletions tests/baselines/reference/commentEmittingInPreserveJsx1.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ var React = require("react");
</div>;
<div>
// Not Comment

{
//Comment just Fine
}
// Another not Comment
</div>;
<div>
// Not Comment
{
//Comment just Fine
"Hi"}
//Comment just Fine
"Hi"}
// Another not Comment
</div>;
<div>
/* Not Comment */
{
//Comment just Fine
"Hi"}
//Comment just Fine
"Hi"}
</div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [commentsOnJSXExpressionsArePreserved.tsx]
// file is intentionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs
namespace JSX {}
class Component {
render() {
return <div>
{/* missing */}
{null/* preserved */}
{
// ??? 1
}
{ // ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */}
</div>;
}
}

//// [commentsOnJSXExpressionsArePreserved.jsx]
var Component = /** @class */ (function () {
function Component() {
}
Component.prototype.render = function () {
return <div>
{/* missing */}
{null /* preserved */}
{
// ??? 1
}
{// ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */ }
</div>;
};
return Component;
}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx ===
// file is intentionally 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))

class Component {
>Component : Symbol(Component, Decl(commentsOnJSXExpressionsArePreserved.tsx, 1, 16))

render() {
>render : Symbol(Component.render, Decl(commentsOnJSXExpressionsArePreserved.tsx, 2, 17))

return <div>
{/* missing */}
{null/* preserved */}
{
// ??? 1
}
{ // ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */}
</div>;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx ===
// file is intentionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs
namespace JSX {}
class Component {
>Component : Component

render() {
>render : () => any

return <div>
><div> {/* missing */} {null/* preserved */} { // ??? 1 } { // ??? 2 } {// ??? 3 } { // ??? 4 /* ??? 5 */} </div> : error
>div : any

{/* missing */}
{null/* preserved */}
>null : null
{
// ??? 1
}
{ // ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */}
</div>;
>div : any
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [commentsOnJSXExpressionsArePreserved.tsx]
// file is intentionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs
namespace JSX {}
class Component {
render() {
return <div>
{/* missing */}
{null/* preserved */}
{
// ??? 1
}
{ // ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */}
</div>;
}
}

//// [commentsOnJSXExpressionsArePreserved.jsx]
var Component = /** @class */ (function () {
function Component() {
}
Component.prototype.render = function () {
return <div>
{/* missing */}
{null /* preserved */}
{
// ??? 1
}
{// ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */ }
</div>;
};
return Component;
}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx ===
// file is intentionally 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))

class Component {
>Component : Symbol(Component, Decl(commentsOnJSXExpressionsArePreserved.tsx, 1, 16))

render() {
>render : Symbol(Component.render, Decl(commentsOnJSXExpressionsArePreserved.tsx, 2, 17))

return <div>
{/* missing */}
{null/* preserved */}
{
// ??? 1
}
{ // ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */}
</div>;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx ===
// file is intentionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs
namespace JSX {}
class Component {
>Component : Component

render() {
>render : () => any

return <div>
><div> {/* missing */} {null/* preserved */} { // ??? 1 } { // ??? 2 } {// ??? 3 } { // ??? 4 /* ??? 5 */} </div> : error
>div : any

{/* missing */}
{null/* preserved */}
>null : null
{
// ??? 1
}
{ // ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */}
</div>;
>div : any
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name 'React'.


==== tests/cases/compiler/commentsOnJSXExpressionsArePreserved.tsx (1 errors) ====
// file is intentionally not a module - this tests for a crash in the module/system transforms alongside the `react-jsx` and `react-jsxdev` outputs
namespace JSX {}
class Component {
render() {
return <div>
~~~
!!! error TS2304: Cannot find name 'React'.
{/* missing */}
{null/* preserved */}
{
// ??? 1
}
{ // ??? 2
}
{// ??? 3
}
{
// ??? 4
/* ??? 5 */}
</div>;
}
}