@@ -1053,6 +1053,64 @@ 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+ 'declare -i n; echo "$(declare +i n)"; n="{{KEY}}"' ,
1083+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1084+ 'printf "%s" "${missing:-{text}"; let x="{{KEY}}"' ,
1085+ 'declare -i n; n="$(printf "%s" "{{KEY}}")"' ,
1086+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1087+ 'declare -i n; n="${missing:-{{KEY}}}"' ,
1088+ 'declare -i n; declare n="{{KEY}}"' ,
1089+ '> /dev/null let x="{{KEY}}"' ,
1090+ 'a[{{KEY}}]=1' ,
1091+ 'a[{{KEY}}]+=1' ,
1092+ 'declare -i n; n="{{KEY}}"' ,
1093+ 'declare -i n\nn="{{KEY}}"' ,
1094+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1095+ 'a=(1 2); echo "${a[{{KEY}}]}"' ,
1096+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1097+ 'a=(1 2); echo "${a[0]:{{KEY}}}"' ,
1098+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1099+ 'a=(1 2); echo "${a[0]:0:{{KEY}}}"' ,
1100+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1101+ 's=abc; echo "${s:{{KEY}}}"' ,
1102+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1103+ 's=abc; echo "${s:0:{{KEY}}}"' ,
1104+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1105+ 's=abc; echo "${s: -1:{{KEY}}}"' ,
1106+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1107+ 'set -- a b; echo "${@:{{KEY}}}"' ,
1108+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1109+ 's=abc; printf "%s\\n" "${missing:-"${s:{{KEY}}}"}"' ,
1110+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1111+ 'cat <<PAYLOAD\n${a[{{KEY}}]}\nPAYLOAD' ,
1112+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1113+ 'cat <<PAYLOAD\n${s:{{KEY}}}\nPAYLOAD' ,
10561114 ] ) ( 'rejects shell placeholders whose values enter arithmetic: %s' , async ( code ) => {
10571115 await expect (
10581116 compileCodePlaceholders ( {
@@ -1087,6 +1145,83 @@ describe('code placeholder compiler', () => {
10871145 )
10881146 } )
10891147
1148+ it . each ( [
1149+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1150+ 'printf "%s\\n" "${missing:-https://{{KEY}}}"' ,
1151+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1152+ 'printf "%s\\n" "${missing:-items[{{KEY}}]}"' ,
1153+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1154+ 's=abc; printf "%s\\n" "${s#[{{KEY}}]}"' ,
1155+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1156+ 's=abc; printf "%s\\n" "${s/[{{KEY}}]/x}"' ,
1157+ 'f() { local -i n; }; n="{{KEY}}"' ,
1158+ 'PREFIX="$(printf "%s" "{{KEY}}")" let total=2' ,
1159+ 'printf "%s" a[{{KEY}}]=1' ,
1160+ 'declare -i n; printf "%s" n={{KEY}}' ,
1161+ 'declare -i n; declare +i n; n="{{KEY}}"' ,
1162+ 'declare -i n; n=1 printf "%s" "{{KEY}}"' ,
1163+ 'declare -i n; n=5 text="{{KEY}}"' ,
1164+ 'declare -i +i n="{{KEY}}"' ,
1165+ 'declare -i n; echo "$(declare +i n; n=\'{{KEY}}\'; printf "%s" "$n")"' ,
1166+ '$(let x=1 <<EOF\n{{KEY}}\nEOF\n)' ,
1167+ ] ) ( 'compiles parameter and prefix text that is not an arithmetic operand: %s' , async ( code ) => {
1168+ // These forms use the value as pattern, default, or a shielded prefix — never arithmetic — so a
1169+ // conservative scanner must not reject them. Each would fail compilation if wrongly rejected.
1170+ await expect (
1171+ compileCodePlaceholders ( {
1172+ code,
1173+ language : CodeLanguage . Shell ,
1174+ environmentVariables : { KEY : 'values[$(printf injected >&2)]' } ,
1175+ } )
1176+ ) . resolves . toBeDefined ( )
1177+ } )
1178+
1179+ it ( 'keeps literal single quotes inside a double-quoted default expansion' , async ( ) => {
1180+ const compiled = await compileCodePlaceholders ( {
1181+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1182+ code : 'printf "%s\\n" "${missing:-\'{{KEY}}\'}"' ,
1183+ language : CodeLanguage . Shell ,
1184+ environmentVariables : { KEY : 'hello world' } ,
1185+ } )
1186+ expect ( executeShell ( compiled . code , compiled . bindings ) ) . toBe ( "'hello world'\n" )
1187+ } )
1188+
1189+ it ( 'compiles shell placeholders beside arithmetic that never evaluates them' , async ( ) => {
1190+ const value = 'values[$(printf injected >&2)]'
1191+ const compiled = await compileCodePlaceholders ( {
1192+ code : [
1193+ '[ "{{KEY}}" -eq 0 ] 2>/dev/null || printf "%s\\n" not-zero' ,
1194+ '[[ "{{KEY}}" == values* && 1 -eq 1 ]] && printf "%s\\n" matched' ,
1195+ 'let total=1+1; printf "%s\\n" "{{KEY}}"' ,
1196+ 'f() { local copy="{{KEY}}"; printf "%s\\n" "$copy"; }; f' ,
1197+ 'declare copy="{{KEY}}"; printf "%s\\n" "$copy"' ,
1198+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1199+ 'printf "%s\\n" "${missing:-{{KEY}}}"' ,
1200+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1201+ 'a=(x y); printf "%s\\n" "${a[1]}{{KEY}}"' ,
1202+ 'printf "%s\\n" "let {{KEY}}"' ,
1203+ 'printf "%s\\n" "items[{{KEY}}]"' ,
1204+ 'PREFIX="{{KEY}}" let total=2' ,
1205+ '# declare -i n' ,
1206+ 'n="{{KEY}}"; printf "%s\\n" "$n"' ,
1207+ 'let total=1 <<EOF' ,
1208+ '{{KEY}}' ,
1209+ 'EOF' ,
1210+ 'printf "%s\\n" "$total"' ,
1211+ 'cat <<PAYLOAD' ,
1212+ '{{KEY}} $(( 1 + 1 ))' ,
1213+ 'PAYLOAD' ,
1214+ ] . join ( '\n' ) ,
1215+ language : CodeLanguage . Shell ,
1216+ environmentVariables : { KEY : value } ,
1217+ } )
1218+
1219+ expect ( executeShell ( `{\n${ compiled . code } \n} 2>&1` , compiled . bindings ) ) . toBe (
1220+ `not-zero\nmatched\n${ value } \n${ value } \n${ value } \n${ value } \ny${ value } \n` +
1221+ `let ${ value } \nitems[${ value } ]\n${ value } \n1\n${ value } 2\n`
1222+ )
1223+ } )
1224+
10901225 it ( 'discovers shell arithmetic placeholders without compiling missing values' , async ( ) => {
10911226 const code = 'total=$(( {{MISSING}} + {{KEY}} ))'
10921227 await expect ( analyzeCodePlaceholders ( code , CodeLanguage . Shell ) ) . resolves . toEqual ( [
0 commit comments