Skip to content

Do not parse template arguments in JavaScript files. - #36673

Merged
Andrew Branch (andrewbranch) merged 1 commit into
microsoft:masterfrom
mprobst:parse-js
Nov 19, 2020
Merged

Do not parse template arguments in JavaScript files.#36673
Andrew Branch (andrewbranch) merged 1 commit into
microsoft:masterfrom
mprobst:parse-js

Conversation

@mprobst

Copy link
Copy Markdown
Contributor

Fixes#36662.

This is somewhat preliminary. The main drawback of the change is that users will get substantially worse error messages if they accidentally include <Type, Arguments> in JavaScript files (see the associated errors.txt changes).

To mitigate, we could:

  1. keep parsing type arguments in JSX element contexts (<Element<Type, Arg> x=1>). I don't think this is ambiguous with any JS syntax.
  2. attempt to special case and pass down a flag to produce a better error message for code like fnCall<Type, Argument>()

(2) could be tricky to get right enough to really improve developer's life, so I think whether it's worth doing depends on how likely we think the scenario is in the first place.

@fatcerberus

Bruce Pascoe (fatcerberus) commented Feb 9, 2020

Copy link
Copy Markdown

users will get substantially worse error messages if they accidentally include <Type, Arguments> in JavaScript files

Worse, they may get no error message if they accidentally include a single <TypeArgument> (particularly in the case that the type argument has representation in value space, e.g. a class) because that will now be parsed as a less-than operator followed by greater-than operator. Unfortunately there's no heuristic that would be able to tell the difference with 100% accuracy 😦

@mprobst

Copy link
Copy Markdown
ContributorAuthor

Bruce Pascoe (@fatcerberus) ack, but that's fundamentally because in JS, foo<TypeArgument>(bar) has a defined and standardized meaning as comparison operations. I think it's ultimately less confusing to be correct according to the spec here rather than being incorrect with better error messages.

@andrewbranch

Copy link
Copy Markdown
Member

Tentatively filing this as a breaking change even though that’s sort of debatable. Most of the time this is going to be swapping one error for another; very occasionally it will be removing an undesired error (which is the point of the PR, #36662), but as Bruce Pascoe (@fatcerberus) pointed out, there could be a possible scenario where you were getting a desired error (type arguments can’t be used in JS) before, and aren’t now.

I’d like to dig into the specifics of whether that could actually ever happen without getting a new error in the vein of “Operator '<' cannot be applied to types...” I guess if enough of the variables in play are declared as any, that would do the trick.

@andrewbranch

Copy link
Copy Markdown
Member

keep parsing type arguments in JSX element contexts

Also FWIW, I do think this would be an easy win if it’s not ambiguous.

@DanielRosenwasser

Copy link
Copy Markdown
Member

For checkJs, you'll get a bad error - but we can potentially do some gymnastics to figure it out. The real break is for API consumers.

@andrewbranch

Andrew Branch (andrewbranch) commented Apr 1, 2020

Copy link
Copy Markdown
Member

For checkJs, you'll get a bad error

But not when a bunch of stuff is any 😕. This would become error-free: https://www.typescriptlang.org/play?noImplicitAny=false&useJavaScript=true#code/MYGwhgzhAEAqCeAHApgQQE4HMCuBbZAdgC7QDeAvgFCUBm2BwRAlgPYHQ0AUNLLANNABGYdAEoylaFOjpkRbOnY8WAHgQoMOfMQB8nYWIDclKkA

@DanielRosenwasser

Copy link
Copy Markdown
Member

Well shame on you for having anys!

@DanielRosenwasser

Copy link
Copy Markdown
Member

Jokes aside, I think I'd be surprised to see a user write explicit type arguments in a JS file that's unchecked. TypeScript users already see explicit type arguments as a bit of a code smell.

@sandersn

Copy link
Copy Markdown
Member

One possible exception is .js files that are flow-checked. I'm not sure whether there are more of those than of .js files with syntax that is currently parsing as type arguments.

@DanielRosenwasser

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) pack this

