Skip to content

Commit

Permalink
PHP: Class names at the start of a string are now highlighted correct…
Browse files Browse the repository at this point in the history
…ly (#2731)

Some class names at the start of the input string were not detected correctly due to the way greedy matching works. This fixes the issue by splitting the `class-name` rule into two rules, one before and one after the `keyword` rule that interfered and caused the issue.
  • Loading branch information
TomPavelec committed Jan 20, 2021
1 parent 6183fd9 commit 04ef309
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
7 changes: 6 additions & 1 deletion components/prism-php.js
Expand Up @@ -33,6 +33,11 @@
'punctuation': /\\/
}
},
'class-name-definition': {
pattern: /(\b(?:class|interface|trait)\s+)\b[a-z_]\w*(?!\\)\b/i,
lookbehind: true,
alias: 'class-name'
},
'keyword': [
{
pattern: /(\(\s*)\b(?:bool|boolean|int|integer|float|string|object|array)\b(?=\s*\))/i,
Expand Down Expand Up @@ -85,7 +90,7 @@
'argument-name': /\b[a-z_]\w*(?=\s*:(?!:))/i,
'class-name': [
{
pattern: /(\b(?:class|interface|extends|implements|trait|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s*\()\b[a-z_]\w*(?!\\)\b/i,
pattern: /(\b(?:extends|implements|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s*\()\b[a-z_]\w*(?!\\)\b/i,
greedy: true,
lookbehind: true
},
Expand Down
2 changes: 1 addition & 1 deletion components/prism-php.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/languages/php/attribute_feature.test
Expand Up @@ -224,7 +224,7 @@ function main() {}
["delimiter", "]"]
]],
["keyword", "class"],
["class-name", "Foo"],
["class-name-definition", "Foo"],
["punctuation", "{"],
["keyword", "public"],
["keyword", "function"],
Expand Down
4 changes: 2 additions & 2 deletions tests/languages/php/class-name_feature.test
Expand Up @@ -170,7 +170,7 @@ class Foo extends \Package\Bar implements App\Baz {}
["punctuation", "}"],

["keyword", "class"],
["class-name", "Foo"],
["class-name-definition", "Foo"],
["keyword", "extends"],
["class-name", "Bar"],
["keyword", "implements"],
Expand All @@ -179,7 +179,7 @@ class Foo extends \Package\Bar implements App\Baz {}
["punctuation", "}"],

["keyword", "class"],
["class-name", "Foo"],
["class-name-definition", "Foo"],
["keyword", "extends"],
["class-name", [
["punctuation", "\\"],
Expand Down
21 changes: 21 additions & 0 deletions tests/languages/php/issue2614.test
@@ -0,0 +1,21 @@
class First {}
class Second {}

----------------------------------------------------

[
["keyword", "class"],
["class-name-definition", "First"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "class"],
["class-name-definition", "Second"],
["punctuation", "{"],
["punctuation", "}"]
]


----------------------------------------------------

Checks for issue #2614.

0 comments on commit 04ef309

Please sign in to comment.