Skip to content

Commit

Permalink
fix(46615): remove useless assertion. change error suggestion for res…
Browse files Browse the repository at this point in the history
…t params (#46628)
  • Loading branch information
a-tarasyuk authored Nov 3, 2021
1 parent d53630f commit ff75b8a
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 46 deletions.
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21221,9 +21221,10 @@ namespace ts {
(isCallSignatureDeclaration(param.parent) || isMethodSignature(param.parent) || isFunctionTypeNode(param.parent)) &&
param.parent.parameters.indexOf(param) > -1 &&
(resolveName(param, param.name.escapedText, SymbolFlags.Type, undefined, param.name.escapedText, /*isUse*/ true) ||
param.name.originalKeywordKind && isTypeNodeKind(param.name.originalKeywordKind))) {
param.name.originalKeywordKind && isTypeNodeKind(param.name.originalKeywordKind))) {
const newName = "arg" + param.parent.parameters.indexOf(param);
errorOrSuggestion(noImplicitAny, declaration, Diagnostics.Parameter_has_a_name_but_no_type_Did_you_mean_0_Colon_1, newName, declarationNameToString(param.name));
const typeName = declarationNameToString(param.name) + (param.dotDotDotToken ? "[]" : "");
errorOrSuggestion(noImplicitAny, declaration, Diagnostics.Parameter_has_a_name_but_no_type_Did_you_mean_0_Colon_1, newName, typeName);
return;
}
diagnostic = (declaration as ParameterDeclaration).dotDotDotToken ?
Expand Down
10 changes: 5 additions & 5 deletions src/services/codefixes/addNameToNamelessParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ namespace ts.codefix {

function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
const token = getTokenAtPosition(sourceFile, pos);
if (!isIdentifier(token)) {
return Debug.fail("add-name-to-nameless-parameter operates on identifiers, but got a " + Debug.formatSyntaxKind(token.kind));
}
const param = token.parent;
if (!isParameter(param)) {
return Debug.fail("Tried to add a parameter name to a non-parameter: " + Debug.formatSyntaxKind(token.kind));
}

const i = param.parent.parameters.indexOf(param);
Debug.assert(!param.type, "Tried to add a parameter name to a parameter that already had one.");
Debug.assert(i > -1, "Parameter not found in parent parameter list.");

const typeNode = factory.createTypeReferenceNode(param.name as Identifier, /*typeArguments*/ undefined);
const replacement = factory.createParameterDeclaration(
/*decorators*/ undefined,
param.modifiers,
param.dotDotDotToken,
"arg" + i,
param.questionToken,
factory.createTypeReferenceNode(token, /*typeArguments*/ undefined),
param.dotDotDotToken ? factory.createArrayTypeNode(typeNode) : typeNode,
param.initializer);
changeTracker.replaceNode(sourceFile, token, replacement);
changeTracker.replaceNode(sourceFile, param, replacement);
}
}
28 changes: 16 additions & 12 deletions tests/baselines/reference/noImplicitAnyNamelessParameter.errors.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(2,17): error TS7051: Parameter has a name but no type. Did you mean 'arg0: string'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(2,25): error TS7051: Parameter has a name but no type. Did you mean 'arg1: C'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(3,19): error TS7051: Parameter has a name but no type. Did you mean 'arg0: C'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(3,22): error TS7051: Parameter has a name but no type. Did you mean 'arg1: number'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(4,20): error TS7051: Parameter has a name but no type. Did you mean 'arg0: boolean'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(4,29): error TS7051: Parameter has a name but no type. Did you mean 'arg1: C'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(4,32): error TS7051: Parameter has a name but no type. Did you mean 'arg2: object'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(4,40): error TS7051: Parameter has a name but no type. Did you mean 'arg3: undefined'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(2,20): error TS7051: Parameter has a name but no type. Did you mean 'arg0: string[]'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(3,17): error TS7051: Parameter has a name but no type. Did you mean 'arg0: string'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(3,25): error TS7051: Parameter has a name but no type. Did you mean 'arg1: C'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(4,19): error TS7051: Parameter has a name but no type. Did you mean 'arg0: C'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(4,22): error TS7051: Parameter has a name but no type. Did you mean 'arg1: number'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(5,20): error TS7051: Parameter has a name but no type. Did you mean 'arg0: boolean'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(5,29): error TS7051: Parameter has a name but no type. Did you mean 'arg1: C'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(5,32): error TS7051: Parameter has a name but no type. Did you mean 'arg2: object'?
tests/cases/compiler/noImplicitAnyNamelessParameter.ts(5,40): error TS7051: Parameter has a name but no type. Did you mean 'arg3: undefined'?


