Skip to content

Use evaluator for isolatedModules enum restrictions - #57966

Merged
Andrew Branch (andrewbranch) merged 6 commits into
microsoft:mainfrom
andrewbranch:bug/isolated-modules-enum-restrictions
Apr 4, 2024
Merged

Use evaluator for isolatedModules enum restrictions#57966
Andrew Branch (andrewbranch) merged 6 commits into
microsoft:mainfrom
andrewbranch:bug/isolated-modules-enum-restrictions

Conversation

@andrewbranch

@andrewbranchAndrew Branch (andrewbranch) commented Mar 27, 2024

Copy link
Copy Markdown
Member

With #57914, it becomes possible to enable isolatedModules in our own codebase. I tried this, but there were a few inconvenient changes required to enum declarations due to #56736. You can see them here: https://github.com/andrewbranch/TypeScript/compare/bug/37774...andrewbranch:TypeScript:enable-isolatedModules?expand=1

The checks added in #56736 seem to be a bit too restrictive. Specifically, references to same-file constants and other enum members were not recognized. I refactored the evaluator to return extra information that lets us produce the same errors with higher fidelity to the capabilities of the evaluator. It seems that transpilers (tested esbuild and Babel) have built their behavior around what our own evaluator can do; they’re just obviously limited to analyzing a single file (when not bundling). So, this PR removes isolatedModules errors from a lot of code that transpiles perfectly fine.

(cc Jan Kühle (@frigus02))

@jakebailey

Copy link
Copy Markdown
Member

The worst PR race; we just merged #57955 which moved this code.

@andrewbranch

Copy link
Copy Markdown
MemberAuthor

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 27, 2024

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

CommandStatusResults
test top400✅ Started✅ Results
user test this✅ Started✅ Results
run dt✅ Started✅ Results
perf test this faster✅ Started👀 Results

@typescript-bot

Copy link
Copy Markdown
Contributor

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure Daniel Rosenwasser (@DanielRosenwasser) and Ryan Cavanaugh (@RyanCavanaugh) are aware of the changes, just as a heads up.

@jakebailey

Copy link
Copy Markdown
Member

Uh oh, the last merge just broke the PR a lot

@andrewbranch

Copy link
Copy Markdown
MemberAuthor

Yeah I was not on the branch I thought I was on 🤦🏻‍♂️

@andrewbranch
Andrew Branch (andrewbranch)force-pushed the bug/isolated-modules-enum-restrictions branch from 2d4e834 to 2035fc6CompareMarch 27, 2024 23:21
@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Andrew Branch (@andrewbranch), the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot

Copy link
Copy Markdown
Contributor

Andrew Branch (@andrewbranch) Here are the results of running the user tests comparing main and refs/pull/57966/merge:

Everything looks good!

@typescript-bot

Copy link
Copy Markdown
Contributor

