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

Conversation

weswigham
Copy link
Member

And, perhaps more importantly, fixes a crash caused by the interaction of the module or system transformers and the new jsx transforms discovered along the way (in the same test case).

Fixes #41754

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Dec 1, 2020
@@ -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.

@@ -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).

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

Seems reasonable although (1) I'm not an expert here (2) I'd like to know what happens for

{
// comment
}

maybe this is nonsensical for JSX though.

@@ -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

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?

src/compiler/emitter.ts Outdated Show resolved Hide resolved
@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Dec 8, 2020
@weswigham weswigham force-pushed the fix-jsx-preserve-comment-emit branch from e358fc9 to d1db247 Compare December 8, 2020 20:16
@weswigham
Copy link
Member Author

@sandersn since nobody else weighed in, I opted to change this to do a full comment scan. It's ready for another look.

@typescript-bot perf test this - this hopefully doesn't hurt material-ui emit time too bad.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 8, 2020

Heya @weswigham, I've started to run the perf test suite on this PR at d1db247. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

@weswigham
The results of the perf run you requested are in!

Here they are:

Comparison Report - master..41757

Metric master 41757 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 344,311k (± 0.04%) 344,219k (± 0.02%) -92k (- 0.03%) 344,066k 344,345k
Parse Time 1.98s (± 0.54%) 1.98s (± 0.56%) +0.00s (+ 0.20%) 1.96s 2.02s
Bind Time 0.83s (± 0.67%) 0.83s (± 0.44%) -0.01s (- 0.72%) 0.82s 0.83s
Check Time 4.94s (± 0.47%) 4.92s (± 0.39%) -0.02s (- 0.45%) 4.87s 4.97s
Emit Time 5.32s (± 0.69%) 5.31s (± 0.56%) -0.01s (- 0.23%) 5.20s 5.36s
Total Time 13.07s (± 0.43%) 13.03s (± 0.25%) -0.03s (- 0.27%) 12.95s 13.11s
Compiler-Unions - node (v10.16.3, x64)
Memory used 205,265k (± 0.06%) 205,298k (± 0.03%) +34k (+ 0.02%) 205,152k 205,411k
Parse Time 0.79s (± 0.87%) 0.79s (± 0.63%) -0.00s (- 0.13%) 0.78s 0.80s
Bind Time 0.50s (± 1.04%) 0.49s (± 1.01%) -0.00s (- 0.80%) 0.48s 0.50s
Check Time 12.01s (± 0.96%) 11.95s (± 0.61%) -0.06s (- 0.51%) 11.80s 12.14s
Emit Time 2.33s (± 0.97%) 2.32s (± 1.20%) -0.00s (- 0.21%) 2.25s 2.39s
Total Time 15.62s (± 0.76%) 15.55s (± 0.42%) -0.07s (- 0.45%) 15.43s 15.71s
Monaco - node (v10.16.3, x64)
Memory used 354,897k (± 0.02%) 354,944k (± 0.03%) +47k (+ 0.01%) 354,659k 355,062k
Parse Time 1.60s (± 0.64%) 1.60s (± 0.45%) +0.00s (+ 0.19%) 1.59s 1.62s
Bind Time 0.72s (± 0.77%) 0.73s (± 0.61%) +0.01s (+ 0.83%) 0.72s 0.74s
Check Time 5.12s (± 0.66%) 5.11s (± 0.36%) -0.01s (- 0.16%) 5.07s 5.15s
Emit Time 2.79s (± 0.64%) 2.80s (± 0.48%) +0.00s (+ 0.14%) 2.78s 2.84s
Total Time 10.23s (± 0.52%) 10.24s (± 0.21%) +0.01s (+ 0.06%) 10.17s 10.27s
TFS - node (v10.16.3, x64)
Memory used 307,938k (± 0.02%) 307,943k (± 0.02%) +5k (+ 0.00%) 307,761k 308,116k
Parse Time 1.24s (± 0.47%) 1.24s (± 0.48%) -0.00s (- 0.08%) 1.22s 1.25s
Bind Time 0.68s (± 1.22%) 0.68s (± 0.69%) +0.00s (+ 0.59%) 0.67s 0.69s
Check Time 4.59s (± 0.81%) 4.59s (± 0.81%) +0.01s (+ 0.15%) 4.50s 4.66s
Emit Time 2.94s (± 0.65%) 2.92s (± 0.94%) -0.02s (- 0.65%) 2.86s 2.99s
Total Time 9.44s (± 0.51%) 9.43s (± 0.60%) -0.01s (- 0.12%) 9.29s 9.53s
material-ui - node (v10.16.3, x64)
Memory used 489,337k (± 0.01%) 489,373k (± 0.01%) +36k (+ 0.01%) 489,268k 489,487k
Parse Time 2.06s (± 0.35%) 2.06s (± 0.32%) -0.00s (- 0.10%) 2.04s 2.07s
Bind Time 0.65s (± 0.69%) 0.65s (± 0.80%) +0.00s (+ 0.46%) 0.64s 0.66s
Check Time 13.53s (± 0.53%) 13.49s (± 0.42%) -0.04s (- 0.29%) 13.33s 13.58s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 16.24s (± 0.44%) 16.20s (± 0.33%) -0.04s (- 0.24%) 16.05s 16.28s
Angular - node (v12.1.0, x64)
Memory used 322,148k (± 0.02%) 322,169k (± 0.02%) +21k (+ 0.01%) 322,057k 322,280k
Parse Time 1.97s (± 0.49%) 1.96s (± 0.64%) -0.01s (- 0.41%) 1.93s 1.99s
Bind Time 0.81s (± 0.80%) 0.81s (± 0.55%) +0.00s (+ 0.00%) 0.80s 0.82s
Check Time 4.86s (± 0.63%) 4.84s (± 0.47%) -0.03s (- 0.58%) 4.79s 4.89s
Emit Time 5.50s (± 0.67%) 5.48s (± 0.54%) -0.02s (- 0.31%) 5.43s 5.56s
Total Time 13.14s (± 0.46%) 13.08s (± 0.29%) -0.05s (- 0.42%) 13.01s 13.19s
Compiler-Unions - node (v12.1.0, x64)
Memory used 191,441k (± 0.06%) 191,388k (± 0.08%) -52k (- 0.03%) 190,877k 191,586k
Parse Time 0.78s (± 0.90%) 0.78s (± 1.07%) -0.00s (- 0.51%) 0.76s 0.79s
Bind Time 0.50s (± 0.89%) 0.50s (± 0.99%) -0.00s (- 0.40%) 0.49s 0.51s
Check Time 10.83s (± 0.87%) 10.60s (± 0.75%) -0.23s (- 2.11%) 10.43s 10.82s
Emit Time 2.38s (± 1.22%) 2.39s (± 1.36%) +0.01s (+ 0.42%) 2.32s 2.47s
Total Time 14.49s (± 0.72%) 14.26s (± 0.63%) -0.23s (- 1.58%) 14.01s 14.44s
Monaco - node (v12.1.0, x64)
Memory used 337,042k (± 0.01%) 337,013k (± 0.01%) -29k (- 0.01%) 336,951k 337,073k
Parse Time 1.58s (± 0.80%) 1.58s (± 0.61%) +0.00s (+ 0.25%) 1.56s 1.60s
Bind Time 0.71s (± 0.87%) 0.71s (± 0.97%) -0.00s (- 0.56%) 0.69s 0.72s
Check Time 4.88s (± 0.39%) 4.90s (± 0.34%) +0.02s (+ 0.43%) 4.87s 4.95s
Emit Time 2.86s (± 0.56%) 2.88s (± 0.97%) +0.02s (+ 0.67%) 2.83s 2.95s
Total Time 10.02s (± 0.32%) 10.06s (± 0.45%) +0.04s (+ 0.39%) 9.98s 10.18s
TFS - node (v12.1.0, x64)
Memory used 292,168k (± 0.02%) 292,205k (± 0.01%) +37k (+ 0.01%) 292,111k 292,295k
Parse Time 1.25s (± 0.60%) 1.25s (± 1.68%) +0.01s (+ 0.56%) 1.23s 1.33s
Bind Time 0.66s (± 0.76%) 0.66s (± 1.04%) +0.00s (+ 0.15%) 0.65s 0.68s
Check Time 4.48s (± 0.49%) 4.51s (± 1.07%) +0.03s (+ 0.62%) 4.46s 4.67s
Emit Time 2.94s (± 0.92%) 2.99s (± 1.08%) +0.05s (+ 1.60%) 2.92s 3.06s
Total Time 9.32s (± 0.40%) 9.41s (± 0.79%) +0.08s (+ 0.88%) 9.29s 9.67s
material-ui - node (v12.1.0, x64)
Memory used 467,410k (± 0.02%) 467,363k (± 0.06%) -46k (- 0.01%) 466,300k 467,643k
Parse Time 2.07s (± 0.56%) 2.08s (± 0.44%) +0.01s (+ 0.34%) 2.06s 2.10s
Bind Time 0.65s (± 1.23%) 0.64s (± 0.81%) -0.01s (- 1.39%) 0.63s 0.65s
Check Time 12.03s (± 0.90%) 12.01s (± 0.99%) -0.01s (- 0.08%) 11.87s 12.47s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 14.75s (± 0.75%) 14.73s (± 0.81%) -0.01s (- 0.09%) 14.59s 15.19s
Angular - node (v8.9.0, x64)
Memory used 346,850k (± 0.02%) 346,940k (± 0.02%) +90k (+ 0.03%) 346,818k 347,079k
Parse Time 2.50s (± 0.38%) 2.51s (± 0.71%) +0.00s (+ 0.20%) 2.48s 2.55s
Bind Time 0.87s (± 0.79%) 0.86s (± 0.52%) -0.00s (- 0.46%) 0.85s 0.87s
Check Time 5.58s (± 0.35%) 5.58s (± 0.46%) +0.00s (+ 0.09%) 5.54s 5.65s
Emit Time 6.33s (± 1.28%) 6.41s (± 1.35%) +0.08s (+ 1.26%) 6.27s 6.65s
Total Time 15.27s (± 0.47%) 15.36s (± 0.70%) +0.09s (+ 0.57%) 15.20s 15.70s
Compiler-Unions - node (v8.9.0, x64)
Memory used 212,931k (± 0.02%) 212,970k (± 0.02%) +39k (+ 0.02%) 212,848k 213,055k
Parse Time 0.95s (± 0.42%) 0.94s (± 0.81%) -0.01s (- 0.63%) 0.93s 0.96s
Bind Time 0.57s (± 0.97%) 0.57s (± 1.04%) +0.00s (+ 0.35%) 0.56s 0.59s
Check Time 14.67s (± 0.77%) 14.55s (± 0.30%) -0.12s (- 0.83%) 14.45s 14.61s
Emit Time 2.76s (± 1.46%) 2.73s (± 2.00%) -0.03s (- 1.23%) 2.60s 2.82s
Total Time 18.96s (± 0.69%) 18.80s (± 0.47%) -0.16s (- 0.85%) 18.61s 18.95s
Monaco - node (v8.9.0, x64)
Memory used 358,795k (± 0.01%) 358,740k (± 0.02%) -55k (- 0.02%) 358,631k 358,892k
Parse Time 1.93s (± 0.50%) 1.93s (± 0.47%) -0.00s (- 0.10%) 1.92s 1.96s
Bind Time 0.90s (± 0.76%) 0.91s (± 0.75%) +0.00s (+ 0.11%) 0.89s 0.92s
Check Time 5.65s (± 0.51%) 5.65s (± 0.50%) -0.00s (- 0.07%) 5.59s 5.70s
Emit Time 3.40s (± 0.87%) 3.40s (± 0.36%) -0.00s (- 0.09%) 3.37s 3.42s
Total Time 11.89s (± 0.37%) 11.88s (± 0.34%) -0.00s (- 0.03%) 11.80s 11.98s
TFS - node (v8.9.0, x64)
Memory used 310,499k (± 0.02%) 310,485k (± 0.02%) -14k (- 0.00%) 310,350k 310,634k
Parse Time 1.56s (± 0.48%) 1.57s (± 0.44%) +0.00s (+ 0.13%) 1.55s 1.58s
Bind Time 0.68s (± 0.59%) 0.68s (± 0.87%) +0.00s (+ 0.59%) 0.67s 0.70s
Check Time 5.32s (± 0.67%) 5.30s (± 0.57%) -0.02s (- 0.39%) 5.24s 5.39s
Emit Time 2.99s (± 1.32%) 2.98s (± 0.76%) -0.01s (- 0.23%) 2.92s 3.03s
Total Time 10.55s (± 0.53%) 10.53s (± 0.44%) -0.02s (- 0.22%) 10.46s 10.68s
material-ui - node (v8.9.0, x64)
Memory used 496,417k (± 0.01%) 496,476k (± 0.01%) +59k (+ 0.01%) 496,394k 496,551k
Parse Time 2.49s (± 0.61%) 2.49s (± 0.45%) -0.00s (- 0.12%) 2.47s 2.51s
Bind Time 0.81s (± 1.04%) 0.80s (± 1.25%) -0.00s (- 0.62%) 0.79s 0.83s
Check Time 18.14s (± 0.63%) 18.06s (± 0.39%) -0.09s (- 0.49%) 17.87s 18.26s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 21.45s (± 0.59%) 21.35s (± 0.34%) -0.10s (- 0.46%) 21.18s 21.56s
Angular - node (v8.9.0, x86)
Memory used 198,883k (± 0.02%) 198,897k (± 0.02%) +14k (+ 0.01%) 198,776k 199,027k
Parse Time 2.42s (± 0.57%) 2.42s (± 0.54%) +0.00s (+ 0.08%) 2.40s 2.45s
Bind Time 1.01s (± 1.15%) 1.01s (± 0.75%) 0.00s ( 0.00%) 1.00s 1.03s
Check Time 5.00s (± 0.61%) 5.01s (± 0.48%) +0.01s (+ 0.14%) 4.95s 5.07s
Emit Time 6.13s (± 0.57%) 6.18s (± 1.44%) +0.05s (+ 0.82%) 5.95s 6.38s
Total Time 14.56s (± 0.46%) 14.62s (± 0.64%) +0.06s (+ 0.42%) 14.47s 14.84s
Compiler-Unions - node (v8.9.0, x86)
Memory used 127,994k (± 0.02%) 128,010k (± 0.05%) +15k (+ 0.01%) 127,846k 128,117k
Parse Time 0.97s (± 1.10%) 0.97s (± 0.62%) -0.00s (- 0.10%) 0.95s 0.98s
Bind Time 0.50s (± 1.20%) 0.49s (± 1.43%) -0.01s (- 1.01%) 0.48s 0.51s
Check Time 13.69s (± 0.60%) 13.62s (± 0.38%) -0.07s (- 0.53%) 13.52s 13.75s
Emit Time 2.63s (± 0.92%) 2.63s (± 1.07%) -0.00s (- 0.11%) 2.57s 2.70s
Total Time 17.79s (± 0.49%) 17.71s (± 0.41%) -0.08s (- 0.46%) 17.55s 17.86s
Monaco - node (v8.9.0, x86)
Memory used 203,198k (± 0.03%) 203,190k (± 0.02%) -7k (- 0.00%) 203,077k 203,260k
Parse Time 1.97s (± 0.72%) 1.99s (± 1.39%) +0.01s (+ 0.66%) 1.94s 2.05s
Bind Time 0.72s (± 1.06%) 0.71s (± 0.69%) -0.00s (- 0.28%) 0.71s 0.73s
Check Time 5.77s (± 0.48%) 5.78s (± 0.46%) +0.01s (+ 0.17%) 5.71s 5.83s
Emit Time 2.75s (± 0.87%) 2.75s (± 0.42%) -0.00s (- 0.15%) 2.73s 2.78s
Total Time 11.22s (± 0.35%) 11.23s (± 0.50%) +0.01s (+ 0.13%) 11.11s 11.37s
TFS - node (v8.9.0, x86)
Memory used 177,597k (± 0.02%) 177,620k (± 0.02%) +23k (+ 0.01%) 177,537k 177,701k
Parse Time 1.61s (± 0.95%) 1.61s (± 0.59%) +0.01s (+ 0.44%) 1.60s 1.64s
Bind Time 0.65s (± 0.69%) 0.65s (± 0.91%) +0.01s (+ 0.93%) 0.64s 0.67s
Check Time 4.87s (± 0.58%) 4.87s (± 0.82%) +0.00s (+ 0.06%) 4.80s 5.00s
Emit Time 2.83s (± 0.95%) 2.81s (± 0.83%) -0.01s (- 0.46%) 2.76s 2.87s
Total Time 9.95s (± 0.43%) 9.95s (± 0.57%) +0.00s (+ 0.02%) 9.88s 10.15s
material-ui - node (v8.9.0, x86)
Memory used 279,509k (± 0.02%) 279,527k (± 0.01%) +18k (+ 0.01%) 279,387k 279,591k
Parse Time 2.54s (± 0.73%) 2.54s (± 0.58%) -0.00s (- 0.08%) 2.52s 2.57s
Bind Time 0.73s (± 4.02%) 0.72s (± 5.20%) -0.01s (- 1.51%) 0.68s 0.86s
Check Time 16.46s (± 0.50%) 16.55s (± 0.66%) +0.09s (+ 0.56%) 16.36s 16.86s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 19.73s (± 0.52%) 19.81s (± 0.65%) +0.08s (+ 0.40%) 19.63s 20.11s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-197-generic
Architecturex64
Available Memory16 GB
Available Memory11 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
  • node (v8.9.0, x64)
  • node (v8.9.0, x86)
