vorpaljs/bash-parser

ParameterExpansion does not allow empty substitution

Opened this issue · 1 comments

I seem to have found that the ParameterExpansion does not allow an empty substitution.

${parameter:-word}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

Example:

# test if var is was set before or not
if [ -z "${var:-}" ]; then
    var="var was not set before"
fi

Could you have look or tell me where i can change the grammar to allow an empty word, so i can make a pull request.

It seems we are not handling empty string in parameter-expansion.js:

if (ret.expand) {
const bashParser = require('../../../index');
for (const prop of ret.expand) {
const ast = bashParser(ret[prop], {mode: 'word-expansion'});
ret[prop] = ast.commands[0].name;
}
delete ret.expand;
}

Since the value to substitute has to be further expanded, we recursively pass it to bash-parser (at the time I can't think of a better way).

When substituting with an empty string, we try to parse an empty string, and that fail. You can try
to change the code there and skip the parsing in this particular case.