Merge pull request #20729 from amcasey/GH19839

Refine Extract Local
This commit is contained in:
Andrew Casey
2017-12-15 16:51:25 -08:00
committed by GitHub
64 changed files with 45 additions and 89 deletions
+1
View File
@@ -171,6 +171,7 @@ namespace ts {
node = getParseTreeNode(node, isExpression);
return node ? getContextualType(node) : undefined;
},
isContextSensitive,
getFullyQualifiedName,
getResolvedSignature: (node, candidatesOutArray, theArgumentCount) => {
node = getParseTreeNode(node, isCallLikeExpression);
+2
View File
@@ -2769,6 +2769,8 @@ namespace ts {
getAugmentedPropertiesOfType(type: Type): Symbol[];
getRootSymbols(symbol: Symbol): Symbol[];
getContextualType(node: Expression): Type | undefined;
/* @internal */ isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElementLike | JsxAttributeLike): boolean;
/**
* returns unknownSignature in the case of an error.
* @param argumentCount Apparent number of arguments, passed in case of a possibly incomplete call. This should come from an ArgumentListInfo. See `signatureHelp.ts`.
+12
View File
@@ -262,6 +262,18 @@ namespace N { // Force this test to be TS-only
y = [#|this.x|];
}
}`);
// TODO (https://github.com/Microsoft/TypeScript/issues/20727): the extracted constant should have a type annotation.
testExtractConstant("extractConstant_ContextualType", `
interface I { a: 1 | 2 | 3 }
let i: I = [#|{ a: 1 }|];
`);
testExtractConstant("extractConstant_ContextualType_Lambda", `
const myObj: { member(x: number, y: string): void } = {
member: [#|(x, y) => x + y|],
}
`);
});
function testExtractConstant(caption: string, text: string) {
+1 -1
View File
@@ -279,7 +279,7 @@ namespace ts.codefix {
changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl);
}
else {
changeTracker.insertNodeAtTopOfFile(sourceFile, importDecl);
changeTracker.insertNodeAtTopOfFile(sourceFile, importDecl, /*blankLineBetween*/ true);
}
});
+3 -3
View File
@@ -1004,7 +1004,7 @@ namespace ts.refactor.extractSymbol {
const localNameText = getUniqueName(isClassLike(scope) ? "newProperty" : "newLocal", file.text);
const isJS = isInJavaScriptFile(scope);
const variableType = isJS
const variableType = isJS || !checker.isContextSensitive(node)
? undefined
: checker.typeToTypeNode(checker.getContextualType(node), scope, NodeBuilderFlags.NoTruncation);
@@ -1077,10 +1077,10 @@ namespace ts.refactor.extractSymbol {
// Declare
const nodeToInsertBefore = getNodeToInsertConstantBefore(node, scope);
if (nodeToInsertBefore.pos === 0) {
changeTracker.insertNodeAtTopOfFile(context.file, newVariableStatement);
changeTracker.insertNodeAtTopOfFile(context.file, newVariableStatement, /*blankLineBetween*/ false);
}
else {
changeTracker.insertNodeBefore(context.file, nodeToInsertBefore, newVariableStatement, /*blankLineBetween*/ true);
changeTracker.insertNodeBefore(context.file, nodeToInsertBefore, newVariableStatement, /*blankLineBetween*/ false);
}
// Consume
+2 -2
View File
@@ -332,11 +332,11 @@ namespace ts.textChanges {
return this;
}
public insertNodeAtTopOfFile(sourceFile: SourceFile, newNode: Statement): void {
public insertNodeAtTopOfFile(sourceFile: SourceFile, newNode: Statement, blankLineBetween: boolean): void {
const pos = getInsertionPositionAtSourceFileTop(sourceFile);
this.insertNodeAt(sourceFile, pos, newNode, {
prefix: pos === 0 ? undefined : this.newLineCharacter,
suffix: isLineBreak(sourceFile.text.charCodeAt(pos)) ? this.newLineCharacter : this.newLineCharacter + this.newLineCharacter,
suffix: (isLineBreak(sourceFile.text.charCodeAt(pos)) ? "" : this.newLineCharacter) + (blankLineBetween ? this.newLineCharacter : ""),
});
}
@@ -7,12 +7,10 @@ const f = () => {
const f = () => {
const newLocal = 2 + 1;
return /*RENAME*/newLocal;
};
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
const f = () => {
return /*RENAME*/newLocal;
};
@@ -7,12 +7,10 @@ const f = () => {
const f = () => {
const newLocal = 2 + 1;
return /*RENAME*/newLocal;
};
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
const f = () => {
return /*RENAME*/newLocal;
};
@@ -2,5 +2,4 @@
const f = () => /*[#|*/2 + 1/*|]*/;
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
const f = () => /*RENAME*/newLocal;
@@ -2,5 +2,4 @@
const f = () => /*[#|*/2 + 1/*|]*/;
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
const f = () => /*RENAME*/newLocal;
@@ -11,7 +11,6 @@ for (let i = 0; i < 10; i++) {
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
const newLocal = i + 1;
const x = /*RENAME*/newLocal;
}
}
@@ -11,7 +11,6 @@ for (let i = 0; i < 10; i++) {
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
const newLocal = i + 1;
const x = /*RENAME*/newLocal;
}
}
@@ -8,7 +8,6 @@ for (let i = 0; i < 10; i++) {
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
@@ -8,7 +8,6 @@ for (let i = 0; i < 10; i++) {
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
@@ -4,7 +4,6 @@ class C {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
x = /*RENAME*/newLocal;
}
@@ -10,7 +10,6 @@ class C {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
x = /*RENAME*/newLocal;
}
@@ -16,13 +16,11 @@ class C {
M2() { }
M3() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
a = 1;
b = 2;
@@ -16,7 +16,6 @@ class C {
M2() { }
M3() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
@@ -34,7 +33,6 @@ class C {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
a = 1;
b = 2;
@@ -16,13 +16,11 @@ class C {
M2() { }
M3() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
a = 1;
M1() { }
@@ -16,7 +16,6 @@ class C {
M2() { }
M3() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
@@ -34,7 +33,6 @@ class C {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
a = 1;
M1() { }
@@ -16,13 +16,11 @@ class C {
M2() { }
M3() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
M1() { }
a = 1;
@@ -16,7 +16,6 @@ class C {
M2() { }
M3() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
@@ -34,7 +33,6 @@ class C {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
M1() { }
a = 1;
@@ -0,0 +1,10 @@
// ==ORIGINAL==
interface I { a: 1 | 2 | 3 }
let i: I = /*[#|*/{ a: 1 }/*|]*/;
// ==SCOPE::Extract to constant in enclosing scope==
interface I { a: 1 | 2 | 3 }
const newLocal = { a: 1 };
let i: I = /*RENAME*/newLocal;
@@ -0,0 +1,11 @@
// ==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,
}
@@ -9,6 +9,5 @@ const x = /*[#|*/2 + 1/*|]*/;
"strict";
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
@@ -9,6 +9,5 @@ const x = /*[#|*/2 + 1/*|]*/;
"strict";
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
@@ -16,7 +16,6 @@ function F() {
let i = 0;
const /*RENAME*/newLocal = i++;
function F() {
}
@@ -16,7 +16,6 @@ function F() {
let i = 0;
const /*RENAME*/newLocal = i++;
function F() {
}
@@ -5,12 +5,10 @@ function F() {
// ==SCOPE::Extract to constant in enclosing scope==
function F() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
function F() {
let x = /*RENAME*/newLocal;
}
@@ -5,12 +5,10 @@ function F() {
// ==SCOPE::Extract to constant in enclosing scope==
function F() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
function F() {
let x = /*RENAME*/newLocal;
}
@@ -8,13 +8,11 @@ class C {
class C {
M() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
M() {
let x = /*RENAME*/newLocal;
@@ -8,7 +8,6 @@ class C {
class C {
M() {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
}
@@ -22,7 +21,6 @@ class C {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
class C {
M() {
let x = /*RENAME*/newLocal;
@@ -17,6 +17,5 @@ const x = /*[#|*/2 + 1/*|]*/;
"strict";
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
@@ -17,6 +17,5 @@ const x = /*[#|*/2 + 1/*|]*/;
"strict";
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
@@ -5,12 +5,10 @@ namespace N {
// ==SCOPE::Extract to constant in enclosing scope==
namespace N {
const newLocal = 1;
let x = /*RENAME*/newLocal;
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 1;
namespace N {
let x = /*RENAME*/newLocal;
}
@@ -7,6 +7,5 @@ function F() {
function F() {
let w = 1;
const newLocal = w + 1;
let x = /*RENAME*/newLocal;
}
@@ -7,6 +7,5 @@ function F() {
function F() {
let w = 1;
const newLocal = w + 1;
let x = /*RENAME*/newLocal;
}
@@ -9,6 +9,5 @@ const x = /*[#|*/2 + 1/*|]*/;
/*! Copyright */
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
@@ -9,6 +9,5 @@ const x = /*[#|*/2 + 1/*|]*/;
/*! Copyright */
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
@@ -10,7 +10,6 @@ const x = /*[#|*/2 + 1/*|]*/;
/*! Copyright */
const newLocal = 2 + 1;
/* About x */
const x = /*RENAME*/newLocal;
@@ -10,7 +10,6 @@ const x = /*[#|*/2 + 1/*|]*/;
/*! Copyright */
const newLocal = 2 + 1;
/* About x */
const x = /*RENAME*/newLocal;
@@ -10,7 +10,6 @@ var q = /*b*/ //c
const newLocal = 1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2;
// a
var q = /*b*/ //c
/*d*/ /*RENAME*/newLocal /*k*/ //l
@@ -10,7 +10,6 @@ var q = /*b*/ //c
const newLocal = 1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2;
// a
var q = /*b*/ //c
/*d*/ /*RENAME*/newLocal /*k*/ //l
@@ -7,12 +7,10 @@ namespace X {
namespace X {
export const j = 10;
const newLocal = j * j;
export const y = /*RENAME*/newLocal;
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = X.j * X.j;
namespace X {
export const j = 10;
export const y = /*RENAME*/newLocal;
@@ -10,7 +10,6 @@ for (let j = 0; j < 10; j++) {
const i = 0;
for (let j = 0; j < 10; j++) {
const newLocal = i + 1;
const x = /*RENAME*/newLocal;
}
@@ -10,7 +10,6 @@ for (let j = 0; j < 10; j++) {
const i = 0;
for (let j = 0; j < 10; j++) {
const newLocal = i + 1;
const x = /*RENAME*/newLocal;
}
@@ -13,7 +13,6 @@ const i = 0;
function F() {
for (let j = 0; j < 10; j++) {
const newLocal = i + 1;
const x = /*RENAME*/newLocal;
}
}
@@ -22,7 +21,6 @@ function F() {
const i = 0;
const newLocal = i + 1;
function F() {
for (let j = 0; j < 10; j++) {
const x = /*RENAME*/newLocal;
@@ -13,7 +13,6 @@ const i = 0;
function F() {
for (let j = 0; j < 10; j++) {
const newLocal = i + 1;
const x = /*RENAME*/newLocal;
}
}
@@ -22,7 +21,6 @@ function F() {
const i = 0;
const newLocal = i + 1;
function F() {
for (let j = 0; j < 10; j++) {
const x = /*RENAME*/newLocal;
@@ -8,7 +8,6 @@ for (let j = 0; j < 10; j++) {
for (let j = 0; j < 10; j++) {
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
}
@@ -8,7 +8,6 @@ for (let j = 0; j < 10; j++) {
for (let j = 0; j < 10; j++) {
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
}
@@ -11,14 +11,12 @@ function F() {
function F() {
for (let j = 0; j < 10; j++) {
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
}
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
function F() {
for (let j = 0; j < 10; j++) {
const x = /*RENAME*/newLocal;
@@ -11,14 +11,12 @@ function F() {
function F() {
for (let j = 0; j < 10; j++) {
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
}
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
function F() {
for (let j = 0; j < 10; j++) {
const x = /*RENAME*/newLocal;
@@ -12,7 +12,6 @@ function F0() {
function F0() {
function F1() {
const newLocal = 2 + 1;
function F2(x = /*RENAME*/newLocal) {
}
}
@@ -22,7 +21,6 @@ function F0() {
function F0() {
const newLocal = 2 + 1;
function F1() {
function F2(x = /*RENAME*/newLocal) {
}
@@ -31,7 +29,6 @@ function F0() {
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
function F0() {
function F1() {
function F2(x = /*RENAME*/newLocal) {
@@ -12,7 +12,6 @@ function F0() {
function F0() {
function F1() {
const newLocal = 2 + 1;
function F2(x = /*RENAME*/newLocal) {
}
}
@@ -22,7 +21,6 @@ function F0() {
function F0() {
const newLocal = 2 + 1;
function F1() {
function F2(x = /*RENAME*/newLocal) {
}
@@ -31,7 +29,6 @@ function F0() {
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
function F0() {
function F1() {
function F2(x = /*RENAME*/newLocal) {
@@ -6,7 +6,6 @@ class C {
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
class C {
x = /*RENAME*/newLocal;
}
@@ -14,7 +14,6 @@ class C {
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
class C {
x = /*RENAME*/newLocal;
}
@@ -16,7 +16,6 @@ class C {
M() {
for (let j = 0; j < 10; j++) {
const newLocal = i + 1;
x = /*RENAME*/newLocal;
}
}
@@ -26,7 +25,6 @@ class C {
const i = 0;
const newLocal = i + 1;
class C {
M() {
for (let j = 0; j < 10; j++) {
@@ -15,8 +15,7 @@ const i = 0;
class C {
M() {
for (let j = 0; j < 10; j++) {
const newLocal: any = i + 1;
const newLocal = i + 1;
x = /*RENAME*/newLocal;
}
}
@@ -26,7 +25,7 @@ class C {
const i = 0;
class C {
private readonly newProperty: any = i + 1;
private readonly newProperty = i + 1;
M() {
for (let j = 0; j < 10; j++) {
@@ -38,8 +37,7 @@ class C {
// ==SCOPE::Extract to constant in global scope==
const i = 0;
const newLocal: any = i + 1;
const newLocal = i + 1;
class C {
M() {
for (let j = 0; j < 10; j++) {
@@ -2,5 +2,4 @@
let x = /*[#|*/1/*|]*/;
// ==SCOPE::Extract to constant in enclosing scope==
const newLocal = 1;
let x = /*RENAME*/newLocal;
@@ -2,5 +2,4 @@
let x = /*[#|*/1/*|]*/;
// ==SCOPE::Extract to constant in enclosing scope==
const newLocal = 1;
let x = /*RENAME*/newLocal;
@@ -9,6 +9,5 @@ const x = /*[#|*/2 + 1/*|]*/;
/// <reference path="path.js"/>
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
@@ -9,6 +9,5 @@ const x = /*[#|*/2 + 1/*|]*/;
/// <reference path="path.js"/>
const newLocal = 2 + 1;
const x = /*RENAME*/newLocal;
@@ -5,6 +5,5 @@ function F<T>(t: T) {
// ==SCOPE::Extract to constant in enclosing scope==
function F<T>(t: T) {
const newLocal = t + 1;
let x = /*RENAME*/newLocal;
}
-1
View File
@@ -9,6 +9,5 @@ edit.applyRefactor({
actionDescription: "Extract to constant in enclosing scope",
newContent:
`const newLocal = 0;
const x = /*RENAME*/newLocal;`
});