Skip to content

Commit a9a5ef7

Browse files
Janthermattiaerre
authored andcommitted
Adding the option to standardise the type declarations (#171)
1 parent fd9517c commit a9a5ef7

18 files changed

Lines changed: 692 additions & 409 deletions

File tree

src/options.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const CATEGORY_GLOBAL = 'Global';
22
const CATEGORY_COMMON = 'Common';
3+
const CATEGORY_SOLIDITY = 'Solidity';
34

45
const options = {
56
bracketSpacing: {
@@ -48,6 +49,27 @@ const options = {
4849
type: 'boolean',
4950
default: false,
5051
description: 'Indent with tabs instead of spaces.'
52+
},
53+
explicitTypes: {
54+
category: CATEGORY_SOLIDITY,
55+
type: 'choice',
56+
default: 'always',
57+
description: 'Change when type aliases are used.',
58+
choices: [
59+
{
60+
value: 'always',
61+
description:
62+
'Prefer the explicit types `uint256`, `int256`, and `bytes1`.'
63+
},
64+
{
65+
value: 'never',
66+
description: 'Prefer the type aliases `uint`, `int`, and `byte`.'
67+
},
68+
{
69+
value: 'preserve',
70+
description: 'Respect the type used by the developer.'
71+
}
72+
]
5173
}
5274
};
5375

src/parser.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const extract = require('extract-comments');
22
// https://prettier.io/docs/en/plugins.html#parsers
33
const parser = require('solidity-parser-antlr');
44

5-
function parse(text) {
5+
function parse(text, parsers, options) {
66
const parsed = parser.parse(text, { loc: true, range: true });
77
parsed.comments = extract(text);
88

@@ -14,6 +14,17 @@ function parse(text) {
1414
if (ctx.loopExpression) {
1515
ctx.loopExpression.omitSemicolon = true;
1616
}
17+
},
18+
ElementaryTypeName(ctx) {
19+
if (options.explicitTypes === 'always') {
20+
if (ctx.name === 'uint') ctx.name = 'uint256';
21+
if (ctx.name === 'int') ctx.name = 'int256';
22+
if (ctx.name === 'byte') ctx.name = 'bytes1';
23+
} else if (options.explicitTypes === 'never') {
24+
if (ctx.name === 'uint256') ctx.name = 'uint';
25+
if (ctx.name === 'int256') ctx.name = 'int';
26+
if (ctx.name === 'bytes1') ctx.name = 'byte';
27+
}
1728
}
1829
});
1930

0 commit comments

Comments
 (0)