@@ -35,6 +35,13 @@ export class Rule extends Lint.Rules.TypedRule {
35
35
}
36
36
}
37
37
38
+ const formatFlags = ts . TypeFormatFlags . UseStructuralFallback
39
+ | ts . TypeFormatFlags . UseFullyQualifiedType
40
+ | ts . TypeFormatFlags . UseAliasDefinedOutsideCurrentScope
41
+ | ts . TypeFormatFlags . NoTruncation
42
+ | ts . TypeFormatFlags . WriteClassExpressionAsTypeLiteral
43
+ | ts . TypeFormatFlags . WriteArrowStyleSignature ;
44
+
38
45
function walk ( ctx : Lint . WalkContext < IOptions > , checker : ts . TypeChecker ) {
39
46
return ts . forEachChild ( ctx . sourceFile , function cb ( node ) : void {
40
47
switch ( node . kind ) {
@@ -190,7 +197,7 @@ function walk(ctx: Lint.WalkContext<IOptions>, checker: ts.TypeChecker) {
190
197
191
198
// TODO this could use a little more effort
192
199
function typesAreEqual ( a : ts . Type , b : ts . Type ) : boolean {
193
- return a === b || checker . typeToString ( a ) === checker . typeToString ( b ) ;
200
+ return a === b || checker . typeToString ( a , undefined , formatFlags ) === checker . typeToString ( b , undefined , formatFlags ) ;
194
201
}
195
202
196
203
function getContextualTypeOfFunction ( func : FunctionExpressionLike ) : ts . Type | undefined {
@@ -277,11 +284,11 @@ function walk(ctx: Lint.WalkContext<IOptions>, checker: ts.TypeChecker) {
277
284
case 1 :
278
285
return [ signatures [ 0 ] , true ] ;
279
286
default :
280
- const str = checker . signatureToString ( signatures [ 0 ] ) ;
287
+ const str = checker . signatureToString ( signatures [ 0 ] , undefined , formatFlags ) ;
281
288
const withoutReturn = removeSignatureReturn ( str ) ;
282
289
let returnUsable = true ;
283
290
for ( let i = 1 ; i < signatures . length ; ++ i ) { // check if all signatures are the same
284
- const sig = checker . signatureToString ( signatures [ i ] ) ;
291
+ const sig = checker . signatureToString ( signatures [ i ] , undefined , formatFlags ) ;
285
292
if ( str !== sig ) {
286
293
if ( withoutReturn !== removeSignatureReturn ( sig ) )
287
294
return ;
@@ -293,17 +300,11 @@ function walk(ctx: Lint.WalkContext<IOptions>, checker: ts.TypeChecker) {
293
300
}
294
301
}
295
302
296
- function removeSignatureReturn ( str : string ) {
297
- let open = 1 ;
298
- let i = 1 ;
299
- for ( ; open !== 0 ; ++ i ) {
300
- if ( str [ i ] === '(' ) {
301
- ++ open ;
302
- } else if ( str [ i ] === ')' ) {
303
- -- open ;
304
- }
305
- }
306
- return str . substr ( 0 , i + 1 ) ;
303
+ function removeSignatureReturn ( str : string ) : string {
304
+ const sourceFile = ts . createSourceFile ( 'tmp.ts' , `var a:${ str } ` , ts . ScriptTarget . ESNext ) ;
305
+ const signature = < ts . FunctionOrConstructorTypeNode > ( < ts . VariableStatement > sourceFile . statements [ 0 ] )
306
+ . declarationList . declarations [ 0 ] . type ! ;
307
+ return sourceFile . text . substring ( 6 , signature . parameters . end + 1 ) ;
307
308
}
308
309
309
310
function getSignaturesOfType ( type : ts . Type ) : ts . Signature [ ] {
0 commit comments