55import javascript
66
77module 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.module` or `goog.declareModuleId`. */
17+ private class ModuleDeclarationCall extends CallExpr {
18+ private string kind ;
19+
20+ ModuleDeclarationCall ( ) {
21+ this .getCallee ( ) .( PropAccess ) .getQualifiedName ( ) = kind and
22+ kind = [ "goog.module" , "goog.declareModuleId" ]
23+ }
24+
25+ /** Gets the declared namespace. */
26+ string getClosureNamespace ( ) { result = this .getArgument ( 0 ) .getStringValue ( ) }
27+
28+ /** Gets the string `goog.module` or `goog.declareModuleId` depending on which method is being called. */
29+ string getModuleKind ( ) { result = kind }
30+ }
31+
832 /**
933 * A reference to a Closure namespace.
1034 */
@@ -109,38 +133,26 @@ module Closure {
109133 class ClosureModuleDeclaration extends ClosureNamespaceRef , DataFlow:: MethodCallNode instanceof DefaultClosureModuleDeclaration
110134 { }
111135
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-
126136 pragma [ noinline]
127- private ClosureRequireCall getARequireInTopLevel ( ClosureModule m ) { result .getTopLevel ( ) = m }
137+ private RequireCallExpr getARequireInTopLevel ( ClosureModule m ) { result .getTopLevel ( ) = m }
128138
129139 /**
130140 * A module using the Closure module system, declared using `goog.module()` or `goog.declareModuleId()`.
131141 */
132142 class ClosureModule extends Module {
133- ClosureModule ( ) { exists ( googModuleDeclExprInContainer ( this ) ) }
143+ private ModuleDeclarationCall decl ;
144+
145+ ClosureModule ( ) { decl .getTopLevel ( ) = this }
134146
135147 /**
136148 * Gets the call to `goog.module` or `goog.declareModuleId` in this module.
137149 */
138- ClosureModuleDeclaration getModuleDeclaration ( ) { result .getTopLevel ( ) = this }
150+ deprecated ClosureModuleDeclaration getModuleDeclaration ( ) { result .getTopLevel ( ) = this }
139151
140152 /**
141153 * Gets the namespace of this module.
142154 */
143- string getClosureNamespace ( ) { result = this . getModuleDeclaration ( ) .getClosureNamespace ( ) }
155+ string getClosureNamespace ( ) { result = decl .getClosureNamespace ( ) }
144156
145157 override Module getAnImportedModule ( ) {
146158 result .( ClosureModule ) .getClosureNamespace ( ) =
@@ -156,7 +168,7 @@ module Closure {
156168 * Has no result for ES6 modules using `goog.declareModuleId`.
157169 */
158170 Variable getExportsVariable ( ) {
159- this . getModuleDeclaration ( ) . getMethodName ( ) = "module" and
171+ decl . getModuleKind ( ) = "goog. module" and
160172 result = this .getScope ( ) .getVariable ( "exports" )
161173 }
162174
0 commit comments