mirror of
https://github.com/github/codeql.git
synced 2026-04-25 00:35:20 +02:00
Merge pull request #19077 from asgerf/js/jsdoc-name-tokens
JS: Separate JSDoc qualified names into individual identifiers
This commit is contained in:
@@ -296,7 +296,7 @@ module DOM {
|
||||
.getType()
|
||||
.getAnUnderlyingType()
|
||||
.(JSDocNamedTypeExpr)
|
||||
.getName())
|
||||
.getRawName())
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -400,8 +400,8 @@ class ConstructorTag extends JSDocTag {
|
||||
abstract private class NamedTypeReferent extends JSDocTag {
|
||||
/** Gets the name of the type to which this tag refers. */
|
||||
string getTarget() {
|
||||
result = this.getType().(JSDocNamedTypeExpr).getName() or
|
||||
result = this.getType().(JSDocAppliedTypeExpr).getHead().(JSDocNamedTypeExpr).getName()
|
||||
result = this.getType().(JSDocNamedTypeExpr).getRawName() or
|
||||
result = this.getType().(JSDocAppliedTypeExpr).getHead().(JSDocNamedTypeExpr).getRawName()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -423,7 +423,7 @@ abstract private class NamedTypeReferent extends JSDocTag {
|
||||
* Gets the source declaration of the type to which `tp` refers, if any.
|
||||
*/
|
||||
private ExternalType sourceDecl(JSDocTypeExpr tp) {
|
||||
result.getQualifiedName() = tp.(JSDocNamedTypeExpr).getName() or
|
||||
result.getQualifiedName() = tp.(JSDocNamedTypeExpr).getRawName() or
|
||||
result = sourceDecl(tp.(JSDocAppliedTypeExpr).getHead()) or
|
||||
result = sourceDecl(tp.(JSDocNullableTypeExpr).getTypeExpr()) or
|
||||
result = sourceDecl(tp.(JSDocNonNullableTypeExpr).getTypeExpr()) or
|
||||
|
||||
@@ -261,17 +261,14 @@ class JSDocVoidTypeExpr extends @jsdoc_void_type_expr, JSDocTypeExpr {
|
||||
}
|
||||
|
||||
/**
|
||||
* A type expression referring to a named type.
|
||||
* An identifier in a JSDoc type expression, such as `Object` or `string`.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```
|
||||
* string
|
||||
* Object
|
||||
* ```
|
||||
* Note that qualified names consist of multiple identifier nodes.
|
||||
*/
|
||||
class JSDocNamedTypeExpr extends @jsdoc_named_type_expr, JSDocTypeExpr {
|
||||
/** Gets the name of the type the expression refers to. */
|
||||
class JSDocIdentifierTypeExpr extends @jsdoc_identifier_type_expr, JSDocTypeExpr {
|
||||
/**
|
||||
* Gets the name of the identifier.
|
||||
*/
|
||||
string getName() { result = this.toString() }
|
||||
|
||||
override predicate isString() { this.getName() = "string" }
|
||||
@@ -300,6 +297,71 @@ class JSDocNamedTypeExpr extends @jsdoc_named_type_expr, JSDocTypeExpr {
|
||||
}
|
||||
|
||||
override predicate isRawFunction() { this.getName() = "Function" }
|
||||
}
|
||||
|
||||
/**
|
||||
* An unqualified identifier in a JSDoc type expression.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```
|
||||
* string
|
||||
* Object
|
||||
* ```
|
||||
*/
|
||||
class JSDocLocalTypeAccess extends JSDocIdentifierTypeExpr {
|
||||
JSDocLocalTypeAccess() { not this = any(JSDocQualifiedTypeAccess a).getNameNode() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A qualified type name in a JSDoc type expression, such as `X.Y`.
|
||||
*/
|
||||
class JSDocQualifiedTypeAccess extends @jsdoc_qualified_type_expr, JSDocTypeExpr {
|
||||
/**
|
||||
* Gets the base of this access, such as the `X` in `X.Y`.
|
||||
*/
|
||||
JSDocTypeExpr getBase() { result = this.getChild(0) }
|
||||
|
||||
/**
|
||||
* Gets the node naming the member being accessed, such as the `Y` node in `X.Y`.
|
||||
*/
|
||||
JSDocIdentifierTypeExpr getNameNode() { result = this.getChild(1) }
|
||||
|
||||
/**
|
||||
* Gets the name being accessed, such as `Y` in `X.Y`.
|
||||
*/
|
||||
string getName() { result = this.getNameNode().getName() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A type expression referring to a named type.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```
|
||||
* string
|
||||
* Object
|
||||
* Namespace.Type
|
||||
* ```
|
||||
*/
|
||||
class JSDocNamedTypeExpr extends JSDocTypeExpr {
|
||||
JSDocNamedTypeExpr() {
|
||||
this instanceof JSDocLocalTypeAccess
|
||||
or
|
||||
this instanceof JSDocQualifiedTypeAccess
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name directly as it appears in this type, including any qualifiers.
|
||||
*
|
||||
* For example, for `X.Y` this gets the string `"X.Y"`.
|
||||
*/
|
||||
string getRawName() { result = this.toString() }
|
||||
|
||||
/**
|
||||
* DEPRECATED. Use `getRawName()` instead.
|
||||
*/
|
||||
deprecated string getName() { result = this.toString() }
|
||||
|
||||
/**
|
||||
* Holds if this name consists of the unqualified name `prefix`
|
||||
@@ -310,8 +372,9 @@ class JSDocNamedTypeExpr extends @jsdoc_named_type_expr, JSDocTypeExpr {
|
||||
* - `Baz` has prefix `Baz` and an empty suffix.
|
||||
*/
|
||||
predicate hasNameParts(string prefix, string suffix) {
|
||||
not this = any(JSDocQualifiedTypeAccess a).getBase() and // restrict size of predicate
|
||||
exists(string regex, string name | regex = "([^.]+)(.*)" |
|
||||
name = this.getName() and
|
||||
name = this.getRawName() and
|
||||
prefix = name.regexpCapture(regex, 1) and
|
||||
suffix = name.regexpCapture(regex, 2)
|
||||
)
|
||||
@@ -340,7 +403,7 @@ class JSDocNamedTypeExpr extends @jsdoc_named_type_expr, JSDocTypeExpr {
|
||||
globalName = this.resolvedName()
|
||||
or
|
||||
not exists(this.resolvedName()) and
|
||||
globalName = this.getName()
|
||||
globalName = this.getRawName()
|
||||
}
|
||||
|
||||
override DataFlow::ClassNode getClass() {
|
||||
|
||||
@@ -1001,7 +1001,7 @@ case @jsdoc_type_expr.kind of
|
||||
| 2 = @jsdoc_undefined_type_expr
|
||||
| 3 = @jsdoc_unknown_type_expr
|
||||
| 4 = @jsdoc_void_type_expr
|
||||
| 5 = @jsdoc_named_type_expr
|
||||
| 5 = @jsdoc_identifier_type_expr
|
||||
| 6 = @jsdoc_applied_type_expr
|
||||
| 7 = @jsdoc_nullable_type_expr
|
||||
| 8 = @jsdoc_non_nullable_type_expr
|
||||
@@ -1011,6 +1011,7 @@ case @jsdoc_type_expr.kind of
|
||||
| 12 = @jsdoc_function_type_expr
|
||||
| 13 = @jsdoc_optional_type_expr
|
||||
| 14 = @jsdoc_rest_type_expr
|
||||
| 15 = @jsdoc_qualified_type_expr
|
||||
;
|
||||
|
||||
#keyset[id, idx]
|
||||
|
||||
@@ -1334,10 +1334,14 @@
|
||||
<v>8</v>
|
||||
</e>
|
||||
<e>
|
||||
<k>@jsdoc_named_type_expr</k>
|
||||
<k>@jsdoc_identifier_type_expr</k>
|
||||
<v>18639</v>
|
||||
</e>
|
||||
<e>
|
||||
<k>@jsdoc_qualified_type_expr</k>
|
||||
<v>1000</v>
|
||||
</e>
|
||||
<e>
|
||||
<k>@jsdoc_applied_type_expr</k>
|
||||
<v>303</v>
|
||||
</e>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,2 @@
|
||||
description: split up qualified names in jsdoc type exprs
|
||||
compatibility: partial
|
||||
@@ -1,5 +1,10 @@
|
||||
| bar.js:5:14:5:14 | x | x |
|
||||
| bar.js:5:14:5:18 | x.Foo | ns.very.long.namespace.Foo |
|
||||
| bar.js:12:14:12:17 | iife | iife |
|
||||
| bar.js:12:14:12:21 | iife.Foo | IIFE.Foo |
|
||||
| closure.js:8:12:8:15 | goog | goog |
|
||||
| closure.js:8:12:8:19 | goog.net | goog.net |
|
||||
| closure.js:8:12:8:28 | goog.net.SomeType | goog.net.SomeType |
|
||||
| closure.js:9:12:9:14 | net | net |
|
||||
| closure.js:9:12:9:23 | net.SomeType | goog.net.SomeType |
|
||||
| closure.js:10:12:10:19 | SomeType | goog.net.SomeType |
|
||||
|
||||
@@ -278,7 +278,11 @@ test_JSDocTypeExpr
|
||||
| tst.js:26:14:26:20 | boolean | tst.js:26:5:26:11 | @define | 0 |
|
||||
| tst.js:31:13:31:19 | boolean | tst.js:31:4:31:10 | @return | 0 |
|
||||
| tst.js:53:11:53:16 | number | tst.js:53:4:53:8 | @enum | 0 |
|
||||
| tst.js:68:14:68:17 | goog | tst.js:68:14:68:20 | goog.ds | 0 |
|
||||
| tst.js:68:14:68:20 | goog.ds | tst.js:68:14:68:34 | goog.ds.BasicNodeList | 0 |
|
||||
| tst.js:68:14:68:34 | goog.ds.BasicNodeList | tst.js:68:4:68:11 | @extends | 0 |
|
||||
| tst.js:68:19:68:20 | ds | tst.js:68:14:68:20 | goog.ds | 1 |
|
||||
| tst.js:68:22:68:34 | BasicNodeList | tst.js:68:14:68:34 | goog.ds.BasicNodeList | 1 |
|
||||
| tst.js:95:17:95:21 | Shape | tst.js:95:4:95:14 | @implements | 0 |
|
||||
| tst.js:110:14:110:18 | Shape | tst.js:110:4:110:11 | @extends | 0 |
|
||||
| tst.js:134:13:134:18 | Object | tst.js:134:4:134:10 | @return | 0 |
|
||||
@@ -298,7 +302,9 @@ test_JSDocTypeExpr
|
||||
| tst.js:216:15:216:29 | (string\|number) | tst.js:216:5:216:12 | @typedef | 0 |
|
||||
| tst.js:216:16:216:21 | string | tst.js:216:15:216:29 | (string\|number) | 0 |
|
||||
| tst.js:216:23:216:28 | number | tst.js:216:15:216:29 | (string\|number) | 1 |
|
||||
| tst.js:219:13:219:16 | goog | tst.js:219:13:219:27 | goog.NumberLike | 0 |
|
||||
| tst.js:219:13:219:27 | goog.NumberLike | tst.js:219:5:219:10 | @param | 0 |
|
||||
| tst.js:219:18:219:27 | NumberLike | tst.js:219:13:219:27 | goog.NumberLike | 1 |
|
||||
| tst.js:223:12:223:36 | {myNum: number, myObject} | tst.js:223:5:223:9 | @type | 0 |
|
||||
| tst.js:223:20:223:25 | number | tst.js:223:12:223:36 | {myNum: number, myObject} | 0 |
|
||||
| tst.js:226:12:226:17 | number | tst.js:226:12:226:18 | number? | 0 |
|
||||
@@ -311,10 +317,18 @@ test_JSDocTypeExpr
|
||||
| tst.js:234:12:234:29 | function (): number | tst.js:234:4:234:9 | @param | 0 |
|
||||
| tst.js:234:24:234:29 | number | tst.js:234:12:234:29 | function (): number | -1 |
|
||||
| tst.js:235:12:235:46 | function (this: goog.ui.Menu, string) | tst.js:235:4:235:9 | @param | 0 |
|
||||
| tst.js:235:26:235:29 | goog | tst.js:235:26:235:32 | goog.ui | 0 |
|
||||
| tst.js:235:26:235:32 | goog.ui | tst.js:235:26:235:37 | goog.ui.Menu | 0 |
|
||||
| tst.js:235:26:235:37 | goog.ui.Menu | tst.js:235:12:235:46 | function (this: goog.ui.Menu, string) | -2 |
|
||||
| tst.js:235:31:235:32 | ui | tst.js:235:26:235:32 | goog.ui | 1 |
|
||||
| tst.js:235:34:235:37 | Menu | tst.js:235:26:235:37 | goog.ui.Menu | 1 |
|
||||
| tst.js:235:40:235:45 | string | tst.js:235:12:235:46 | function (this: goog.ui.Menu, string) | 0 |
|
||||
| tst.js:236:12:236:45 | function (new: goog.ui.Menu, string) | tst.js:236:4:236:9 | @param | 0 |
|
||||
| tst.js:236:25:236:28 | goog | tst.js:236:25:236:31 | goog.ui | 0 |
|
||||
| tst.js:236:25:236:31 | goog.ui | tst.js:236:25:236:36 | goog.ui.Menu | 0 |
|
||||
| tst.js:236:25:236:36 | goog.ui.Menu | tst.js:236:12:236:45 | function (new: goog.ui.Menu, string) | -2 |
|
||||
| tst.js:236:30:236:31 | ui | tst.js:236:25:236:31 | goog.ui | 1 |
|
||||
| tst.js:236:33:236:36 | Menu | tst.js:236:25:236:36 | goog.ui.Menu | 1 |
|
||||
| tst.js:236:39:236:44 | string | tst.js:236:12:236:45 | function (new: goog.ui.Menu, string) | 0 |
|
||||
| tst.js:237:12:237:48 | function (string, ...[number]): number | tst.js:237:4:237:9 | @param | 0 |
|
||||
| tst.js:237:21:237:26 | string | tst.js:237:12:237:48 | function (string, ...[number]): number | 0 |
|
||||
|
||||
@@ -5,6 +5,8 @@ test_isNumber
|
||||
test_QualifiedName
|
||||
| VarType | tst.js:9:13:9:19 | VarType |
|
||||
| boolean | tst.js:5:14:5:20 | boolean |
|
||||
| foo | tst.js:4:12:4:14 | foo |
|
||||
| foo.bar | tst.js:4:12:4:18 | foo.bar |
|
||||
| foo.bar.baz | tst.js:4:12:4:22 | foo.bar.baz |
|
||||
| number | tst.js:3:12:3:17 | number |
|
||||
| string | tst.js:2:12:2:17 | string |
|
||||
|
||||
Reference in New Issue
Block a user