Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7da341b

Browse files
committed
JS: Merge getInferredName() => getName()
1 parent b2da2d2 commit 7da341b

11 files changed

Lines changed: 20 additions & 32 deletions

File tree

javascript/ql/src/semmle/javascript/Classes.qll

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ class ClassOrInterface extends @classorinterface, TypeParameterized {
1414
/** Gets the identifier naming the declared type, if any. */
1515
Identifier getIdentifier() { none() } // Overridden in subtypes.
1616

17-
/** Gets the name of the defined class or interface, if any. */
18-
string getName() { result = getIdentifier().getName() }
19-
2017
/**
2118
* Gets the name of the defined class or interface, possibly inferred
2219
* from the context if this is an anonymous class expression.
2320
*
2421
* Has no result if no name could be determined.
2522
*/
26-
string getInferredName() {
27-
result = getName() // Overridden in ClassExpr
23+
string getName() {
24+
result = getIdentifier().getName() // Overridden in ClassExpr
2825
}
2926

3027
/** 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
175172
*/
176173
private string inferNameFromVarDef() {
177174
// in ambiguous cases like `let C = class D {}`, prefer `D` to `C`
178-
if exists(getName())
179-
then result = "class " + getName()
175+
if exists(getIdentifier())
176+
then result = "class " + getIdentifier().getName()
180177
else
181178
exists(VarDef vd | this = vd.getSource() |
182179
result = "class " + vd.getTarget().(VarRef).getName()
@@ -220,8 +217,8 @@ class ClassDeclStmt extends @classdeclstmt, ClassDefinition, Stmt {
220217
* A class expression.
221218
*/
222219
class ClassExpr extends @classexpr, ClassDefinition, Expr {
223-
override string getInferredName() {
224-
result = getName()
220+
override string getName() {
221+
result = ClassDefinition.super.getName()
225222
or
226223
exists(VarDef vd | this = vd.getSource() | result = vd.getTarget().(VarRef).getName())
227224
or

javascript/ql/src/semmle/javascript/Expr.qll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,6 @@ class SpreadProperty extends Property {
751751
* ```
752752
*/
753753
class FunctionExpr extends @functionexpr, Expr, Function {
754-
/** Gets the name of this function expression, if any. */
755-
override string getName() { result = getId().getName() }
756-
757754
/** Holds if this function expression is a property setter. */
758755
predicate isSetter() { exists(PropertySetter s | s.getInit() = this) }
759756

javascript/ql/src/semmle/javascript/Functions.qll

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
4747
/** Gets the identifier specifying the name of this function, if any. */
4848
VarDecl getId() { result = getChildExpr(-1) }
4949

50-
/**
51-
* Gets the name of this function, if it is a function declaration
52-
* or named function expression.
53-
*/
54-
string getName() { result = getId().getName() }
55-
5650
/**
5751
* Gets the name of this function if it has one, or a name inferred from its context.
5852
*
@@ -61,8 +55,8 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
6155
* members), this is the name of the variable or property. If no meaningful name
6256
* can be inferred, there is no result.
6357
*/
64-
string getInferredName() {
65-
result = getName()
58+
string getName() {
59+
result = getId().getName()
6660
or
6761
exists(VarDef vd | this = vd.getSource() | result = vd.getTarget().(VarRef).getName())
6862
or
@@ -271,8 +265,8 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
271265
*/
272266
private string inferNameFromVarDef() {
273267
// in ambiguous cases like `var f = function g() {}`, prefer `g` to `f`
274-
if exists(getName())
275-
then result = "function " + getName()
268+
if exists(getId())
269+
then result = "function " + getId().getName()
276270
else
277271
exists(VarDef vd | this = vd.getSource() |
278272
result = "function " + vd.getTarget().(VarRef).getName()

javascript/ql/src/semmle/javascript/dataflow/Nodes.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ class FunctionNode extends DataFlow::ValueNode, DataFlow::SourceNode {
345345
predicate hasRestParameter() { astNode.hasRestParameter() }
346346

347347
/** Gets the unqualified name of this function, if it has one or one can be determined from the context. */
348-
string getName() { result = astNode.getInferredName() }
348+
string getName() { result = astNode.getName() }
349349

350350
/** Gets a data flow node corresponding to a return value of this function. */
351351
DataFlow::Node getAReturn() { result = astNode.getAReturnedExpr().flow() }
@@ -783,7 +783,7 @@ module ClassNode {
783783
private class ES6Class extends Range, DataFlow::ValueNode {
784784
override ClassDefinition astNode;
785785

786-
override string getName() { result = astNode.getInferredName() }
786+
override string getName() { result = astNode.getName() }
787787

788788
override string describe() { result = astNode.describe() }
789789

@@ -844,7 +844,7 @@ module ClassNode {
844844
)
845845
}
846846

847-
override string getName() { result = astNode.getInferredName() }
847+
override string getName() { result = astNode.getName() }
848848

849849
override string describe() { result = astNode.describe() }
850850

javascript/ql/test/library-tests/TypeInference/AnalyzedModule/AnalyzedModule_getAnExportedValue.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| amd3 | default | amd3.js:1:1:4:0 | exports object of module amd3 |
88
| amd3 | foo | file://:0:0:0:0 | non-zero value |
99
| commonjs | default | commonjs.js:1:1:3:0 | exports object of module commonjs |
10-
| commonjs | foo | commonjs.js:1:15:1:27 | function foo |
10+
| commonjs | foo | commonjs.js:1:15:1:27 | anonymous function |
1111
| commonjs | x | file://:0:0:0:0 | non-empty, non-numeric string |
1212
| commonjs2 | default | commonjs2.js:1:1:4:0 | exports object of module commonjs2 |
1313
| commonjs2 | default | commonjs2.js:1:18:3:1 | object literal |

javascript/ql/test/library-tests/TypeInference/CallWithAnalyzedReturnFlow/CalleeNodeValue.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
| tst.js:56:5:56:12 | myModule | file://:0:0:0:0 | indefinite value (call) |
4040
| tst.js:56:5:56:12 | myModule | file://:0:0:0:0 | undefined |
4141
| tst.js:56:5:56:12 | myModule | myModule.js:1:1:4:0 | exports object of module myModule |
42-
| tst.js:56:5:56:12 | myModule | myModule.js:1:18:1:30 | function exports |
42+
| tst.js:56:5:56:12 | myModule | myModule.js:1:18:1:30 | anonymous function |
4343
| tst.js:57:5:57:22 | myModule.myMethod1 | file://:0:0:0:0 | indefinite value (call) |
4444
| tst.js:57:5:57:22 | myModule.myMethod1 | file://:0:0:0:0 | indefinite value (heap) |
4545
| tst.js:58:5:58:22 | myModule.myMethod2 | file://:0:0:0:0 | indefinite value (call) |

javascript/ql/test/library-tests/TypeInference/CallWithAnalyzedReturnFlow/InvokeNodeValue.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
| tst.js:1:18:1:38 | require ... odule') | file://:0:0:0:0 | indefinite value (call) |
22
| tst.js:2:16:2:36 | require ... odule') | file://:0:0:0:0 | indefinite value (call) |
33
| tst.js:2:16:2:36 | require ... odule') | myModule.js:1:1:4:0 | exports object of module myModule |
4-
| tst.js:2:16:2:36 | require ... odule') | myModule.js:1:18:1:30 | function exports |
4+
| tst.js:2:16:2:36 | require ... odule') | myModule.js:1:18:1:30 | anonymous function |
55
| tst.js:4:1:4:16 | (function(){})() | file://:0:0:0:0 | undefined |
66
| tst.js:5:1:5:25 | (functi ... n; })() | file://:0:0:0:0 | undefined |
77
| tst.js:6:1:6:30 | (functi ... e; })() | file://:0:0:0:0 | true |

javascript/ql/test/library-tests/ppNames/ppClassName.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
getInferredName
1+
getName
22
| tst.js:16:1:19:1 | class C ... ss C"\\n} | C |
33
| tst.js:21:2:23:1 | class D ... lass"\\n} | D |
44
| tst.js:25:11:25:18 | class {} | E |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import javascript
22

3-
query string getInferredName(ClassDefinition c) { result = c.getInferredName() }
3+
query string getName(ClassDefinition c) { result = c.getName() }
44

55
from ClassDefinition c
66
select c, c.describe()

javascript/ql/test/library-tests/ppNames/ppFnName.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
getInferredName
1+
getName
22
| tst.js:1:1:1:15 | function f() {} | f |
33
| tst.js:2:2:2:16 | function g() {} | g |
44
| tst.js:4:9:4:22 | function () {} | h |

0 commit comments

Comments
 (0)