BashSupport-Pro / bashsupport-pro

Public issue tracking for BashSupport Pro. This is a plugin, which provides advanced support for Bash scripts for JetBrains IDEs.
https://www.bashsupport.com
48 stars 2 forks source link

bash support pro does not support passing local variables from a parent function to any child function called within that parent function #183

Closed mylovesaber closed 1 month ago

mylovesaber commented 1 month ago

bash support pro(I call it as BSP)

BSP seems only support this type:

#!/bin/bash
f1() {
    local aa
    aa=2
    f2 "${aa}"
}
f2() {
    local cc
    aa="${1}"
    echo "aa=${aa}"
    cc="${1}"
    echo "cc=${cc}"
}

f1

None of these type are supported: Directly called subfunction

#!/bin/bash
f1() {
    local bb
    bb=3
    f4
}

f4() {
    echo "bb=${bb}"   # bb  not  recognized here. It comes from parent function. Bash support this usage.
}
f1

Indirectly called subfunction

#!/bin/bash
f1() {
    local bb
    bb=3
    f3
}
f3() {
    f4
}
f4() {
    echo "bb=${bb}"
}
f1
jansorg commented 1 month ago

Thanks for the report!

You're right that this is not supported. The reason is that local variables set by the calling function is runtime behavior and can't be tracked reliably in the editor (using static analysis). If f4 needs input from f1, then I suggest to use arguments. Just like in your first example.

For example, if you call f4 directly and not f1, then the variable is undefined. I think it's better to show a warning because local is not meant to be used like a global variable, as fas as I understand Bash.

#!/bin/bash
f1() {
    local bb
    bb=3
    f4
}

f4() {
    echo "bb=${bb}"
}
f4 # <-- this would print "bb=" because bb is undefined at runtime
jansorg commented 1 month ago

I'm closing this now as I don't think that it could be supported reliably with BashSupport Pro, but I'm still open for your feedback.