Skip to content

Commit 4487917

Browse files
authored
Quick fix for no-implicit-any errors to add explicit type annotation (#14786)
* Infer from usage quick fix * Change full function singature * Add property/element access support * Fix a few issues * Some cleanup * Expose getArrayType and getPromiseType * Switch to collecting all usage before infering * Infer array and promise type arguments * Handel enums in binary operators * consolidate usage of addCandidateTypes * Handel rest paramters * Properly handel `+=` and `+` inference for numbers and strings * Add print quickfixes debug helper * Add rest param tests * Add optional paramter tests * Handel set accessors * Support getters * Support no implicit any error for variable at use site * Support properties * Only offer quick fix if an infered type other than any is available * Rename functions * Move to a separate namespace * Check cancellation token * Cleanup * Check for accesibile symbols where serializing types * Remove JS support * Reorganize functions * Mark APIs as internal * Fix lint errors * Removed conflict markers. * Update 'createSymbol' to use '__String'. * Fixed most problems relating to '__String' and 'includeJsDocComments' in the fix itself. * Addressed most API changes. * Make all helpers internal * Use a diffrent writer and not the built-in single line write * Infer types for all parameters in a parameter list instead of one at a time * Accept baselines * Code review commments * Respond to code review comments
1 parent b5e6b89 commit 4487917

26 files changed

+893
-9
lines changed

Diff for: src/compiler/checker.ts

+18
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,23 @@ namespace ts {
226226
return tryFindAmbientModule(moduleName, /*withAugmentations*/ false);
227227
},
228228
getApparentType,
229+
getUnionType,
230+
createAnonymousType,
231+
createSignature,
232+
createSymbol,
233+
createIndexInfo,
234+
getAnyType: () => anyType,
235+
getStringType: () => stringType,
236+
getNumberType: () => numberType,
237+
createPromiseType,
238+
createArrayType,
239+
getBooleanType: () => booleanType,
240+
getVoidType: () => voidType,
241+
getUndefinedType: () => undefinedType,
242+
getNullType: () => nullType,
243+
getESSymbolType: () => esSymbolType,
244+
getNeverType: () => neverType,
245+
isSymbolAccessible,
229246
isArrayLikeType,
230247
getAllPossiblePropertiesOfTypes,
231248
getSuggestionForNonexistentProperty: (node, type) => getSuggestionForNonexistentProperty(node, type),
@@ -3675,6 +3692,7 @@ namespace ts {
36753692

36763693
function buildParameterDisplay(p: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, symbolStack?: Symbol[]) {
36773694
const parameterNode = <ParameterDeclaration>p.valueDeclaration;
3695+
36783696
if (parameterNode ? isRestParameter(parameterNode) : isTransientSymbol(p) && p.isRestParameter) {
36793697
writePunctuation(writer, SyntaxKind.DotDotDotToken);
36803698
}

Diff for: src/compiler/core.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,13 @@ namespace ts {
213213
return undefined;
214214
}
215215

216-
export function zipWith<T, U>(arrayA: ReadonlyArray<T>, arrayB: ReadonlyArray<U>, callback: (a: T, b: U, index: number) => void): void {
216+
export function zipWith<T, U, V>(arrayA: ReadonlyArray<T>, arrayB: ReadonlyArray<U>, callback: (a: T, b: U, index: number) => V): V[] {
217+
const result: V[] = [];
217218
Debug.assert(arrayA.length === arrayB.length);
218219
for (let i = 0; i < arrayA.length; i++) {
219-
callback(arrayA[i], arrayB[i], i);
220+
result.push(callback(arrayA[i], arrayB[i], i));
220221
}
222+
return result;
221223
}
222224

223225
export function zipToMap<T>(keys: ReadonlyArray<string>, values: ReadonlyArray<T>): Map<T> {

Diff for: src/compiler/diagnosticMessages.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -3681,6 +3681,7 @@
36813681
"category": "Message",
36823682
"code": 90017
36833683
},
3684+
36843685
"Disable checking for this file.": {
36853686
"category": "Message",
36863687
"code": 90018
@@ -3725,7 +3726,6 @@
37253726
"category": "Message",
37263727
"code": 90028
37273728
},
3728-
37293729
"Convert function to an ES2015 class": {
37303730
"category": "Message",
37313731
"code": 95001
@@ -3734,34 +3734,36 @@
37343734
"category": "Message",
37353735
"code": 95002
37363736
},
3737-
37383737
"Extract symbol": {
37393738
"category": "Message",
37403739
"code": 95003
37413740
},
3742-
37433741
"Extract to {0} in {1}": {
37443742
"category": "Message",
37453743
"code": 95004
37463744
},
3747-
37483745
"Extract function": {
37493746
"category": "Message",
37503747
"code": 95005
37513748
},
3752-
37533749
"Extract constant": {
37543750
"category": "Message",
37553751
"code": 95006
37563752
},
3757-
37583753
"Extract to {0} in enclosing scope": {
37593754
"category": "Message",
37603755
"code": 95007
37613756
},
3762-
37633757
"Extract to {0} in {1} scope": {
37643758
"category": "Message",
37653759
"code": 95008
3760+
},
3761+
"Infer type of '{0}' from usage.": {
3762+
"category": "Message",
3763+
"code": 95009
3764+
},
3765+
"Infer parameter types from usage.": {
3766+
"category": "Message",
3767+
"code": 95010
37663768
}
37673769
}

Diff for: src/compiler/types.ts

+18
Original file line numberDiff line numberDiff line change
@@ -2695,6 +2695,24 @@ namespace ts {
26952695
getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string | undefined;
26962696
/* @internal */ getBaseConstraintOfType(type: Type): Type | undefined;
26972697

2698+
/* @internal */ getAnyType(): Type;
2699+
/* @internal */ getStringType(): Type;
2700+
/* @internal */ getNumberType(): Type;
2701+
/* @internal */ getBooleanType(): Type;
2702+
/* @internal */ getVoidType(): Type;
2703+
/* @internal */ getUndefinedType(): Type;
2704+
/* @internal */ getNullType(): Type;
2705+
/* @internal */ getESSymbolType(): Type;
2706+
/* @internal */ getNeverType(): Type;
2707+
/* @internal */ getUnionType(types: Type[], subtypeReduction?: boolean): Type;
2708+
/* @internal */ createArrayType(elementType: Type): Type;
2709+
/* @internal */ createPromiseType(type: Type): Type;
2710+
2711+
/* @internal */ createAnonymousType(symbol: Symbol, members: SymbolTable, callSignatures: Signature[], constructSignatures: Signature[], stringIndexInfo: IndexInfo, numberIndexInfo: IndexInfo): Type;
2712+
/* @internal */ createSignature(declaration: SignatureDeclaration, typeParameters: TypeParameter[], thisParameter: Symbol | undefined, parameters: Symbol[], resolvedReturnType: Type, typePredicate: TypePredicate, minArgumentCount: number, hasRestParameter: boolean, hasLiteralTypes: boolean): Signature;
2713+
/* @internal */ createSymbol(flags: SymbolFlags, name: __String): TransientSymbol;
2714+
/* @internal */ createIndexInfo(type: Type, isReadonly: boolean, declaration?: SignatureDeclaration): IndexInfo;
2715+
/* @internal */ isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags, shouldComputeAliasToMarkVisible: boolean): SymbolAccessibilityResult;
26982716
/* @internal */ tryFindAmbientModuleWithoutAugmentations(moduleName: string): Symbol | undefined;
26992717

27002718
/* @internal */ getSymbolWalker(accept?: (symbol: Symbol) => boolean): SymbolWalker;

Diff for: src/compiler/utilities.ts

+8
Original file line numberDiff line numberDiff line change
@@ -5649,6 +5649,14 @@ namespace ts {
56495649
return node.kind >= SyntaxKind.FirstJSDocTagNode && node.kind <= SyntaxKind.LastJSDocTagNode;
56505650
}
56515651

5652+
export function isSetAccessor(node: Node): node is SetAccessorDeclaration {
5653+
return node.kind === SyntaxKind.SetAccessor;
5654+
}
5655+
5656+
export function isGetAccessor(node: Node): node is GetAccessorDeclaration {
5657+
return node.kind === SyntaxKind.GetAccessor;
5658+
}
5659+
56525660
/** True if has jsdoc nodes attached to it. */
56535661
/* @internal */
56545662
export function hasJSDocNodes(node: Node): node is HasJSDoc {

Diff for: src/services/codefixes/fixes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
/// <reference path='importFixes.ts' />
1414
/// <reference path='disableJsDiagnostics.ts' />
1515
/// <reference path='helpers.ts' />
16+
/// <reference path='inferFromUsage.ts' />

0 commit comments

Comments
 (0)