Skip to content

Commit 9f96a33

Browse files
authored
fix(core): stop custom commands from overwriting $0 and re-filling $ARGUMENTS you typed (#59198)
1 parent 0ca7885 commit 9f96a33

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎packages/core/src/config/plugin/command.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ function evaluateTemplate(
193193
const args = parseArguments(input)
194194
const placeholders = template.match(placeholderRegex) ?? []
195195
const last = Math.max(0, ...placeholders.map((item) => Number(item.slice(1))))
196-
const expanded = template.replaceAll(placeholderRegex, (_, index) => {
196+
const withArguments = template.replaceAll(argumentRegex, (match, index) => {
197+
if (match === "$ARGUMENTS") return input
197198
const position = Number(index)
198199
const argIndex = position - 1
199200
if (argIndex >= args.length) return ""
200201
if (position === last) return args.slice(argIndex).join(" ")
201202
return args[argIndex]
202203
})
203-
const withArguments = expanded.replaceAll("$ARGUMENTS", () => input)
204204
const text =
205205
placeholders.length === 0 && !template.includes("$ARGUMENTS") && input.trim()
206206
? `${withArguments}\n\n${input}`.trim()
@@ -239,6 +239,7 @@ function parseArguments(input: string) {
239239
}
240240

241241
const argsRegex = /(?:\[Image\s+\d+\]|"[^"]*"|'[^']*'|[^\s"']+)/gi
242-
const placeholderRegex = /\$(\d+)/g
242+
const placeholderRegex = /\$([1-9]\d*)/g
243+
const argumentRegex = /\$(?:ARGUMENTS|([1-9]\d*))/g
243244
const quoteTrimRegex = /^["']|["']$/g
244245
const shellRegex = /!`([^`]+)`/g

‎packages/core/test/config/command.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ describe("ConfigCommandPlugin.Plugin", () => {
8989
input: '"alpha beta" gamma delta',
9090
expected: "First alpha beta. Rest gamma delta.",
9191
},
92+
{
93+
template: "Run awk '{print $0}' on $1.",
94+
input: "app.log",
95+
expected: "Run awk '{print $0}' on app.log.",
96+
},
97+
{
98+
template: "Run awk '{print $0}'.",
99+
input: "app.log",
100+
expected: "Run awk '{print $0}'.\n\napp.log",
101+
},
102+
{
103+
template: "First $1. Rest $2.",
104+
input: '"$ARGUMENTS" tail',
105+
expected: "First $ARGUMENTS. Rest tail.",
106+
},
92107
{ template: "$ARGUMENTS / $ARGUMENTS", input: "$& $$", expected: "$& $$ / $& $$" },
93108
]) {
94109
it.live(`interpolates ${JSON.stringify(item.template)} with literal input ${JSON.stringify(item.input)}`, () =>

0 commit comments

Comments
 (0)