Skip to content

Commit 5e2dec7

Browse files
authored
Merge pull request microsoft#20729 from amcasey/GH19839
Refine Extract Local
2 parents ae73a91 + 804949f commit 5e2dec7

64 files changed

Lines changed: 45 additions & 89 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ namespace ts {
171171
node = getParseTreeNode(node, isExpression);
172172
return node ? getContextualType(node) : undefined;
173173
},
174+
isContextSensitive,
174175
getFullyQualifiedName,
175176
getResolvedSignature: (node, candidatesOutArray, theArgumentCount) => {
176177
node = getParseTreeNode(node, isCallLikeExpression);

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,8 @@ namespace ts {
27692769
getAugmentedPropertiesOfType(type: Type): Symbol[];
27702770
getRootSymbols(symbol: Symbol): Symbol[];
27712771
getContextualType(node: Expression): Type | undefined;
2772+
/* @internal */ isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElementLike | JsxAttributeLike): boolean;
2773+
27722774
/**
27732775
* returns unknownSignature in the case of an error.
27742776
* @param argumentCount Apparent number of arguments, passed in case of a possibly incomplete call. This should come from an ArgumentListInfo. See `signatureHelp.ts`.

src/harness/unittests/extractConstants.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ namespace N { // Force this test to be TS-only
262262
y = [#|this.x|];
263263
}
264264
}`);
265+
266+
// TODO (https://github.com/Microsoft/TypeScript/issues/20727): the extracted constant should have a type annotation.
267+
testExtractConstant("extractConstant_ContextualType", `
268+
interface I { a: 1 | 2 | 3 }
269+
let i: I = [#|{ a: 1 }|];
270+
`);
271+
272+
testExtractConstant("extractConstant_ContextualType_Lambda", `
273+
const myObj: { member(x: number, y: string): void } = {
274+
member: [#|(x, y) => x + y|],
275+
}
276+
`);
265277
});
266278

267279
function testExtractConstant(caption: string, text: string) {

src/services/codefixes/importFixes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ namespace ts.codefix {
279279
changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl);
280280
}
281281
else {
282-
changeTracker.insertNodeAtTopOfFile(sourceFile, importDecl);
282+
changeTracker.insertNodeAtTopOfFile(sourceFile, importDecl, /*blankLineBetween*/ true);
283283
}
284284
});
285285

src/services/refactors/extractSymbol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ namespace ts.refactor.extractSymbol {
10041004
const localNameText = getUniqueName(isClassLike(scope) ? "newProperty" : "newLocal", file.text);
10051005
const isJS = isInJavaScriptFile(scope);
10061006

1007-
const variableType = isJS
1007+
const variableType = isJS || !checker.isContextSensitive(node)
10081008
? undefined
10091009
: checker.typeToTypeNode(checker.getContextualType(node), scope, NodeBuilderFlags.NoTruncation);
10101010

@@ -1077,10 +1077,10 @@ namespace ts.refactor.extractSymbol {
10771077
// Declare
10781078
const nodeToInsertBefore = getNodeToInsertConstantBefore(node, scope);
10791079
if (nodeToInsertBefore.pos === 0) {
1080-
changeTracker.insertNodeAtTopOfFile(context.file, newVariableStatement);
1080+
changeTracker.insertNodeAtTopOfFile(context.file, newVariableStatement, /*blankLineBetween*/ false);
10811081
}
10821082
else {
1083-
changeTracker.insertNodeBefore(context.file, nodeToInsertBefore, newVariableStatement, /*blankLineBetween*/ true);
1083+
changeTracker.insertNodeBefore(context.file, nodeToInsertBefore, newVariableStatement, /*blankLineBetween*/ false);
10841084
}
10851085

10861086
// Consume

src/services/textChanges.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,11 @@ namespace ts.textChanges {
332332
return this;
333333
}
334334

335-
public insertNodeAtTopOfFile(sourceFile: SourceFile, newNode: Statement): void {
335+
public insertNodeAtTopOfFile(sourceFile: SourceFile, newNode: Statement, blankLineBetween: boolean): void {
336336
const pos = getInsertionPositionAtSourceFileTop(sourceFile);
337337
this.insertNodeAt(sourceFile, pos, newNode, {
338338
prefix: pos === 0 ? undefined : this.newLineCharacter,
339-
suffix: isLineBreak(sourceFile.text.charCodeAt(pos)) ? this.newLineCharacter : this.newLineCharacter + this.newLineCharacter,
339+
suffix: (isLineBreak(sourceFile.text.charCodeAt(pos)) ? "" : this.newLineCharacter) + (blankLineBetween ? this.newLineCharacter : ""),
340340
});
341341
}
342342

tests/baselines/reference/extractConstant/extractConstant_ArrowFunction_Block.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ const f = () => {
77

88
const f = () => {
99
const newLocal = 2 + 1;
10-
1110
return /*RENAME*/newLocal;
1211
};
1312
// ==SCOPE::Extract to constant in global scope==
1413
const newLocal = 2 + 1;
15-
1614
const f = () => {
1715
return /*RENAME*/newLocal;
1816
};

tests/baselines/reference/extractConstant/extractConstant_ArrowFunction_Block.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ const f = () => {
77

88
const f = () => {
99
const newLocal = 2 + 1;
10-
1110
return /*RENAME*/newLocal;
1211
};
1312
// ==SCOPE::Extract to constant in global scope==
1413
const newLocal = 2 + 1;
15-
1614
const f = () => {
1715
return /*RENAME*/newLocal;
1816
};

tests/baselines/reference/extractConstant/extractConstant_ArrowFunction_Expression.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
const f = () => /*[#|*/2 + 1/*|]*/;
33
// ==SCOPE::Extract to constant in global scope==
44
const newLocal = 2 + 1;
5-
65
const f = () => /*RENAME*/newLocal;

tests/baselines/reference/extractConstant/extractConstant_ArrowFunction_Expression.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
const f = () => /*[#|*/2 + 1/*|]*/;
33
// ==SCOPE::Extract to constant in global scope==
44
const newLocal = 2 + 1;
5-
65
const f = () => /*RENAME*/newLocal;

0 commit comments

Comments
 (0)