Skip to content

Refine Extract Local#20729

Merged
amcasey merged 6 commits into
microsoft:masterfrom
amcasey:GH19839
Dec 16, 2017
Merged

Refine Extract Local#20729
amcasey merged 6 commits into
microsoft:masterfrom
amcasey:GH19839

Conversation

@amcasey
Copy link
Copy Markdown
Member

@amcasey amcasey commented Dec 15, 2017

  1. Don't insert a blank line after extracted locals
  2. Don't add a type annotation unless the expression is context-sensitive.

Fixes #19839.

Still insert a blank line after extracted properties.
Expose `isContextSensitive` from the checker and omit type annotations
for expressions for which it returns false.
Copy link
Copy Markdown
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, although I wonder whether there are many people who always want the type annotation. I think this is common in C#, but I don't have a good guess for Typescript.

@amcasey
Copy link
Copy Markdown
Member Author

amcasey commented Dec 16, 2017

@sandersn, yes, it should probably be controlled by a setting.

@amcasey
Copy link
Copy Markdown
Member Author

amcasey commented Dec 16, 2017

Note that this change will produce too few type annotations until #20727 is fixed.

Copy link
Copy Markdown
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I'm a little wary of using isContextSensitive as is, but it's likely a decent heuristic for now. Just to check if we've covered the other things contextual types are used for correctly (and to verify that this does what you'd like in more cases), can you add a test extracting the lambda from:

const myObj: { member(x: number, y: string): void } = { 
    member: (x, y) => x + y,
}

(or point me at a similar one)? (I expect the extracted constant to have an annotation, in this case)

@amcasey
Copy link
Copy Markdown
Member Author

amcasey commented Dec 16, 2017

@weswigham Like this?

// ==ORIGINAL==

const myObj: { member(x: number, y: string): void } = {
    member: /*[#|*/(x, y) => x + y/*|]*/,
}

// ==SCOPE::Extract to constant in enclosing scope==
const newLocal: (x: number, y: string) => void = (x, y) => x + y;
const myObj: { member(x: number, y: string): void } = {
    member: /*RENAME*/newLocal,
}

Copy link
Copy Markdown
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amcasey Perfect

@amcasey amcasey merged commit 5e2dec7 into microsoft:master Dec 16, 2017
@amcasey amcasey deleted the GH19839 branch December 16, 2017 00:51
@amcasey
Copy link
Copy Markdown
Member Author

amcasey commented Dec 16, 2017

Note to anyone reverting this change: the blank line portion (unfortunately, spread across three commits) is essentially a separate change and can be retained even if the context-sensitive check turns out to be infeasible.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants