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

[JavaScript] Handle type argument in JSDoc documentation #2629

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions JavaScript/JavaScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ variables:

# '@' followed by a pattern like \S but excluding literal '*' and '@'.
jsdoc_block_tag: \@[^\n\t\f\v *@]+
# Tags that are optionally followed by a type expression.
jsdoc_block_tag_with_type: \@(?:arg(?:ument)?|param|typedef|type|returns?|prop(?:erty)?|enum|implements|member|module|namespace|private|protected|template|this|throws|exception|yields?|var)\b

contexts:
main:
Expand Down Expand Up @@ -164,12 +166,58 @@ contexts:
push: jsdoc-block-tag

jsdoc-block-tag:
- match: '{{jsdoc_block_tag_with_type}}'
scope: entity.other.attribute-name.documentation.js
set:
- match: '\s+(?=\{)'
set: jsdoc-type-expression
- match: ''
pop: 1
- match: '{{jsdoc_block_tag}}'
scope: entity.other.attribute-name.documentation.js
pop: 1
- match: (?=\S)|$
pop: 1

jsdoc-type-expression:
- match: '\{'
scope: punctuation.definition.type.begin.js, meta.tag.jsdoc
set:
- match: '\}'
scope: meta.tag.jsdoc punctuation.definition.type.end.js
pop: 1
- include: jsdoc-type-expression-body

jsdoc-type-expression-body:
- match: (?=\*/)
pop: 1
- match: '^\s*(\*)'
captures:
1: punctuation.definition.comment.js
- match: \.
scope: meta.type.js punctuation.accessor.dot.js
- match: '\{'
scope: meta.type.js
rchl marked this conversation as resolved.
Show resolved Hide resolved
push: jsdoc-type-expression-body-nested
- match: |-
(?x:
(?! # break on patterns handled explicitly above
\*\/|
^\s*\*|
\}|
\{|
\.
).
# consume as many consecutive characters as possible to not break the ligatures
)*
scope: meta.type.js

jsdoc-type-expression-body-nested:
- match: '\}'
scope: meta.type.js
pop: 1
- include: jsdoc-type-expression-body

line-comments:
- match: /{4,}
scope: punctuation.definition.comment.js
Expand Down
44 changes: 44 additions & 0 deletions JavaScript/tests/syntax_test_js_jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,47 @@
/*@a */
//^^ - entity.other.attribute-name.documentation
// ^^ punctuation.definition.comment.end

/** @type {string[]} */
// ^^^^^^^^ meta.type
// ^^^^ - meta.type
// ^ punctuation.definition.type.begin
// ^ punctuation.definition.type.end

/** @type {import('fs').FSWatcher} */
// ^^^^^^^^^^^^^^^^^^^^^^ meta.type
// ^^^^ - meta.type
// ^ punctuation.accessor.dot
// ^ punctuation.definition.type.end

/**
* @param {{
// ^ meta.type
// ^ comment.block.documentation punctuation.definition.comment
* foo: A.B
// ^ comment.block.documentation punctuation.definition.comment
//^^ - meta.type
// ^^^^^^^^^^^ meta.type
// ^ punctuation.accessor.dot
* }} foo
// ^^ meta.type
// ^^^^^ - meta.type
*
// ^ comment.block.documentation punctuation.definition.comment
*/

/** @type{string} */
// ^^^^^^^^ - meta.type

/** @type {string */
// ^^^^^^^ meta.type
// ^^ punctuation.definition.comment.end - meta.type

/** @type {{foo: number */
// ^^^^^^^^^^^^^ meta.type
// ^^ punctuation.definition.comment.end - meta.type

// Mostly just for visual inspection to ensure that ligatures are not broken.
/** @type {p: number => string} */
// ^^^^^^^^^^^^^^^^^^^ meta.type
// ^^ punctuation.definition.comment.end - meta.type