Skip to content

Commit 66541f2

Browse files
authored
Merge pull request github#4012 from erik-krogh/getId
Approved by asgerf, esbena
2 parents aa9dfa0 + 656ff9c commit 66541f2

File tree

14 files changed

+72
-48
lines changed

14 files changed

+72
-48
lines changed

javascript/ql/src/Declarations/ConflictingFunctions.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ where
2424
// ignore ambient, abstract, and overloaded declarations in TypeScript
2525
f.hasBody() and
2626
g.hasBody()
27-
select f.getId(), "Declaration of " + f.describe() + " conflicts with $@ in the same scope.",
28-
g.getId(), "another declaration"
27+
select f.getIdentifier(),
28+
"Declaration of " + f.describe() + " conflicts with $@ in the same scope.", g.getIdentifier(),
29+
"another declaration"

javascript/ql/src/Declarations/DeadStoreOfLocal.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ where
4242
access.isAmbient()
4343
) and
4444
// don't flag function expressions
45-
not exists(FunctionExpr fe | dead = fe.getId()) and
45+
not exists(FunctionExpr fe | dead = fe.getIdentifier()) and
4646
// don't flag function declarations nested inside blocks or other compound statements;
4747
// their meaning is only partially specified by the standard
4848
not exists(FunctionDeclStmt fd, StmtContainer outer |
49-
dead = fd.getId() and outer = fd.getEnclosingContainer()
49+
dead = fd.getIdentifier() and outer = fd.getEnclosingContainer()
5050
|
5151
not fd = outer.getBody().(BlockStmt).getAStmt()
5252
) and

javascript/ql/src/Declarations/RedeclaredVariable.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ where
2222
not decl.isAmbient() and
2323
not redecl.isAmbient() and
2424
// Redeclaring a namespace extends the previous definition.
25-
not decl = any(NamespaceDeclaration ns).getId() and
26-
not redecl = any(NamespaceDeclaration ns).getId()
25+
not decl = any(NamespaceDeclaration ns).getIdentifier() and
26+
not redecl = any(NamespaceDeclaration ns).getIdentifier()
2727
select redecl, "This variable has already been declared $@.", decl, "here"

