Add inline variable refactor#54281
Conversation
|
The TypeScript team hasn't accepted the linked issue #18459. If you can get it accepted, this PR will have a better chance of being reviewed. |
|
The TypeScript team hasn't accepted the linked issue #18459. If you can get it accepted, this PR will have a better chance of being reviewed. |
1 similar comment
|
The TypeScript team hasn't accepted the linked issue #18459. If you can get it accepted, this PR will have a better chance of being reviewed. |
fae73f0 to
2c58756
Compare
There was a problem hiding this comment.
I think there are a couple other invalid inlinings to consider.
- Type queries:
const Bar = class Bar {} type BarConstructor = typeof Bar;
typeofcan only take an identifier reference. - Merged type/value meanings:
There’s one symbol for
const a = "hello"; type a = any; type T = a;
awith two declarations, so Find All References might returnaintype T = aas a reference, even though you wouldn’t want to inline"hello"there. The real case for merging meanings is usually more likeand in those cases it seems undesirable to inline anyway. I think you can implement a simple rule that says if a symbol has more than one declaration, it can’t be inlined.// Simulate type/value duality of real class declarations const SomeClass = mixin(OtherClass); type SomeClass = typeof SomeClass;
andrewbranch
left a comment
There was a problem hiding this comment.
This is looking really good! The implementation is impressively small and readable and I like that each test is very focused.
Have you tried to beat this up on some more complicated real-world code? Do that if you haven’t already, and then I would also suggest having one or two kitchen-sink tests that exercise more complex cases with several references and complex expressions (multi-line function expression?)
|
@andrewbranch I've added a couple more tests with more complex scenarios like call expressions. LMK what you think :) |
|
I still think inlining a multi-line expression would be good to test: function Component() {
const onClick /*inline me*/ = () => {
console.log("clicked");
};
return (
<button onClick={onClick}>
Do useful thing
</button>
);
} |
|
@typescript-bot pack this |
andrewbranch
left a comment
There was a problem hiding this comment.
Looks good to me, nice job 👍
|
I SAID @typescript-bot pack this |
|
Heya @DanielRosenwasser, I've started to run the diff-based user code test suite (tsserver) on this PR at 2f97c5c. You can monitor the build here. Update: The results are in! |
|
Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at 2f97c5c. You can monitor the build here. |
|
Heya @DanielRosenwasser, I've started to run the diff-based top-repos suite (tsserver) on this PR at 2f97c5c. You can monitor the build here. Update: The results are in! |
|
That's what I thought. |
|
Hey @DanielRosenwasser, I've packed this into an installable tgz. You can install it for testing by referencing it in your and then running There is also a playground for this build and an npm module you can use via |
|
@DanielRosenwasser Here are the results of running the user test suite comparing Everything looks good! |
|
Here's a fun one I just tried out: const /**/yadda = () => yadda; |
Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com>
436e243 to
9f39bfb
Compare
@DanielRosenwasser I believe we should stick with option 1, since there would be an ambiguity about what to inline in cases like However, if I change this bit to: if (isIdentifier(token) && isInitializedVariable(parent) && isVariableDeclarationInVariableStatement(parent)) {The refactor is still available in |
|
@typescript-bot pack this |
|
Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at 67a5880. You can monitor the build here. |
|
Hey @DanielRosenwasser, I've packed this into an installable tgz. You can install it for testing by referencing it in your and then running There is also a playground for this build and an npm module you can use via |
|
Inlining variable const data = new Map()
const getter = () => data.get('foo')
data.set('foo', 'bar')
console.log(getter()) |
Fixes #18459