@@ -654,6 +654,7 @@ export class SimpleLanguageService implements ISimpleLanguageService {
654
654
! isFunctionDeclaration ( current ) &&
655
655
! isMethodDeclaration ( current ) &&
656
656
! isArrowFunction ( current ) &&
657
+ ! isPropertyDeclaration ( current ) &&
657
658
! isSourceFile ( current ) ) {
658
659
if ( current . parent == null ) break ;
659
660
current = current . parent ;
@@ -680,6 +681,16 @@ export class SimpleLanguageService implements ISimpleLanguageService {
680
681
return name == null ? SimpleLanguageService . GLOBAL : name ;
681
682
}
682
683
684
+ if (
685
+ isPropertyDeclaration ( block )
686
+ ) {
687
+ if ( block . parent == null ) {
688
+ const name = this . getName ( block ) ;
689
+ return name == null ? SimpleLanguageService . GLOBAL : name ;
690
+ }
691
+ return this . traceBlockScopeName ( block . parent ) ;
692
+ }
693
+
683
694
if ( isArrowFunction ( block ) ) {
684
695
return SimpleLanguageService . ANONYMOUS ;
685
696
}
@@ -704,14 +715,24 @@ export class SimpleLanguageService implements ISimpleLanguageService {
704
715
}
705
716
706
717
if (
707
- isMethodDeclaration ( block ) ||
708
718
isClassDeclaration ( block ) ||
709
719
isClassExpression ( block )
710
720
) {
711
721
const name = this . getName ( block ) ;
712
722
return name == null ? SimpleLanguageService . GLOBAL : name ;
713
723
}
714
724
725
+ if (
726
+ isMethodDeclaration ( block ) ||
727
+ isPropertyDeclaration ( block )
728
+ ) {
729
+ if ( block . parent == null ) {
730
+ const name = this . getName ( block ) ;
731
+ return name == null ? SimpleLanguageService . GLOBAL : name ;
732
+ }
733
+ return this . traceThis ( block . parent ) ;
734
+ }
735
+
715
736
if ( isArrowFunction ( block ) ) {
716
737
return block . parent == null ? SimpleLanguageService . GLOBAL : this . traceThis ( block . parent ) ;
717
738
}
@@ -1808,7 +1829,7 @@ export class SimpleLanguageService implements ISimpleLanguageService {
1808
1829
1809
1830
else if ( isIClassDeclaration ( substitution ) ) {
1810
1831
const statics = part . name !== "this" && part . name === substitution . name && ! hadNewExpression ;
1811
- sub = this . stringifyIClassDeclaration ( substitution , statics ) ;
1832
+ sub = this . stringifyIClassDeclaration ( substitution , statics , part . name , scope ) ;
1812
1833
}
1813
1834
1814
1835
else if ( isIEnumDeclaration ( substitution ) ) {
@@ -3329,27 +3350,30 @@ export class SimpleLanguageService implements ISimpleLanguageService {
3329
3350
return < string > this . marshaller . marshal < ArbitraryValue , string > ( flattened , "" ) ;
3330
3351
}
3331
3352
3332
- private stringifyIClassDeclaration ( classDeclaration : IClassDeclaration , statics : boolean ) : string {
3353
+ private stringifyIClassDeclaration ( classDeclaration : IClassDeclaration , statics : boolean , identifier : string , scope : string | null ) : string {
3333
3354
const map : { [ key : string ] : string | null | undefined } = { } ;
3334
3355
3335
- Object . keys ( classDeclaration . props ) . forEach ( propKey => {
3336
- if ( statics && ! classDeclaration . props [ propKey ] . isStatic ) return ;
3337
- if ( ! statics && classDeclaration . props [ propKey ] . isStatic ) return ;
3356
+ for ( const propKey of Object . keys ( classDeclaration . props ) ) {
3357
+ if ( statics && ! classDeclaration . props [ propKey ] . isStatic ) continue ;
3358
+ if ( ! statics && classDeclaration . props [ propKey ] . isStatic ) continue ;
3338
3359
3339
3360
const value = classDeclaration . props [ propKey ] . value ;
3340
3361
map [ propKey ] = value . hasDoneFirstResolve ( ) ? value . resolved : value . resolve ( ) ;
3362
+ }
3341
3363
3342
- } ) ;
3343
-
3344
- Object . keys ( classDeclaration . methods ) . forEach ( methodKey => {
3345
- if ( statics && ! classDeclaration . methods [ methodKey ] . isStatic ) return ;
3346
- if ( ! statics && classDeclaration . methods [ methodKey ] . isStatic ) return ;
3364
+ for ( const methodKey of Object . keys ( classDeclaration . methods ) ) {
3365
+ if ( statics && ! classDeclaration . methods [ methodKey ] . isStatic ) continue ;
3366
+ if ( ! statics && classDeclaration . methods [ methodKey ] . isStatic ) continue ;
3347
3367
3348
3368
const method = classDeclaration . methods [ methodKey ] ;
3349
3369
const value = method . value ;
3350
3370
3351
3371
const hasReturnStatement = method . returnStatementStartsAt >= 0 ;
3352
3372
let resolvedValue = value . hasDoneFirstResolve ( ) ? value . resolved : value . resolve ( ) ;
3373
+
3374
+ // We have a self-reference here. Since 'this' refers to the mapped object, we just need to return "this".
3375
+ if ( identifier === "this" && scope === classDeclaration . name ) return "this" ;
3376
+
3353
3377
if ( this . mostProbableTypeOf ( resolvedValue ) === "string" && resolvedValue != null && ! ( resolvedValue . trim ( ) . startsWith ( "return" ) ) ) resolvedValue = < string > this . quoteIfNecessary ( resolvedValue ) ;
3354
3378
3355
3379
const startsWithReturn = hasReturnStatement && resolvedValue != null && resolvedValue . trim ( ) . startsWith ( "return" ) ;
@@ -3358,8 +3382,7 @@ export class SimpleLanguageService implements ISimpleLanguageService {
3358
3382
const parameters = this . stringifyIParameterBody ( method . parameters ) ;
3359
3383
3360
3384
map [ methodKey ] = `(function ${ SimpleLanguageService . FUNCTION_OUTER_SCOPE_NAME } (${ parameters } ) ${ bracketed } )` ;
3361
-
3362
- } ) ;
3385
+ }
3363
3386
3364
3387
return < string > this . marshaller . marshal < ArbitraryValue , string > ( map , "" ) ;
3365
3388
}
0 commit comments