Skip to content

Commit 0955168

Browse files
Janthermattiaerre
authored andcommitted
Adding support for the common configuration spacingBrackets (#167)
* Adding the common configuration spacingBrackets and a list of every option supported by this plugin. * adding spacingBrackets and indentation to EnumDefinitions
1 parent c7437b7 commit 0955168

File tree

10 files changed

+208
-17
lines changed

10 files changed

+208
-17
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { handleComments } = require('./prettier-comments');
22

33
const massageAstNode = require('./clean');
44
const loc = require('./loc');
5+
const options = require('./options');
56
const parse = require('./parser');
67
const print = require('./printer');
78

@@ -57,13 +58,14 @@ const printers = {
5758

5859
// https://prettier.io/docs/en/plugins.html#defaultoptions
5960
const defaultOptions = {
60-
// @TODO fix indentation in block comments
61+
bracketSpacing: false,
6162
tabWidth: 4
6263
};
6364

6465
module.exports = {
6566
languages,
6667
parsers,
6768
printers,
69+
options,
6870
defaultOptions
6971
};

src/nodes/EnumDefinition.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
const {
22
doc: {
3-
builders: { concat, join }
3+
builders: { concat, group, indent, join, line, softline }
44
}
55
} = require('prettier');
66

77
const EnumDefinition = {
8-
print: ({ node, path, print }) =>
9-
concat([
10-
'enum ',
11-
node.name,
12-
' {',
13-
join(', ', path.map(print, 'members')),
14-
'}'
15-
])
8+
print: ({ node, path, print, options }) =>
9+
group(
10+
concat([
11+
'enum ',
12+
node.name,
13+
' {',
14+
indent(
15+
concat([
16+
options.bracketSpacing ? line : softline,
17+
join(concat([',', line]), path.map(print, 'members'))
18+
])
19+
),
20+
options.bracketSpacing ? line : softline,
21+
'}'
22+
])
23+
)
1624
};
1725

1826
module.exports = EnumDefinition;

src/nodes/FunctionCall.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ const {
44
}
55
} = require('prettier');
66

7-
const printObject = (node, path, print) =>
7+
const printObject = (node, path, print, options) =>
88
group(
99
concat([
1010
'{',
1111
indent(
1212
concat([
13-
softline,
13+
options.bracketSpacing ? line : softline,
1414
join(
1515
concat([',', line]),
1616
path
@@ -19,7 +19,7 @@ const printObject = (node, path, print) =>
1919
)
2020
])
2121
),
22-
softline,
22+
options.bracketSpacing ? line : softline,
2323
'}'
2424
])
2525
);
@@ -37,9 +37,9 @@ const printParameters = (node, path, print) =>
3737
])
3838
);
3939

