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

Commit 1aa8ddc

Browse files
authored
Merge pull request #445 from BladeMF/phpdoc-nullable-types
Detect nullable types in phpdoc
2 parents bbd03e9 + a056647 commit 1aa8ddc

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

grammars/php.cson

+6-2
Original file line numberDiff line numberDiff line change
@@ -2457,7 +2457,7 @@
24572457
{
24582458
# Tags followed by a type expression
24592459
# - @<tag> type
2460-
'begin': '(@(?:global|param|property(-(read|write))?|return|throws|var))\\s+(?=[A-Za-z_\\x{7f}-\\x{7fffffff}\\\\]|\\()'
2460+
'begin': '(@(?:global|param|property(-(read|write))?|return|throws|var))\\s+(?=[?A-Za-z_\\x{7f}-\\x{7fffffff}\\\\]|\\()'
24612461
'beginCaptures':
24622462
'1':
24632463
'name': 'keyword.other.phpdoc.php'
@@ -2495,10 +2495,14 @@
24952495
}
24962496
]
24972497
'php_doc_types':
2498-
'match': '(?i)[a-z_\\x{7f}-\\x{7fffffff}\\\\][a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]*([|&][a-z_\\x{7f}-\\x{7fffffff}\\\\][a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]*)*'
2498+
'match': '(?i)\\??[a-z_\\x{7f}-\\x{7fffffff}\\\\][a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]*([|&]\\??[a-z_\\x{7f}-\\x{7fffffff}\\\\][a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]*)*'
24992499
'captures':
25002500
'0':
25012501
'patterns': [
2502+
{
2503+
'match': '\\?'
2504+
'name': 'keyword.operator.nullable-type.php'
2505+
}
25022506
{
25032507
'match': '''(?x)\\b
25042508
(string|integer|int|boolean|bool|float|double|object|mixed

spec/php-spec.coffee

+38
Original file line numberDiff line numberDiff line change
@@ -2561,6 +2561,29 @@ describe 'PHP grammar', ->
25612561
expect(lines[1][3]).toEqual value: 'Test', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
25622562
expect(lines[1][4]).toEqual value: ' description', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
25632563

2564+
it 'should tokenize a single nullable type', ->
2565+
lines = grammar.tokenizeLines '''
2566+
/**
2567+
*@param ?int description
2568+
'''
2569+
2570+
expect(lines[1][1]).toEqual value: '@param', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'keyword.other.phpdoc.php']
2571+
expect(lines[1][2]).toEqual value: ' ', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
2572+
expect(lines[1][3]).toEqual value: '?', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'keyword.operator.nullable-type.php']
2573+
expect(lines[1][4]).toEqual value: 'int', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'keyword.other.type.php']
2574+
expect(lines[1][5]).toEqual value: ' description', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
2575+
2576+
lines = grammar.tokenizeLines '''
2577+
/**
2578+
*@param ?Test description
2579+
'''
2580+
2581+
expect(lines[1][1]).toEqual value: '@param', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'keyword.other.phpdoc.php']
2582+
expect(lines[1][2]).toEqual value: ' ', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
2583+
expect(lines[1][3]).toEqual value: '?', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'keyword.operator.nullable-type.php']
2584+
expect(lines[1][4]).toEqual value: 'Test', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
2585+
expect(lines[1][5]).toEqual value: ' description', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
2586+
25642587
it 'should tokenize a single namespaced type', ->
25652588
lines = grammar.tokenizeLines '''
25662589
/**
@@ -2588,6 +2611,21 @@ describe 'PHP grammar', ->
25882611
expect(lines[1][5]).toEqual value: 'Class', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
25892612
expect(lines[1][6]).toEqual value: ' description', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
25902613

2614+
it 'should tokenize multiple nullable types', ->
2615+
lines = grammar.tokenizeLines '''
2616+
/**
2617+
*@param ?int|?Class description
2618+
'''
2619+
2620+
expect(lines[1][1]).toEqual value: '@param', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'keyword.other.phpdoc.php']
2621+
expect(lines[1][2]).toEqual value: ' ', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
2622+
expect(lines[1][3]).toEqual value: '?', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'keyword.operator.nullable-type.php']
2623+
expect(lines[1][4]).toEqual value: 'int', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'keyword.other.type.php']
2624+
expect(lines[1][5]).toEqual value: '|', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'punctuation.separator.delimiter.php']
2625+
expect(lines[1][6]).toEqual value: '?', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'keyword.operator.nullable-type.php']
2626+
expect(lines[1][7]).toEqual value: 'Class', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
2627+
expect(lines[1][8]).toEqual value: ' description', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
2628+
25912629
it 'should tokenize intersection types', ->
25922630
lines = grammar.tokenizeLines '''
25932631
/**

0 commit comments

Comments
 (0)