JavaScript: add capture.derived() for transformed capture values in templates#6954
Draft
knutwannheden wants to merge 1 commit intomainfrom
Draft
JavaScript: add capture.derived() for transformed capture values in templates#6954knutwannheden wants to merge 1 commit intomainfrom
capture.derived() for transformed capture values in templates#6954knutwannheden wants to merge 1 commit intomainfrom
Conversation
capture.derived() for transformed capture values in templates
Derived captures compute their template substitution from another capture's matched value, eliminating the need for dynamic `after` functions in many rewrite rules. The transform function receives the matched node and returns either a RawCode (for identifier/property name substitution) or a J node (for direct AST substitution). Derived captures throw if used in patterns (they have no matching behavior) and support optional type attribution for proper parsing.
d8c0f38 to
af15435
Compare
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.
Example
Before — dynamic
afterfunction required to transform a captured value:After — fully declarative with
capture.derived():Summary
capture.derived(source, transform, options?)that computes template substitutions from another capture's matched valueDerivedCaptureresolves at template application time: looks up the source capture in match results, applies the transform function, and uses the result for substitutionRawCode(for identifier/property name positions) or aJnode (for direct AST substitution)beforepattern (derived captures have no matching behavior){ type }for type attribution, same as regular capturesafter: (match) => Templatein many rewrite rules, keeping rules fully declarativeTest plan
raw()output maps a captured literal value to a property namebeforepattern throws a clear errorafter