Crazy checker.ts refactor experiment (25kloc)
See original GitHub issueI have made tool-assisted refactoring experiment on typeChecker. The goal is to convert checker.ts from incapsulated js scope to a class. For better extensibility and publicity (#17680).
All tests are passing, excluding linting (because of auto converting). We can fix linting issues later.
How I refactored 25k lines of code:
- All types moved to ts namespace out of createTypeChecker;
- Class TypeCheckerImpl was created.
- All variables from createTypeChecker moved to public properties of TypeCheckerImpl;
- Property initializers were moved to constructor. (because of constructor parameters dependency)
declarations now looks ugly, because
is only way to auto infer type of function return value; We can fix it later.public propName = (false as true) && this.someMethod();
- All nested functions from createTypeChecker converted to public methods of TypeCheckerImpl;
- For all converted functions auto inserted “this” keyword.
- For methods that contain nested functions I have to create ugly conv_self to enclosure “this”.
- Added ugly code to bind all methods. We can fix it later.
- Now TypeCheckerImpl instance is not exposed to public, but I want to do it later.
Here is commit in my fork: https://github.com/Busyrev/TypeScript/commit/697d90ef5cc328127b8c167701a34365da20e795 Unfortunetly GitHub can`t show diff, and that is one more point for necessity of refactoring.
Raw checker.ts before
https://raw.githubusercontent.com/Microsoft/TypeScript/master/src/compiler/checker.ts
after
This is only experiment.
I have spend around 16 hours for these transforms. I think that this experiment is successful. For better typescript future I think checker.ts should be separated into multiple files/classes, and this is the first step. This code is not merge ready. Of course we should think about methods to be provided to public api, and may be unsafe access for people who do really complicated transformations.
What do you think about it?
I can dedicate more time, improve documents and publish tools I’ve made, if more people support this experiment.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:18
- Comments:13 (10 by maintainers)
I ran our internal performance tests and it looks like there is a large increase in check time. (There were also large increases in emit time, which may just be my hard drive acting up.) There was no change to parse or bind time or memory usage.
Monaco - node (v8.2.1, x64)
Monaco - tsc (x86)
TFS - node (v8.2.1, x64)
TFS - tsc (x86)
I’d really like to see the checker cut down to size – it’s so big that vscode doesn’t support collapsing code, and it takes several seconds to update errors. I think some of the candidates for removal from
checker.ts
wereisTypeAssignableTo
andtypeToString
. @sandersn might have some comments.This gets asked a ton of times, so here’s the short version
Imagine you have this code structure
It is a fact in modern JS engines that bare-name lookups like
y1
andy2
are much faster than property accesses likeobj.y1
andobj.y2
. The challenge, then: how do you “split up”fn
in a way that preserves that performance improvement?