JS: Update getTypeAnnotation() to return TypeAnnotations

This commit is contained in:
Asger F
2019-04-16 11:03:56 +01:00
parent be5d90d4e7
commit c92a6b72b5
4 changed files with 34 additions and 8 deletions

View File

@@ -551,7 +551,11 @@ class SetterMethodSignature extends SetterMethodDeclaration, AccessorMethodSigna
*/
class FieldDeclaration extends MemberDeclaration, @field {
/** Gets the type annotation of this field, if any, such as `T` in `{ x: T }`. */
TypeExpr getTypeAnnotation() { result = getChildTypeExpr(2) }
TypeAnnotation getTypeAnnotation() {
result = getChildTypeExpr(2)
or
result = getDocumentation().getATagByTitle("type").getType()
}
/** Holds if this is a TypeScript field annotated with the `readonly` keyword. */
predicate isReadonly() { hasReadonlyKeyword(this) }
@@ -591,7 +595,7 @@ class ParameterField extends FieldDeclaration, @parameter_field {
override Expr getNameExpr() { result = getParameter() }
override TypeExpr getTypeAnnotation() { result = getParameter().getTypeAnnotation() }
override TypeAnnotation getTypeAnnotation() { result = getParameter().getTypeAnnotation() }
}
/**

View File

@@ -38,7 +38,11 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
*
* `this` parameter types are specific to TypeScript.
*/
TypeExpr getThisTypeAnnotation() { result = getChildTypeExpr(-4) }
TypeAnnotation getThisTypeAnnotation() {
result = getChildTypeExpr(-4)
or
result = getDocumentation().getATagByTitle("this").getType()
}
/** Gets the identifier specifying the name of this function, if any. */
VarDecl getId() { result = getChildExpr(-1) }
@@ -76,7 +80,13 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
int getNumBodyStmt() { result = count(getABodyStmt()) }
/** Gets the return type annotation on this function, if any. */
TypeExpr getReturnTypeAnnotation() { typeexprs(result, _, this, -3, _) }
TypeAnnotation getReturnTypeAnnotation() {
typeexprs(result, _, this, -3, _)
or
exists(string title | title = "return" or title = "returns" |
result = getDocumentation().getATagByTitle(title).getType()
)
}
/** Holds if this function is a generator function. */
predicate isGenerator() {

View File

@@ -17,6 +17,12 @@ class JSDoc extends @jsdoc, Locatable {
/** Gets a JSDoc tag within this JSDoc comment. */
JSDocTag getATag() { result.getParent() = this }
/** Gets a JSDoc tag within this JSDoc comment with the given title. */
JSDocTag getATagByTitle(string title) {
result = getATag() and
result.getTitle() = title
}
override string toString() { result = getComment().toString() }
}

View File

@@ -326,7 +326,7 @@ class BindingPattern extends @pattern, Expr {
predicate isLValue() { any() }
/**
* Returns the TypeScript type annotation for this variable or pattern, if any.
* Returns the type annotation for this variable or pattern, if any.
*
* Only the outermost part of a binding pattern can have a type annotation.
* For instance, in the declaration,
@@ -336,7 +336,7 @@ class BindingPattern extends @pattern, Expr {
* the variable `x` has no type annotation, whereas the pattern `{x}` has the type
* annotation `Point`.
*/
TypeExpr getTypeAnnotation() {
TypeAnnotation getTypeAnnotation() {
exists(VariableDeclarator decl | decl.getBindingPattern() = this |
result = decl.getTypeAnnotation()
)
@@ -501,7 +501,11 @@ class VariableDeclarator extends Expr, @vardeclarator {
Expr getInit() { result = this.getChildExpr(1) }
/** Gets the TypeScript type annotation for the declared variable or binding pattern. */
TypeExpr getTypeAnnotation() { result = this.getChildTypeExpr(2) }
TypeAnnotation getTypeAnnotation() {
result = this.getChildTypeExpr(2)
or
result = getDeclStmt().getDocumentation().getATagByTitle("type").getType()
}
/** Holds if this is a TypeScript variable marked as definitely assigned with the `!` operator. */
predicate hasDefiniteAssignmentAssertion() { hasDefiniteAssignmentAssertion(this) }
@@ -569,8 +573,10 @@ class Parameter extends BindingPattern {
}
/** Gets the type annotation for this parameter, if any. */
override TypeExpr getTypeAnnotation() {
override TypeAnnotation getTypeAnnotation() {
exists(Function f, int n | this = f.getParameter(n) | result = f.getChildTypeExpr(-(4 * n + 6)))
or
result = getJSDocTag().getType()
}
/** Holds if this parameter is a rest parameter. */