Add a docs option to the types and block tags - #4870
Conversation
|
I like |
|
I forgot to close the issue, since I closed the initial PR #4770. We went for symfony/ux#3694 in UX, and also in the PhpStorm plugin Haehnchen/idea-php-symfony2-plugin#2826 |
|
See #4871 |
|
📋 PR Summary This PR adds an optional Changes
|
| // "docs" followed by "=" is the docs option; "docs" alone might be an | ||
| // expression used as the shortcut syntax for the block body | ||
| $docs = null; | ||
| if ($stream->test(Token::NAME_TYPE, 'docs') && $stream->look()->test(Token::OPERATOR_TYPE, '=')) { |
There was a problem hiding this comment.
🔵 Info — Duplicated, divergent option parsing across two tags raises future maintenance cost.
The docs option is parsed differently in the two tags: BlockTokenParser uses test()+look() and silently falls back to the shortcut body when docs is not followed by =, while TypesTokenParser uses nextIf()+expect() and raises The "docs" option must be followed by an equal sign (=). The divergent block behavior is required to preserve the {% block title docs %} shortcut, but the two implementations duplicate the option-detection logic with different error semantics, so a future change to the option (e.g. renaming docs to desc) must be made in two places kept manually in sync.
|
Closing in favor of #4871 |
This PR was squashed before being merged into the 3.x branch. Discussion ---------- Attach documentation comments to nodes Alternatives to #4870 To avoid BC breaks, I have another idea, using `##` as a new syntax, a bit like `/** */` in PHP vs `/* */`. "Documentation" is attached as metadata to the next relevant node: ```twig {## The main content displayed on the page #} {% block content %} ... {% endblock %} ``` Documentation comments can also describe variables declared with the `types` tag: ```twig {% types { ## The unique identifier of the article id: 'string', ## Whether the article should be highlighted featured?: 'boolean', } %} ``` Node visitors can access this metadata through `Node::getDocumentation()`, allowing IDEs, static analyzers, and documentation generators to consume it without affecting template rendering. Documentation is preserved when visitors or optimizations replace nodes. Closes #4768 Closes #4870 Commits ------- 6806e30 Attach documentation comments to nodes
Implements the design proposed by @fabpot in #4768:
{% types %}entry accepts an optionaldocs="..."option after the type string; it is stored in theTypesNodemapping (docsisnullwhen not provided){% block %}tag accepts an optionaldocs="..."option after the block name; it is stored as adocsattribute on theBlockNode(new optional constructor argument, BC)The tags themselves do not use this documentation: it is metadata for tools (IDEs, documentation generators, the Symfony UX Toolkit) that analyze the parsed nodes.
Notes:
{% block %},docsis only treated as an option when followed by=, so the shortcut syntax{% block title docs %}(printing a variable nameddocs) keeps working; this is covered by a test.docsas originally proposed; renaming it todescis a one-line change if preferred.Closes #4768