Andrew Branch (@andrewbranch)
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Angular - node (v18.15.0, x64)
Memory used295,692k (± 0.01%)295,678k (± 0.00%)~295,663k295,686kp=0.936 n=6
Parse Time2.66s (± 0.24%)2.66s (± 0.31%)~2.65s2.67sp=0.432 n=6
Bind Time0.83s (± 0.62%)0.83s (± 0.00%)~0.83s0.83sp=0.174 n=6
Check Time8.24s (± 0.26%)8.25s (± 0.13%)~8.23s8.26sp=1.000 n=6
Emit Time7.02s (± 0.19%)7.05s (± 0.39%)~7.00s7.07sp=0.142 n=6
Total Time18.76s (± 0.11%)18.78s (± 0.11%)~18.75s18.81sp=0.091 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used195,095k (± 0.68%)193,283k (± 0.97%)~192,035k195,817kp=0.230 n=6
Parse Time1.65s (± 0.99%)1.66s (± 1.32%)~1.64s1.70sp=0.805 n=6
Bind Time0.88s (± 1.18%)0.88s (± 1.33%)~0.86s0.89sp=0.801 n=6
Check Time11.27s (± 0.47%)11.22s (± 0.37%)~11.18s11.28sp=0.107 n=6
Emit Time3.16s (± 0.47%)3.13s (± 0.72%)~3.11s3.17sp=0.089 n=6
Total Time16.96s (± 0.19%)16.89s (± 0.29%)-0.07s (- 0.40%)16.83s16.98sp=0.045 n=6
Monaco - node (v18.15.0, x64)
Memory used347,421k (± 0.01%)347,517k (± 0.00%)+96k (+ 0.03%)347,492k347,537kp=0.005 n=6
Parse Time3.68s (± 1.37%)3.67s (± 0.72%)~3.64s3.71sp=1.000 n=6
Bind Time1.38s (± 2.35%)1.38s (± 0.59%)~1.37s1.39sp=1.000 n=6
Check Time10.23s (± 0.48%)10.24s (± 0.46%)~10.16s10.29sp=0.630 n=6
Emit Time6.03s (± 0.49%)6.02s (± 0.60%)~5.98s6.06sp=0.627 n=6
Total Time21.31s (± 0.27%)21.30s (± 0.25%)~21.20s21.34sp=0.809 n=6
TFS - node (v18.15.0, x64)
Memory used302,767k (± 0.01%)302,797k (± 0.01%)+30k (+ 0.01%)302,761k302,812kp=0.037 n=6
Parse Time2.42s (± 0.85%)2.39s (± 0.89%)-0.04s (- 1.44%)2.36s2.42sp=0.034 n=6
Bind Time1.23s (± 1.08%)1.19s (± 0.34%)🟩-0.04s (- 3.26%)1.18s1.19sp=0.003 n=6
Check Time7.51s (± 0.40%)7.51s (± 0.22%)~7.48s7.53sp=0.871 n=6
Emit Time4.26s (± 0.76%)4.26s (± 0.75%)~4.22s4.31sp=0.809 n=6
Total Time15.42s (± 0.25%)15.35s (± 0.26%)-0.07s (- 0.48%)15.30s15.41sp=0.020 n=6
material-ui - node (v18.15.0, x64)
Memory used510,066k (± 0.01%)510,143k (± 0.01%)+77k (+ 0.02%)510,078k510,231kp=0.045 n=6
Parse Time3.18s (± 0.44%)3.19s (± 0.32%)~3.17s3.20sp=0.410 n=6
Bind Time1.18s (± 1.07%)1.18s (± 0.44%)~1.18s1.19sp=0.448 n=6
Check Time20.56s (± 0.38%)20.56s (± 0.25%)~20.46s20.60sp=0.872 n=6
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=6
Total Time24.92s (± 0.34%)24.94s (± 0.20%)~24.84s24.97sp=0.415 n=6
mui-docs - node (v18.15.0, x64)
Memory used1,744,450k (± 0.00%)1,744,496k (± 0.00%)+46k (+ 0.00%)1,744,448k1,744,544kp=0.045 n=6
Parse Time9.61s (± 0.60%)9.62s (± 0.73%)~9.51s9.70sp=0.748 n=6
Bind Time3.47s (± 1.71%)3.48s (± 0.60%)~3.44s3.50sp=0.872 n=6
Check Time82.18s (± 0.54%)81.78s (± 0.47%)~81.43s82.48sp=0.199 n=6
Emit Time0.19s (± 0.00%)0.19s (± 4.22%)~0.19s0.21sp=0.405 n=6
Total Time95.45s (± 0.49%)95.06s (± 0.43%)~94.62s95.76sp=0.199 n=6
self-build-src - node (v18.15.0, x64)
Memory used2,396,011k (± 0.04%)2,397,757k (± 0.03%)+1,746k (+ 0.07%)2,396,700k2,398,381kp=0.013 n=6
Parse Time5.96s (± 0.72%)6.04s (± 0.79%)+0.07s (+ 1.26%)5.99s6.10sp=0.005 n=6
Bind Time2.25s (± 0.83%)2.27s (± 0.87%)~2.24s2.30sp=0.568 n=6
Check Time39.82s (± 0.42%)39.74s (± 0.26%)~39.61s39.85sp=0.521 n=6
Emit Time3.15s (± 1.41%)3.17s (± 3.66%)~3.07s3.39sp=1.000 n=6
Total Time51.22s (± 0.35%)51.24s (± 0.44%)~51.00s51.64sp=0.936 n=6
self-compiler - node (v18.15.0, x64)
Memory used415,832k (± 0.01%)416,010k (± 0.01%)+178k (+ 0.04%)415,940k416,053kp=0.005 n=6
Parse Time3.34s (± 1.01%)3.33s (± 0.48%)~3.31s3.35sp=0.568 n=6
Bind Time1.30s (± 0.94%)1.29s (± 0.69%)~1.28s1.30sp=0.445 n=6
Check Time18.21s (± 0.25%)18.24s (± 0.32%)~18.17s18.33sp=0.463 n=6
Emit Time1.36s (± 0.72%)1.36s (± 0.89%)~1.34s1.37sp=0.498 n=6
Total Time24.21s (± 0.21%)24.21s (± 0.27%)~24.11s24.31sp=0.627 n=6
vscode - node (v18.15.0, x64)
Memory used2,896,320k (± 0.00%)2,896,637k (± 0.00%)+317k (+ 0.01%)2,896,566k2,896,778kp=0.005 n=6
Parse Time15.95s (± 0.29%)15.96s (± 0.28%)~15.91s16.04sp=0.687 n=6
Bind Time5.06s (± 0.59%)5.05s (± 0.69%)~5.01s5.10sp=0.808 n=6
Check Time87.48s (± 0.43%)87.59s (± 0.49%)~87.19s88.42sp=0.936 n=6
Emit Time23.77s (± 0.54%)23.69s (± 0.80%)~23.43s23.90sp=0.470 n=6
Total Time132.26s (± 0.32%)132.30s (± 0.30%)~131.83s133.03sp=0.810 n=6
webpack - node (v18.15.0, x64)
Memory used408,950k (± 0.03%)408,855k (± 0.02%)~408,752k409,039kp=0.173 n=6
Parse Time3.91s (± 0.65%)3.89s (± 0.73%)~3.85s3.93sp=0.197 n=6
Bind Time1.69s (± 0.31%)1.68s (± 0.72%)~1.66s1.69sp=0.142 n=6
Check Time16.81s (± 0.10%)16.81s (± 0.25%)~16.74s16.85sp=0.372 n=6
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=6
Total Time22.40s (± 0.16%)22.38s (± 0.13%)~22.34s22.43sp=0.293 n=6
xstate - node (v18.15.0, x64)
Memory used513,385k (± 0.00%)513,386k (± 0.01%)~513,338k513,424kp=0.936 n=6
Parse Time3.28s (± 0.36%)3.27s (± 0.66%)~3.23s3.29sp=0.864 n=6
Bind Time1.57s (± 0.52%)1.57s (± 0.52%)~1.56s1.58sp=1.000 n=6
Check Time2.91s (± 0.29%)2.91s (± 0.52%)~2.90s2.94sp=0.503 n=6
Emit Time0.07s (± 7.03%)0.07s (±11.12%)~0.07s0.09sp=0.752 n=6
Total Time7.84s (± 0.24%)7.83s (± 0.22%)~7.81s7.86sp=0.469 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Developer Information:

