Skip to content

Commit b26892e

Browse files
authored
Upgrade parser, unicode strings, catch Panic (#444)
* Handle catch clauses with Panic * Add support for unicode strings * Upgrade parser * Check coverage threshold on CI
1 parent 7910b61 commit b26892e

File tree

11 files changed

+136
-10
lines changed

11 files changed

+136
-10
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ node_js:
77
- '13'
88
script:
99
- npm run lint
10-
- npm test
10+
- npm run test:all

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ module.exports = {
88
'!src/prettier-comments/**/*.js'
99
],
1010
coverageDirectory: './coverage/',
11+
coverageThreshold: {
12+
global: {
13+
branches: 100,
14+
functions: 100,
15+
lines: 100,
16+
statements: 100
17+
}
18+
},
1119
setupFiles: ['<rootDir>/tests_config/run_spec.js'],
1220
snapshotSerializers: ['<rootDir>/tests_config/raw-serializer.js'],
1321
testEnvironment: 'node',

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"jest-watch-typeahead": "^0.6.1"
7373
},
7474
"dependencies": {
75-
"@solidity-parser/parser": "^0.11.0",
75+
"@solidity-parser/parser": "^0.12.0",
7676
"dir-to-object": "^2.0.0",
7777
"emoji-regex": "^9.2.1",
7878
"escape-string-regexp": "^4.0.0",

src/nodes/CatchClause.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const printSeparatedList = require('./print-separated-list');
99
const parameters = (node, path, print) =>
1010
node.parameters
1111
? concat([
12-
node.isReasonStringType ? 'Error' : '',
12+
node.kind || '',
1313
'(',
1414
printSeparatedList(path.map(print, 'parameters')),
1515
') '

src/nodes/StringLiteral.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ const { printString } = require('../prettier-comments/common/util');
77

88
const StringLiteral = {
99
print: ({ node, options }) => {
10-
const list = node.parts.map((part) => printString(part, options));
10+
const list = node.parts.map((part, index) =>
11+
printString(part, {
12+
...options,
13+
// node.isUnicode is an array of the same length as node.parts
14+
// that indicates if that string fragment has the unicode prefix
15+
isUnicode: node.isUnicode[index]
16+
})
17+
);
18+
1119
return join(line, list);
1220
}
1321
};

src/prettier-comments/common/util.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,12 @@ function printString(rawContent, options) {
475475
options.parser === "css" ||
476476
options.parser === "less" ||
477477
options.parser === "scss"
478-
)
478+
),
479+
options.isUnicode
479480
);
480481
}
481482

482-
function makeString(rawContent, enclosingQuote, unescapeUnnecessaryEscapes) {
483+
function makeString(rawContent, enclosingQuote, unescapeUnnecessaryEscapes, isUnicode) {
483484
const otherQuote = enclosingQuote === '"' ? "'" : '"';
484485

485486
// Matches _any_ escape and unescaped quotes (both single and double).
@@ -514,7 +515,7 @@ function makeString(rawContent, enclosingQuote, unescapeUnnecessaryEscapes) {
514515
: "\\" + escaped;
515516
});
516517

517-
return enclosingQuote + newContent + enclosingQuote;
518+
return (isUnicode ? "unicode" : "") + enclosingQuote + newContent + enclosingQuote;
518519
}
519520

520521
function printNumber(rawNumber) {

tests/TryCatch/TryCatch.sol

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,30 @@ contract FeedConsumer {
7474
errorCount++;
7575
return (0, false);
7676
} }
77+
78+
function rate4(address token) public returns (uint value, bool success) {
79+
// Permanently disable the mechanism if there are
80+
// more than 10 errors.
81+
require(errorCount < 10);
82+
try feed.getData(token) returns (uint v) {
83+
return (v, true);
84+
} catch Error(string memory /*reason*/) {
85+
// This is executed in case
86+
// revert was called inside getData
87+
// and a reason string was provided.
88+
errorCount++;
89+
return (0, false);
90+
} catch Panic(uint /*errorCode*/) {
91+
// This is executed in case of a panic,
92+
// i.e. a serious error like division by zero
93+
// or overflow. The error code can be used
94+
// to determine the kind of error.
95+
errorCount++;
96+
return (0, false);
97+
} catch (bytes memory /*lowLevelData*/) {
98+
// This is executed in case revert() was used.
99+
errorCount++;
100+
return (0, false);
101+
}
102+
}
77103
}

tests/TryCatch/__snapshots__/jsfmt.spec.js.snap

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ contract FeedConsumer {
7777
errorCount++;
7878
return (0, false);
7979
} }
80+
81+
function rate4(address token) public returns (uint value, bool success) {
82+
// Permanently disable the mechanism if there are
83+
// more than 10 errors.
84+
require(errorCount < 10);
85+
try feed.getData(token) returns (uint v) {
86+
return (v, true);
87+
} catch Error(string memory /*reason*/) {
88+
// This is executed in case
89+
// revert was called inside getData
90+
// and a reason string was provided.
91+
errorCount++;
92+
return (0, false);
93+
} catch Panic(uint /*errorCode*/) {
94+
// This is executed in case of a panic,
95+
// i.e. a serious error like division by zero
96+
// or overflow. The error code can be used
97+
// to determine the kind of error.
98+
errorCount++;
99+
return (0, false);
100+
} catch (bytes memory /*lowLevelData*/) {
101+
// This is executed in case revert() was used.
102+
errorCount++;
103+
return (0, false);
104+
}
105+
}
80106
}
81107
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82108
pragma solidity ^0.6.0;
@@ -171,6 +197,38 @@ contract FeedConsumer {
171197
return (0, false);
172198
}
173199
}
200+
201+
function rate4(address token) public returns (uint256 value, bool success) {
202+
// Permanently disable the mechanism if there are
203+
// more than 10 errors.
204+
require(errorCount < 10);
205+
try feed.getData(token) returns (uint256 v) {
206+
return (v, true);
207+
} catch Error(
208+
string memory /*reason*/
209+
) {
210+
// This is executed in case
211+
// revert was called inside getData
212+
// and a reason string was provided.
213+
errorCount++;
214+
return (0, false);
215+
} catch Panic(
216+
uint256 /*errorCode*/
217+
) {
218+
// This is executed in case of a panic,
219+
// i.e. a serious error like division by zero
220+
// or overflow. The error code can be used
221+
// to determine the kind of error.
222+
errorCount++;
223+
return (0, false);
224+
} catch (
225+
bytes memory /*lowLevelData*/
226+
) {
227+
// This is executed in case revert() was used.
228+
errorCount++;
229+
return (0, false);
230+
}
231+
}
174232
}
175233
176234
`;

tests/quotes/Quotes.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ contract Foo {
1616
string escapeDoubleQuotes = "The \"quick\" brown fox";
1717
string hex1 = hex'DeadBeef';
1818
string hex2 = hex"DeadBeef";
19+
string withUnicode1 = unicode'hello 🦄 world';
20+
string withUnicode2 = unicode"hello 🦄 world";
21+
string multiPartAndUnicode1 = unicode'hello 🦄' ' world';
22+
string multiPartAndUnicode2 = 'hello ' unicode' 🦄world';
23+
string multiPartAndUnicode3 = unicode'hello 🦄' unicode' world 🦄';
1924
}

0 commit comments

Comments
 (0)