javascript/ql/src/Declarations/UnusedVariable.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ predicate isEnumMember(VarDecl decl) { decl = any(EnumMember member).getIdentifi
9393
* "function f", "variable v" or "class c".
9494
*/
9595
string describeVarDecl(VarDecl vd) {
96-
if vd = any(Function f).getId()
96+
if vd = any(Function f).getIdentifier()
9797
then result = "function " + vd.getName()
9898
else
9999
if vd = any(ClassDefinition c).getIdentifier()
@@ -115,7 +115,7 @@ class ImportVarDeclProvider extends Stmt {
115115
*/
116116
VarDecl getAVarDecl() {
117117
result = this.(ImportDeclaration).getASpecifier().getLocal() or
118-
result = this.(ImportEqualsDeclaration).getId()
118+
result = this.(ImportEqualsDeclaration).getIdentifier()
119119
}
120120

121121
/**

javascript/ql/src/Expressions/SuspiciousPropAccess.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private import semmle.javascript.dataflow.InferredTypes
2323
* if it is part of a const enum access, so we conservatively silence the alert in that case.
2424
*/
2525
predicate namespaceOrConstEnumAccess(VarAccess e) {
26-
exists(NamespaceDeclaration decl | e.getVariable().getADeclaration() = decl.getId())
26+
exists(NamespaceDeclaration decl | e.getVariable().getADeclaration() = decl.getIdentifier())
2727
or
2828
exists(EnumDeclaration decl | e.getVariable().getADeclaration() = decl.getIdentifier() |
2929
decl.isConst()

javascript/ql/src/semmle/javascript/DefUse.qll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ private predicate defn(ControlFlowNode def, Expr lhs, AST::ValueNode rhs) {
3030
or
3131
exists(VariableDeclarator vd | def = vd | lhs = vd.getBindingPattern() and rhs = vd.getInit())
3232
or
33-
exists(Function f | def = f.getId() | lhs = def and rhs = f)
33+
exists(Function f | def = f.getIdentifier() | lhs = def and rhs = f)
3434
or
3535
exists(ClassDefinition c | lhs = c.getIdentifier() | def = c and rhs = c and not c.isAmbient())
3636
or
37-
exists(NamespaceDeclaration n | def = n | lhs = n.getId() and rhs = n)
37+
exists(NamespaceDeclaration n | def = n | lhs = n.getIdentifier() and rhs = n)
3838
or
3939
exists(EnumDeclaration ed | def = ed.getIdentifier() | lhs = def and rhs = ed)
4040
or
41-
exists(ImportEqualsDeclaration i | def = i | lhs = i.getId() and rhs = i.getImportedEntity())
41+
exists(ImportEqualsDeclaration i | def = i |
42+
lhs = i.getIdentifier() and rhs = i.getImportedEntity()
43+
)
4244
or
4345
exists(ImportSpecifier i | def = i | lhs = i.getLocal() and rhs = i)
4446
or
@@ -149,7 +151,7 @@ class RValue extends RefExpr {
149151
or
150152
this = any(UpdateExpr u).getOperand().getUnderlyingReference()
151153
or
152-
this = any(NamespaceDeclaration decl).getId()
154+
this = any(NamespaceDeclaration decl).getIdentifier()
153155
}
154156
}
155157

javascript/ql/src/semmle/javascript/ES2015Modules.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class ExportDefaultDeclaration extends ExportDeclaration, @exportdefaultdeclarat
330330
/** Gets the declaration, if any, exported by this default export. */
331331
VarDecl getADecl() {
332332
exists(ExprOrStmt op | op = getOperand() |
333-
result = op.(FunctionDeclStmt).getId() or
333+
result = op.(FunctionDeclStmt).getIdentifier() or
334334
result = op.(ClassDeclStmt).getIdentifier()
335335
)
336336
}
@@ -364,13 +364,13 @@ class ExportNamedDeclaration extends ExportDeclaration, @exportnameddeclaration
364364
Identifier getAnExportedDecl() {
365365
exists(ExprOrStmt op | op = getOperand() |
366366
result = op.(DeclStmt).getADecl().getBindingPattern().getABindingVarRef() or
367-
result = op.(FunctionDeclStmt).getId() or
367+
result = op.(FunctionDeclStmt).getIdentifier() or
368368
result = op.(ClassDeclStmt).getIdentifier() or
369-
result = op.(NamespaceDeclaration).getId() or
369+
result = op.(NamespaceDeclaration).getIdentifier() or
370370
result = op.(EnumDeclaration).getIdentifier() or
371371
result = op.(InterfaceDeclaration).getIdentifier() or
372372
result = op.(TypeAliasDeclaration).getIdentifier() or
373-
result = op.(ImportEqualsDeclaration).getId()
373+
result = op.(ImportEqualsDeclaration).getIdentifier()
374374
)
375375
}
376376

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,15 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
7777
result = getDocumentation().getATagByTitle("this").getType()
7878
}
7979

80+
/**
81+
* DEPRECATED: Use `getIdentifier()` instead.
82+
*
83+
* Gets the identifier specifying the name of this function, if any.
84+
*/
85+
deprecated VarDecl getId() { result = getIdentifier() }
86+
8087
/** Gets the identifier specifying the name of this function, if any. */
81-
VarDecl getId() { result = getChildExpr(-1) }
88+
VarDecl getIdentifier() { result = getChildExpr(-1) }
8289

8390
/**
8491
* Gets the name of this function if it has one, or a name inferred from its context.
@@ -89,9 +96,9 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
8996
* can be inferred, there is no result.
9097
*/
9198
string getName() {
92-
result = getId().getName()
99+
result = getIdentifier().getName()
93100
or
94-
not exists(getId()) and
101+
not exists(getIdentifier()) and
95102
(
96103
exists(VarDef vd | this = vd.getSource() | result = vd.getTarget().(VarRef).getName())
97104
or
@@ -111,7 +118,7 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
111118
}
112119

113120
/** Gets the variable holding this function. */
114-
Variable getVariable() { result = getId().getVariable() }
121+
Variable getVariable() { result = getIdentifier().getVariable() }
115122

116123
/** Gets the `arguments` variable of this function, if any. */
117124
ArgumentsVariable getArgumentsVariable() { result.getFunction() = this }
@@ -183,11 +190,11 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
183190
not exists(getAParameter()) and
184191
(
185192
// if the function has a name, the opening parenthesis comes right after it
186-
result = getId().getLastToken().getNextToken()
193+
result = getIdentifier().getLastToken().getNextToken()
187194
or
188195
// otherwise this must be an arrow function with no parameters, so the opening
189196
// parenthesis is the very first token of the function
190-
not exists(getId()) and result = getFirstToken()
197+
not exists(getIdentifier()) and result = getFirstToken()
191198
)
192199
}
193200

@@ -309,8 +316,8 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
309316
*/
310317
private string inferNameFromVarDef() {
311318
// in ambiguous cases like `var f = function g() {}`, prefer `g` to `f`
312-
if exists(getId())
313-
then result = "function " + getId().getName()
319+
if exists(getIdentifier())
320+
then result = "function " + getIdentifier().getName()
314321
else
315322
exists(VarDef vd | this = vd.getSource() |
316323
result = "function " + vd.getTarget().(VarRef).getName()

javascript/ql/src/semmle/javascript/GlobalAccessPaths.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ module AccessPath {
267267
or
268268
exists(FunctionDeclStmt fun |
269269
node = DataFlow::valueNode(fun) and
270-
result = fun.getId().(GlobalVarDecl).getName() and
270+
result = fun.getIdentifier().(GlobalVarDecl).getName() and
271271
root.isGlobal()
272272
)
273273
or
@@ -285,7 +285,7 @@ module AccessPath {
285285
or
286286
exists(NamespaceDeclaration decl |
287287
node = DataFlow::valueNode(decl) and
288-
result = decl.getId().(GlobalVarDecl).getName() and
288+
result = decl.getIdentifier().(GlobalVarDecl).getName() and
289289
root.isGlobal()
290290
)
291291
}

javascript/ql/src/semmle/javascript/TypeScript.qll

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import javascript
88
*/
99
class NamespaceDefinition extends Stmt, @namespacedefinition, AST::ValueNode {
1010
/**
11+
* DEPRECATED: Use `getIdentifier()` instead.
12+
*
1113
* Gets the identifier naming the namespace.
1214
*/
13-
Identifier getId() {
14-
result = this.(NamespaceDeclaration).getId()
15-
or
16-
result = this.(EnumDeclaration).getIdentifier()
17-
}
15+
deprecated Identifier getId() { result = getIdentifier() }
16+
17+
/**
18+
* Gets the identifier naming the namespace.
19+
*/
20+
Identifier getIdentifier() { none() } // Overridden in subtypes.
1821

1922
/**
2023
* Gets unqualified name of the namespace being defined.
@@ -29,7 +32,7 @@ class NamespaceDefinition extends Stmt, @namespacedefinition, AST::ValueNode {
2932
* Gets the local namespace name induced by this namespace.
3033
*/
3134
LocalNamespaceName getLocalNamespaceName() {
32-
result = getId().(LocalNamespaceDecl).getLocalNamespaceName()
35+
result = getIdentifier().(LocalNamespaceDecl).getLocalNamespaceName()
3336
}
3437

3538
/**
@@ -55,10 +58,10 @@ class NamespaceDefinition extends Stmt, @namespacedefinition, AST::ValueNode {
5558
*/
5659
class NamespaceDeclaration extends NamespaceDefinition, StmtContainer, @namespacedeclaration {
5760
/** Gets the name of this namespace. */
58-
override Identifier getId() { result = getChildExpr(-1) }
61+
override Identifier getIdentifier() { result = getChildExpr(-1) }
5962

6063
/** Gets the name of this namespace as a string. */
61-
override string getName() { result = getId().getName() }
64+
override string getName() { result = getIdentifier().getName() }
6265

6366
/** Gets the `i`th statement in this namespace. */
6467
Stmt getStmt(int i) {
@@ -83,7 +86,7 @@ class NamespaceDeclaration extends NamespaceDefinition, StmtContainer, @namespac
8386
predicate isInstantiated() { isInstantiated(this) }
8487

8588
override ControlFlowNode getFirstControlFlowNode() {
86-
if hasDeclareKeyword(this) then result = this else result = getId()
89+
if hasDeclareKeyword(this) then result = this else result = getIdentifier()
8790
}
8891
}
8992

@@ -178,13 +181,20 @@ class GlobalAugmentationDeclaration extends Stmt, StmtContainer, @globalaugmenta
178181

179182
/** A TypeScript "import-equals" declaration. */
180183
class ImportEqualsDeclaration extends Stmt, @importequalsdeclaration {
184+
/**
185+
* DEPRECATED: Use `getIdentifier()` instead.
186+
*
187+
* Gets the name under which the imported entity is imported.
188+
*/
189+
deprecated Identifier getId() { result = getIdentifier() }
190+
181191
/** Gets the name under which the imported entity is imported. */
182-
Identifier getId() { result = getChildExpr(0) }
192+
Identifier getIdentifier() { result = getChildExpr(0) }
183193

184194
/** Gets the expression specifying the imported module or entity. */
185195
Expr getImportedEntity() { result = getChildExpr(1) }
186196

187-
override ControlFlowNode getFirstControlFlowNode() { result = getId() }
197+
override ControlFlowNode getFirstControlFlowNode() { result = getIdentifier() }
188198
}
189199

190200
/**
@@ -348,7 +358,7 @@ class TypeDecl extends Identifier, TypeRef, LexicalDecl {
348358
this = any(ClassOrInterface ci).getIdentifier() or
349359
this = any(TypeParameter tp).getIdentifier() or
350360
this = any(ImportSpecifier im).getLocal() or
351-
this = any(ImportEqualsDeclaration im).getId() or
361+
this = any(ImportEqualsDeclaration im).getIdentifier() or
352362
this = any(TypeAliasDeclaration td).getIdentifier() or
353363
this = any(EnumDeclaration ed).getIdentifier() or
354364
this = any(EnumMember member).getIdentifier()
@@ -1226,8 +1236,8 @@ abstract class NamespaceRef extends ASTNode { }
12261236
*/
12271237
class LocalNamespaceDecl extends VarDecl, NamespaceRef {
12281238
LocalNamespaceDecl() {
1229-
any(NamespaceDeclaration nd).getId() = this or
1230-
any(ImportEqualsDeclaration im).getId() = this or
1239+
any(NamespaceDeclaration nd).getIdentifier() = this or
1240+
any(ImportEqualsDeclaration im).getIdentifier() = this or
12311241
any(ImportSpecifier im).getLocal() = this or
12321242
any(EnumDeclaration ed).getIdentifier() = this
12331243
}
@@ -1325,7 +1335,7 @@ class ImportVarTypeAccess extends VarTypeAccess, ImportTypeExpr, @importvartypea
13251335
*/
13261336
class EnumDeclaration extends NamespaceDefinition, @enumdeclaration, AST::ValueNode {
13271337
/** Gets the name of this enum, such as `E` in `enum E { A, B }`. */
1328-
Identifier getIdentifier() { result = getChildExpr(0) }
1338+
override Identifier getIdentifier() { result = getChildExpr(0) }
13291339

13301340
/** Gets the name of this enum as a string. */
13311341
override string getName() { result = getIdentifier().getName() }

0 commit comments

Comments
 (0)