Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 19, 2025

Refactoring rules need to know the expansion context ('module, 'expression, 'internal-definition) of forms to determine valid transformations. This adds an expansion analyzer that labels each form with its context based on its position in the expanded syntax tree.

Implementation

  • New analyzer: expansion-context-analyzer.rkt traverses expanded syntax using syntax-search to annotate forms based on structural position

    • Forms directly under #%module-begin'module context
    • Subforms of #%plain-app'expression context
    • Lambda/let body forms → 'internal-definition context
  • Test suite: expansion-context-analyzer-test.rkt validates context labeling per the issue spec

Status

Core analyzer structure is in place but patterns aren't matching as expected. The #%module-begin pattern should annotate forms at paths like /3/1, but currently only wrapper-level paths (/1, /2, /3) are being annotated. Likely cause: syntax-search recursion semantics or phase handling in pattern matching.

Test output shows properties are being created but filtered out due to "multiple expanded forms claim to originate from that path" - suggesting the analyzer is annotating expanded paths that map ambiguously to original code positions.

Needs further debugging of pattern matching behavior and the relationship between expanded syntax paths and original syntax paths in the analysis infrastructure.

Original prompt

This section details on the original issue you should resolve

<issue_title>Expansion context analyzer</issue_title>
<issue_description>Whenever a form is being expanded, the result of syntax-local-context has one of five values: 'expression, 'top-level, 'module, 'module-begin, or a value representing a specific definition context. This is useful information to know when inside refactoring rules, as it can affect what sort of transformations might be possible or reasonable. There should be a custom expansion analyzer that labels the expansion context of all forms in the fully expanded syntax. Here's some test cases:

#lang resyntax/test
header: - #lang racket/base

analysis-test: "code in a module is in a module context"
- (+ 1 2 3)
@inspect - (+ 1 2 3)
@property expansion-context
@assert module

analysis-test "function arguments are in an expression context"
- (+ 1 2 3)
@inspect - 2
@property expansion-context
@assert expression

analysis-test "code in a function body is in an internal definition context"
--------------------
(define (f)
  (+ 1 2 3))
--------------------
@inspect - (+ 1 2 3)
@property expansion-context
@assert internal-definition
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add custom expansion context analyzer for forms Add expansion context analyzer for refactoring rules Dec 19, 2025
Copilot AI requested a review from jackfirth December 19, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expansion context analyzer

2 participants