@@ -466,6 +466,44 @@ DataFlow::SourceNode moduleMember(string path, string m) {
466466 )
467467}
468468
469+ /**
470+ * The string `method`, `getter`, or `setter`, representing the kind of a function member
471+ * in a class.
472+ *
473+ * Can can be used with `ClassNode.getInstanceMember` to obtain members of a specific kind.
474+ */
475+ class MemberKind extends string {
476+ MemberKind ( ) { this = "method" or this = "getter" or this = "setter" }
477+ }
478+
479+ module MemberKind {
480+ /** The kind of a method, such as `m() {}` */
481+ MemberKind method ( ) { result = "method" }
482+
483+ /** The kind of a getter accessor, such as `get f() {}`. */
484+ MemberKind getter ( ) { result = "getter" }
485+
486+ /** The kind of a setter accessor, such as `set f() {}`. */
487+ MemberKind setter ( ) { result = "setter" }
488+
489+ /** The `getter` and `setter` kinds. */
490+ MemberKind accessor ( ) { result = getter ( ) or result = setter ( ) }
491+
492+ /**
493+ * Gets the member kind of a given syntactic member declaration in a class.
494+ */
495+ MemberKind of ( MemberDeclaration decl ) {
496+ decl instanceof GetterMethodDeclaration and result = getter ( )
497+ or
498+ decl instanceof SetterMethodDeclaration and result = setter ( )
499+ or
500+ decl instanceof MethodDeclaration and
501+ not decl instanceof AccessorMethodDeclaration and
502+ not decl instanceof ConstructorDeclaration and
503+ result = method ( )
504+ }
505+ }
506+
469507/**
470508 * A data flow node corresponding to a class definition or a function definition
471509 * acting as a class.
@@ -516,7 +554,7 @@ class ClassNode extends DataFlow::SourceNode {
516554 *
517555 * Does not include methods from superclasses.
518556 */
519- FunctionNode getInstanceMethod ( string name ) { result = impl .getInstanceMethod ( name ) }
557+ FunctionNode getInstanceMethod ( string name ) { result = impl .getInstanceMember ( name , MemberKind :: method ( ) ) }
520558
521559 /**
522560 * Gets an instance method declared in this class.
@@ -525,7 +563,28 @@ class ClassNode extends DataFlow::SourceNode {
525563 *
526564 * Does not include methods from superclasses.
527565 */
528- FunctionNode getAnInstanceMethod ( ) { result = impl .getAnInstanceMethod ( ) }
566+ FunctionNode getAnInstanceMethod ( ) { result = impl .getAnInstanceMember ( MemberKind:: method ( ) ) }
567+
568+ /**
569+ * Gets the instance method, getter, or setter with the given name and kind.
570+ *
571+ * Does not include members from superclasses.
572+ */
573+ FunctionNode getInstanceMember ( string name , MemberKind kind ) { result = impl .getInstanceMember ( name , kind ) }
574+
575+ /**
576+ * Gets an instance method, getter, or setter with the given kind.
577+ *
578+ * Does not include members from superclasses.
579+ */
580+ FunctionNode getAnInstanceMember ( MemberKind kind ) { result = impl .getAnInstanceMember ( kind ) }
581+
582+ /**
583+ * Gets an instance method, getter, or setter declared in this class.
584+ *
585+ * Does not include members from superclasses.
586+ */
587+ FunctionNode getAnInstanceMember ( ) { result = impl .getAnInstanceMember ( _) }
529588
530589 /**
531590 * Gets the static method declared in this class with the given name.
@@ -576,16 +635,14 @@ module ClassNode {
576635 abstract FunctionNode getConstructor ( ) ;
577636
578637 /**
579- * Gets an instance method with the given name, if any .
638+ * Gets the instance member with the given name and kind .
580639 */
581- abstract FunctionNode getInstanceMethod ( string name ) ;
640+ abstract FunctionNode getInstanceMember ( string name , MemberKind kind ) ;
582641
583642 /**
584- * Gets an instance method of this class.
585- *
586- * The constructor is not considered an instance method.
643+ * Gets an instance member with the given kind.
587644 */
588- abstract FunctionNode getAnInstanceMethod ( ) ;
645+ abstract FunctionNode getAnInstanceMember ( MemberKind kind ) ;
589646
590647 /**
591648 * Gets the static method of this class with the given name.
@@ -618,20 +675,20 @@ module ClassNode {
618675
619676 override FunctionNode getConstructor ( ) { result = astNode .getConstructor ( ) .getBody ( ) .flow ( ) }
620677
621- override FunctionNode getInstanceMethod ( string name ) {
678+ override FunctionNode getInstanceMember ( string name , MemberKind kind ) {
622679 exists ( MethodDeclaration method |
623680 method = astNode .getMethod ( name ) and
624681 not method .isStatic ( ) and
625- not method instanceof ConstructorDeclaration and
682+ kind = MemberKind :: of ( method ) and
626683 result = method .getBody ( ) .flow ( )
627684 )
628685 }
629686
630- override FunctionNode getAnInstanceMethod ( ) {
687+ override FunctionNode getAnInstanceMember ( MemberKind kind ) {
631688 exists ( MethodDeclaration method |
632689 method = astNode .getAMethod ( ) and
633690 not method .isStatic ( ) and
634- not method instanceof ConstructorDeclaration and
691+ kind = MemberKind :: of ( method ) and
635692 result = method .getBody ( ) .flow ( )
636693 )
637694 }
@@ -671,12 +728,36 @@ module ClassNode {
671728
672729 override FunctionNode getConstructor ( ) { result = this }
673730
674- override FunctionNode getInstanceMethod ( string name ) {
731+ private PropertyAccessor getAnAccessor ( MemberKind kind ) {
732+ result .getObjectExpr ( ) = getAPrototypeReference ( ) .asExpr ( ) and
733+ (
734+ kind = MemberKind:: getter ( ) and
735+ result instanceof PropertyGetter
736+ or
737+ kind = MemberKind:: setter ( ) and
738+ result instanceof PropertySetter
739+ )
740+ }
741+
742+ override FunctionNode getInstanceMember ( string name , MemberKind kind ) {
743+ kind = MemberKind:: method ( ) and
675744 result = getAPrototypeReference ( ) .getAPropertySource ( name )
745+ or
746+ exists ( PropertyAccessor accessor |
747+ accessor = getAnAccessor ( kind ) and
748+ accessor .getName ( ) = name and
749+ result = accessor .getInit ( ) .flow ( )
750+ )
676751 }
677752
678- override FunctionNode getAnInstanceMethod ( ) {
753+ override FunctionNode getAnInstanceMember ( MemberKind kind ) {
754+ kind = MemberKind:: method ( ) and
679755 result = getAPrototypeReference ( ) .getAPropertyWrite ( ) .getRhs ( ) .getALocalSource ( )
756+ or
757+ exists ( PropertyAccessor accessor |
758+ accessor = getAnAccessor ( kind ) and
759+ result = accessor .getInit ( ) .flow ( )
760+ )
680761 }
681762
682763 override FunctionNode getStaticMethod ( string name ) {
0 commit comments