Skip to content

Commit 9bcb096

Browse files
authored
Print list (#234)
* implementing printList pattern. * Adding some edge cases * small refactor of StructDefinition * more cases for printList * using options instead of multiple arguments
1 parent 234790a commit 9bcb096

24 files changed

Lines changed: 280 additions & 297 deletions

src/nodes/AssemblyCall.js

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

7+
const printList = require('./print-list');
8+
79
const AssemblyCall = {
8-
print: ({ node, path, print }) => {
9-
if (node.arguments.length === 0) {
10-
return node.functionName;
11-
}
12-
return concat([
13-
node.functionName,
14-
'(',
15-
group(
16-
concat([
17-
indent(
18-
concat([
19-
softline,
20-
join(concat([',', line]), path.map(print, 'arguments'))
21-
])
22-
),
23-
softline
10+
print: ({ node, path, print }) =>
11+
node.arguments.length === 0
12+
? node.functionName
13+
: concat([
14+
node.functionName,
15+
'(',
16+
printList(path.map(print, 'arguments')),
17+
')'
2418
])
25-
),
26-
')'
27-
]);
28-
}
2919
};
3020

3121
module.exports = AssemblyCall;

src/nodes/AssemblyFunctionDefinition.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
const {
22
doc: {
3-
builders: { concat, group, indent, join, line, softline }
3+
builders: { concat, group, indent, line }
44
}
55
} = require('prettier/standalone');
66

7+
const printList = require('./print-list');
8+
79
const AssemblyFunctionDefinition = {
810
print: ({ node, path, print }) =>
911
concat([
1012
'function ',
1113
node.name,
1214
'(',
15+
printList(path.map(print, 'arguments')),
16+
')',
1317
group(
1418
concat([
1519
indent(
1620
concat([
17-
softline,
18-
join(concat([',', line]), path.map(print, 'arguments'))
21+
line,
22+
'->',
23+
printList(path.map(print, 'returnArguments'), {
24+
firstSeparator: line,
25+
lastSeparator: ''
26+
})
1927
])
2028
),
21-
softline
29+
line
2230
])
2331
),
24-
')',
25-
' -> ',
26-
join(', ', path.map(print, 'returnArguments')),
27-
' ',
2832
path.call(print, 'body')
2933
])
3034
};

src/nodes/AssemblyLocalDefinition.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
const {
22
doc: {
3-
builders: { join }
3+
builders: { concat, line }
44
}
55
} = require('prettier/standalone');
66

7+
const printList = require('./print-list');
8+
79
const AssemblyLocalDefinition = {
810
print: ({ path, print }) =>
9-
join(' ', [
11+
concat([
1012
'let',
11-
join(', ', path.map(print, 'names')),
12-
':=',
13+
printList(path.map(print, 'names'), { firstSeparator: line }),
14+
':= ',
1315
path.call(print, 'expression')
1416
])
1517
};

src/nodes/AssemblySwitch.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
const {
22
doc: {
3-
builders: { concat, hardline, indent, join }
3+
builders: { concat, hardline }
44
}
55
} = require('prettier/standalone');
66

7+
const printList = require('./print-list');
8+
79
const AssemblySwitch = {
8-
print: ({ path, print }) => {
9-
const doc = join(hardline, path.map(print, 'cases'));
10-
return concat([
10+
print: ({ path, print }) =>
11+
concat([
1112
'switch ',
1213
path.call(print, 'expression'),
13-
indent(hardline),
14-
indent(doc)
15-
]);
16-
}
14+
printList(path.map(print, 'cases'), {
15+
firstSeparator: hardline,
16+
separator: hardline,
17+
lastSeparator: ''
18+
})
19+
])
1720
};
1821

1922
module.exports = AssemblySwitch;

src/nodes/CatchClause.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
const {
22
doc: {
3-
builders: { concat, group, join, indent, line, softline }
3+
builders: { concat, group }
44
}
55
} = require('prettier/standalone');
66

7+
const printList = require('./print-list');
8+
79
const CatchClause = {
810
print: ({ node, path, print }) => {
911
return group(
1012
concat([
1113
'catch ',
1214
node.isReasonStringType ? 'Error' : '',
1315
'(',
14-
group(
15-
concat([
16-
indent(
17-
concat([
18-
softline,
19-
join(concat([',', line]), path.map(print, 'parameters'))
20-
])
21-
),
22-
softline
23-
])
24-
),
16+
printList(path.map(print, 'parameters')),
2517
') ',
2618
path.call(print, 'body')
2719
])

src/nodes/ContractDefinition.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
const {
22
doc: {
3-
builders: { concat, group, indent, join, line }
3+
builders: { concat, group, indent, line }
44
}
55
} = require('prettier/standalone');
6+
7+
const printList = require('./print-list');
68
const printPreservingEmptyLines = require('./print-preserving-empty-lines');
79

8-
const inheritance = (node, path, print) => {
9-
if (node.baseContracts.length > 0) {
10-
return concat([
11-
' is',
12-
indent(
13-
concat([
14-
line,
15-
join(concat([',', line]), path.map(print, 'baseContracts'))
16-
])
17-
)
18-
]);
19-
}
20-
return '';
21-
};
10+
const inheritance = (node, path, print) =>
11+
node.baseContracts.length > 0
12+
? concat([
13+
' is',
14+
printList(path.map(print, 'baseContracts'), { firstSeparator: line })
15+
])
16+
: line;
2217

23-
const body = (node, path, options, print) => {
24-
if (node.subNodes.length > 0) {
25-
return concat([
26-
indent(line),
27-
indent(printPreservingEmptyLines(path, 'subNodes', options, print)),
28-
line
29-
]);
30-
}
31-
return '';
32-
};
18+
const body = (node, path, options, print) =>
19+
node.subNodes.length > 0
20+
? concat([
21+
indent(line),
22+
indent(printPreservingEmptyLines(path, 'subNodes', options, print)),
23+
line
24+
])
25+
: '';
3326

3427
const ContractDefinition = {
3528
print: ({ node, options, path, print }) =>
@@ -40,7 +33,6 @@ const ContractDefinition = {
4033
' ',
4134
node.name,
4235
inheritance(node, path, print),
43-
line,
4436
'{'
4537
])
4638
),

src/nodes/EnumDefinition.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
const {
22
doc: {
3-
builders: { concat, group, indent, join, line, softline }
3+
builders: { concat, group, line, softline }
44
}
55
} = require('prettier/standalone');
66

7+
const printList = require('./print-list');
8+
79
const EnumDefinition = {
810
print: ({ node, path, print, options }) =>
911
group(
1012
concat([
1113
'enum ',
1214
node.name,
1315
' {',
14-
indent(
15-
concat([
16-
options.bracketSpacing ? line : softline,
17-
join(concat([',', line]), path.map(print, 'members'))
18-
])
19-
),
20-
options.bracketSpacing ? line : softline,
16+
printList(path.map(print, 'members'), {
17+
firstSeparator: options.bracketSpacing ? line : softline
18+
}),
2119
'}'
2220
])
2321
)

src/nodes/EventDefinition.js

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

7-
const parameters = (node, path, print) => {
8-
if (node.parameters && node.parameters.length > 0) {
9-
return group(
10-
concat([
11-
indent(
12-
concat([
13-
softline,
14-
join(concat([',', line]), path.map(print, 'parameters'))
15-
])
16-
),
17-
softline
18-
])
19-
);
20-
}
21-
return '';
22-
};
7+
const printList = require('./print-list');
8+
9+
const parameters = (node, path, print) =>
10+
node.parameters && node.parameters.length > 0
11+
? printList(path.map(print, 'parameters'))
12+
: '';
2313

2414
const EventDefinition = {
2515
print: ({ node, path, print }) =>

src/nodes/FunctionCall.js

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

7+
const printList = require('./print-list');
8+
79
const printObject = (node, path, print, options) =>
810
group(
911
concat([
1012
'{',
11-
indent(
12-
concat([
13-
options.bracketSpacing ? line : softline,
14-
join(
15-
concat([',', line]),
16-
path
17-
.map(print, 'arguments')
18-
.map((arg, index) => concat([node.names[index], ': ', arg]))
19-
)
20-
])
13+
printList(
14+
path
15+
.map(print, 'arguments')
16+
.map((arg, index) => concat([node.names[index], ': ', arg])),
17+
{ firstSeparator: options.bracketSpacing ? line : softline }
2118
),
22-
options.bracketSpacing ? line : softline,
2319
'}'
2420
])
2521
);
2622

27-
const printParameters = (node, path, print) =>
28-
group(
29-
concat([
30-
indent(
31-
concat([
32-
softline,
33-
join(concat([',', line]), path.map(print, 'arguments'))
34-
])
35-
),
36-
softline
37-
])
38-
);
39-
4023
const printArguments = (node, path, print, options) => {
4124
if (node.names && node.names.length > 0) {
4225
return printObject(node, path, print, options);
4326
}
4427
if (node.arguments && node.arguments.length > 0) {
45-
return printParameters(node, path, print);
28+
return printList(path.map(print, 'arguments'));
4629
}
4730
return '';
4831
};

src/nodes/FunctionDefinition.js

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

7+
const printList = require('./print-list');
8+
79
const functionName = node => {
810
if (node.isConstructor && !node.name) return 'constructor';
911
if (node.name) return `function ${node.name}`;
1012
return 'function';
1113
};
1214

13-
const parameters = (parametersType, node, path, print) => {
14-
if (node[parametersType] && node[parametersType].length > 0) {
15-
return group(
16-
concat([
17-
indent(
18-
concat([
19-
softline,
20-
join(concat([',', line]), path.map(print, parametersType))
21-
])
22-
),
23-
softline
24-
])
25-
);
26-
}
27-
return '';
28-
};
15+
const parameters = (parametersType, node, path, print) =>
16+
node[parametersType] && node[parametersType].length > 0
17+
? printList(path.map(print, parametersType))
18+
: '';
2919

3020
const visibility = node => {
3121
if (node.visibility && node.visibility !== 'default') {

0 commit comments

Comments
 (0)