@typescript-bot

TypeScript Bot (typescript-bot) commented Apr 3, 2020

Copy link
Copy Markdown
Contributor

Heya Daniel Rosenwasser (@DanielRosenwasser), I've started to run the tarball bundle task on this PR at f308ac1. You can monitor the build here.

@DanielRosenwasser

Copy link
Copy Markdown
Member

Just want to make sure we still give good errors on

  • type parameters function declarations and expressions
  • type parameters arrow functions
  • and type references

Maybe Brian Terlson (@bterlson) can help find examples that are valid JS we also currently have issues with

@DanielRosenwasser

Copy link
Copy Markdown
Member

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 10, 2020

Copy link
Copy Markdown
Contributor

Heya Daniel Rosenwasser (@DanielRosenwasser), I've started to run the parallelized Definitely Typed test suite on this PR at f308ac1. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 10, 2020

Copy link
Copy Markdown
Contributor

Heya Daniel Rosenwasser (@DanielRosenwasser), I've started to run the tarball bundle task on this PR at f308ac1. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 10, 2020

Copy link
Copy Markdown
Contributor

Heya Daniel Rosenwasser (@DanielRosenwasser), I've started to run the parallelized community code test suite on this PR at f308ac1. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 10, 2020

Copy link
Copy Markdown
Contributor

Heya Daniel Rosenwasser (@DanielRosenwasser), I've started to run the perf test suite on this PR at f308ac1. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 10, 2020

Copy link
Copy Markdown
Contributor

Heya Daniel Rosenwasser (@DanielRosenwasser), I've started to run the extended test suite on this PR at f308ac1. You can monitor the build here.

@typescript-bot

Copy link
Copy Markdown
Contributor

Daniel Rosenwasser (@DanielRosenwasser)
The results of the perf run you requested are in!

Here they are:

Comparison Report - master..36673

