/*** @name Wrong use of 'this' for static method* @description A reference to a static method from within an instance method needs to be qualified with the class name, not `this`.* @kind problem* @problem.severity error* @id js/mixed-static-instance-this-access* @tags correctness* methods* @precision high*/import javascript/** Holds if `base` declares or inherits method `m` with the given `name`. */predicate hasMethod(ClassDefinition base, string name, MethodDeclaration m) {m = base.getMethod(name) orhasMethod(base.getSuperClassDefinition(), name, m)}/*** Holds if `access` is in`fromMethod`, and it references `toMethod` through `this`,* where `fromMethod` and `toMethod` are of kind `fromKind` and `toKind`, respectively.*/predicate isLocalMethodAccess(PropAccess access, MethodDefinition fromMethod, string fromKind, MethodDeclaration toMethod,string toKind) {hasMethod(fromMethod.getDeclaringClass(), access.getPropertyName(), toMethod) andaccess.getEnclosingFunction() = fromMethod.getBody() andaccess.getBase() instanceof ThisExpr andfromKind = getKind(fromMethod) andtoKind = getKind(toMethod)}string getKind(MethodDeclaration m) {if m.isStatic() then result = "static" else result = "instance"}fromPropAccess access, MethodDefinition fromMethod, MethodDefinition toMethod, string fromKind,string toKindwhereisLocalMethodAccess(access, fromMethod, fromKind, toMethod, toKind) andtoKind != fromKind and// exceptionsnot (// the class has a second member with the same name and the right kindisLocalMethodAccess(access, fromMethod, _, _, fromKind)or// there is a dynamically assigned second member with the same name and the right kindexists(AnalyzedPropertyWrite apw, AbstractClass declaringClass, AbstractValue base |"static" = fromKind and base = declaringClassor"instance" = fromKind and base = TAbstractInstance(declaringClass)|declaringClass = TAbstractClass(fromMethod.getDeclaringClass()) andapw.writes(base, access.getPropertyName(), _))or// the access is an assignment, probably deliberateaccess instanceof LValue)select access,"Access to " + toKind + " method $@ from " + fromKind +" method $@ is not possible through `this`.", toMethod, toMethod.getName(), fromMethod,fromMethod.getName()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。