==== tests/cases/compiler/noImplicitAnyNamelessParameter.ts (8 errors) ====
==== tests/cases/compiler/noImplicitAnyNamelessParameter.ts (9 errors) ====
class C { }
declare var x: (string, C) => void;
declare var a: { m(...string): void }
~~~~~~~~~
!!! error TS7051: Parameter has a name but no type. Did you mean 'arg0: string[]'?
declare var b: (string, C) => void;
~~~~~~
!!! error TS7051: Parameter has a name but no type. Did you mean 'arg0: string'?
~
!!! error TS7051: Parameter has a name but no type. Did you mean 'arg1: C'?
declare var y: { (C, number): void };
declare var c: { (C, number): void };
~
!!! error TS7051: Parameter has a name but no type. Did you mean 'arg0: C'?
~~~~~~
!!! error TS7051: Parameter has a name but no type. Did you mean 'arg1: number'?
declare var z: { m(boolean, C, object, undefined): void }
declare var d: { m(boolean, C, object, undefined): void }
~~~~~~~
!!! error TS7051: Parameter has a name but no type. Did you mean 'arg0: boolean'?
~
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/noImplicitAnyNamelessParameter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//// [noImplicitAnyNamelessParameter.ts]
class C { }
declare var x: (string, C) => void;
declare var y: { (C, number): void };
declare var z: { m(boolean, C, object, undefined): void }
declare var a: { m(...string): void }
declare var b: (string, C) => void;
declare var c: { (C, number): void };
declare var d: { m(boolean, C, object, undefined): void }
// note: null and void do not parse correctly without a preceding parameter name


Expand Down
35 changes: 20 additions & 15 deletions tests/baselines/reference/noImplicitAnyNamelessParameter.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
class C { }
>C : Symbol(C, Decl(noImplicitAnyNamelessParameter.ts, 0, 0))

declare var x: (string, C) => void;
>x : Symbol(x, Decl(noImplicitAnyNamelessParameter.ts, 1, 11))
>string : Symbol(string, Decl(noImplicitAnyNamelessParameter.ts, 1, 16))
>C : Symbol(C, Decl(noImplicitAnyNamelessParameter.ts, 1, 23))
declare var a: { m(...string): void }
>a : Symbol(a, Decl(noImplicitAnyNamelessParameter.ts, 1, 11))
>m : Symbol(m, Decl(noImplicitAnyNamelessParameter.ts, 1, 16))
>string : Symbol(string, Decl(noImplicitAnyNamelessParameter.ts, 1, 19))

declare var y: { (C, number): void };
>y : Symbol(y, Decl(noImplicitAnyNamelessParameter.ts, 2, 11))
>C : Symbol(C, Decl(noImplicitAnyNamelessParameter.ts, 2, 18))
>number : Symbol(number, Decl(noImplicitAnyNamelessParameter.ts, 2, 20))
declare var b: (string, C) => void;
>b : Symbol(b, Decl(noImplicitAnyNamelessParameter.ts, 2, 11))
>string : Symbol(string, Decl(noImplicitAnyNamelessParameter.ts, 2, 16))
>C : Symbol(C, Decl(noImplicitAnyNamelessParameter.ts, 2, 23))

