Merge with master

This commit is contained in:
Ryan Cavanaugh 2018-06-11 13:17:25 -07:00
commit 6876d981f2
31 changed files with 438 additions and 46 deletions

View file

@ -4402,5 +4402,17 @@
"Convert named imports to namespace import": {
"category": "Message",
"code": 95057
},
"Add or remove braces in an arrow function": {
"category": "Message",
"code": 95058
},
"Add braces to arrow function": {
"category": "Message",
"code": 95059
},
"Remove braces from arrow function": {
"category": "Message",
"code": 95060
}
}

View file

@ -202,22 +202,6 @@ namespace ts.codefix {
}
}
function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile) {
forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => {
if (kind === SyntaxKind.MultiLineCommentTrivia) {
// Remove leading /*
pos += 2;
// Remove trailing */
end -= 2;
}
else {
// Remove leading //
pos += 2;
}
addSyntheticLeadingComment(targetNode, kind, sourceFile.text.slice(pos, end), htnl);
});
}
function getModifierKindFromSource(source: Node, kind: SyntaxKind): ReadonlyArray<Modifier> | undefined {
return filter(source.modifiers, modifier => modifier.kind === kind);
}

View file

@ -148,20 +148,9 @@ namespace ts.codefix {
return false;
}
function tryDeleteDeclaration(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined, checker: TypeChecker, isFixAll: boolean): void {
switch (token.kind) {
case SyntaxKind.Identifier:
tryDeleteIdentifier(changes, sourceFile, <Identifier>token, deletedAncestors, checker, isFixAll);
deleteAssignments(changes, sourceFile, token as Identifier, checker);
break;
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.NamespaceImport:
if (deletedAncestors) deletedAncestors.add(token.parent);
changes.deleteNode(sourceFile, token.parent);
break;
default:
tryDeleteDefault(changes, sourceFile, token, deletedAncestors);
}
function tryDeleteDeclaration(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined, checker: TypeChecker, isFixAll: boolean) {
tryDeleteDeclarationWorker(changes, sourceFile, token, deletedAncestors, checker, isFixAll);
if (isIdentifier(token)) deleteAssignments(changes, sourceFile, token, checker);
}
function deleteAssignments(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Identifier, checker: TypeChecker) {
@ -173,19 +162,8 @@ namespace ts.codefix {
});
}
function tryDeleteDefault(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined): void {
if (isDeclarationName(token)) {
if (deletedAncestors) deletedAncestors.add(token.parent);
changes.deleteNode(sourceFile, token.parent);
}
else if (isLiteralComputedPropertyDeclarationName(token)) {
if (deletedAncestors) deletedAncestors.add(token.parent.parent);
changes.deleteNode(sourceFile, token.parent.parent);
}
}
function tryDeleteIdentifier(changes: textChanges.ChangeTracker, sourceFile: SourceFile, identifier: Identifier, deletedAncestors: NodeSet | undefined, checker: TypeChecker, isFixAll: boolean): void {
const parent = identifier.parent;
function tryDeleteDeclarationWorker(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined, checker: TypeChecker, isFixAll: boolean): void {
const parent = token.parent;
switch (parent.kind) {
case SyntaxKind.VariableDeclaration:
tryDeleteVariableDeclaration(changes, sourceFile, <VariableDeclaration>parent, deletedAncestors);
@ -250,7 +228,7 @@ namespace ts.codefix {
// handle case where 'import a = A;'
case SyntaxKind.ImportEqualsDeclaration:
const importEquals = getAncestor(identifier, SyntaxKind.ImportEqualsDeclaration)!;
const importEquals = getAncestor(token, SyntaxKind.ImportEqualsDeclaration)!;
changes.deleteNode(sourceFile, importEquals);
break;
@ -290,7 +268,14 @@ namespace ts.codefix {
break;
default:
tryDeleteDefault(changes, sourceFile, identifier, deletedAncestors);
if (isDeclarationName(token)) {
if (deletedAncestors) deletedAncestors.add(token.parent);
changes.deleteNode(sourceFile, token.parent);
}
else if (isLiteralComputedPropertyDeclarationName(token)) {
if (deletedAncestors) deletedAncestors.add(token.parent.parent);
changes.deleteNode(sourceFile, token.parent.parent);
}
break;
}
}

View file

@ -32,7 +32,7 @@ namespace ts.GoToDefinition {
const sigInfo = createDefinitionFromSignatureDeclaration(typeChecker, calledDeclaration);
// For a function, if this is the original function definition, return just sigInfo.
// If this is the original constructor definition, parent is the class.
if (typeChecker.getRootSymbols(symbol).some(s => calledDeclaration.symbol === s || calledDeclaration.symbol.parent === s) ||
if (typeChecker.getRootSymbols(symbol).some(s => symbolMatchesSignature(s, calledDeclaration)) ||
// TODO: GH#23742 Following check shouldn't be necessary if 'require' is an alias
symbol.declarations.some(d => isVariableDeclaration(d) && !!d.initializer && isRequireCall(d.initializer, /*checkArgumentIsStringLiteralLike*/ false))) {
return [sigInfo];
@ -93,6 +93,15 @@ namespace ts.GoToDefinition {
return getDefinitionFromSymbol(typeChecker, symbol, node);
}
/**
* True if we should not add definitions for both the signature symbol and the definition symbol.
* True for `const |f = |() => 0`, false for `function |f() {} const |g = f;`.
*/
function symbolMatchesSignature(s: Symbol, calledDeclaration: SignatureDeclaration) {
return s === calledDeclaration.symbol || s === calledDeclaration.symbol.parent ||
isVariableDeclaration(calledDeclaration.parent) && s === calledDeclaration.parent.symbol;
}
export function getReferenceAtPosition(sourceFile: SourceFile, position: number, program: Program): { fileName: string, file: SourceFile } | undefined {
const referencePath = findReferenceInPosition(sourceFile.referencedFiles, position);
if (referencePath) {

View file

@ -0,0 +1,96 @@
/* @internal */
namespace ts.refactor.addOrRemoveBracesToArrowFunction {
const refactorName = "Add or remove braces in an arrow function";
const refactorDescription = Diagnostics.Add_or_remove_braces_in_an_arrow_function.message;
const addBracesActionName = "Add braces to arrow function";
const removeBracesActionName = "Remove braces from arrow function";
const addBracesActionDescription = Diagnostics.Add_braces_to_arrow_function.message;
const removeBracesActionDescription = Diagnostics.Remove_braces_from_arrow_function.message;
registerRefactor(refactorName, { getEditsForAction, getAvailableActions });
interface Info {
func: ArrowFunction;
expression: Expression | undefined;
returnStatement?: ReturnStatement;
addBraces: boolean;
}
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
const { file, startPosition } = context;
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
if (!info) return undefined;
return [{
name: refactorName,
description: refactorDescription,
actions: [
info.addBraces ?
{
name: addBracesActionName,
description: addBracesActionDescription
} : {
name: removeBracesActionName,
description: removeBracesActionDescription
}
]
}];
}
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const { file, startPosition } = context;
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
if (!info) return undefined;
const { expression, returnStatement, func } = info;
let body: ConciseBody;
if (actionName === addBracesActionName) {
const returnStatement = createReturn(expression);
body = createBlock([returnStatement], /* multiLine */ true);
suppressLeadingAndTrailingTrivia(body);
copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true);
}
else if (actionName === removeBracesActionName && returnStatement) {
const actualExpression = expression || createVoidZero();
body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression;
suppressLeadingAndTrailingTrivia(body);
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
}
else {
Debug.fail("invalid action");
}
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func.body, body));
return { renameFilename: undefined, renameLocation: undefined, edits };
}
function needsParentheses(expression: Expression) {
return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression);
}
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number): Info | undefined {
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
const func = getContainingFunction(node);
if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node) || rangeContainsRange(func.body, node))) return undefined;
if (isExpression(func.body)) {
return {
func,
addBraces: true,
expression: func.body
};
}
else if (func.body.statements.length === 1) {
const firstStatement = first(func.body.statements);
if (isReturnStatement(firstStatement)) {
return {
func,
addBraces: false,
expression: firstStatement.expression,
returnStatement: firstStatement
};
}
}
return undefined;
}
}

