Skip to content

Add name-to-string, reserved-type, attribute-resolution and doc-comment helpers - #1

Merged
TomasVotruba merged 1 commit into
mainfrom
tv-attr-name-doc-helpers
Sep 3, 2026
Merged

Add name-to-string, reserved-type, attribute-resolution and doc-comment helpers#1
TomasVotruba merged 1 commit into
mainfrom
tv-attr-name-doc-helpers

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Adds four small, self-contained helpers that downstream analyzers (linters, refactoring tools, metrics) currently reimplement or work around. Motivated by porting class-leak onto this parser, where each of these was needed.

1. ast.ToString(node)

Resolves a Name / NameFullyQualified / NameRelative / NamePart / Identifier to its string (parts joined by \). The same logic already existed inside the namespace resolver as the unexported concatNameParts / nameNodeParts; this exposes it so callers stop rewriting it.

2. ast.IsReservedType(name)

Reports whether a name is a PHP reserved type keyword (int, string, void, iterable, self, static, mixed, never, ...), matched case-insensitively. The parser produces a Name node for reserved types just like for class references, so consumers need this to tell them apart.

3. Attribute name resolution in NamespaceResolver

The resolver visited every class-name position except attribute names, so #[Route] / #[AsCommand] usages never landed in ResolvedNames. Adds an Attribute handler that resolves node.Name, so attribute references resolve like any other name.

4. visitor.GetDocComment(node) / GetDocCommentText(node)

Returns the /** */ doc block immediately preceding a declaration - class, interface, trait, enum, function, method, property list, or class-constant list - by reading the free-floating tokens before the node's first significant token. Returns nil / "" when there is none. Needed by any tool that reads PHPDoc or annotations.

Tests

Unit tests for each: ast.ToString / IsReservedType (pkg/ast/name_test.go), attribute resolution over parsed source (pkg/visitor/nsresolver/attribute_test.go), and doc-comment extraction with and without a preceding attribute (pkg/visitor/doccomment_test.go).

Local run is clean for go test ./..., gofmt -s, go fix, gopls modernize, and golangci-lint (0 issues). No new dependencies.

…nt helpers

Fill four gaps that downstream analyzers repeatedly work around:

- ast.ToString resolves a Name/NameFullyQualified/NameRelative/NamePart/
  Identifier node to its string, exposing what was only available through
  the namespace resolver's unexported helpers.
- ast.IsReservedType reports whether a name is a PHP reserved type keyword,
  so consumers can tell scalar types apart from class references (the parser
  produces a Name node for both).
- NamespaceResolver now resolves attribute names, so #[Attr] usages appear in
  ResolvedNames like every other class reference.
- visitor.GetDocComment / GetDocCommentText return the doc block preceding a
  declaration (class, interface, trait, enum, function, method, property, or
  class-constant list).

Claude-Session: https://claude.ai/code/session_015acu9zNHedrFe34MFQTgXE
@TomasVotruba
TomasVotruba merged commit e18e2e3 into main Sep 3, 2026
4 checks passed
@TomasVotruba
TomasVotruba deleted the tv-attr-name-doc-helpers branch September 3, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant