Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3f59aa9
stash
Kingwl Aug 10, 2018
7671221
add surmise for return type
Kingwl Aug 14, 2018
0b1e6cc
add support for more case
Kingwl Aug 14, 2018
1f9b9c0
add more test case
Kingwl Aug 16, 2018
aca1722
add more testcase and fix all test
Kingwl Aug 20, 2018
7cfd0fb
Merge branch 'master' into returnValueSurmise
Kingwl Apr 29, 2019
cbe59bb
fix changed diagnosis
Kingwl Apr 30, 2019
48f1cca
Merge branch 'master' into returnValueSurmise
Kingwl May 8, 2019
98d7eba
fix broken test case
Kingwl May 8, 2019
537e806
add more case
Kingwl May 8, 2019
505e299
Merge branch 'master' into returnValueSurmise
Kingwl Jan 9, 2020
6fecf32
rename quickfix
Kingwl Jan 9, 2020
4c0af72
fix conflict
Kingwl Jan 9, 2020
98377f3
fix fix desc
Kingwl Jan 9, 2020
49d21bf
fix semi
Kingwl Jan 9, 2020
a2b15ed
Merge branch 'master' into returnValueSurmise
Kingwl Mar 4, 2020
eabc5a4
Merge branch 'master' into returnValueSurmise
Kingwl Mar 4, 2020
944ddf4
Avoid replace brace with paren
Kingwl Mar 18, 2020
6c88a01
Split fix all action
Kingwl Mar 18, 2020
94f845a
Add return work in same line
Kingwl Mar 18, 2020
a7f37a3
Merge branch 'master' into returnValueSurmise
Kingwl Mar 18, 2020
7fe9a82
fix test cases
Kingwl Mar 18, 2020
af9552e
rename baseline
Kingwl Mar 26, 2020
601fc5e
refactor and handle comment
Kingwl Mar 26, 2020
175cf4e
Support semi
Kingwl Mar 26, 2020
8b332fe
Merge branch 'master' into returnValueSurmise
Kingwl Mar 26, 2020
5d8355c
make helper internal
Kingwl Mar 26, 2020
522cac8
Merge branch 'master' into returnValueSurmise
Kingwl Apr 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add return work in same line
  • Loading branch information
Kingwl committed Mar 18, 2020
commit 94f845af510946ba473348792b94912292db04e4
26 changes: 16 additions & 10 deletions src/services/codefixes/returnValueCorrect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ namespace ts.codefix {
kind: FixKind.MissingReturnStatement;
declaration: FunctionLikeDeclaration;
expression: Expression;
statement: Statement;
}

interface MissingParenInfo {
kind: FixKind.MissingParentheses;
declaration: ArrowFunction;
expression: Expression;
statement: Statement;
}

type Info = MissingReturnInfo | MissingParenInfo;
Expand All @@ -39,7 +41,7 @@ namespace ts.codefix {

if (info.kind === FixKind.MissingReturnStatement) {
return append(
[getActionForfixAddReturnStatement(context, info.declaration, info.expression)],
[getActionForfixAddReturnStatement(context, info.expression, info.statement)],
isArrowFunction(info.declaration) ? getActionForfixRemoveBlockBodyBrace(context, info.declaration, info.expression): undefined);
}
else {
Expand All @@ -52,7 +54,7 @@ namespace ts.codefix {

switch (context.fixId) {
case fixIdAddReturnStatement:
addReturnStatement(changes, diag.file, info.declaration, info.expression);
addReturnStatement(changes, diag.file, info.expression, info.statement);
break;
case fixIdRemoveBlockBodyBrace:
if (!isArrowFunction(info.declaration)) return undefined;
Expand Down Expand Up @@ -95,7 +97,8 @@ namespace ts.codefix {
return {
declaration,
kind: FixKind.MissingReturnStatement,
expression: firstStatement.expression
expression: firstStatement.expression,
statement: firstStatement
};
}
else if (isLabeledStatement(firstStatement) && isExpressionStatement(firstStatement.statement)) {
Expand All @@ -104,11 +107,13 @@ namespace ts.codefix {
return isArrowFunction(declaration) ? {
declaration,
kind: FixKind.MissingParentheses,
expression: node
expression: node,
statement: firstStatement
} : {
declaration,
kind: FixKind.MissingReturnStatement,
expression: node
expression: node,
statement: firstStatement
};
}
}
Expand All @@ -120,7 +125,8 @@ namespace ts.codefix {
return {
declaration,
kind: FixKind.MissingReturnStatement,
expression: node
expression: node,
statement: firstStatement
};
}
}
Expand Down Expand Up @@ -176,8 +182,8 @@ namespace ts.codefix {
}
}

function addReturnStatement(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: FunctionLikeDeclaration, expression: Expression) {
changes.replaceNode(sourceFile, declaration.body!, createBlock([createReturn(expression)]));
function addReturnStatement(changes: textChanges.ChangeTracker, sourceFile: SourceFile, expression: Expression, statement: Statement) {
changes.replaceNode(sourceFile, statement, createReturn(expression));
}

function removeBlockBodyBrace(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ArrowFunction, expression: Expression, withParen: boolean) {
Expand All @@ -188,8 +194,8 @@ namespace ts.codefix {
changes.replaceNode(sourceFile, declaration.body, createParen(expression));
}

function getActionForfixAddReturnStatement(context: CodeFixContext, declaration: FunctionLikeDeclaration, expression: Expression) {
const changes = textChanges.ChangeTracker.with(context, t => addReturnStatement(t, context.sourceFile, declaration, expression));
function getActionForfixAddReturnStatement(context: CodeFixContext, expression: Expression, statement: Statement) {
const changes = textChanges.ChangeTracker.with(context, t => addReturnStatement(t, context.sourceFile, expression, statement));
return createCodeFixAction(fixId, changes, Diagnostics.Add_a_return_statement, fixIdAddReturnStatement, Diagnostics.Add_all_missing_return_statement);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/cases/fourslash/codeFixSurmiseReturnValue_all1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
//// const baz5: ((() => number) | (() => A)) = () => {
//// bar: '1'
//// }
//// const baz6: () => number = () => { 1 }
////
//// const test: { a: () => A } = { a: () => { bar: '1' } }

Expand Down Expand Up @@ -114,8 +115,7 @@ const baz4: ((() => number) | (() => A)) = () => {
const baz5: ((() => number) | (() => A)) = () => {
return { bar: '1' }
}
const baz6: () => number = () => { return 1 }

const test: { a: () => A } = { a: () => {
return { bar: '1' }
} }`,
const test: { a: () => A } = { a: () => { return { bar: '1' } } }`,
});