Metricmaster36673DeltaBestWorst
Angular - node (v10.16.3, x64)
Memory used343,962k (± 0.01%)343,423k (± 0.01%)-540k (- 0.16%)343,333k343,543k
Parse Time2.00s (± 0.50%)2.02s (± 0.70%)+0.01s (+ 0.60%)2.00s2.06s
Bind Time0.81s (± 0.64%)0.82s (± 0.54%)+0.01s (+ 0.86%)0.81s0.83s
Check Time4.73s (± 0.29%)4.74s (± 0.80%)+0.01s (+ 0.23%)4.64s4.79s
Emit Time5.19s (± 0.59%)5.21s (± 1.48%)+0.02s (+ 0.42%)5.12s5.51s
Total Time12.73s (± 0.25%)12.78s (± 0.87%)+0.05s (+ 0.40%)12.65s13.19s
Monaco - node (v10.16.3, x64)
Memory used339,143k (± 0.03%)339,267k (± 0.03%)+124k (+ 0.04%)339,064k339,415k
Parse Time1.58s (± 0.55%)1.58s (± 0.92%)+0.00s (+ 0.32%)1.55s1.61s
Bind Time0.71s (± 0.78%)0.72s (± 0.51%)+0.00s (+ 0.56%)0.71s0.72s
Check Time4.91s (± 0.64%)4.89s (± 0.64%)-0.01s (- 0.29%)4.83s4.95s
Emit Time2.74s (± 0.35%)2.74s (± 0.83%)-0.00s (- 0.11%)2.69s2.80s
Total Time9.95s (± 0.32%)9.94s (± 0.49%)-0.01s (- 0.08%)9.80s10.02s
TFS - node (v10.16.3, x64)
Memory used302,064k (± 0.02%)302,062k (± 0.02%)-2k (- 0.00%)301,968k302,186k
Parse Time1.20s (± 0.68%)1.22s (± 0.67%)+0.01s (+ 1.16%)1.20s1.24s
Bind Time0.66s (± 1.03%)0.67s (± 1.02%)+0.00s (+ 0.15%)0.65s0.68s
Check Time4.39s (± 0.39%)4.42s (± 0.82%)+0.03s (+ 0.78%)4.35s4.49s
Emit Time2.89s (± 0.92%)2.88s (± 0.69%)-0.01s (- 0.48%)2.82s2.91s
Total Time9.15s (± 0.40%)9.18s (± 0.37%)+0.03s (+ 0.34%)9.12s9.26s
material-ui - node (v10.16.3, x64)
Memory used459,445k (± 0.01%)459,143k (± 0.01%)-303k (- 0.07%)459,034k459,262k
Parse Time2.04s (± 0.29%)2.04s (± 0.40%)-0.00s (- 0.05%)2.02s2.06s
Bind Time0.65s (± 1.88%)0.65s (± 1.57%)0.00s ( 0.00%)0.63s0.67s
Check Time12.93s (± 0.92%)12.85s (± 0.78%)-0.07s (- 0.57%)12.72s13.21s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time15.62s (± 0.75%)15.55s (± 0.67%)-0.07s (- 0.46%)15.39s15.92s
Angular - node (v12.1.0, x64)
Memory used320,857k (± 0.13%)320,761k (± 0.02%)-96k (- 0.03%)320,601k320,882k
Parse Time1.98s (± 0.60%)1.99s (± 0.56%)+0.01s (+ 0.71%)1.97s2.02s
Bind Time0.80s (± 1.34%)0.80s (± 0.42%)+0.00s (+ 0.25%)0.80s0.81s
Check Time4.63s (± 0.83%)4.66s (± 0.56%)+0.03s (+ 0.69%)4.62s4.74s
Emit Time5.37s (± 1.29%)5.36s (± 0.64%)-0.01s (- 0.19%)5.31s5.44s
Total Time12.78s (± 0.75%)12.82s (± 0.22%)+0.03s (+ 0.26%)12.77s12.89s
Monaco - node (v12.1.0, x64)
Memory used321,581k (± 0.02%)321,575k (± 0.03%)-6k (- 0.00%)321,394k321,817k
Parse Time1.54s (± 0.99%)1.56s (± 0.48%)+0.02s (+ 1.10%)1.54s1.57s
Bind Time0.69s (± 1.40%)0.69s (± 0.99%)+0.01s (+ 0.87%)0.69s0.72s
Check Time4.65s (± 1.27%)4.68s (± 0.37%)+0.03s (+ 0.62%)4.64s4.73s
Emit Time2.78s (± 1.48%)2.79s (± 0.42%)+0.02s (+ 0.61%)2.77s2.82s
Total Time9.66s (± 1.21%)9.73s (± 0.20%)+0.07s (+ 0.67%)9.68s9.76s
TFS - node (v12.1.0, x64)
Memory used286,569k (± 0.01%)286,574k (± 0.03%)+5k (+ 0.00%)286,459k286,772k
Parse Time1.23s (± 0.74%)1.24s (± 0.66%)+0.01s (+ 0.49%)1.22s1.26s
Bind Time0.64s (± 0.81%)0.64s (± 0.90%)+0.00s (+ 0.63%)0.63s0.65s
Check Time4.30s (± 0.59%)4.32s (± 0.58%)+0.02s (+ 0.44%)4.26s4.38s
Emit Time2.92s (± 0.80%)2.94s (± 1.25%)+0.02s (+ 0.82%)2.89s3.05s
Total Time9.09s (± 0.46%)9.14s (± 0.38%)+0.05s (+ 0.57%)9.07s9.22s
material-ui - node (v12.1.0, x64)
Memory used437,822k (± 0.01%)437,380k (± 0.07%)-441k (- 0.10%)436,236k437,679k
Parse Time2.04s (± 0.88%)2.04s (± 0.29%)-0.00s (- 0.10%)2.02s2.05s
Bind Time0.64s (± 0.94%)0.63s (± 0.82%)-0.00s (- 0.63%)0.62s0.64s
Check Time11.65s (± 0.72%)11.61s (± 0.79%)-0.04s (- 0.31%)11.43s11.85s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time14.32s (± 0.55%)14.28s (± 0.66%)-0.04s (- 0.29%)14.08s14.52s
Angular - node (v8.9.0, x64)
Memory used340,444k (± 0.01%)340,091k (± 0.01%)-352k (- 0.10%)339,972k340,191k
Parse Time2.53s (± 0.27%)2.53s (± 0.37%)+0.00s (+ 0.04%)2.51s2.54s
Bind Time0.85s (± 0.58%)0.85s (± 0.70%)-0.01s (- 0.94%)0.83s0.86s
Check Time5.35s (± 0.46%)5.36s (± 0.52%)+0.01s (+ 0.09%)5.30s5.42s
Emit Time5.90s (± 1.08%)5.96s (± 1.13%)+0.06s (+ 0.97%)5.87s6.15s
Total Time14.64s (± 0.51%)14.69s (± 0.46%)+0.05s (+ 0.37%)14.57s14.84s
Monaco - node (v8.9.0, x64)
Memory used340,492k (± 0.01%)340,497k (± 0.01%)+5k (+ 0.00%)340,379k340,609k
Parse Time1.87s (± 0.43%)1.87s (± 0.40%)+0.00s (+ 0.11%)1.86s1.89s
Bind Time0.88s (± 0.25%)0.88s (± 0.77%)+0.00s (+ 0.34%)0.87s0.90s
Check Time5.40s (± 0.62%)5.41s (± 0.43%)+0.01s (+ 0.19%)5.36s5.45s
Emit Time3.22s (± 0.37%)3.23s (± 0.56%)+0.01s (+ 0.25%)3.19s3.27s
Total Time11.37s (± 0.36%)11.40s (± 0.37%)+0.03s (+ 0.23%)11.33s11.47s
TFS - node (v8.9.0, x64)
Memory used303,833k (± 0.02%)303,847k (± 0.02%)+15k (+ 0.00%)303,752k303,972k
Parse Time1.54s (± 0.48%)1.54s (± 0.53%)-0.00s (- 0.00%)1.53s1.56s
Bind Time0.67s (± 0.77%)0.67s (± 0.71%)-0.00s (- 0.60%)0.66s0.68s
Check Time5.03s (± 1.38%)4.94s (± 0.95%)-0.09s (- 1.79%)4.85s5.09s
Emit Time3.03s (± 2.64%)3.13s (± 1.85%)+0.10s (+ 3.14%)2.91s3.22s
Total Time10.27s (± 0.35%)10.28s (± 0.39%)+0.00s (+ 0.03%)10.21s10.39s
material-ui - node (v8.9.0, x64)
Memory used463,640k (± 0.01%)463,426k (± 0.01%)-214k (- 0.05%)463,334k463,499k
Parse Time2.41s (± 0.34%)2.40s (± 0.70%)-0.02s (- 0.75%)2.36s2.43s
Bind Time0.78s (± 0.85%)0.77s (± 0.96%)-0.01s (- 1.66%)0.76s0.79s
Check Time17.19s (± 1.33%)17.11s (± 1.13%)-0.08s (- 0.46%)16.59s17.49s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time20.39s (± 1.12%)20.28s (± 1.02%)-0.11s (- 0.53%)19.73s20.71s
Angular - node (v8.9.0, x86)
Memory used195,323k (± 0.03%)195,070k (± 0.01%)-253k (- 0.13%)195,004k195,116k
Parse Time2.45s (± 0.82%)2.47s (± 1.17%)+0.02s (+ 0.82%)2.42s2.56s
Bind Time0.99s (± 0.62%)0.99s (± 0.66%)-0.00s (- 0.10%)0.97s1.00s
Check Time4.83s (± 0.84%)4.87s (± 1.02%)+0.03s (+ 0.68%)4.79s5.01s
Emit Time5.95s (± 1.07%)5.93s (± 1.13%)-0.02s (- 0.27%)5.77s6.08s
Total Time14.22s (± 0.67%)14.26s (± 0.82%)+0.04s (+ 0.26%)14.06s14.62s
Monaco - node (v8.9.0, x86)
Memory used193,511k (± 0.02%)193,501k (± 0.03%)-10k (- 0.01%)193,355k193,640k
Parse Time1.92s (± 0.99%)1.91s (± 0.46%)-0.02s (- 0.88%)1.88s1.92s
Bind Time0.70s (± 0.43%)0.70s (± 1.43%)+0.01s (+ 0.86%)0.69s0.74s
Check Time5.50s (± 0.48%)5.49s (± 0.47%)-0.01s (- 0.18%)5.43s5.55s
Emit Time2.68s (± 0.65%)2.68s (± 0.96%)+0.00s (+ 0.11%)2.63s2.74s
Total Time10.80s (± 0.38%)10.78s (± 0.40%)-0.02s (- 0.17%)10.66s10.86s
TFS - node (v8.9.0, x86)
Memory used173,771k (± 0.01%)173,763k (± 0.02%)-8k (- 0.00%)173,678k173,874k
Parse Time1.58s (± 0.67%)1.61s (± 1.15%)+0.03s (+ 2.03%)1.57s1.65s
Bind Time0.64s (± 1.01%)0.65s (± 1.05%)+0.01s (+ 1.25%)0.64s0.67s
Check Time4.68s (± 0.79%)4.69s (± 0.55%)+0.01s (+ 0.15%)4.63s4.75s
Emit Time2.80s (± 1.33%)2.82s (± 1.10%)+0.02s (+ 0.54%)2.73s2.87s
Total Time9.70s (± 0.56%)9.76s (± 0.52%)+0.06s (+ 0.60%)9.63s9.89s
material-ui - node (v8.9.0, x86)
Memory used262,544k (± 0.02%)262,408k (± 0.02%)-136k (- 0.05%)262,299k262,499k
Parse Time2.45s (± 0.47%)2.47s (± 0.78%)+0.03s (+ 1.06%)2.44s2.53s
Bind Time0.67s (± 1.91%)0.67s (± 1.26%)+0.00s (+ 0.45%)0.66s0.69s
Check Time15.67s (± 0.51%)15.66s (± 0.74%)-0.01s (- 0.06%)15.45s15.91s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time18.79s (± 0.40%)18.80s (± 0.57%)+0.02s (+ 0.09%)18.60s19.02s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-166-generic
Architecturex64
Available Memory16 GB
Available Memory1 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)
  • 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)
BenchmarkNameIterations
Current3667310
Baselinemaster10

@DanielRosenwasserDaniel Rosenwasser (DanielRosenwasser) added the Domain: API Relates to the public API for TypeScript label Nov 19, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your patience, everyone. I think we should take this 🌟

@DanielRosenwasser

Copy link
Copy Markdown
Member

Marking this with the API label just because if you were ever using TS to parse Flow-y code, you're now going to have issues.

@andrewbranch
Andrew Branch (andrewbranch) merged commit 6b04f50 into microsoft:masterNov 19, 2020
@mprobst
Martin Probst (mprobst) deleted the parse-js branch June 15, 2021 10:08
@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Breaking ChangeWould introduce errors in existing codeDomain: APIRelates to the public API for TypeScriptFor Backlog BugPRs that fix a backlog bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

JS value being parsed as a TS type argument

6 participants

@mprobst@fatcerberus@andrewbranch@DanielRosenwasser@sandersn@typescript-bot