Scenarios
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Angular - node (v8.9.0, x64)
  • Angular - node (v8.9.0, x86)
  • Compiler-Unions - node (v10.16.3, x64)
  • Compiler-Unions - node (v12.1.0, x64)
  • Compiler-Unions - node (v8.9.0, x64)
  • Compiler-Unions - node (v8.9.0, x86)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • Monaco - node (v8.9.0, x64)
  • Monaco - node (v8.9.0, x86)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • TFS - node (v8.9.0, x64)
  • TFS - node (v8.9.0, x86)
  • material-ui - node (v10.16.3, x64)
  • material-ui - node (v12.1.0, x64)
  • material-ui - node (v8.9.0, x64)
  • material-ui - node (v8.9.0, x86)
Benchmark Name Iterations
Current 41757 10
Baseline master 10

@weswigham
Copy link
Member Author

Eyyy, and we don't have emit on for material-ui in the perf suite anyway. Nice. Well, it shouldn't be too bad - line information is cached on the source file, and comment iteration is simple character matching.

@sandersn sandersn added this to Not started in PR Backlog Dec 15, 2020
PR Backlog automation moved this from Not started to Needs merge Dec 18, 2020
@weswigham weswigham merged commit c3ff0d4 into microsoft:master Dec 18, 2020
PR Backlog automation moved this from Needs merge to Done Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
PR Backlog
  
Done
Development

Successfully merging this pull request may close these issues.

Comments within JSX Expressions are not preserved
3 participants