Download Benchmarks

Comment threadsrc/compiler/types.ts
@@ -5626,6 +5626,7 @@ export interface EmitResolver {
isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult;
// Returns the constant value this property access resolves to, or 'undefined' for a non-constant
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): string | number | undefined;

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.

Does this still need to have EnumMember?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Probably not, but the same signature is public API on TypeChecker so maybe it’s best not to change it?

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.

Actually, declarations.ts still calls getConstantValue but only on an enum value, which makes me think that the resolver wouldn't need that func at all anymore. But, ts.ts's tryGetConstEnumValue still calls it for property access / element accesses :(

@typescript-bot

Copy link
Copy Markdown
Contributor

Andrew Branch (@andrewbranch) Here are the results of running the top 400 repos comparing main and refs/pull/57966/merge:

Everything looks good!

Comment threadsrc/compiler/checker.ts Outdated
equateValues,
escapeLeadingUnderscores,
escapeString,
type EvaluatorResult,

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.

This is a nit but we haven't really used type imports anywhere else quite yet (besides that protocol.ts blunder); I don't think we've set the settings in settings.json yet that would allow sorting to be inline either... Maybe we just avoid type imports until we can switch them all?

@andrewbranchAndrew Branch (andrewbranch)Apr 2, 2024

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Fine with me, but this is what auto-imports did automatically 😬 My bad, that’s my own setting; I guess I was testing it a while back. It works!

@jakebaileyJake Bailey (jakebailey) left a comment

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.

This seems good to me, though I'm still hoping that we can reduce the EmitResolver API surface since there are so few calls to the old API left...

Maybe Wesley Wigham (@weswigham)Titian Cernicova-Dragomir (@dragomirtitian) have some feedback on that front just from needing to implement that resolver in a minimal fashion, but given this code all exists and works via the existing evaluator that was just pulled out, I doubt this is really a big deal.

@dragomirtitian

Titian Cernicova-Dragomir (dragomirtitian) commented Apr 3, 2024

Copy link
Copy Markdown
Contributor

Maybe Wesley Wigham (@weswigham)Titian Cernicova-Dragomir (@dragomirtitian) have some feedback on that front just from needing to implement that resolver in a minimal fashion.

This looks ok to me from the point of view of creating a minimalist resolver.

@andrewbranch
Andrew Branch (andrewbranch) merged commit 9f8a231 into microsoft:mainApr 4, 2024
@andrewbranch
Andrew Branch (andrewbranch) deleted the bug/isolated-modules-enum-restrictions branch April 4, 2024 16:56
@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamFor Uncommitted BugPR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@andrewbranch@jakebailey@typescript-bot@dragomirtitian