5
5
import javascript
6
6
7
7
module Closure {
8
+ /** A call to `goog.require` */
9
+ class RequireCallExpr extends CallExpr {
10
+ RequireCallExpr ( ) { this .getCallee ( ) .( PropAccess ) .getQualifiedName ( ) = "goog.require" }
11
+
12
+ /** Gets the imported namespace name. */
13
+ string getClosureNamespace ( ) { result = this .getArgument ( 0 ) .getStringValue ( ) }
14
+ }
15
+
16
+ /** A call to `goog.provide` */
17
+ class ProvideCallExpr extends CallExpr {
18
+ ProvideCallExpr ( ) { this .getCallee ( ) .( PropAccess ) .getQualifiedName ( ) = "goog.provide" }
19
+
20
+ /** Gets the imported namespace name. */
21
+ string getClosureNamespace ( ) { result = this .getArgument ( 0 ) .getStringValue ( ) }
22
+ }
23
+
24
+ /** A call to `goog.module` or `goog.declareModuleId`. */
25
+ private class ModuleDeclarationCall extends CallExpr {
26
+ private string kind ;
27
+
28
+ ModuleDeclarationCall ( ) {
29
+ this .getCallee ( ) .( PropAccess ) .getQualifiedName ( ) = kind and
30
+ kind = [ "goog.module" , "goog.declareModuleId" ]
31
+ }
32
+
33
+ /** Gets the declared namespace. */
34
+ string getClosureNamespace ( ) { result = this .getArgument ( 0 ) .getStringValue ( ) }
35
+
36
+ /** Gets the string `goog.module` or `goog.declareModuleId` depending on which method is being called. */
37
+ string getModuleKind ( ) { result = kind }
38
+ }
39
+
8
40
/**
9
41
* A reference to a Closure namespace.
10
42
*/
11
- class ClosureNamespaceRef extends DataFlow:: Node instanceof ClosureNamespaceRef:: Range {
43
+ deprecated class ClosureNamespaceRef extends DataFlow:: Node instanceof ClosureNamespaceRef:: Range {
12
44
/**
13
45
* Gets the namespace being referenced.
14
46
*/
15
47
string getClosureNamespace ( ) { result = super .getClosureNamespace ( ) }
16
48
}
17
49
18
- module ClosureNamespaceRef {
50
+ deprecated module ClosureNamespaceRef {
19
51
/**
20
52
* A reference to a Closure namespace.
21
53
*
@@ -32,10 +64,10 @@ module Closure {
32
64
/**
33
65
* A data flow node that returns the value of a closure namespace.
34
66
*/
35
- class ClosureNamespaceAccess extends ClosureNamespaceRef instanceof ClosureNamespaceAccess:: Range {
36
- }
67
+ deprecated class ClosureNamespaceAccess extends ClosureNamespaceRef instanceof ClosureNamespaceAccess:: Range
68
+ { }
37
69
38
- module ClosureNamespaceAccess {
70
+ deprecated module ClosureNamespaceAccess {
39
71
/**
40
72
* A data flow node that returns the value of a closure namespace.
41
73
*
@@ -47,7 +79,7 @@ module Closure {
47
79
/**
48
80
* A call to a method on the `goog.` namespace, as a closure reference.
49
81
*/
50
- abstract private class DefaultNamespaceRef extends DataFlow:: MethodCallNode ,
82
+ abstract deprecated private class DefaultNamespaceRef extends DataFlow:: MethodCallNode ,
51
83
ClosureNamespaceRef:: Range
52
84
{
53
85
DefaultNamespaceRef ( ) { this = DataFlow:: globalVarRef ( "goog" ) .getAMethodCall ( ) }
@@ -59,14 +91,14 @@ module Closure {
59
91
* Holds if `node` is the data flow node corresponding to the expression in
60
92
* a top-level expression statement.
61
93
*/
62
- private predicate isTopLevelExpr ( DataFlow:: Node node ) {
94
+ deprecated private predicate isTopLevelExpr ( DataFlow:: Node node ) {
63
95
any ( TopLevel tl ) .getAChildStmt ( ) .( ExprStmt ) .getExpr ( ) .flow ( ) = node
64
96
}
65
97
66
98
/**
67
99
* A top-level call to `goog.provide`.
68
100
*/
69
- private class DefaultClosureProvideCall extends DefaultNamespaceRef {
101
+ deprecated private class DefaultClosureProvideCall extends DefaultNamespaceRef {
70
102
DefaultClosureProvideCall ( ) {
71
103
this .getMethodName ( ) = "provide" and
72
104
isTopLevelExpr ( this )
@@ -76,27 +108,28 @@ module Closure {
76
108
/**
77
109
* A top-level call to `goog.provide`.
78
110
*/
79
- class ClosureProvideCall extends ClosureNamespaceRef , DataFlow:: MethodCallNode instanceof DefaultClosureProvideCall
111
+ deprecated class ClosureProvideCall extends ClosureNamespaceRef , DataFlow:: MethodCallNode instanceof DefaultClosureProvideCall
80
112
{ }
81
113
82
114
/**
83
115
* A call to `goog.require`.
84
116
*/
85
- private class DefaultClosureRequireCall extends DefaultNamespaceRef , ClosureNamespaceAccess:: Range
117
+ deprecated private class DefaultClosureRequireCall extends DefaultNamespaceRef ,
118
+ ClosureNamespaceAccess:: Range
86
119
{
87
120
DefaultClosureRequireCall ( ) { this .getMethodName ( ) = "require" }
88
121
}
89
122
90
123
/**
91
124
* A call to `goog.require`.
92
125
*/
93
- class ClosureRequireCall extends ClosureNamespaceAccess , DataFlow:: MethodCallNode instanceof DefaultClosureRequireCall
126
+ deprecated class ClosureRequireCall extends ClosureNamespaceAccess , DataFlow:: MethodCallNode instanceof DefaultClosureRequireCall
94
127
{ }
95
128
96
129
/**
97
130
* A top-level call to `goog.module` or `goog.declareModuleId`.
98
131
*/
99
- private class DefaultClosureModuleDeclaration extends DefaultNamespaceRef {
132
+ deprecated private class DefaultClosureModuleDeclaration extends DefaultNamespaceRef {
100
133
DefaultClosureModuleDeclaration ( ) {
101
134
( this .getMethodName ( ) = "module" or this .getMethodName ( ) = "declareModuleId" ) and
102
135
isTopLevelExpr ( this )
@@ -106,41 +139,29 @@ module Closure {
106
139
/**
107
140
* A top-level call to `goog.module` or `goog.declareModuleId`.
108
141
*/
109
- class ClosureModuleDeclaration extends ClosureNamespaceRef , DataFlow:: MethodCallNode instanceof DefaultClosureModuleDeclaration
142
+ deprecated class ClosureModuleDeclaration extends ClosureNamespaceRef , DataFlow:: MethodCallNode instanceof DefaultClosureModuleDeclaration
110
143
{ }
111
144
112
- private GlobalVariable googVariable ( ) { variables ( result , "goog" , any ( GlobalScope sc ) ) }
113
-
114
- pragma [ nomagic]
115
- private MethodCallExpr googModuleDeclExpr ( ) {
116
- result .getReceiver ( ) = googVariable ( ) .getAnAccess ( ) and
117
- result .getMethodName ( ) = [ "module" , "declareModuleId" ]
118
- }
119
-
120
- pragma [ nomagic]
121
- private MethodCallExpr googModuleDeclExprInContainer ( StmtContainer container ) {
122
- result = googModuleDeclExpr ( ) and
123
- container = result .getContainer ( )
124
- }
125
-
126
145
pragma [ noinline]
127
- private ClosureRequireCall getARequireInTopLevel ( ClosureModule m ) { result .getTopLevel ( ) = m }
146
+ private RequireCallExpr getARequireInTopLevel ( ClosureModule m ) { result .getTopLevel ( ) = m }
128
147
129
148
/**
130
149
* A module using the Closure module system, declared using `goog.module()` or `goog.declareModuleId()`.
131
150
*/
132
151
class ClosureModule extends Module {
133
- ClosureModule ( ) { exists ( googModuleDeclExprInContainer ( this ) ) }
152
+ private ModuleDeclarationCall decl ;
153
+
154
+ ClosureModule ( ) { decl .getTopLevel ( ) = this }
134
155
135
156
/**
136
157
* Gets the call to `goog.module` or `goog.declareModuleId` in this module.
137
158
*/
138
- ClosureModuleDeclaration getModuleDeclaration ( ) { result .getTopLevel ( ) = this }
159
+ deprecated ClosureModuleDeclaration getModuleDeclaration ( ) { result .getTopLevel ( ) = this }
139
160
140
161
/**
141
162
* Gets the namespace of this module.
142
163
*/
143
- string getClosureNamespace ( ) { result = this . getModuleDeclaration ( ) .getClosureNamespace ( ) }
164
+ string getClosureNamespace ( ) { result = decl .getClosureNamespace ( ) }
144
165
145
166
override Module getAnImportedModule ( ) {
146
167
result .( ClosureModule ) .getClosureNamespace ( ) =
@@ -156,7 +177,7 @@ module Closure {
156
177
* Has no result for ES6 modules using `goog.declareModuleId`.
157
178
*/
158
179
Variable getExportsVariable ( ) {
159
- this . getModuleDeclaration ( ) . getMethodName ( ) = "module" and
180
+ decl . getModuleKind ( ) = "goog. module" and
160
181
result = this .getScope ( ) .getVariable ( "exports" )
161
182
}
162
183
@@ -185,23 +206,23 @@ module Closure {
185
206
ClosureScript ( ) {
186
207
not this instanceof ClosureModule and
187
208
(
188
- any ( ClosureProvideCall provide ) .getTopLevel ( ) = this
209
+ any ( ProvideCallExpr provide ) .getTopLevel ( ) = this
189
210
or
190
- any ( ClosureRequireCall require ) .getTopLevel ( ) = this
211
+ any ( RequireCallExpr require ) .getTopLevel ( ) = this
191
212
)
192
213
}
193
214
194
215
/** Gets the identifier of a namespace required by this module. */
195
216
string getARequiredNamespace ( ) {
196
- exists ( ClosureRequireCall require |
217
+ exists ( RequireCallExpr require |
197
218
require .getTopLevel ( ) = this and
198
219
result = require .getClosureNamespace ( )
199
220
)
200
221
}
201
222
202
223
/** Gets the identifer of a namespace provided by this module. */
203
224
string getAProvidedNamespace ( ) {
204
- exists ( ClosureProvideCall require |
225
+ exists ( ProvideCallExpr require |
205
226
require .getTopLevel ( ) = this and
206
227
result = require .getClosureNamespace ( )
207
228
)
@@ -213,7 +234,13 @@ module Closure {
213
234
*/
214
235
pragma [ noinline]
215
236
predicate isClosureNamespace ( string name ) {
216
- exists ( string namespace | namespace = any ( ClosureNamespaceRef ref ) .getClosureNamespace ( ) |
237
+ exists ( string namespace |
238
+ namespace =
239
+ [
240
+ any ( RequireCallExpr ref ) .getClosureNamespace ( ) ,
241
+ any ( ModuleDeclarationCall c ) .getClosureNamespace ( )
242
+ ]
243
+ |
217
244
name = namespace .substring ( 0 , namespace .indexOf ( "." ) )
218
245
or
219
246
name = namespace
0 commit comments