|
| 1 | +/** |
| 2 | + * Provides a limited public interface to name/type resolution information. |
| 3 | + */ |
| 4 | + |
| 5 | +private import javascript |
| 6 | +private import semmle.javascript.internal.NameResolution |
| 7 | +private import semmle.javascript.internal.TypeResolution |
| 8 | +private import semmle.javascript.internal.UnderlyingTypes |
| 9 | + |
| 10 | +/** |
| 11 | + * Interface for accessing name-resolution info about type names. |
| 12 | + */ |
| 13 | +class TypeNameBindingNode extends NameResolution::Node { |
| 14 | + /** |
| 15 | + * Holds if type refers to, or is an alias for, the given type name relative to the global scope. |
| 16 | + * |
| 17 | + * For example: |
| 18 | + * ```ts |
| 19 | + * var x: Document; // hasQualifiedName("Document") |
| 20 | + * var x: Electron; // hasQualifiedName("Electron") |
| 21 | + * var x: Electron.BrowserWindow; // hasQualifiedName("Electron.BrowserWindow") |
| 22 | + * ``` |
| 23 | + */ |
| 24 | + predicate hasQualifiedName(string qualifiedName) { |
| 25 | + NameResolution::nodeRefersToModule(this, "global", qualifiedName) |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Holds if this refers a value exported by the given module, with the given |
| 30 | + * qualified name. If the `qualifiedName` is empty, this refers to the module itself. |
| 31 | + * |
| 32 | + * For example, the type annotations below have the following name bindings: |
| 33 | + * ```ts |
| 34 | + * import { Request } from "express"; |
| 35 | + * |
| 36 | + * var x: Request; // hasUnderlyingType("express", "Request") |
| 37 | + * var x: Request | null; // no result (see hasUnderlyingType) |
| 38 | + * var x: Request & { prop: string }; // no result (see hasUnderlyingType) |
| 39 | + * |
| 40 | + * interface CustomSubtype extends Request {} |
| 41 | + * |
| 42 | + * var x: CustomSubtype; // no result (see hasUnderlyingType) |
| 43 | + * |
| 44 | + * var x: typeof import("express"); // hasUnderlyingType("express", "") |
| 45 | + * ``` |
| 46 | + */ |
| 47 | + predicate hasQualifiedName(string moduleName, string qualifiedName) { |
| 48 | + NameResolution::nodeRefersToModule(this, moduleName, qualifiedName) |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Holds if this type refers to the given type exported from the given module, after |
| 53 | + * unfolding unions and intersections, and following subtype relations. |
| 54 | + * |
| 55 | + * For example: |
| 56 | + * ```ts |
| 57 | + * import { Request } from "express"; |
| 58 | + * |
| 59 | + * var x: Request; // hasUnderlyingType("express", "Request") |
| 60 | + * var x: Request | null; // hasUnderlyingType("express", "Request") |
| 61 | + * var x: Request & { prop: string }; // hasUnderlyingType("express", "Request") |
| 62 | + * |
| 63 | + * interface CustomSubtype extends Request {} |
| 64 | + * |
| 65 | + * var x: CustomSubtype; // hasUnderlyingType("express", "Request") |
| 66 | + * ``` |
| 67 | + */ |
| 68 | + predicate hasUnderlyingType(string moduleName, string qualifiedName) { |
| 69 | + UnderlyingTypes::nodeHasUnderlyingType(this, moduleName, qualifiedName) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Holds if this type refers to the given type from the global scope, after |
| 74 | + * unfolding unions and intersections, and following subtype relations. |
| 75 | + * |
| 76 | + * For example: |
| 77 | + * ```ts |
| 78 | + * var x: Document; // hasUnderlyingType("Document") |
| 79 | + * var x: Document | null; // hasUnderlyingType("Document") |
| 80 | + * var x: Document & { prop: string }; // hasUnderlyingType("Document") |
| 81 | + * |
| 82 | + * interface CustomSubtype extends Document {} |
| 83 | + * |
| 84 | + * var x: CustomSubtype; // hasUnderlyingType("Document") |
| 85 | + * ``` |
| 86 | + */ |
| 87 | + predicate hasUnderlyingType(string qualifiedName) { |
| 88 | + UnderlyingTypes::nodeHasUnderlyingType(this, qualifiedName) |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Gets the declaration of the type being referenced by this name. |
| 93 | + * |
| 94 | + * For example: |
| 95 | + * ```ts |
| 96 | + * class Foo {} |
| 97 | + * |
| 98 | + * type T = Foo; |
| 99 | + * var x: T; // getTypeDefinition() maps T to the class Foo above |
| 100 | + * ``` |
| 101 | + * |
| 102 | + * Note that this has no result for function-style classes referenced from |
| 103 | + * a JSDoc comment. |
| 104 | + */ |
| 105 | + TypeDefinition getTypeDefinition() { TypeResolution::trackType(result) = this } |
| 106 | + |
| 107 | + /** |
| 108 | + * Gets a class that this type refers to, after unfolding unions and intersections (but not subtyping). |
| 109 | + * |
| 110 | + * For example, the type of `x` maps to the class `C` in each example below: |
| 111 | + * ```ts |
| 112 | + * class C {} |
| 113 | + * |
| 114 | + * var x: C; |
| 115 | + * var x: C | null; |
| 116 | + * var x: C & { prop: string }; |
| 117 | + * ``` |
| 118 | + */ |
| 119 | + DataFlow::ClassNode getAnUnderlyingClass() { |
| 120 | + UnderlyingTypes::nodeHasUnderlyingClassType(this, result) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Interface for accessing name-resolution info about expressions. |
| 126 | + */ |
| 127 | +class ExprNameBindingNode extends NameResolution::Node { |
| 128 | + /** |
| 129 | + * Holds if this refers a value exported by the given module, with the given |
| 130 | + * qualified name. If the `qualifiedName` is empty, this refers to the module itself. |
| 131 | + * |
| 132 | + * For example, the type annotations below have the following name bindings: |
| 133 | + * ```ts |
| 134 | + * import * as f from "foo"; |
| 135 | + * |
| 136 | + * var x = f; // hasQualifiedName(f, "") |
| 137 | + * var x = f.x.y; // hasQualifiedName(f, "x.y") |
| 138 | + * ``` |
| 139 | + */ |
| 140 | + predicate hasQualifiedName(string moduleName, string qualifiedName) { |
| 141 | + NameResolution::nodeRefersToModule(this, moduleName, qualifiedName) |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Gets the class, or function acting as a class, referenced by this name. |
| 146 | + * |
| 147 | + * ```ts |
| 148 | + * class Foo {} |
| 149 | + * const T = Foo; |
| 150 | + * var x = T; // getClassNode() maps T to the class Foo above |
| 151 | + * |
| 152 | + * function Bar() {} |
| 153 | + * Bar.prototype.blah = function() {}; |
| 154 | + * const S = Bar; |
| 155 | + * var x = S; // getClassNode() maps S to the function Bar above |
| 156 | + * ``` |
| 157 | + */ |
| 158 | + DataFlow::ClassNode getClassNode() { NameResolution::nodeRefersToClass(this, result) } |
| 159 | +} |
0 commit comments