JS: Add TypeAnnotations class

This commit is contained in:
Asger F
2019-04-12 14:44:43 +01:00
parent 7d2c17f27c
commit 967752c6c1
4 changed files with 73 additions and 58 deletions

View File

@@ -49,6 +49,7 @@ import semmle.javascript.StringConcatenation
import semmle.javascript.StringOps
import semmle.javascript.Templates
import semmle.javascript.Tokens
import semmle.javascript.TypeAnnotations
import semmle.javascript.TypeScript
import semmle.javascript.Util
import semmle.javascript.Variables

View File

@@ -0,0 +1,70 @@
/**
* Provides classes for reasoning about type annotations independently of dialect.
*/
import javascript
/**
* A type annotation, either in the form of a TypeScript type or a JSDoc comment.
*/
class TypeAnnotation extends @type_annotation {
/** Gets a string representation of this type. */
string toString() { none() }
/** Holds if this is the `any` type. */
predicate isAny() { none() }
/** Holds if this is the `string` type. Does not hold for the (rarely used) `String` type. */
predicate isString() { none() }
/** Holds if this is the `string` or `String` type. */
predicate isStringy() { none() }
/** Holds if this is the `number` type. Does not hold for the (rarely used) `Number` type. */
predicate isNumber() { none() }
/** Holds if this is the `number` or `Number`s type. */
predicate isNumbery() { none() }
/** Holds if this is the `boolean` type. Does not hold for the (rarely used) `Boolean` type. */
predicate isBoolean() { none() }
/** Holds if this is the `boolean` or `Boolean` type. */
predicate isBooleany() { none() }
/** Holds if this is the `undefined` type. */
predicate isUndefined() { none() }
/** Holds if this is the `null` type. */
predicate isNull() { none() }
/** Holds if this is the `void` type. */
predicate isVoid() { none() }
/** Holds if this is the `never` type, or an equivalent type representing the empty set of values. */
predicate isNever() { none() }
/** Holds if this is the `this` type. */
predicate isThis() { none() }
/** Holds if this is the `symbol` type. */
predicate isSymbol() { none() }
/** Holds if this is the `unique symbol` type. */
predicate isUniqueSymbol() { none() }
/** Holds if this is the `Function` type. */
predicate isRawFunction() { none() }
/** Holds if this is the `object` type. */
predicate isObjectKeyword() { none() }
/** Holds if this is the `unknown` type. */
predicate isUnknownKeyword() { none() }
/** Holds if this is the `bigint` type. */
predicate isBigInt() { none() }
/** Holds if this is the `const` keyword, occurring in a type assertion such as `x as const`. */
predicate isConstKeyword() { none() }
}

View File

@@ -528,66 +528,9 @@ class LocalNamespaceName extends @local_namespace_name, LexicalName {
* This class includes only explicit type annotations -
* types inferred by the TypeScript compiler are not type expressions.
*/
class TypeExpr extends ExprOrType, @typeexpr {
class TypeExpr extends ExprOrType, @typeexpr, TypeAnnotation {
override string toString() { typeexprs(this, _, _, _, result) }
/** Holds if this is the `any` type. */
predicate isAny() { none() }
/** Holds if this is the `string` type. Does not hold for the (rarely used) `String` type. */
predicate isString() { none() }
/** Holds if this is the `string` or `String` type. */
predicate isStringy() { none() }
/** Holds if this is the `number` type. Does not hold for the (rarely used) `Number` type. */
predicate isNumber() { none() }
/** Holds if this is the `number` or `Number`s type. */
predicate isNumbery() { none() }
/** Holds if this is the `boolean` type. Does not hold for the (rarely used) `Boolean` type. */
predicate isBoolean() { none() }
/** Holds if this is the `boolean` or `Boolean` type. */
predicate isBooleany() { none() }
/** Holds if this is the `undefined` type. */
predicate isUndefined() { none() }
/** Holds if this is the `null` type. */
predicate isNull() { none() }
/** Holds if this is the `void` type. */
predicate isVoid() { none() }
/** Holds if this is the `never` type. */
predicate isNever() { none() }
/** Holds if this is the `this` type. */
predicate isThis() { none() }
/** Holds if this is the `symbol` type. */
predicate isSymbol() { none() }
/** Holds if this is the `unique symbol` type. */
predicate isUniqueSymbol() { none() }
/** Holds if this is the `Function` type. */
predicate isRawFunction() { none() }
/** Holds if this is the `object` type. */
predicate isObjectKeyword() { none() }
/** Holds if this is the `unknown` type. */
predicate isUnknownKeyword() { none() }
/** Holds if this is the `bigint` type. */
predicate isBigInt() { none() }
/** Holds if this is the `const` keyword, occurring in a type assertion such as `x as const`. */
predicate isConstKeyword() { none() }
/** Gets this type expression, with any surrounding parentheses removed. */
override TypeExpr stripParens() { result = this }

View File

@@ -232,6 +232,7 @@ isDelegating (int yield: @yieldexpr ref);
@exprortype = @expr | @typeexpr;
@exprparent = @exprorstmt | @property | @functiontypeexpr;
@arraylike = @arrayexpr | @arraypattern;
@type_annotation = @typeexpr | @jsdoc_type_expr;
case @expr.kind of
0 = @label