declare var z: { m(boolean, C, object, undefined): void }
>z : Symbol(z, Decl(noImplicitAnyNamelessParameter.ts, 3, 11))
>m : Symbol(m, Decl(noImplicitAnyNamelessParameter.ts, 3, 16))
>boolean : Symbol(boolean, Decl(noImplicitAnyNamelessParameter.ts, 3, 19))
>C : Symbol(C, Decl(noImplicitAnyNamelessParameter.ts, 3, 27))
>object : Symbol(object, Decl(noImplicitAnyNamelessParameter.ts, 3, 30))
>undefined : Symbol(undefined, Decl(noImplicitAnyNamelessParameter.ts, 3, 38))
declare var c: { (C, number): void };
>c : Symbol(c, Decl(noImplicitAnyNamelessParameter.ts, 3, 11))
>C : Symbol(C, Decl(noImplicitAnyNamelessParameter.ts, 3, 18))
>number : Symbol(number, Decl(noImplicitAnyNamelessParameter.ts, 3, 20))

declare var d: { m(boolean, C, object, undefined): void }
>d : Symbol(d, Decl(noImplicitAnyNamelessParameter.ts, 4, 11))
>m : Symbol(m, Decl(noImplicitAnyNamelessParameter.ts, 4, 16))
>boolean : Symbol(boolean, Decl(noImplicitAnyNamelessParameter.ts, 4, 19))
>C : Symbol(C, Decl(noImplicitAnyNamelessParameter.ts, 4, 27))
>object : Symbol(object, Decl(noImplicitAnyNamelessParameter.ts, 4, 30))
>undefined : Symbol(undefined, Decl(noImplicitAnyNamelessParameter.ts, 4, 38))

// note: null and void do not parse correctly without a preceding parameter name

17 changes: 11 additions & 6 deletions tests/baselines/reference/noImplicitAnyNamelessParameter.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
class C { }
>C : C

declare var x: (string, C) => void;
>x : (string: any, C: any) => void
declare var a: { m(...string): void }
>a : { m(...string: any[]): void; }
>m : (...string: any[]) => void
>string : any[]

declare var b: (string, C) => void;
>b : (string: any, C: any) => void
>string : any
>C : any

declare var y: { (C, number): void };
>y : (C: any, number: any) => void
declare var c: { (C, number): void };
>c : (C: any, number: any) => void
>C : any
>number : any

declare var z: { m(boolean, C, object, undefined): void }
>z : { m(boolean: any, C: any, object: any, undefined: any): void; }
declare var d: { m(boolean, C, object, undefined): void }
>d : { m(boolean: any, C: any, object: any, undefined: any): void; }
>m : (boolean: any, C: any, object: any, undefined: any) => void
>boolean : any
>C : any
Expand Down
7 changes: 4 additions & 3 deletions tests/cases/compiler/noImplicitAnyNamelessParameter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @noImplicitAny: true
class C { }
declare var x: (string, C) => void;
declare var y: { (C, number): void };
declare var z: { m(boolean, C, object, undefined): void }
declare var a: { m(...string): void }
declare var b: (string, C) => void;
declare var c: { (C, number): void };
declare var d: { m(boolean, C, object, undefined): void }
// note: null and void do not parse correctly without a preceding parameter name
6 changes: 6 additions & 0 deletions tests/cases/fourslash/codeFixAddParameterNames2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitAny: true
////type Rest = ([|...number|]) => void;

verify.rangeAfterCodeFix("...arg0: number[]");
6 changes: 6 additions & 0 deletions tests/cases/fourslash/codeFixAddParameterNames3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitAny: true
////type Rest = ([|public string|]) => void;

verify.rangeAfterCodeFix("public arg0: string");

0 comments on commit ff75b8a

Please sign in to comment.