Skip to content

Commit 2b1da6e

Browse files
committed
JS: Replace implementation of hasUnderlyingType
1 parent 0f26940 commit 2b1da6e

File tree

3 files changed

+8
-89
lines changed

3 files changed

+8
-89
lines changed

javascript/ql/lib/semmle/javascript/JSDoc.qll

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -423,46 +423,6 @@ class JSDocNamedTypeExpr extends JSDocTypeExpr {
423423
suffix = name.regexpCapture(regex, 2)
424424
)
425425
}
426-
427-
pragma[noinline]
428-
pragma[nomagic]
429-
private predicate hasNamePartsAndEnv(string prefix, string suffix, JSDoc::Environment env) {
430-
// Force join ordering
431-
this.hasNameParts(prefix, suffix) and
432-
env.isContainerInScope(this.getContainer())
433-
}
434-
435-
/**
436-
* Gets the qualified name of this name by resolving its prefix, if any.
437-
*/
438-
cached
439-
private string resolvedName() {
440-
exists(string prefix, string suffix, JSDoc::Environment env |
441-
this.hasNamePartsAndEnv(prefix, suffix, env) and
442-
result = env.resolveAlias(prefix) + suffix
443-
)
444-
}
445-
446-
override predicate hasQualifiedName(string globalName) {
447-
globalName = this.resolvedName()
448-
or
449-
not exists(this.resolvedName()) and
450-
globalName = this.getRawName()
451-
}
452-
453-
override DataFlow::ClassNode getClass() {
454-
exists(string name |
455-
this.hasQualifiedName(name) and
456-
result.hasQualifiedName(name)
457-
)
458-
or
459-
// Handle case where a local variable has a reference to the class,
460-
// but the class doesn't have a globally qualified name.
461-
exists(string alias, JSDoc::Environment env |
462-
this.hasNamePartsAndEnv(alias, "", env) and
463-
result.getAClassReference().flowsTo(env.getNodeFromAlias(alias))
464-
)
465-
}
466426
}
467427

468428
/**
@@ -492,10 +452,6 @@ class JSDocAppliedTypeExpr extends @jsdoc_applied_type_expr, JSDocTypeExpr {
492452
*/
493453
JSDocTypeExpr getAnArgument() { result = this.getArgument(_) }
494454

495-
override predicate hasQualifiedName(string globalName) {
496-
this.getHead().hasQualifiedName(globalName)
497-
}
498-
499455
override DataFlow::ClassNode getClass() { result = this.getHead().getClass() }
500456
}
501457

javascript/ql/lib/semmle/javascript/TypeAnnotations.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import javascript
66
private import internal.StmtContainers
7+
private import internal.NameResolution
8+
private import internal.UnderlyingTypes
79

810
/**
911
* A type annotation, either in the form of a TypeScript type or a JSDoc comment.
@@ -77,12 +79,16 @@ class TypeAnnotation extends @type_annotation, NodeInStmtContainer {
7779
/**
7880
* Holds if this is a reference to the type with qualified name `globalName` relative to the global scope.
7981
*/
80-
predicate hasQualifiedName(string globalName) { none() }
82+
final predicate hasQualifiedName(string globalName) {
83+
UnderlyingTypes::nodeHasUnderlyingType(this, "global", globalName)
84+
}
8185

8286
/**
8387
* Holds if this is a reference to the type exported from `moduleName` under the name `exportedName`.
8488
*/
85-
predicate hasQualifiedName(string moduleName, string exportedName) { none() }
89+
final predicate hasQualifiedName(string moduleName, string exportedName) {
90+
UnderlyingTypes::nodeHasUnderlyingType(this, moduleName, exportedName)
91+
}
8692

8793
/** Gets the statement in which this type appears. */
8894
Stmt getEnclosingStmt() { none() }

javascript/ql/lib/semmle/javascript/TypeScript.qll

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -699,41 +699,6 @@ class TypeAccess extends @typeaccess, TypeExpr, TypeRef {
699699
*/
700700
TypeName getTypeName() { ast_node_symbol(this, result) }
701701

702-
override predicate hasQualifiedName(string globalName) {
703-
this.getTypeName().hasQualifiedName(globalName)
704-
or
705-
exists(LocalTypeAccess local | local = this |
706-
not exists(local.getLocalTypeName()) and // Without a local type name, the type is looked up in the global scope.
707-
globalName = local.getName()
708-
)
709-
}
710-
711-
override predicate hasQualifiedName(string moduleName, string exportedName) {
712-
this.getTypeName().hasQualifiedName(moduleName, exportedName)
713-
or
714-
exists(ImportDeclaration imprt, ImportSpecifier spec |
715-
moduleName = getImportName(imprt) and
716-
spec = imprt.getASpecifier()
717-
|
718-
spec.getImportedName() = exportedName and
719-
this = spec.getLocal().(TypeDecl).getLocalTypeName().getAnAccess()
720-
or
721-
(spec instanceof ImportNamespaceSpecifier or spec instanceof ImportDefaultSpecifier) and
722-
this =
723-
spec.getLocal().(LocalNamespaceDecl).getLocalNamespaceName().getAMemberAccess(exportedName)
724-
)
725-
or
726-
exists(ImportEqualsDeclaration imprt |
727-
moduleName = getImportName(imprt.getImportedEntity()) and
728-
this =
729-
imprt
730-
.getIdentifier()
731-
.(LocalNamespaceDecl)
732-
.getLocalNamespaceName()
733-
.getAMemberAccess(exportedName)
734-
)
735-
}
736-
737702
override string getAPrimaryQlClass() { result = "TypeAccess" }
738703
}
739704

@@ -823,14 +788,6 @@ class GenericTypeExpr extends @generic_typeexpr, TypeExpr {
823788
/** Gets the number of type arguments. This is always at least one. */
824789
int getNumTypeArgument() { result = count(this.getATypeArgument()) }
825790

826-
override predicate hasQualifiedName(string globalName) {
827-
this.getTypeAccess().hasQualifiedName(globalName)
828-
}
829-
830-
override predicate hasQualifiedName(string moduleName, string exportedName) {
831-
this.getTypeAccess().hasQualifiedName(moduleName, exportedName)
832-
}
833-
834791
override string getAPrimaryQlClass() { result = "GenericTypeExpr" }
835792
}
836793

0 commit comments

Comments
 (0)