Skip to content

Commit e441dd4

Browse files
committed
JS: Expose hasBothNamedAndDefaultExports()
1 parent 96f8a02 commit e441dd4

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ class ES2015Module extends Module {
3939
// modules are implicitly strict
4040
any()
4141
}
42+
43+
/**
44+
* Holds if this module contains both named and `default` exports.
45+
*
46+
* This is used to determine whether a default-import of the module should be reinterpreted
47+
* as a namespace-import, to accommodate the non-standard behavior implemented by some compilers.
48+
*
49+
* When a module has both named and `default` exports, the non-standard interpretation can lead to
50+
* ambiguities, so we only allow the standard interpretation in that case.
51+
*/
52+
predicate hasBothNamedAndDefaultExports() {
53+
hasNamedExports(this) and
54+
hasDefaultExport(this)
55+
}
4256
}
4357

4458
/**
@@ -64,17 +78,6 @@ private predicate hasDefaultExport(ES2015Module mod) {
6478
mod.getAnExport().(ExportNamedDeclaration).getASpecifier().getExportedName() = "default"
6579
}
6680

67-
/**
68-
* Holds if `mod` contains both named and `default` exports.
69-
*
70-
* This is used to determine whether a default-import of the module should be reinterpreted
71-
* as a namespace-import, to accommodate the non-standard behavior implemented by some compilers.
72-
*/
73-
private predicate hasBothNamedAndDefaultExports(ES2015Module mod) {
74-
hasNamedExports(mod) and
75-
hasDefaultExport(mod)
76-
}
77-
7881
/**
7982
* An import declaration.
8083
*
@@ -131,7 +134,7 @@ class ImportDeclaration extends Stmt, Import, @import_declaration {
131134
// For compatibility with the non-standard implementation of default imports,
132135
// treat default imports as namespace imports in cases where it can't cause ambiguity
133136
// between named exports and the properties of a default-exported object.
134-
not hasBothNamedAndDefaultExports(this.getImportedModule()) and
137+
not this.getImportedModule().(ES2015Module).hasBothNamedAndDefaultExports() and
135138
is.getImportedName() = "default"
136139
)
137140
or

0 commit comments

Comments
 (0)