Skip to content

Commit

Permalink
🐛 Fix erroneous type annotations for repeatList
Browse files Browse the repository at this point in the history
There's a potential case that the `append` function that it uses returns
a `Parser<null>` but we know that this usecase won't happen in our
scenario, because we feed it a sequence of copies of the original
parser, which is a `Parser<T>`. Thus we also know the return type of
`append` will always be a `Parser<T>`.

So we had to let Psalm know that we know this, and that the inference
that's being made is too eager.
  • Loading branch information
turanct committed Jun 25, 2021
1 parent 1a16fed commit 9f18018
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/combinators.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,36 @@ function repeat(int $n, Parser $parser): Parser
*
* @template T
*
* $psalm-param positive-int $n
* @psalm-param Parser<T> $parser
*
* @psalm-return Parser<T>
* @psalm-return Parser<list<T>>
* @api
*/
function repeatList(int $n, Parser $parser): Parser
{
$parser = map($parser, /** @psalm-param mixed $output */ fn($output): array => [$output]);
/** @palm-var Parser<list<T>> $parser */
$parser = map(
$parser,
/**
* @psalm-param T $output
* @psalm-return list<T>
*/
fn($output): array => [$output]
);

$parsers = array_fill(0, $n - 1, $parser);

return foldl(
$parsers,
/**
* @psalm-param Parser<list<T>> $l
* @psalm-param Parser<list<T>> $r
* @psalm-return Parser<list<T>>
*
* @psalm-suppress InvalidReturnType
* @psalm-suppress InvalidReturnStatement
*/
fn(Parser $l, Parser $r): Parser => append($l, $r),
$parser
)->label("$n times " . $parser->getLabel());
Expand Down

0 comments on commit 9f18018

Please sign in to comment.