Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/GherkinCompatibilityMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public function shouldSupportNewlineEscapeSequenceInTableCell(): bool
};
}

/**
* @internal
*/
public function shouldUnespaceDocStringDelimiters(): bool
{
return match ($this) {
self::LEGACY => false,
default => true,
};
}

/**
* @internal
*/
Expand Down
17 changes: 16 additions & 1 deletion src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @phpstan-type TStepToken array{type: 'Step', value: string, line: int, deferred: bool, keyword_type: string, text: string}
* @phpstan-type TTagToken array{type: 'Tag', value: null, line: int, deferred: bool, tags: list<string>}
* @phpstan-type TTableRowToken array{type: 'TableRow', value: null, line: int, deferred: bool, columns: list<string>}
* @phpstan-type TDocStringSeparator '"""'|'```'
*/
class Lexer
{
Expand Down Expand Up @@ -80,6 +81,9 @@ class Lexer
private bool $allowFeature = true;
private bool $allowMultilineArguments = false;
private bool $allowSteps = false;
/**
* @phpstan-var TDocStringSeparator|null
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan is smart enough to analyse the regex used to match the delimiter and figure out that the delimiter capturing group will only ever match one of the 2 supported delimiter strings.
Using a more precise type for the property in which we assign that value allows us to avoid a dead default branch in the match statement finding the corresponding escaped delimiter.

*/
private ?string $pyStringDelimiter = null;

public function __construct(
Expand Down Expand Up @@ -719,7 +723,18 @@ protected function scanPyStringContent()

$token = $this->scanText();
// swallow trailing spaces
$token['value'] = (string) preg_replace('/^\s{0,' . $this->pyStringSwallow . '}/u', '', $token['value'] ?? '');
$value = (string) preg_replace('/^\s{0,' . $this->pyStringSwallow . '}/u', '', $token['value'] ?? '');

if ($this->compatibilityMode->shouldUnespaceDocStringDelimiters()) {
\assert($this->pyStringDelimiter !== null);
$escapedDelimiter = match ($this->pyStringDelimiter) {
'"""' => '\\"\\"\\"',
'```' => '\\`\\`\\`',
};
$value = str_replace($escapedDelimiter, $this->pyStringDelimiter, $value);
}

$token['value'] = $value;

return $token;
}
Expand Down
1 change: 0 additions & 1 deletion tests/Cucumber/CompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class CompatibilityTest extends TestCase
],
'gherkin-32' => [
'complex_background.feature' => 'Rule keyword not supported',
'docstrings.feature' => 'Escaped delimiters in docstrings are not unescaped',
'rule.feature' => 'Rule keyword not supported',
'rule_with_tag.feature' => 'Rule keyword not supported',
'tags.feature' => 'Rule keyword not supported',
Expand Down

This file was deleted.