Skip to content

Commit 4ff1533

Browse files
committed
Use multi-line modifier when matching based on start of line
1 parent 18cdf88 commit 4ff1533

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

src/Languages/Dockerfile/Patterns/ImageAliasKeywordPattern.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
#[PatternTest(input: 'FROM php', output: null)]
1313
#[PatternTest(input: 'FROM php:8.1', output: null)]
1414
#[PatternTest(input: 'FROM php:8.1 AS stage-one', output: 'AS')]
15+
#[PatternTest(input: ' FROM php:8.1 AS stage-one ', output: 'AS')]
1516
final readonly class ImageAliasKeywordPattern implements Pattern
1617
{
1718
use IsPattern;
1819

1920
public function getPattern(): string
2021
{
21-
return "^FROM[\s][\S]+[\s](?<match>AS)[\s][\S]+";
22+
return "/^[\s]*FROM[\s][\S]+[\s](?<match>AS)[\s][\S]+/m";
2223
}
2324

2425
public function getTokenType(): TokenTypeEnum

src/Languages/Dockerfile/Patterns/ImageAliasNamePattern.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
#[PatternTest(input: 'FROM php', output: null)]
1313
#[PatternTest(input: 'FROM php:8.1', output: null)]
1414
#[PatternTest(input: 'FROM php:8.1 AS stage-one', output: 'stage-one')]
15+
#[PatternTest(input: ' FROM php:8.1 AS stage-one ', output: 'stage-one')]
1516
final readonly class ImageAliasNamePattern implements Pattern
1617
{
1718
use IsPattern;
1819

1920
public function getPattern(): string
2021
{
21-
return "^FROM[\s][\S]+[\s]AS[\s](?<match>[\S]+)";
22+
return "/^[\s]*FROM[\s][\S]+[\s]AS[\s](?<match>[\S]+)/m";
2223
}
2324

2425
public function getTokenType(): TokenTypeEnum

src/Languages/Dockerfile/Patterns/ImageNamePattern.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Tempest\Highlight\Tokens\TokenTypeEnum;
1111

1212
#[PatternTest(input: 'FROM php', output: 'php')]
13+
#[PatternTest(input: ' FROM php', output: 'php')]
1314
#[PatternTest(input: 'FROM php:8.1', output: 'php')]
1415
#[PatternTest(input: 'FROM php:8.1 AS stage-one', output: 'php')]
1516
final readonly class ImageNamePattern implements Pattern
@@ -18,7 +19,7 @@
1819

1920
public function getPattern(): string
2021
{
21-
return "^FROM (?<match>[\w]+)";
22+
return "/^[\s]*FROM[\s](?<match>[\w]+)/m";
2223
}
2324

2425
public function getTokenType(): TokenTypeEnum

src/Languages/Dockerfile/Patterns/KeywordPattern.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(private string $keyword)
1818

1919
public function getPattern(): string
2020
{
21-
return "^(?<match>{$this->keyword})[\s].*";
21+
return "/^[\s]*(?<match>{$this->keyword})[\s].*/m";
2222
}
2323

2424
public function getTokenType(): TokenTypeEnum

tests/Languages/Dockerfile/Patterns/KeywordPatternTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,17 @@ public function test_pattern()
2828

2929
$this->assertMatches(
3030
pattern: new KeywordPattern('FROM'),
31-
content: 'RUN some command with FROM in it',
32-
expected: null,
31+
content: ' FROM image:tag AS alias',
32+
expected: 'FROM',
33+
);
34+
35+
$this->assertMatches(
36+
pattern: new KeywordPattern('COPY'),
37+
content: <<<'DOCKERFILE'
38+
FROM image:tag
39+
COPY . /usr/share/nginx/html
40+
DOCKERFILE,
41+
expected: 'COPY',
3342
);
3443
}
3544
}

0 commit comments

Comments
 (0)