Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 37d716c

Browse files
Readonly properties (#435)
1 parent 8909c50 commit 37d716c

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

grammars/php.cson

+9-4
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@
590590
{
591591
# Constructor Property Promotion
592592
'begin': '''(?xi)
593-
(public|private|protected) \\s+
593+
((?:(?:public|private|protected|readonly)(?:\\s+|(?=\\?)))++)
594594
(?: (
595595
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
596596
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
@@ -599,7 +599,12 @@
599599
'''
600600
'beginCaptures':
601601
'1':
602-
'name': 'storage.modifier.php'
602+
'patterns': [
603+
{
604+
'match': 'public|private|protected|readonly'
605+
'name': 'storage.modifier.php'
606+
}
607+
]
603608
'2':
604609
'patterns': [
605610
{
@@ -697,7 +702,7 @@
697702
}
698703
{
699704
'match': '''(?xi)
700-
((?:(?:public|private|protected|static)(?:\\s+|(?=\\?)))++) # At least one modifier
705+
((?:(?:public|private|protected|static|readonly)(?:\\s+|(?=\\?)))++) # At least one modifier
701706
(
702707
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
703708
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
@@ -708,7 +713,7 @@
708713
'1':
709714
'patterns': [
710715
{
711-
'match': 'public|private|protected|static'
716+
'match': 'public|private|protected|static|readonly'
712717
'name': 'storage.modifier.php'
713718
}
714719
]

spec/php-spec.coffee

+44
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,30 @@ describe 'PHP grammar', ->
852852
expect(lines[3][13]).toEqual value: '$', scopes: ["source.php", "meta.class.php", "meta.class.body.php", "variable.other.php", "punctuation.definition.variable.php"]
853853
expect(lines[3][14]).toEqual value: 'c2', scopes: ["source.php", "meta.class.php", "meta.class.body.php", "variable.other.php"]
854854

855+
it 'tokenizes readonly properties', ->
856+
lines = grammar.tokenizeLines '''
857+
class Foo {
858+
public readonly mixed $a;
859+
readonly string $b;
860+
readonly public mixed $c;
861+
}
862+
'''
863+
864+
expect(lines[1][1]).toEqual value: 'public', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'storage.modifier.php']
865+
expect(lines[1][3]).toEqual value: 'readonly', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'storage.modifier.php']
866+
expect(lines[1][5]).toEqual value: 'mixed', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'keyword.other.type.php']
867+
expect(lines[1][7]).toEqual value: '$', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php', 'punctuation.definition.variable.php']
868+
expect(lines[1][8]).toEqual value: 'a', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php']
869+
expect(lines[2][1]).toEqual value: 'readonly', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'storage.modifier.php']
870+
expect(lines[2][3]).toEqual value: 'string', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'keyword.other.type.php']
871+
expect(lines[2][5]).toEqual value: '$', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php', 'punctuation.definition.variable.php']
872+
expect(lines[2][6]).toEqual value: 'b', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php']
873+
expect(lines[3][1]).toEqual value: 'readonly', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'storage.modifier.php']
874+
expect(lines[3][3]).toEqual value: 'public', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'storage.modifier.php']
875+
expect(lines[3][5]).toEqual value: 'mixed', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'keyword.other.type.php']
876+
expect(lines[3][7]).toEqual value: '$', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php', 'punctuation.definition.variable.php']
877+
expect(lines[3][8]).toEqual value: 'c', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php']
878+
855879
describe 'methods', ->
856880
it 'tokenizes basic method', ->
857881
lines = grammar.tokenizeLines '''
@@ -943,6 +967,26 @@ describe 'PHP grammar', ->
943967
expect(lines[1][17]).toEqual value: '$', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.typehinted.php', 'variable.other.php', 'punctuation.definition.variable.php']
944968
expect(lines[1][18]).toEqual value: 'b', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.typehinted.php', 'variable.other.php']
945969

970+
it 'tokenizes readonly promoted properties', ->
971+
lines = grammar.tokenizeLines '''
972+
class Test {
973+
public function __construct(public readonly int $a, readonly protected? string $b) {}
974+
}
975+
'''
976+
977+
expect(lines[1][5]).toEqual value: '__construct', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'support.function.constructor.php']
978+
expect(lines[1][7]).toEqual value: 'public', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'storage.modifier.php']
979+
expect(lines[1][9]).toEqual value: 'readonly', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'storage.modifier.php']
980+
expect(lines[1][11]).toEqual value: 'int', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'keyword.other.type.php']
981+
expect(lines[1][13]).toEqual value: '$', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'variable.other.php', 'punctuation.definition.variable.php']
982+
expect(lines[1][14]).toEqual value: 'a', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'variable.other.php']
983+
expect(lines[1][17]).toEqual value: 'readonly', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'storage.modifier.php']
984+
expect(lines[1][19]).toEqual value: 'protected', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'storage.modifier.php']
985+
expect(lines[1][20]).toEqual value: '?', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'keyword.operator.nullable-type.php']
986+
expect(lines[1][22]).toEqual value: 'string', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'keyword.other.type.php']
987+
expect(lines[1][24]).toEqual value: '$', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'variable.other.php', 'punctuation.definition.variable.php']
988+
expect(lines[1][25]).toEqual value: 'b', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.promoted-property.php', 'variable.other.php']
989+
946990
it 'tokenizes constructor with illegal return type declaration', ->
947991
lines = grammar.tokenizeLines '''
948992
class Test {

0 commit comments

Comments
 (0)