-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Add inline variable refactor #54281
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excited to test this out!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are a couple other invalid inlinings to consider.
- Type queries:
const Bar = class Bar {} type BarConstructor = typeof Bar;
typeof
can only take an identifier reference. - Merged type/value meanings:
There’s one symbol for
const a = "hello"; type a = any; type T = a;
a
with two declarations, so Find All References might returna
intype T = a
as 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;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
Co-authored-by: Daniel Rosenwasser <[email protected]>
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