Skip to content

Refactor Suggestion: Inline Symbol #53724

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

Closed
5 tasks done
gioragutt opened this issue Apr 10, 2023 · 4 comments
Closed
5 tasks done

Refactor Suggestion: Inline Symbol #53724

gioragutt opened this issue Apr 10, 2023 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@gioragutt
Copy link

gioragutt commented Apr 10, 2023

Suggestion

πŸ” Search Terms

  • Refactor
  • Inline
  • Symbol
  • Variable
  • Function

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

The suggestion is basically to implement the Inline Symbol refactor that already exists in JetBrains IDEs.

As the article linked above shows, there are several types of symbols that can be inlined, but a good idea would be to start with the Inline Variable refactor, as it's likely to be the simplest one.

πŸ“ƒ Motivating Example

Consider the following code:

const message = 'Something happened!';

console.log(message);
console.log(message);

The Inline Symbol refactor, when applied on message (the VariableDeclarator['id']), would replace the instances where the identifier is referenced with the expression that message is assigned to (the VariableDeclarator['init']).

This would be the output:

console.log('Something happened!');
console.log('Something happened!');

πŸ’» Use Cases

Basically, just like Extract Constant, this refactor's main purpose is to reduce time spent making boilerplate-y operations that could be entirely automatic.

Although I'm not a fan of WebStorm, I've used IntelliJ heavily, and the refactorings available there are really top-notch. They save a serious amount of time. For reference, from colleagues using WebStorm, I know these refactorings are implemented for JS/TS as well, so that's proof to me there's no technically blocker from getting it done.

I'd have contributed all the missing refactorings, but that's a bit far, so I'd like to start with the seemingly (I assume it'll be harder than I imagine) easiest one, with the smallest scope.

@MadaraUchiha
Copy link

This operation is not entirely safe in all cases, and it can be difficult to determine whether it is safe or not. Consider the following example

const message = calculateMessage(); // causes a side-effect

console.log(message);
console.log(message);

Refactoring this to

console.log(calculateMessage());
console.log(calculateMessage());

Would cause the side effect to happen twice.

This means that you can't really argue that it can be done automatically, since it carries risks.

I think that you can probably get away with it if you only do it for literals ('Something happened!', true, 3, etc), but any calls or derefs to object properties or variables is going to be risky.

@RyanCavanaugh
Copy link
Member

Duplicate #18459

FWIW I think this is one of those refactorings that is implicitly not necessarily semantics-preserving.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 11, 2023
@gioragutt
Copy link
Author

Thanks for the reply @RyanCavanaugh πŸ™πŸ»

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants