10000 Quick fix for functions lacking return expressions by Kingwl · Pull Request #26434 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

Quick fix for functions lacking return expressions #26434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Apr 2, 2020
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
Merge branch 'master' into returnValueSurmise
  • Loading branch information
Kingwl committed Apr 29, 2019
commit 7cfd0fb1db39e0ac93431abdd1fbfcddd52f18c3
54 changes: 49 additions & 5 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4911,24 +4911,68 @@
"category": "Message",
"code": 95064
},
"Add a return statement": {
"Convert to async function": {
"category": "Message",
"code": 95065
},
"Remove block body braces": {
"Convert all to async functions": {
"category": "Message",
"code": 95066
},
"Add 'unknown' conversion for non-overlapping types": {
"category": "Message",
"code": 95069
},
"Add 'unknown' to all conversions of non-overlapping types": {
"category": "Message",
"code": 95070
},
"Add missing 'new' operator to call": {
"category": "Message",
"code": 95071
},
"Add missing 'new' operator to all calls": {
"category": "Message",
"code": 95072
},
"Add names to all parameters without names": {
"category": "Message",
"code": 95073
},
"Enable the 'experimentalDecorators' option in your configuration file": {
"category": "Message",
"code": 95074
},
"Convert parameters to destructured object": {
"category": "Message",
"code": 95075
},
"Allow accessing UMD globals from modules.": {
"category": "Message",
"code": 95076
},
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer." :{
"category": "Error",
"code": 18004
},
"Add a return statement": {
"category": "Message",
"code": 95077
},
"Remove block body braces": {
"category": "Message",
"code": 95078
},
"Replace braces with parentheses": {
"category": "Message",
"code": 95067
"code": 95079
},
"Wrap this block with parentheses": {
"category": "Message",
"code": 95068
"code": 95080
},
"Surmise all return value": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got to admit: I don't know what the word surmise means, and I don't know if users will either. What's the intent here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Correct all return expressions" would be best here

"surmise" is not in basic English vocabulary and we need to avoid it entirely for clarity's sake.

"category": "Message",
"code": 95069
"code": 95081
}
}
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3260,7 +3260,7 @@ namespace ts {
* True if `contextualType` should not be considered for completions because
* e.g. it specifies `kind: "a"` and obj has `kind: "b"`.
*/
8000 /* @internal */ isTypeInvalidDueToUnionDiscriminant(contextualType: Type, obj: ObjectLiteralExpression): boolean;
/* @internal */ isTypeInvalidDueToUnionDiscriminant(contextualType: Type, obj: ObjectLiteralExpression | JsxAttributes): boolean;
/* @internal */ isTypeAssignableTo(type1: Type, type2: Type): boolean;
/**
* For a union, will include a property if it's defined in *any* of the member types.
Expand Down
93 changes: 93 additions & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1903,4 +1903,97 @@ namespace ts {
export function needsParentheses(expression: Expression) {
return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression);
}

export function getContextualTypeFromParent(node: Expression, checker: TypeChecker): Type | undefined {
const { parent } = node;
switch (parent.kind) {
case SyntaxKind.NewExpression:
return checker.getContextualType(parent as NewExpression);
case SyntaxKind.BinaryExpression: {
const { left, operatorToken, right } = parent as BinaryExpression;
return isEqualityOperatorKind(operatorToken.kind)
? checker.getTypeAtLocation(node === right ? left : right)
: checker.getContextualType(node);
}
case SyntaxKind.CaseClause:
return (parent as CaseClause).expression === node ? getSwitchedType(parent as CaseClause, checker) : undefined;
default:
return checker.getContextualType(node);
}
}

export function quote(text: string, preferences: UserPreferences): string {
if (/^\d+$/.test(text)) {
return text;
}
// Editors can pass in undefined or empty string - we want to infer the preference in those cases.
const quotePreference = preferences.quotePreference || "auto";
const quoted = JSON.stringify(text);
switch (quotePreference) {
// TODO use getQuotePreference to infer the actual quote style.
case "auto":
case "double":
return quoted;
case "single":
return `'${stripQuotes(quoted).replace("'", "\\'").replace('\\"', '"')}'`;
default:
return Debug.assertNever(quotePreference);
}
}

export function isEqualityOperatorKind(kind: SyntaxKind): kind is EqualityOperator {
switch (kind) {
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.EqualsEqualsToken:
case SyntaxKind.ExclamationEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
return true;
default:
return false;
}
}

export function isStringLiteralOrTemplate(node: Node): node is StringLiteralLike | TemplateExpression | TaggedTemplateExpression {
switch (node.kind) {
case SyntaxKind.StringLiteral:
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.TemplateExpression:
case SyntaxKind.TaggedTemplateExpression:
return true;
default:
return false;
}
}

export function hasIndexSignature(type: Type): boolean {
return !!type.getStringIndexType() || !!type.getNumberIndexType();
}

export function getSwitchedType(caseClause: CaseClause, checker: TypeChecker): Type | undefined {
return checker.getTypeAtLocation(caseClause.parent.parent.expression);
}

export function getTypeNodeIfAccessible(type: Type, enclosingScope: Node, program: Program, host: LanguageServiceHost): TypeNode | undefined {
const checker = program.getTypeChecker();
let typeIsAccessible = true;
const notAccessible = () => { typeIsAccessible = false; };
const res = checker.typeToTypeNode(type, enclosingScope, /*flags*/ undefined, {
trackSymbol: (symbol, declaration, meaning) => {
// TODO: GH#18217
typeIsAccessible = typeIsAccessible && checker.isSymbolAccessible(symbol, declaration, meaning!, /*shouldComputeAliasToMarkVisible*/ false).accessibility === SymbolAccessibility.Accessible;
},
reportInaccessibleThisError: notAccessible,
reportPrivateInBaseOfClassExpression: notAccessible,
reportInaccessibleUniqueSymbolError: notAccessible,
moduleResolverHost: {
readFile: host.readFile,
fileExists: host.fileExists,
directoryExists: host.directoryExists,
getSourceFiles: program.getSourceFiles,
getCurrentDirectory: program.getCurrentDirectory,
getCommonSourceDirectory: program.getCommonSourceDirectory,
}
});
return typeIsAccessible ? res : undefined;
}
}
You are viewing a condensed version of this merge commit. You can view the full changes here.
0