View file

@ -77,6 +77,7 @@
"refactors/extractSymbol.ts",
"refactors/generateGetAccessorAndSetAccessor.ts",
"refactors/moveToNewFile.ts",
"refactors/addOrRemoveBracesToArrowFunction.ts",
"sourcemaps.ts",
"services.ts",
"breakpoints.ts",

View file

@ -1720,6 +1720,22 @@ namespace ts {
return lastPos;
}
export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, commentKind?: CommentKind, hasTrailingNewLine?: boolean) {
forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => {
if (kind === SyntaxKind.MultiLineCommentTrivia) {
// Remove leading /*
pos += 2;
// Remove trailing */
end -= 2;
}
else {
// Remove leading //
pos += 2;
}
addSyntheticLeadingComment(targetNode, commentKind || kind, sourceFile.text.slice(pos, end), hasTrailingNewLine !== undefined ? hasTrailingNewLine : htnl);
});
}
function indexInTextChange(change: string, name: string): number {
if (startsWith(change, name)) return 0;
// Add a " " to avoid references inside words

View file

@ -8,8 +8,17 @@
////[|/*useG*/g|]();
////[|/*useH*/h|]();
////const i = /*i*/() => 0;
////const /*j*/j = i;
////[|/*useI*/i|]();
////[|/*useJ*/j|]();
verify.goToDefinition({
useF: "f",
useG: ["g", "f"],
useH: ["h", "f"],
useI: "i",
useJ: ["j", "i"],
});

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => a + 1;
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Add braces to arrow function",
actionDescription: "Add braces to arrow function",
newContent: `const foo = a => {
return a + 1;
};`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return (1, 2, 3); };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => (1, 2, 3);`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return 1, 2, 3; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => (1, 2, 3);`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return "foo"; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => "foo";`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return null; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => null;`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return undefined; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => undefined;`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return void 0; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => void 0;`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return {}; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => ({});`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return `abc{a}`; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => \`abc{a}\`;`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return `abc`; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => \`abc\`;`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return a; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => a;`,
});

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => ({ a: 1 });
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Add braces to arrow function",
actionDescription: "Add braces to arrow function",
newContent: `const foo = a => {
return ({ a: 1 });
};`,
});

View file

@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => {
//// // return comment
//// return a;
//// };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => /* return comment*/ a;`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => void 0;`,
});

View file

@ -0,0 +1,27 @@
/// <reference path='fourslash.ts' />
//// const /*a*/foo/*b*/ = /*c*/(/*d*//*e*/aa/*f*/aa, /*g*/b/*h*/) /*i*//*j*/ /*k*/=>/*l*/ /*m*/{/*n*/ /*o*/return/*p*/ 1; };
goTo.select("a", "b");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("c", "d");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("e", "f");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("g", "h");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("i", "j");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("k", "l");
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("m", "n");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")
goTo.select("o", "p");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function")

View file

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
//// const /*a*/foo/*b*/ = /*c*/()/*d*/ /*e*//*f*/ /*g*/=>/*h*/ /*i*/1/*j*/;
goTo.select("a", "b");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
goTo.select("c", "d");
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
goTo.select("e", "f");
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
goTo.select("g", "h");
verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")
goTo.select("i", "j");
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function")

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => 1;
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Add braces to arrow function",
actionDescription: "Add braces to arrow function",
newContent: `const foo = a => {
return 1;
};`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return a + 1; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => a + 1;`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return { a: 1 }; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => ({ a: 1 });`,
});

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { return 1; };
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const foo = a => 1;`,
});

View file

@ -0,0 +1,6 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => { };
goTo.select("a", "b");
verify.not.refactorAvailable("Add or remove braces in an arrow function");

View file

@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => {
//// const b = 1;
//// return a + b;
//// };
goTo.select("a", "b");
verify.not.refactorAvailable("Add or remove braces in an arrow function");

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
//// const foo = /*a*/a/*b*/ => (1, 2, 3);
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Add braces to arrow function",
actionDescription: "Add braces to arrow function",
newContent: `const foo = a => {
return (1, 2, 3);
};`,
});