Add name-to-string, reserved-type, attribute-resolution and doc-comment helpers - #1
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds four small, self-contained helpers that downstream analyzers (linters, refactoring tools, metrics) currently reimplement or work around. Motivated by porting
class-leakonto this parser, where each of these was needed.1.
ast.ToString(node)Resolves a
Name/NameFullyQualified/NameRelative/NamePart/Identifierto its string (parts joined by\). The same logic already existed inside the namespace resolver as the unexportedconcatNameParts/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 aNamenode for reserved types just like for class references, so consumers need this to tell them apart.3. Attribute name resolution in
NamespaceResolverThe resolver visited every class-name position except attribute names, so
#[Route]/#[AsCommand]usages never landed inResolvedNames. Adds anAttributehandler that resolvesnode.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.