Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@knutwannheden
Copy link
Contributor

This PR adds support for named captures in the JavaScript templating API, allowing for more concise pattern matching and template generation.

Added optional name parameter to the capture() function, enabling inline named captures for cleaner rewrite rules.

Key Changes

Named Captures Support

The capture() function now accepts an optional name parameter:

// Inline named captures (new!)
pattern`${capture('left')} + ${capture('right')}`
template`${capture('right')} + ${capture('left')}`

// Unnamed captures (existing)
const left = capture();
const right = capture();
pattern`${left} + ${right}`
template`${right} + ${left}`

Trade-offs

Slightly more verbose for rewrite rules:

rewrite(() => ({
    before: pattern`${capture('left')} + ${capture('right')}`,
    after: template`${capture('right')} + ${capture('left')}`
}))

But enables direct literal string insertion:

const type = 'Date';
template`${arg} instanceof ${type}`  // Generates: arg instanceof Date

This is valuable for common use cases where you want to insert variable code fragments.

API Behavior

Template parameter types:

  • capture() objects → Late binding via the values map (for pattern matching)
  • Tree nodes → Direct AST substitution
  • Strings/numbers/booleans → Inserted literally as code

The explicit capture() requirement makes the API more extensible for future enhancements while keeping common literal insertion cases simple.

This PR adds support for named captures in the JavaScript templating API, allowing for more concise pattern matching and template generation.

Added optional `name` parameter to the `capture()` function, enabling inline named captures for cleaner rewrite rules.

### Key Changes

#### Named Captures Support

The `capture()` function now accepts an optional name parameter:

```typescript
// Inline named captures (new!)
pattern`${capture('left')} + ${capture('right')}`
template`${capture('right')} + ${capture('left')}`

// Unnamed captures (existing)
const left = capture();
const right = capture();
pattern`${left} + ${right}`
template`${right} + ${left}`
```

### Trade-offs

Slightly more verbose for rewrite rules:

```typescript
rewrite(() => ({
    before: pattern`${capture('left')} + ${capture('right')}`,
    after: template`${capture('right')} + ${capture('left')}`
}))
```

But enables direct literal string insertion:

```typescript
const type = 'Date';
template`${arg} instanceof ${type}`  // Generates: arg instanceof Date
```

This is valuable for common use cases where you want to insert variable code fragments.

### API Behavior

Template parameter types:
- `capture()` objects → Late binding via the values map (for pattern matching)
- Tree nodes → Direct AST substitution
- Strings/numbers/booleans → Inserted literally as code

The explicit `capture()` requirement makes the API more extensible for future enhancements while keeping common literal insertion cases simple.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Oct 14, 2025
@jkschneider jkschneider merged commit ec23d82 into main Oct 14, 2025
2 checks passed
@jkschneider jkschneider deleted the named-captures branch October 14, 2025 21:33
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants