Skip to content

Commit a6674c6

Browse files
authored
Fix chained ternary conditionals instrumentation (#830)
1 parent 9b64cba commit a6674c6

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

lib/parse.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ parse.Conditional = function(contract, expression, skipStatementRegistry) {
9191
parse[expression.condition.type] &&
9292
parse[expression.condition.type](contract, expression.condition, true);
9393

94+
if (expression.trueExpression && expression.trueExpression.type === 'Conditional'){
95+
parse[expression.trueExpression.type](contract, expression.trueExpression, true);
96+
}
97+
98+
if (expression.falseExpression && expression.falseExpression.type === 'Conditional'){
99+
parse[expression.falseExpression.type](contract, expression.falseExpression, true);
100+
}
101+
94102
register.conditional(contract, expression);
95103
};
96104

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pragma solidity ^0.7.0;
2+
3+
contract Test {
4+
function a() public {
5+
int min = 1;
6+
int max = 2;
7+
int value = 3;
8+
value < min
9+
? min
10+
: value > max
11+
? max
12+
: value;
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pragma solidity ^0.7.0;
2+
3+
contract Test {
4+
function a() public {
5+
int min = 1;
6+
int max = 2;
7+
int value = 3;
8+
value < min ? min : value > max ? max : value;
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pragma solidity ^0.7.0;
2+
3+
contract Test {
4+
function a() public {
5+
int min = 1;
6+
int max = 2;
7+
int value = 3;
8+
value < min
9+
? value > max
10+
? max
11+
: value
12+
: min;
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pragma solidity ^0.7.0;
2+
3+
contract Test {
4+
function a() public {
5+
int min = 1;
6+
int max = 2;
7+
int value = 3;
8+
(value < min) ? min : (value > max) ? max : value;
9+
}
10+
}

test/units/conditional.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,24 @@ describe('ternary conditionals', () => {
233233
const info = util.instrumentAndCompile('conditional/ternary-with-unbracketed-else');
234234
util.report(info.solcOutput.errors);
235235
});
236+
237+
it('should compile after instrumenting a chain of ternary conditionals (single line)', () => {
238+
const info = util.instrumentAndCompile('conditional/chained-singleline');
239+
util.report(info.solcOutput.errors);
240+
});
241+
242+
it('should compile after instrumenting a chain of ternary conditionals (multi-line)', () => {
243+
const info = util.instrumentAndCompile('conditional/chained-multiline');
244+
util.report(info.solcOutput.errors);
245+
});
246+
247+
it('should compile after instrumenting a chain of ternary conditionals (parens)', () => {
248+
const info = util.instrumentAndCompile('conditional/chained-with-parens');
249+
util.report(info.solcOutput.errors);
250+
});
251+
252+
it('should compile after instrumenting a chain of ternary conditionals (true expr is tern)', () => {
253+
const info = util.instrumentAndCompile('conditional/chained-true');
254+
util.report(info.solcOutput.errors);
255+
});
236256
});

0 commit comments

Comments
 (0)