microsoft / microsoft/TypeScript

Extract to constant in enclosing scope should optionally also replace later usage

Open
#44,534 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Given this code:

	public setParts(lineNumber: number, parts: GhostTextPart[]): void {
		const foreground = this.themeService.getColorTheme().getColor(ghostTextForeground);
		let opacity: string | undefined = undefined;
		let color: string | undefined = undefined;
		if (foreground) {


			opacity = String(foreground.rgba.a);
			color = Color.Format.CSS.format(opaque(foreground))!;
		}

		const borderColor = this.themeService.getColorTheme().getColor(ghostTextBorder);
		let border: string | undefined = undefined;
		if (borderColor) {
			border = `2px dashed ${borderColor}`;
		}

I would like to factor our this.themeService.getColorTheme() and extract it into its own constant. This should include all later usages.
Since there could be side effects in between, I suggest to do it only if later occurences are also selected.

This is my current workflow and I don't feel smart doing it like that. Multicursors don't really help here.

recording

Also, it would be nice if the name of the new local is heuristically derived from the expression ((...).getFooBar(...) -> fooBar`).

I'm willing to do a PR if I get some pointers.

Let's make the editing experience in VS Code even more amazing 😉

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the extract-to-constant refactoring implementation and its tests in the TypeScript repository. Reproduce the shown TypeScript example, then trace how selection boundaries and later occurrences are handled. Done means an optional enclosing-scope extraction can include later uses only when selected, with heuristic naming considered separately.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.