add support for rest types elements in the middle of a tuple

This commit is contained in:
Erik Krogh Kristensen
2021-02-18 11:08:15 +01:00
parent 74630b0fd8
commit f385c55f2c
10 changed files with 2479 additions and 11 deletions

View File

@@ -2149,18 +2149,23 @@ class TupleType extends ArrayType, @tuple_type {
int getMinimumLength() { tuple_type_min_length(this, result) }
/**
* Holds if this tuple type ends with a rest element, such as `[number, ...string[]]`.
* Gets the index of the rest element.
* E.g. for a type `[number, ...string[]]` the result is 1,
* or for a type `[...number[], string]` the result is 0.
*/
predicate hasRestElement() { tuple_type_rest(this) }
int getRestElementIndex() { tuple_type_rest_index(this, result) }
/**
* Holds if this tuple type has a rest element, such as `[number, ...string[]]` or `[...number[], string]`.
*/
predicate hasRestElement() { exists(getRestElementIndex()) }
/**
* Gets the type of the rest element, if there is one.
*
* For example, the rest element of `[number, ...string[]]` is `string`.
*/
Type getRestElementType() {
hasRestElement() and result = getElementType(getNumElementType() - 1)
}
Type getRestElementType() { result = getElementType(getRestElementIndex()) }
}
/**

View File

@@ -799,8 +799,9 @@ tuple_type_min_length(
int minLength: int ref
);
tuple_type_rest(
unique int typ: @type ref
tuple_type_rest_index(
unique int typ: @type ref,
int index: int ref
);
// comments