@@ -1053,6 +1053,51 @@ 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+ 'a[{{KEY}}]=1' ,
1080+ 'a[{{KEY}}]+=1' ,
1081+ 'declare -i n; n="{{KEY}}"' ,
1082+ 'declare -i n\nn="{{KEY}}"' ,
1083+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1084+ 'a=(1 2); echo "${a[{{KEY}}]}"' ,
1085+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1086+ 'a=(1 2); echo "${a[0]:{{KEY}}}"' ,
1087+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1088+ 'a=(1 2); echo "${a[0]:0:{{KEY}}}"' ,
1089+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1090+ 's=abc; echo "${s:{{KEY}}}"' ,
1091+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1092+ 's=abc; echo "${s:0:{{KEY}}}"' ,
1093+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1094+ 's=abc; echo "${s: -1:{{KEY}}}"' ,
1095+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1096+ 'set -- a b; echo "${@:{{KEY}}}"' ,
1097+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1098+ 'cat <<PAYLOAD\n${a[{{KEY}}]}\nPAYLOAD' ,
1099+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1100+ 'cat <<PAYLOAD\n${s:{{KEY}}}\nPAYLOAD' ,
10561101 ] ) ( 'rejects shell placeholders whose values enter arithmetic: %s' , async ( code ) => {
10571102 await expect (
10581103 compileCodePlaceholders ( {
@@ -1087,6 +1132,42 @@ describe('code placeholder compiler', () => {
10871132 )
10881133 } )
10891134
1135+ it ( 'compiles shell placeholders beside arithmetic that never evaluates them' , async ( ) => {
1136+ const value = 'values[$(printf injected >&2)]'
1137+ const compiled = await compileCodePlaceholders ( {
1138+ code : [
1139+ '[ "{{KEY}}" -eq 0 ] 2>/dev/null || printf "%s\\n" not-zero' ,
1140+ '[[ "{{KEY}}" == values* && 1 -eq 1 ]] && printf "%s\\n" matched' ,
1141+ 'let total=1+1; printf "%s\\n" "{{KEY}}"' ,
1142+ 'f() { local copy="{{KEY}}"; printf "%s\\n" "$copy"; }; f' ,
1143+ 'declare copy="{{KEY}}"; printf "%s\\n" "$copy"' ,
1144+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1145+ 'printf "%s\\n" "${missing:-{{KEY}}}"' ,
1146+ // biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1147+ 'a=(x y); printf "%s\\n" "${a[1]}{{KEY}}"' ,
1148+ 'printf "%s\\n" "let {{KEY}}"' ,
1149+ 'printf "%s\\n" "items[{{KEY}}]"' ,
1150+ 'PREFIX="{{KEY}}" let total=2' ,
1151+ '# declare -i n' ,
1152+ 'n="{{KEY}}"; printf "%s\\n" "$n"' ,
1153+ 'let total=1 <<EOF' ,
1154+ '{{KEY}}' ,
1155+ 'EOF' ,
1156+ 'printf "%s\\n" "$total"' ,
1157+ 'cat <<PAYLOAD' ,
1158+ '{{KEY}} $(( 1 + 1 ))' ,
1159+ 'PAYLOAD' ,
1160+ ] . join ( '\n' ) ,
1161+ language : CodeLanguage . Shell ,
1162+ environmentVariables : { KEY : value } ,
1163+ } )
1164+
1165+ expect ( executeShell ( `{\n${ compiled . code } \n} 2>&1` , compiled . bindings ) ) . toBe (
1166+ `not-zero\nmatched\n${ value } \n${ value } \n${ value } \n${ value } \ny${ value } \n` +
1167+ `let ${ value } \nitems[${ value } ]\n${ value } \n1\n${ value } 2\n`
1168+ )
1169+ } )
1170+
10901171 it ( 'discovers shell arithmetic placeholders without compiling missing values' , async ( ) => {
10911172 const code = 'total=$(( {{MISSING}} + {{KEY}} ))'
10921173 await expect ( analyzeCodePlaceholders ( code , CodeLanguage . Shell ) ) . resolves . toEqual ( [
0 commit comments