Skip to content

Commit afa74da

Browse files
Copilotnzakas
andcommitted
fix: skip optional -- after printf -v in format rule
Co-authored-by: nzakas <38546+nzakas@users.noreply.github.com>
1 parent 21cf354 commit afa74da

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎docs/rules/no-variables-in-printf-format.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The first argument to `printf` is a format string: `%` starts a conversion and `
88

99
## Rule Details
1010

11-
This rule warns when the format argument of `printf` contains a parameter expansion or command substitution, whether or not it's quoted. The format is the first argument, or the argument after `-v name`, `-vname`, or `--`.
11+
This rule warns when the format argument of `printf` contains a parameter expansion or command substitution, whether or not it's quoted. The format is the first argument after an optional `-v name` or `-vname` and an optional `--`.
1212

1313
Variables in the arguments after the format are fine. Arithmetic expansions such as `$((...))` in the format are not checked.
1414

‎src/rules/no-variables-in-printf-format.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ ruleTester.run("no-variables-in-printf-format", rule as never, {
2121
'printf "%s\\n" "$var"',
2222
'printf \'%s: %d\\n\' "$name" "$count"',
2323
'printf -v result "%s" "$var"',
24+
'printf -v result -- "%s" "$var"',
25+
'printf -vresult -- "%s" "$var"',
26+
'printf -- "%s" "$var"',
2427
'printf "static text\\n"',
2528
// Not printf
2629
'echo "$var"',
@@ -49,6 +52,18 @@ ruleTester.run("no-variables-in-printf-format", rule as never, {
4952
code: 'printf -v out "$fmt" x',
5053
errors: [{ messageId: "variableInFormat" }],
5154
},
55+
{
56+
code: 'printf -v out -- "$fmt" x',
57+
errors: [{ messageId: "variableInFormat" }],
58+
},
59+
{
60+
code: 'printf -vout -- "$fmt" x',
61+
errors: [{ messageId: "variableInFormat" }],
62+
},
63+
{
64+
code: 'printf -- "$fmt" x',
65+
errors: [{ messageId: "variableInFormat" }],
66+
},
5267
{
5368
code: 'printf "count: $(wc -l < file)\\n"',
5469
errors: [{ messageId: "variableInFormat" }],

‎src/rules/no-variables-in-printf-format.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ const rule: BashRuleDefinition<{ MessageIds: "variableInFormat" }> = {
4040
} else if (first !== null && first.startsWith("-v")) {
4141
// `printf -vvar format ...`
4242
formatIndex = 1;
43-
} else if (first === "--") {
44-
formatIndex = 1;
43+
}
44+
45+
const possibleFormat = node.arguments[formatIndex];
46+
if (possibleFormat && getStaticText(possibleFormat) === "--") {
47+
formatIndex++;
4548
}
4649

4750
const format = node.arguments[formatIndex];

0 commit comments

Comments
 (0)