40-
const printArguments = (node, path, print) => {
40+
const printArguments = (node, path, print, options) => {
4141
if (node.names && node.names.length > 0) {
42-
return printObject(node, path, print);
42+
return printObject(node, path, print, options);
4343
}
4444
if (node.arguments && node.arguments.length > 0) {
4545
return printParameters(node, path, print);
@@ -48,11 +48,11 @@ const printArguments = (node, path, print) => {
4848
};
4949

5050
const FunctionCall = {
51-
print: ({ node, path, print }) =>
51+
print: ({ node, path, print, options }) =>
5252
concat([
5353
path.call(print, 'expression'),
5454
'(',
55-
printArguments(node, path, print),
55+
printArguments(node, path, print, options),
5656
')'
5757
])
5858
};

src/options.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const CATEGORY_GLOBAL = 'Global';
2+
const CATEGORY_COMMON = 'Common';
3+
4+
const options = {
5+
bracketSpacing: {
6+
since: '0.0.0',
7+
category: CATEGORY_COMMON,
8+
type: 'boolean',
9+
default: false,
10+
description: 'Print spaces between brackets.',
11+
oppositeDescription: 'Do not print spaces between brackets.'
12+
},
13+
printWidth: {
14+
since: '0.0.0',
15+
category: CATEGORY_GLOBAL,
16+
type: 'int',
17+
default: 80,
18+
description: 'The line length where Prettier will try wrap.',
19+
range: {
20+
start: 0,
21+
end: Infinity,
22+
step: 1
23+
}
24+
},
25+
// TODO: uncomment when https://github.com/prettier-solidity/prettier-plugin-solidity/pull/144
26+
// is merged.
27+
// singleQuote: {
28+
// since: '0.0.0',
29+
// category: CATEGORY_COMMON,
30+
// type: 'boolean',
31+
// default: false,
32+
// description: 'Use single quotes instead of double quotes.'
33+
// },
34+
tabWidth: {
35+
type: 'int',
36+
category: CATEGORY_GLOBAL,
37+
default: 4,
38+
description: 'Number of spaces per indentation level.',
39+
range: {
40+
start: 0,
41+
end: Infinity,
42+
step: 1
43+
}
44+
},
45+
useTabs: {
46+
since: '1.0.0',
47+
category: CATEGORY_GLOBAL,
48+
type: 'boolean',
49+
default: false,
50+
description: 'Indent with tabs instead of spaces.'
51+
}
52+
};
53+
54+
module.exports = options;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
contract EnumDefinitions {
2+
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
3+
enum States { State1, State2, State3, State4, State5, State6, State7, State8, State9 }
4+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`EnumDefinitions.sol 1`] = `
4+
contract EnumDefinitions {
5+
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
6+
enum States { State1, State2, State3, State4, State5, State6, State7, State8, State9 }
7+
}
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
contract EnumDefinitions {
10+
enum ActionChoices {GoLeft, GoRight, GoStraight, SitStill}
11+
enum States {
12+
State1,
13+
State2,
14+
State3,
15+
State4,
16+
State5,
17+
State6,
18+
State7,
19+
State8,
20+
State9
21+
}
22+
}
23+
24+
`;
25+
26+
exports[`EnumDefinitions.sol 2`] = `
27+
contract EnumDefinitions {
28+
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
29+
enum States { State1, State2, State3, State4, State5, State6, State7, State8, State9 }
30+
}
31+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32+
contract EnumDefinitions {
33+
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
34+
enum States {
35+
State1,
36+
State2,
37+
State3,
38+
State4,
39+
State5,
40+
State6,
41+
State7,
42+
State8,
43+
State9
44+
}
45+
}
46+
47+
`;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run_spec(__dirname);
2+
run_spec(__dirname, { bracketSpacing: true });

tests/FunctionCalls/FunctionCalls.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,18 @@ contract FunctionCalls {
22
function foo() {
33
address veryLongValidatorAddress = veryVeryVeryLongSignature.popLast20Bytes();
44
}
5+
6+
function foo() {
7+
Voter you = Voter(1, true);
8+
9+
Voter me = Voter({
10+
weight: 2,
11+
voted: abstain()
12+
});
13+
14+
Voter airbnb = Voter({
15+
weight: 2,
16+
voted: true,
17+
});
18+
}
519
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,72 @@ contract FunctionCalls {
55
function foo() {
66
address veryLongValidatorAddress = veryVeryVeryLongSignature.popLast20Bytes();
77
}
8+
9+
function foo() {
10+
Voter you = Voter(1, true);
11+
12+
Voter me = Voter({
13+
weight: 2,
14+
voted: abstain()
15+
});
16+
17+
Voter airbnb = Voter({
18+
weight: 2,
19+
voted: true,
20+
});
21+
}
22+
}
23+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24+
contract FunctionCalls {
25+
function foo() {
26+
address veryLongValidatorAddress = veryVeryVeryLongSignature.popLast20Bytes();
27+
}
28+
29+
function foo() {
30+
Voter you = Voter(1, true);
31+
32+
Voter me = Voter({weight: 2, voted: abstain()});
33+
34+
Voter airbnb = Voter({weight: 2, voted: true});
35+
}
36+
}
37+
38+
`;
39+
40+
exports[`FunctionCalls.sol 2`] = `
41+
contract FunctionCalls {
42+
function foo() {
43+
address veryLongValidatorAddress = veryVeryVeryLongSignature.popLast20Bytes();
44+
}
45+
46+
function foo() {
47+
Voter you = Voter(1, true);
48+
49+
Voter me = Voter({
50+
weight: 2,
51+
voted: abstain()
52+
});
53+
54+
Voter airbnb = Voter({
55+
weight: 2,
56+
voted: true,
57+
});
58+
}
859
}
960
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1061
contract FunctionCalls {
1162
function foo() {
1263
address veryLongValidatorAddress = veryVeryVeryLongSignature
1364
.popLast20Bytes();
1465
}
66+
67+
function foo() {
68+
Voter you = Voter(1, true);
69+
70+
Voter me = Voter({ weight: 2, voted: abstain() });
71+
72+
Voter airbnb = Voter({ weight: 2, voted: true });
73+
}
1574
}
1675
1776
`;

tests/FunctionCalls/jsfmt.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
run_spec(__dirname);
2+
run_spec(__dirname, { bracketSpacing: true });

0 commit comments

Comments
 (0)