From 967752c6c1b4c133062fe77bd7e03f1db5f335d0 Mon Sep 17 00:00:00 2001 From: Asger F Date: Fri, 12 Apr 2019 14:44:43 +0100 Subject: [PATCH] JS: Add TypeAnnotations class --- javascript/ql/src/javascript.qll | 1 + .../src/semmle/javascript/TypeAnnotations.qll | 70 +++++++++++++++++++ .../ql/src/semmle/javascript/TypeScript.qll | 59 +--------------- .../ql/src/semmlecode.javascript.dbscheme | 1 + 4 files changed, 73 insertions(+), 58 deletions(-) create mode 100644 javascript/ql/src/semmle/javascript/TypeAnnotations.qll diff --git a/javascript/ql/src/javascript.qll b/javascript/ql/src/javascript.qll index 13b71233fdd..bf4613b8471 100644 --- a/javascript/ql/src/javascript.qll +++ b/javascript/ql/src/javascript.qll @@ -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 diff --git a/javascript/ql/src/semmle/javascript/TypeAnnotations.qll b/javascript/ql/src/semmle/javascript/TypeAnnotations.qll new file mode 100644 index 00000000000..1ad74b76c9a --- /dev/null +++ b/javascript/ql/src/semmle/javascript/TypeAnnotations.qll @@ -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() } +} diff --git a/javascript/ql/src/semmle/javascript/TypeScript.qll b/javascript/ql/src/semmle/javascript/TypeScript.qll index 22dd2e3f249..1ff16a84c8c 100644 --- a/javascript/ql/src/semmle/javascript/TypeScript.qll +++ b/javascript/ql/src/semmle/javascript/TypeScript.qll @@ -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 } diff --git a/javascript/ql/src/semmlecode.javascript.dbscheme b/javascript/ql/src/semmlecode.javascript.dbscheme index eee0408097f..64ea4d71964 100644 --- a/javascript/ql/src/semmlecode.javascript.dbscheme +++ b/javascript/ql/src/semmlecode.javascript.dbscheme @@ -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