Skip to content

Commit

Permalink
Null coalescing <uninitialized> fix (#474)
Browse files Browse the repository at this point in the history
* Fix <uninitialized> in null coalescing output

* Fix docs
  • Loading branch information
TwitchBronBron committed Jan 5, 2022
1 parent 3d71e7a commit 08998f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/null-coalescing-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ user = getUser(userId ?? globalSettings.defaultUserId) ?? getDefaultUser()
transpiles to:
```brightscript
user = (function(getDefaultUser, getUser, globalSettings, userId)
__bsConsequent = getUser((function(globalSettings)
__bsConsequent = getUser((function(globalSettings, userId)
__bsConsequent = userId
if __bsConsequent <> invalid then
return __bsConsequent
else
return globalSettings.defaultUserId
end if
end function)(globalSettings))
end function)(globalSettings, userId))
if __bsConsequent <> invalid then
return __bsConsequent
else
Expand Down
17 changes: 15 additions & 2 deletions src/parser/tests/expression/NullCoalescenceExpression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,29 @@ describe('NullCoalescingExpression', () => {
`);
});

it('transpiles null coalesence assignment for variable alternate- complex consequent', () => {
testTranspile(`a = obj.link ?? fallback`, `
a = (function(fallback, obj)
__bsConsequent = obj.link
if __bsConsequent <> invalid then
return __bsConsequent
else
return fallback
end if
end function)(fallback, obj)
`);
});

it('properly transpiles null coalesence assignments - complex alternate', () => {
testTranspile(`a = user ?? m.defaults.getAccount(settings.name)`, `
a = (function(m, settings)
a = (function(m, settings, user)
__bsConsequent = user
if __bsConsequent <> invalid then
return __bsConsequent
else
return m.defaults.getAccount(settings.name)
end if
end function)(m, settings)
end function)(m, settings, user)
`);
});
});
Expand Down
14 changes: 10 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1043,19 +1043,25 @@ export class Util {
const variableExpressions = [] as VariableExpression[];
const uniqueVarNames = new Set<string>();

// Collect all expressions. Most of these expressions are fairly small so this should be quick!
// This should only be called during transpile time and only when we actually need it.
expression?.walk((expression) => {
function expressionWalker(expression) {
if (isExpression(expression)) {
expressions.push(expression);
}
if (isVariableExpression(expression)) {
variableExpressions.push(expression);
uniqueVarNames.add(expression.name.text);
}
}, {
}

// Collect all expressions. Most of these expressions are fairly small so this should be quick!
// This should only be called during transpile time and only when we actually need it.
expression?.walk(expressionWalker, {
walkMode: WalkMode.visitExpressions
});

//handle the expression itself (for situations when expression is a VariableExpression)
expressionWalker(expression);

return { expressions: expressions, varExpressions: variableExpressions, uniqueVarNames: [...uniqueVarNames] };
}

Expand Down

0 comments on commit 08998f4

Please sign in to comment.