mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
@@ -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);
|
||||
|
||||
@@ -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`.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -279,7 +279,7 @@ namespace ts.codefix {
|
||||
changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl);
|
||||
}
|
||||
else {
|
||||
changeTracker.insertNodeAtTopOfFile(sourceFile, importDecl);
|
||||
changeTracker.insertNodeAtTopOfFile(sourceFile, importDecl, /*blankLineBetween*/ true);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 : ""),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user