JS: Merge getInferredName() => getName()

This commit is contained in:
Asger F
2019-07-16 11:21:33 +01:00
parent b2da2d2a4b
commit 7da341ba04
11 changed files with 20 additions and 32 deletions

View File

@@ -14,17 +14,14 @@ class ClassOrInterface extends @classorinterface, TypeParameterized {
/** Gets the identifier naming the declared type, if any. */
Identifier getIdentifier() { none() } // Overridden in subtypes.
/** Gets the name of the defined class or interface, if any. */
string getName() { result = getIdentifier().getName() }
/**
* Gets the name of the defined class or interface, possibly inferred
* from the context if this is an anonymous class expression.
*
* Has no result if no name could be determined.
*/
string getInferredName() {
result = getName() // Overridden in ClassExpr
string getName() {
result = getIdentifier().getName() // Overridden in ClassExpr
}
/** Gets the nearest enclosing function or toplevel in which this class or interface occurs. */
@@ -175,8 +172,8 @@ class ClassDefinition extends @classdefinition, ClassOrInterface, AST::ValueNode
*/
private string inferNameFromVarDef() {
// in ambiguous cases like `let C = class D {}`, prefer `D` to `C`
if exists(getName())
then result = "class " + getName()
if exists(getIdentifier())
then result = "class " + getIdentifier().getName()
else
exists(VarDef vd | this = vd.getSource() |
result = "class " + vd.getTarget().(VarRef).getName()
@@ -220,8 +217,8 @@ class ClassDeclStmt extends @classdeclstmt, ClassDefinition, Stmt {
* A class expression.
*/
class ClassExpr extends @classexpr, ClassDefinition, Expr {
override string getInferredName() {
result = getName()
override string getName() {
result = ClassDefinition.super.getName()
or
exists(VarDef vd | this = vd.getSource() | result = vd.getTarget().(VarRef).getName())
or

View File

@@ -751,9 +751,6 @@ class SpreadProperty extends Property {
* ```
*/
class FunctionExpr extends @functionexpr, Expr, Function {
/** Gets the name of this function expression, if any. */
override string getName() { result = getId().getName() }
/** Holds if this function expression is a property setter. */
predicate isSetter() { exists(PropertySetter s | s.getInit() = this) }

View File

@@ -47,12 +47,6 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
/** Gets the identifier specifying the name of this function, if any. */
VarDecl getId() { result = getChildExpr(-1) }
/**
* Gets the name of this function, if it is a function declaration
* or named function expression.
*/
string getName() { result = getId().getName() }
/**
* Gets the name of this function if it has one, or a name inferred from its context.
*
@@ -61,8 +55,8 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
* members), this is the name of the variable or property. If no meaningful name
* can be inferred, there is no result.
*/
string getInferredName() {
result = getName()
string getName() {
result = getId().getName()
or
exists(VarDef vd | this = vd.getSource() | result = vd.getTarget().(VarRef).getName())
or
@@ -271,8 +265,8 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
*/
private string inferNameFromVarDef() {
// in ambiguous cases like `var f = function g() {}`, prefer `g` to `f`
if exists(getName())
then result = "function " + getName()
if exists(getId())
then result = "function " + getId().getName()
else
exists(VarDef vd | this = vd.getSource() |
result = "function " + vd.getTarget().(VarRef).getName()

View File

@@ -345,7 +345,7 @@ class FunctionNode extends DataFlow::ValueNode, DataFlow::SourceNode {
predicate hasRestParameter() { astNode.hasRestParameter() }
/** Gets the unqualified name of this function, if it has one or one can be determined from the context. */
string getName() { result = astNode.getInferredName() }
string getName() { result = astNode.getName() }
/** Gets a data flow node corresponding to a return value of this function. */
DataFlow::Node getAReturn() { result = astNode.getAReturnedExpr().flow() }
@@ -783,7 +783,7 @@ module ClassNode {
private class ES6Class extends Range, DataFlow::ValueNode {
override ClassDefinition astNode;
override string getName() { result = astNode.getInferredName() }
override string getName() { result = astNode.getName() }
override string describe() { result = astNode.describe() }
@@ -844,7 +844,7 @@ module ClassNode {
)
}
override string getName() { result = astNode.getInferredName() }
override string getName() { result = astNode.getName() }
override string describe() { result = astNode.describe() }

View File

@@ -7,7 +7,7 @@
| amd3 | default | amd3.js:1:1:4:0 | exports object of module amd3 |
| amd3 | foo | file://:0:0:0:0 | non-zero value |
| commonjs | default | commonjs.js:1:1:3:0 | exports object of module commonjs |
| commonjs | foo | commonjs.js:1:15:1:27 | function foo |
| commonjs | foo | commonjs.js:1:15:1:27 | anonymous function |
| commonjs | x | file://:0:0:0:0 | non-empty, non-numeric string |
| commonjs2 | default | commonjs2.js:1:1:4:0 | exports object of module commonjs2 |
| commonjs2 | default | commonjs2.js:1:18:3:1 | object literal |

View File

@@ -39,7 +39,7 @@
| tst.js:56:5:56:12 | myModule | file://:0:0:0:0 | indefinite value (call) |
| tst.js:56:5:56:12 | myModule | file://:0:0:0:0 | undefined |
| tst.js:56:5:56:12 | myModule | myModule.js:1:1:4:0 | exports object of module myModule |
| tst.js:56:5:56:12 | myModule | myModule.js:1:18:1:30 | function exports |
| tst.js:56:5:56:12 | myModule | myModule.js:1:18:1:30 | anonymous function |
| tst.js:57:5:57:22 | myModule.myMethod1 | file://:0:0:0:0 | indefinite value (call) |
| tst.js:57:5:57:22 | myModule.myMethod1 | file://:0:0:0:0 | indefinite value (heap) |
| tst.js:58:5:58:22 | myModule.myMethod2 | file://:0:0:0:0 | indefinite value (call) |

View File

@@ -1,7 +1,7 @@
| tst.js:1:18:1:38 | require ... odule') | file://:0:0:0:0 | indefinite value (call) |
| tst.js:2:16:2:36 | require ... odule') | file://:0:0:0:0 | indefinite value (call) |
| tst.js:2:16:2:36 | require ... odule') | myModule.js:1:1:4:0 | exports object of module myModule |
| tst.js:2:16:2:36 | require ... odule') | myModule.js:1:18:1:30 | function exports |
| tst.js:2:16:2:36 | require ... odule') | myModule.js:1:18:1:30 | anonymous function |
| tst.js:4:1:4:16 | (function(){})() | file://:0:0:0:0 | undefined |
| tst.js:5:1:5:25 | (functi ... n; })() | file://:0:0:0:0 | undefined |
| tst.js:6:1:6:30 | (functi ... e; })() | file://:0:0:0:0 | true |

View File

@@ -1,4 +1,4 @@
getInferredName
getName
| tst.js:16:1:19:1 | class C ... ss C"\\n} | C |
| tst.js:21:2:23:1 | class D ... lass"\\n} | D |
| tst.js:25:11:25:18 | class {} | E |

View File

@@ -1,6 +1,6 @@
import javascript
query string getInferredName(ClassDefinition c) { result = c.getInferredName() }
query string getName(ClassDefinition c) { result = c.getName() }
from ClassDefinition c
select c, c.describe()

View File

@@ -1,4 +1,4 @@
getInferredName
getName
| tst.js:1:1:1:15 | function f() {} | f |
| tst.js:2:2:2:16 | function g() {} | g |
| tst.js:4:9:4:22 | function () {} | h |

View File

@@ -1,6 +1,6 @@
import javascript
query string getInferredName(Function f) { result = f.getInferredName() }
query string getName(Function f) { result = f.getName() }
from Function f
select f, f.describe()