Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support array shapes (close #27) #39

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
52 changes: 45 additions & 7 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,19 @@ module.exports = grammar({
_var_tag: ($) =>
seq(
alias('@var', $.tag_name),
choice(
// @var int|string[]|array<string, int> description
seq($._type, $._description_after_type),
// @var int|string[]|array<string, int>
seq($._type, $.variable_name),
// @var int|string[]|array<string, int> $foo description
seq($._type, $.variable_name, $.description),
$._type,
// for psalm and phpstan, the var name is optional
optional(
choice(
// @var int description
$._description_after_type,

// @var int $foo
$.variable_name,

// @var int $foo description
seq($.variable_name, $.description),
),
),
),

Expand Down Expand Up @@ -540,6 +546,7 @@ module.exports = grammar({
alias($._phpdoc_array_types, $.array_type),
alias($._psalm_generic_array_types, $.array_type),
alias($._psalm_list_array_types, $.array_type),
alias($._psalm_shaped_array_types, $.array_type),
),
_regular_types: ($) => PHP.rules._types,
_phpdoc_array_types: ($) => seq($._regular_types, repeat1('[]')),
Expand All @@ -563,6 +570,37 @@ module.exports = grammar({
'>',
),

_psalm_shaped_array_types: ($) =>
prec(
2,
seq(
'array',
'{',
sep1(alias($._shaped_array_element, $.array_element), ','),
'}',
),
),

_shaped_array_element: ($) =>
seq(
field(
'key',
optional(
seq(
choice(
$.name,
seq("'", $.name, "'"),
seq('"', $.name, '"'),
alias(/\d+/, $.name),
),
optional('?'),
':',
),
),
),
field('value', $._regular_types),
),

_psalm_scalar_type: ($) =>
choice(
seq('class-string', optional($._type_argument_named_type)),
Expand Down
211 changes: 182 additions & 29 deletions src/grammar.json

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

Loading
Loading