@@ -1053,6 +1053,56 @@ describe('code placeholder compiler', () => {
10531053 'total=$(( $(( 1 + 1 )) + {{KEY}} ))' ,
10541054 'cat <<PAYLOAD\n$(( {{KEY}} * 2 ))\nPAYLOAD' ,
10551055 'cat <<PAYLOAD\n$[ {{KEY}} * 2 ]\nPAYLOAD' ,
1056+ 'echo $(( $(cat <<EOF\n{{KEY}}\nEOF\n) + 1 ))' ,
1057+ "echo $(( $(cat <<'EOF'\n{{KEY}}\nEOF\n) + 1 ))" ,
1058+ "echo $(( $(cat <<-'EOF'\n\t{{KEY}}\n\tEOF\n) + 1 ))" ,
1059+ '[[ {{KEY}} -eq 0 ]] && echo zero' ,
1060+ '[[ "{{KEY}}" -eq 0 ]] && echo zero' ,
1061+ 'if [[ 0 -lt {{KEY}} ]]; then echo positive; fi' ,
1062+ '[[ -n x && ( "{{KEY}}" -ge 1 ) ]]' ,
1063+ '[[ $(printf "%s" "{{KEY}}") -ne 0 ]]' ,
1064+ '[[ $(printf "%s" "{{KEY}}"; echo 1) -le 0 ]]' ,
1065+ '[[ $(cat <<EOF\n{{KEY}}\nEOF\n) -gt 0 ]]' ,
1066+ 'let "x={{KEY}}"' ,
1067+ 'let x={{KEY}}+1' ,
1068+ 'declare -i x="{{KEY}}"' ,
1069+ 'typeset -i x={{KEY}}' ,
1070+ 'f() { local -i x="{{KEY}}"; }' ,
1071+ 'declare -ai values=("{{KEY}}")' ,
1072+ 'declare -x -i x="{{KEY}}"' ,
1073+ 'let x="$(printf "%s" "{{KEY}}")"' ,
1074+ 'declare -i x="$(printf "%s" "{{KEY}}")"' ,
1075+ '>/dev/null let x="{{KEY}}"' ,
1076+ '2>/dev/null let x="{{KEY}}"' ,
1077+ 'builtin let x="{{KEY}}"' ,
1078+ 'command let x="{{KEY}}"' ,
1079+ 'declare "-i" n="{{KEY}}"' ,
1080+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1081+ '[[ ${missing:-{{KEY}}} -eq 0 ]]' ,
1082+ 'a[{{KEY}}]=1' ,
1083+ 'a[{{KEY}}]+=1' ,
1084+ 'declare -i n; n="{{KEY}}"' ,
1085+ 'declare -i n\nn="{{KEY}}"' ,
1086+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1087+ 'a=(1 2); echo "${a[{{KEY}}]}"' ,
1088+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1089+ 'a=(1 2); echo "${a[0]:{{KEY}}}"' ,
1090+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1091+ 'a=(1 2); echo "${a[0]:0:{{KEY}}}"' ,
1092+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1093+ 's=abc; echo "${s:{{KEY}}}"' ,
1094+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1095+ 's=abc; echo "${s:0:{{KEY}}}"' ,
1096+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1097+ 's=abc; echo "${s: -1:{{KEY}}}"' ,
1098+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1099+ 'set -- a b; echo "${@:{{KEY}}}"' ,
1100+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1101+ 's=abc; printf "%s\\n" "${missing:-"${s:{{KEY}}}"}"' ,
1102+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1103+ 'cat <<PAYLOAD\n${a[{{KEY}}]}\nPAYLOAD' ,
1104+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1105+ 'cat <<PAYLOAD\n${s:{{KEY}}}\nPAYLOAD' ,
10561106 ] ) ( 'rejects shell placeholders whose values enter arithmetic: %s' , async ( code ) => {
10571107 await expect (
10581108 compileCodePlaceholders ( {
@@ -1087,6 +1137,78 @@ describe('code placeholder compiler', () => {
10871137 )
10881138 } )
10891139
1140+ it . each ( [
1141+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1142+ 'printf "%s\\n" "${missing:-https://{{KEY}}}"' ,
1143+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1144+ 'printf "%s\\n" "${missing:-items[{{KEY}}]}"' ,
1145+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1146+ 's=abc; printf "%s\\n" "${s#[{{KEY}}]}"' ,
1147+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1148+ 's=abc; printf "%s\\n" "${s/[{{KEY}}]/x}"' ,
1149+ 'f() { local -i n; }; n="{{KEY}}"' ,
1150+ 'PREFIX="$(printf "%s" "{{KEY}}")" let total=2' ,
1151+ 'printf "%s" a[{{KEY}}]=1' ,
1152+ 'declare -i n; printf "%s" n={{KEY}}' ,
1153+ 'declare -i n; declare +i n; n="{{KEY}}"' ,
1154+ ] ) ( 'compiles parameter and prefix text that is not an arithmetic operand: %s' , async ( code ) => {
1155+ // These forms use the value as pattern, default, or a shielded prefix — never arithmetic — so a
1156+ // conservative scanner must not reject them. Each would fail compilation if wrongly rejected.
1157+ await expect (
1158+ compileCodePlaceholders ( {
1159+ code,
1160+ language : CodeLanguage . Shell ,
1161+ environmentVariables : { KEY : 'values[$(printf injected >&2)]' } ,
1162+ } )
1163+ ) . resolves . toBeDefined ( )
1164+ } )
1165+
1166+ it ( 'keeps literal single quotes inside a double-quoted default expansion' , async ( ) => {
1167+ const compiled = await compileCodePlaceholders ( {
1168+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1169+ code : 'printf "%s\\n" "${missing:-\'{{KEY}}\'}"' ,
1170+ language : CodeLanguage . Shell ,
1171+ environmentVariables : { KEY : 'hello world' } ,
1172+ } )
1173+ expect ( executeShell ( compiled . code , compiled . bindings ) ) . toBe ( "'hello world'\n" )
1174+ } )
1175+
1176+ it ( 'compiles shell placeholders beside arithmetic that never evaluates them' , async ( ) => {
1177+ const value = 'values[$(printf injected >&2)]'
1178+ const compiled = await compileCodePlaceholders ( {
1179+ code : [
1180+ '[ "{{KEY}}" -eq 0 ] 2>/dev/null || printf "%s\\n" not-zero' ,
1181+ '[[ "{{KEY}}" == values* && 1 -eq 1 ]] && printf "%s\\n" matched' ,
1182+ 'let total=1+1; printf "%s\\n" "{{KEY}}"' ,
1183+ 'f() { local copy="{{KEY}}"; printf "%s\\n" "$copy"; }; f' ,
1184+ 'declare copy="{{KEY}}"; printf "%s\\n" "$copy"' ,
1185+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1186+ 'printf "%s\\n" "${missing:-{{KEY}}}"' ,
1187+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1188+ 'a=(x y); printf "%s\\n" "${a[1]}{{KEY}}"' ,
1189+ 'printf "%s\\n" "let {{KEY}}"' ,
1190+ 'printf "%s\\n" "items[{{KEY}}]"' ,
1191+ 'PREFIX="{{KEY}}" let total=2' ,
1192+ '# declare -i n' ,
1193+ 'n="{{KEY}}"; printf "%s\\n" "$n"' ,
1194+ 'let total=1 <<EOF' ,
1195+ '{{KEY}}' ,
1196+ 'EOF' ,
1197+ 'printf "%s\\n" "$total"' ,
1198+ 'cat <<PAYLOAD' ,
1199+ '{{KEY}} $(( 1 + 1 ))' ,
1200+ 'PAYLOAD' ,
1201+ ] . join ( '\n' ) ,
1202+ language : CodeLanguage . Shell ,
1203+ environmentVariables : { KEY : value } ,
1204+ } )
1205+
1206+ expect ( executeShell ( `{\n${ compiled . code } \n} 2>&1` , compiled . bindings ) ) . toBe (
1207+ `not-zero\nmatched\n${ value } \n${ value } \n${ value } \n${ value } \ny${ value } \n` +
1208+ `let ${ value } \nitems[${ value } ]\n${ value } \n1\n${ value } 2\n`
1209+ )
1210+ } )
1211+
10901212 it ( 'discovers shell arithmetic placeholders without compiling missing values' , async ( ) => {
10911213 const code = 'total=$(( {{MISSING}} + {{KEY}} ))'
10921214 await expect ( analyzeCodePlaceholders ( code , CodeLanguage . Shell ) ) . resolves . toEqual ( [
0 commit comments