Skip to content

Commit e9c2594

Browse files
authored
Merge pull request github#13002 from kaspersv/kaspersv/javascript-explicit-this-receiver
JS: Make implicit this receivers explicit
2 parents be08b97 + efdaffe commit e9c2594

File tree

2 files changed

+59
-59
lines changed

2 files changed

+59
-59
lines changed

javascript/ql/lib/semmle/javascript/ES2015Modules.qll

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ class ES2015Module extends Module {
2020
override ModuleScope getScope() { result.getScopeElement() = this }
2121

2222
/** Gets the full path of the file containing this module. */
23-
override string getPath() { result = getFile().getAbsolutePath() }
23+
override string getPath() { result = this.getFile().getAbsolutePath() }
2424

2525
/** Gets the short name of this module without file extension. */
26-
override string getName() { result = getFile().getStem() }
26+
override string getName() { result = this.getFile().getStem() }
2727

2828
/** Gets an export declaration in this module. */
2929
ExportDeclaration getAnExport() { result.getTopLevel() = this }
3030

3131
override DataFlow::Node getAnExportedValue(string name) {
32-
exists(ExportDeclaration ed | ed = getAnExport() and result = ed.getSourceNode(name))
32+
exists(ExportDeclaration ed | ed = this.getAnExport() and result = ed.getSourceNode(name))
3333
}
3434

3535
/** Holds if this module exports variable `v` under the name `name`. */
36-
predicate exportsAs(LexicalName v, string name) { getAnExport().exportsAs(v, name) }
36+
predicate exportsAs(LexicalName v, string name) { this.getAnExport().exportsAs(v, name) }
3737

3838
override predicate isStrict() {
3939
// modules are implicitly strict
@@ -86,9 +86,9 @@ private predicate hasBothNamedAndDefaultExports(ES2015Module mod) {
8686
* ```
8787
*/
8888
class ImportDeclaration extends Stmt, Import, @import_declaration {
89-
override ES2015Module getEnclosingModule() { result = getTopLevel() }
89+
override ES2015Module getEnclosingModule() { result = this.getTopLevel() }
9090

91-
override PathExpr getImportedPath() { result = getChildExpr(-1) }
91+
override PathExpr getImportedPath() { result = this.getChildExpr(-1) }
9292

9393
/**
9494
* Gets the object literal passed as part of the `assert` clause in this import declaration.
@@ -101,24 +101,24 @@ class ImportDeclaration extends Stmt, Import, @import_declaration {
101101
ObjectExpr getImportAssertion() { result = this.getChildExpr(-10) }
102102

103103
/** Gets the `i`th import specifier of this import declaration. */
104-
ImportSpecifier getSpecifier(int i) { result = getChildExpr(i) }
104+
ImportSpecifier getSpecifier(int i) { result = this.getChildExpr(i) }
105105

106106
/** Gets an import specifier of this import declaration. */
107-
ImportSpecifier getASpecifier() { result = getSpecifier(_) }
107+
ImportSpecifier getASpecifier() { result = this.getSpecifier(_) }
108108

109109
override DataFlow::Node getImportedModuleNode() {
110110
// `import * as http from 'http'` or `import http from `http`'
111111
exists(ImportSpecifier is |
112-
is = getASpecifier() and
112+
is = this.getASpecifier() and
113113
result = DataFlow::valueNode(is)
114114
|
115115
is instanceof ImportNamespaceSpecifier and
116-
count(getASpecifier()) = 1
116+
count(this.getASpecifier()) = 1
117117
or
118118
// For compatibility with the non-standard implementation of default imports,
119119
// treat default imports as namespace imports in cases where it can't cause ambiguity
120120
// between named exports and the properties of a default-exported object.
121-
not hasBothNamedAndDefaultExports(getImportedModule()) and
121+
not hasBothNamedAndDefaultExports(this.getImportedModule()) and
122122
is.getImportedName() = "default"
123123
)
124124
or
@@ -136,7 +136,7 @@ class ImportDeclaration extends Stmt, Import, @import_declaration {
136136
private class LiteralImportPath extends PathExpr, ConstantString {
137137
LiteralImportPath() { exists(ImportDeclaration req | this = req.getChildExpr(-1)) }
138138

139-
override string getValue() { result = getStringValue() }
139+
override string getValue() { result = this.getStringValue() }
140140
}
141141

142142
/**
@@ -159,7 +159,7 @@ private class LiteralImportPath extends PathExpr, ConstantString {
159159
*/
160160
class ImportSpecifier extends Expr, @import_specifier {
161161
/** Gets the imported symbol; undefined for default and namespace import specifiers. */
162-
Identifier getImported() { result = getChildExpr(0) }
162+
Identifier getImported() { result = this.getChildExpr(0) }
163163

164164
/**
165165
* Gets the name of the imported symbol.
@@ -176,10 +176,10 @@ class ImportSpecifier extends Expr, @import_specifier {
176176
* The names of the imported symbols for the first three of them are, respectively,
177177
* `x`, `y` and `default`, while the last one does not import an individual symbol.
178178
*/
179-
string getImportedName() { result = getImported().getName() }
179+
string getImportedName() { result = this.getImported().getName() }
180180

181181
/** Gets the local variable into which this specifier imports. */
182-
VarDecl getLocal() { result = getChildExpr(1) }
182+
VarDecl getLocal() { result = this.getChildExpr(1) }
183183

184184
override string getAPrimaryQlClass() { result = "ImportSpecifier" }
185185

@@ -240,10 +240,10 @@ class ImportNamespaceSpecifier extends ImportSpecifier, @import_namespace_specif
240240
* ```
241241
*/
242242
class BulkImportDeclaration extends ImportDeclaration {
243-
BulkImportDeclaration() { getASpecifier() instanceof ImportNamespaceSpecifier }
243+
BulkImportDeclaration() { this.getASpecifier() instanceof ImportNamespaceSpecifier }
244244

245245
/** Gets the local namespace variable under which the module is imported. */
246-
VarDecl getLocal() { result = getASpecifier().getLocal() }
246+
VarDecl getLocal() { result = this.getASpecifier().getLocal() }
247247
}
248248

249249
/**
@@ -260,12 +260,12 @@ class SelectiveImportDeclaration extends ImportDeclaration {
260260

261261
/** Holds if `local` is the local variable into which `imported` is imported. */
262262
predicate importsAs(string imported, LexicalDecl local) {
263-
exists(ImportSpecifier spec | spec = getASpecifier() |
263+
exists(ImportSpecifier spec | spec = this.getASpecifier() |
264264
imported = spec.getImported().getName() and
265265
local = spec.getLocal()
266266
)
267267
or
268-
imported = "default" and local = getASpecifier().(ImportDefaultSpecifier).getLocal()
268+
imported = "default" and local = this.getASpecifier().(ImportDefaultSpecifier).getLocal()
269269
}
270270
}
271271

@@ -347,15 +347,15 @@ abstract class ExportDeclaration extends Stmt, @export_declaration {
347347
*/
348348
class BulkReExportDeclaration extends ReExportDeclaration, @export_all_declaration {
349349
/** Gets the name of the module from which this declaration re-exports. */
350-
override ConstantString getImportedPath() { result = getChildExpr(0) }
350+
override ConstantString getImportedPath() { result = this.getChildExpr(0) }
351351

352352
override predicate exportsAs(LexicalName v, string name) {
353-
getReExportedES2015Module().exportsAs(v, name) and
353+
this.getReExportedES2015Module().exportsAs(v, name) and
354354
not isShadowedFromBulkExport(this, name)
355355
}
356356

357357
override DataFlow::Node getSourceNode(string name) {
358-
result = getReExportedES2015Module().getAnExport().getSourceNode(name)
358+
result = this.getReExportedES2015Module().getAnExport().getSourceNode(name)
359359
}
360360
}
361361

@@ -392,22 +392,22 @@ private predicate isShadowedFromBulkExport(BulkReExportDeclaration reExport, str
392392
*/
393393
class ExportDefaultDeclaration extends ExportDeclaration, @export_default_declaration {
394394
/** Gets the operand statement or expression that is exported by this declaration. */
395-
ExprOrStmt getOperand() { result = getChild(0) }
395+
ExprOrStmt getOperand() { result = this.getChild(0) }
396396

397397
override predicate exportsAs(LexicalName v, string name) {
398-
name = "default" and v = getADecl().getVariable()
398+
name = "default" and v = this.getADecl().getVariable()
399399
}
400400

401401
/** Gets the declaration, if any, exported by this default export. */
402402
VarDecl getADecl() {
403-
exists(ExprOrStmt op | op = getOperand() |
403+
exists(ExprOrStmt op | op = this.getOperand() |
404404
result = op.(FunctionDeclStmt).getIdentifier() or
405405
result = op.(ClassDeclStmt).getIdentifier()
406406
)
407407
}
408408

409409
override DataFlow::Node getSourceNode(string name) {
410-
name = "default" and result = DataFlow::valueNode(getOperand())
410+
name = "default" and result = DataFlow::valueNode(this.getOperand())
411411
}
412412
}
413413

@@ -424,7 +424,7 @@ class ExportDefaultDeclaration extends ExportDeclaration, @export_default_declar
424424
*/
425425
class ExportNamedDeclaration extends ExportDeclaration, @export_named_declaration {
426426
/** Gets the operand statement or expression that is exported by this declaration. */
427-
ExprOrStmt getOperand() { result = getChild(-1) }
427+
ExprOrStmt getOperand() { result = this.getChild(-1) }
428428

429429
/**
430430
* Gets an identifier, if any, exported as part of a declaration by this named export.
@@ -433,7 +433,7 @@ class ExportNamedDeclaration extends ExportDeclaration, @export_named_declaratio
433433
* That is, it includes the `v` in `export var v` but not in `export {v}`.
434434
*/
435435
Identifier getAnExportedDecl() {
436-
exists(ExprOrStmt op | op = getOperand() |
436+
exists(ExprOrStmt op | op = this.getOperand() |
437437
result = op.(DeclStmt).getADecl().getBindingPattern().getABindingVarRef() or
438438
result = op.(FunctionDeclStmt).getIdentifier() or
439439
result = op.(ClassDeclStmt).getIdentifier() or
@@ -446,35 +446,35 @@ class ExportNamedDeclaration extends ExportDeclaration, @export_named_declaratio
446446
}
447447

448448
/** Gets the variable declaration, if any, exported by this named export. */
449-
VarDecl getADecl() { result = getAnExportedDecl() }
449+
VarDecl getADecl() { result = this.getAnExportedDecl() }
450450

451451
override predicate exportsAs(LexicalName v, string name) {
452-
exists(LexicalDecl vd | vd = getAnExportedDecl() |
452+
exists(LexicalDecl vd | vd = this.getAnExportedDecl() |
453453
name = vd.getName() and v = vd.getALexicalName()
454454
)
455455
or
456-
exists(ExportSpecifier spec | spec = getASpecifier() and name = spec.getExportedName() |
456+
exists(ExportSpecifier spec | spec = this.getASpecifier() and name = spec.getExportedName() |
457457
v = spec.getLocal().(LexicalAccess).getALexicalName()
458458
or
459459
this.(ReExportDeclaration).getReExportedES2015Module().exportsAs(v, spec.getLocalName())
460460
)
461461
}
462462

463463
override DataFlow::Node getSourceNode(string name) {
464-
exists(VarDef d | d.getTarget() = getADecl() |
464+
exists(VarDef d | d.getTarget() = this.getADecl() |
465465
name = d.getTarget().(VarDecl).getName() and
466466
result = DataFlow::valueNode(d.getSource())
467467
)
468468
or
469-
exists(ObjectPattern obj | obj = getOperand().(DeclStmt).getADecl().getBindingPattern() |
469+
exists(ObjectPattern obj | obj = this.getOperand().(DeclStmt).getADecl().getBindingPattern() |
470470
exists(DataFlow::PropRead read | read = result |
471471
read.getBase() = obj.flow() and
472472
name = read.getPropertyName()
473473
)
474474
)
475475
or
476-
exists(ExportSpecifier spec | spec = getASpecifier() and name = spec.getExportedName() |
477-
not exists(getImportedPath()) and result = DataFlow::valueNode(spec.getLocal())
476+
exists(ExportSpecifier spec | spec = this.getASpecifier() and name = spec.getExportedName() |
477+
not exists(this.getImportedPath()) and result = DataFlow::valueNode(spec.getLocal())
478478
or
479479
exists(ReExportDeclaration red | red = this |
480480
result = red.getReExportedES2015Module().getAnExport().getSourceNode(spec.getLocalName())
@@ -483,20 +483,20 @@ class ExportNamedDeclaration extends ExportDeclaration, @export_named_declaratio
483483
}
484484

485485
/** Gets the module from which the exports are taken if this is a re-export. */
486-
ConstantString getImportedPath() { result = getChildExpr(-2) }
486+
ConstantString getImportedPath() { result = this.getChildExpr(-2) }
487487

488488
/** Gets the `i`th export specifier of this declaration. */
489-
ExportSpecifier getSpecifier(int i) { result = getChildExpr(i) }
489+
ExportSpecifier getSpecifier(int i) { result = this.getChildExpr(i) }
490490

491491
/** Gets an export specifier of this declaration. */
492-
ExportSpecifier getASpecifier() { result = getSpecifier(_) }
492+
ExportSpecifier getASpecifier() { result = this.getSpecifier(_) }
493493
}
494494

495495
/**
496496
* An export declaration with the `type` modifier.
497497
*/
498498
private class TypeOnlyExportDeclaration extends ExportNamedDeclaration {
499-
TypeOnlyExportDeclaration() { isTypeOnly() }
499+
TypeOnlyExportDeclaration() { this.isTypeOnly() }
500500

501501
override predicate exportsAs(LexicalName v, string name) {
502502
super.exportsAs(v, name) and
@@ -530,13 +530,13 @@ private class TypeOnlyExportDeclaration extends ExportNamedDeclaration {
530530
*/
531531
class ExportSpecifier extends Expr, @exportspecifier {
532532
/** Gets the declaration to which this specifier belongs. */
533-
ExportDeclaration getExportDeclaration() { result = getParent() }
533+
ExportDeclaration getExportDeclaration() { result = this.getParent() }
534534

535535
/** Gets the local symbol that is being exported. */
536-
Identifier getLocal() { result = getChildExpr(0) }
536+
Identifier getLocal() { result = this.getChildExpr(0) }
537537

538538
/** Gets the name under which the symbol is exported. */
539-
Identifier getExported() { result = getChildExpr(1) }
539+
Identifier getExported() { result = this.getChildExpr(1) }
540540

541541
/**
542542
* Gets the local name of the exported symbol, that is, the name
@@ -562,7 +562,7 @@ class ExportSpecifier extends Expr, @exportspecifier {
562562
* The sixth one (unlike the fourth one) _does_ have a local name
563563
* (that is, `default`), since it is a re-export.
564564
*/
565-
string getLocalName() { result = getLocal().getName() }
565+
string getLocalName() { result = this.getLocal().getName() }
566566

567567
/**
568568
* Gets the name under which the symbol is exported.
@@ -581,7 +581,7 @@ class ExportSpecifier extends Expr, @exportspecifier {
581581
* `x`, `z`, `f` and `default`, while the last one does not have
582582
* an exported name since it does not export a unique symbol.
583583
*/
584-
string getExportedName() { result = getExported().getName() }
584+
string getExportedName() { result = this.getExported().getName() }
585585

586586
override string getAPrimaryQlClass() { result = "ExportSpecifier" }
587587
}
@@ -630,11 +630,11 @@ class ExportDefaultSpecifier extends ExportSpecifier, @export_default_specifier
630630
* ```
631631
*/
632632
class ReExportDefaultSpecifier extends ExportDefaultSpecifier {
633-
ReExportDefaultSpecifier() { getExportDeclaration() instanceof ReExportDeclaration }
633+
ReExportDefaultSpecifier() { this.getExportDeclaration() instanceof ReExportDeclaration }
634634

635635
override string getLocalName() { result = "default" }
636636

637-
override string getExportedName() { result = getExported().getName() }
637+
override string getExportedName() { result = this.getExported().getName() }
638638
}
639639

640640
/**
@@ -671,15 +671,15 @@ abstract class ReExportDeclaration extends ExportDeclaration {
671671
abstract ConstantString getImportedPath();
672672

673673
/** Gets the module from which this declaration re-exports, if it is an ES2015 module. */
674-
ES2015Module getReExportedES2015Module() { result = getReExportedModule() }
674+
ES2015Module getReExportedES2015Module() { result = this.getReExportedModule() }
675675

676676
/** Gets the module from which this declaration re-exports. */
677677
cached
678678
Module getReExportedModule() {
679679
Stages::Imports::ref() and
680-
result.getFile() = getEnclosingModule().resolve(getImportedPath())
680+
result.getFile() = this.getEnclosingModule().resolve(this.getImportedPath())
681681
or
682-
result = resolveFromTypeRoot()
682+
result = this.resolveFromTypeRoot()
683683
}
684684

685685
/**
@@ -689,9 +689,9 @@ abstract class ReExportDeclaration extends ExportDeclaration {
689689
result.getFile() =
690690
min(TypeRootFolder typeRoot |
691691
|
692-
typeRoot.getModuleFile(getImportedPath().getStringValue())
692+
typeRoot.getModuleFile(this.getImportedPath().getStringValue())
693693
order by
694-
typeRoot.getSearchPriority(getFile().getParentContainer())
694+
typeRoot.getSearchPriority(this.getFile().getParentContainer())
695695
)
696696
}
697697
}
@@ -700,7 +700,7 @@ abstract class ReExportDeclaration extends ExportDeclaration {
700700
private class LiteralReExportPath extends PathExpr, ConstantString {
701701
LiteralReExportPath() { exists(ReExportDeclaration bred | this = bred.getImportedPath()) }
702702

703-
override string getValue() { result = getStringValue() }
703+
override string getValue() { result = this.getStringValue() }
704704
}
705705

706706
/**

0 commit comments

Comments
 (0)