Query module InconsistentNew

name
Inconsistent use of ‘new’
description
If a function is intended to be a constructor, it should always be invoked with ‘new’. Otherwise, it should always be invoked as a normal function, that is, without ‘new’.
kind
problem
problem.severity
warning
id
js/inconsistent-use-of-new
tags
reliability correctness language-features
precision
very-high

Imports

RestrictedLocations

Provides classes for restricting the locations reported for program elements.

javascript

Provides classes for working with JavaScript programs, as well as JSON, YAML and HTML.

Predicates

calls

Holds if callee is a function that may be invoked at callsite cs, where imprecision is a heuristic measure of how likely it is that callee is only suggested as a potential callee due to imprecise analysis of global variables and is not, in fact, a viable callee at all.

getALikelyCallee

Gets a function that may be invoked at cs, preferring callees that are less likely to be derived due to analysis imprecision and excluding whitelisted call sites and callees. Additionally, isNew is bound to true if cs is a new expression, and to false otherwise.

getFirstInvocation

Get the new or call (depending on whether isNew is true or false) of f that comes first under a lexicographical ordering by file path, start line and start column.

guardsAgainstMissingNew

Holds if f contains code to guard against being invoked without new.

whitelistedCall

Holds if call should be whitelisted because it cannot cause problems with inconsistent new.

whitelistedCallee

Holds if f should be whitelisted, either because it guards against inconsistent new or we do not want to report it.