Skip to content

Commit b3647e8

Browse files
committed
fix(placeholders): add fast-path guards and complete test/doc coverage for apm-input-token
- Add strings.Contains("${input:") guard before regexp in ContainsBodyToken, MaskBodyTokens, and stripBodyTokens to avoid regex cost on non-matching text - Add APMInputToken to TestIsKnown and TestValidate all-valid lists - Add apm-input-token to MDS004 row in placeholder-grammar.md opt-in table Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Cua8NLiPy6fhj58LvUWf1
1 parent 12d6f8d commit b3647e8

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

docs/background/concepts/placeholder-grammar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ from `internal/placeholders`:
7575
| Rule ID | Rule name | Useful tokens |
7676
| ------- | -------------------------------- | ------------------------------------------------------------------------- |
7777
| MDS003 | `heading-increment` | `heading-question`, `placeholder-section`, `var-token` |
78-
| MDS004 | `first-line-heading` | `heading-question`, `var-token`, `placeholder-section` |
78+
| MDS004 | `first-line-heading` | `heading-question`, `var-token`, `placeholder-section`, `apm-input-token` |
7979
| MDS018 | `no-emphasis-as-heading` | `var-token`, `heading-question`, `placeholder-section`, `apm-input-token` |
8080
| MDS020 | `required-structure` | `cue-frontmatter` |
8181
| MDS023 | `paragraph-readability` | `var-token`, `heading-question`, `placeholder-section`, `apm-input-token` |

internal/placeholders/placeholders.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func ContainsBodyToken(text string, tokens []string) bool {
8181
return true
8282
}
8383
case APMInputToken:
84-
if apmInputRe.MatchString(text) {
84+
if strings.Contains(text, "${input:") && apmInputRe.MatchString(text) {
8585
return true
8686
}
8787
}
@@ -109,7 +109,9 @@ func MaskBodyTokens(text string, tokens []string) string {
109109
return neutralText[PlaceholderSection]
110110
}
111111
case APMInputToken:
112-
text = apmInputRe.ReplaceAllLiteralString(text, neutralText[APMInputToken])
112+
if strings.Contains(text, "${input:") {
113+
text = apmInputRe.ReplaceAllLiteralString(text, neutralText[APMInputToken])
114+
}
113115
}
114116
}
115117
return text
@@ -144,7 +146,9 @@ func stripBodyTokens(text string, tokens []string) string {
144146
return ""
145147
}
146148
case APMInputToken:
147-
text = apmInputRe.ReplaceAllLiteralString(text, "")
149+
if strings.Contains(text, "${input:") {
150+
text = apmInputRe.ReplaceAllLiteralString(text, "")
151+
}
148152
}
149153
}
150154
return text

internal/placeholders/placeholders_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func TestIsKnown(t *testing.T) {
1212
assert.True(t, placeholders.IsKnown(placeholders.HeadingQuestion))
1313
assert.True(t, placeholders.IsKnown(placeholders.PlaceholderSection))
1414
assert.True(t, placeholders.IsKnown(placeholders.CUEFrontmatter))
15+
assert.True(t, placeholders.IsKnown(placeholders.APMInputToken))
1516
assert.False(t, placeholders.IsKnown("unknown-token"))
1617
assert.False(t, placeholders.IsKnown(""))
1718
}
@@ -25,6 +26,7 @@ func TestValidate(t *testing.T) {
2526
placeholders.HeadingQuestion,
2627
placeholders.PlaceholderSection,
2728
placeholders.CUEFrontmatter,
29+
placeholders.APMInputToken,
2830
}))
2931
err := placeholders.Validate([]string{"bad-token"})
3032
assert.ErrorContains(t, err, `unknown placeholder token "bad-token"`)

0 commit comments

Comments
 (0)