Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print list #234

Merged
merged 5 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions src/nodes/AssemblyCall.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
const {
doc: {
builders: { concat, group, indent, join, line, softline }
builders: { concat }
}
} = require('prettier/standalone');

const printList = require('./print-list');

const AssemblyCall = {
print: ({ node, path, print }) => {
if (node.arguments.length === 0) {
return node.functionName;
}
return concat([
node.functionName,
'(',
group(
concat([
indent(
concat([
softline,
join(concat([',', line]), path.map(print, 'arguments'))
])
),
softline
print: ({ node, path, print }) =>
node.arguments.length === 0
? node.functionName
: concat([
node.functionName,
'(',
printList(path.map(print, 'arguments')),
')'
])
),
')'
]);
}
};

module.exports = AssemblyCall;
20 changes: 12 additions & 8 deletions src/nodes/AssemblyFunctionDefinition.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
const {
doc: {
builders: { concat, group, indent, join, line, softline }
builders: { concat, group, indent, line }
}
} = require('prettier/standalone');

const printList = require('./print-list');

const AssemblyFunctionDefinition = {
print: ({ node, path, print }) =>
concat([
'function ',
node.name,
'(',
printList(path.map(print, 'arguments')),
')',
group(
concat([
indent(
concat([
softline,
join(concat([',', line]), path.map(print, 'arguments'))
line,
'->',
printList(path.map(print, 'returnArguments'), {
firstSeparator: line,
lastSeparator: ''
})
])
),
softline
line
])
),
')',
' -> ',
join(', ', path.map(print, 'returnArguments')),
' ',
path.call(print, 'body')
])
};
Expand Down
10 changes: 6 additions & 4 deletions src/nodes/AssemblyLocalDefinition.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const {
doc: {
builders: { join }
builders: { concat, line }
}
} = require('prettier/standalone');

const printList = require('./print-list');

const AssemblyLocalDefinition = {
print: ({ path, print }) =>
join(' ', [
concat([
'let',
join(', ', path.map(print, 'names')),
':=',
printList(path.map(print, 'names'), { firstSeparator: line }),
':= ',
path.call(print, 'expression')
])
};
Expand Down
19 changes: 11 additions & 8 deletions src/nodes/AssemblySwitch.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
const {
doc: {
builders: { concat, hardline, indent, join }
builders: { concat, hardline }
}
} = require('prettier/standalone');

const printList = require('./print-list');

const AssemblySwitch = {
print: ({ path, print }) => {
const doc = join(hardline, path.map(print, 'cases'));
return concat([
print: ({ path, print }) =>
concat([
'switch ',
path.call(print, 'expression'),
indent(hardline),
indent(doc)
]);
}
printList(path.map(print, 'cases'), {
firstSeparator: hardline,
separator: hardline,
lastSeparator: ''
})
])
};

module.exports = AssemblySwitch;
16 changes: 4 additions & 12 deletions src/nodes/CatchClause.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
const {
doc: {
builders: { concat, group, join, indent, line, softline }
builders: { concat, group }
}
} = require('prettier/standalone');

const printList = require('./print-list');

const CatchClause = {
print: ({ node, path, print }) => {
return group(
concat([
'catch ',
node.isReasonStringType ? 'Error' : '',
'(',
group(
concat([
indent(
concat([
softline,
join(concat([',', line]), path.map(print, 'parameters'))
])
),
softline
])
),
printList(path.map(print, 'parameters')),
') ',
path.call(print, 'body')
])
Expand Down
44 changes: 18 additions & 26 deletions src/nodes/ContractDefinition.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
const {
doc: {
builders: { concat, group, indent, join, line }
builders: { concat, group, indent, line }
}
} = require('prettier/standalone');

const printList = require('./print-list');
const printPreservingEmptyLines = require('./print-preserving-empty-lines');

const inheritance = (node, path, print) => {
if (node.baseContracts.length > 0) {
return concat([
' is',
indent(
concat([
line,
join(concat([',', line]), path.map(print, 'baseContracts'))
])
)
]);
}
return '';
};
const inheritance = (node, path, print) =>
node.baseContracts.length > 0
? concat([
' is',
printList(path.map(print, 'baseContracts'), { firstSeparator: line })
])
: line;

const body = (node, path, options, print) => {
if (node.subNodes.length > 0) {
return concat([
indent(line),
indent(printPreservingEmptyLines(path, 'subNodes', options, print)),
line
]);
}
return '';
};
const body = (node, path, options, print) =>
node.subNodes.length > 0
? concat([
indent(line),
indent(printPreservingEmptyLines(path, 'subNodes', options, print)),
line
])
: '';

const ContractDefinition = {
print: ({ node, options, path, print }) =>
Expand All @@ -40,7 +33,6 @@ const ContractDefinition = {
' ',
node.name,
inheritance(node, path, print),
line,
'{'
])
),
Expand Down
14 changes: 6 additions & 8 deletions src/nodes/EnumDefinition.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
const {
doc: {
builders: { concat, group, indent, join, line, softline }
builders: { concat, group, line, softline }
}
} = require('prettier/standalone');

const printList = require('./print-list');

const EnumDefinition = {
print: ({ node, path, print, options }) =>
group(
concat([
'enum ',
node.name,
' {',
indent(
concat([
options.bracketSpacing ? line : softline,
join(concat([',', line]), path.map(print, 'members'))
])
),
options.bracketSpacing ? line : softline,
printList(path.map(print, 'members'), {
firstSeparator: options.bracketSpacing ? line : softline
}),
'}'
])
)
Expand Down
24 changes: 7 additions & 17 deletions src/nodes/EventDefinition.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
const {
doc: {
builders: { concat, group, indent, join, line, softline }
builders: { concat }
}
} = require('prettier/standalone');

const parameters = (node, path, print) => {
if (node.parameters && node.parameters.length > 0) {
return group(
concat([
indent(
concat([
softline,
join(concat([',', line]), path.map(print, 'parameters'))
])
),
softline
])
);
}
return '';
};
const printList = require('./print-list');

const parameters = (node, path, print) =>
node.parameters && node.parameters.length > 0
? printList(path.map(print, 'parameters'))
: '';

const EventDefinition = {
print: ({ node, path, print }) =>
Expand Down
35 changes: 9 additions & 26 deletions src/nodes/FunctionCall.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,31 @@
const {
doc: {
builders: { concat, group, indent, join, line, softline }
builders: { concat, group, line, softline }
}
} = require('prettier/standalone');

const printList = require('./print-list');

const printObject = (node, path, print, options) =>
group(
concat([
'{',
indent(
concat([
options.bracketSpacing ? line : softline,
join(
concat([',', line]),
path
.map(print, 'arguments')
.map((arg, index) => concat([node.names[index], ': ', arg]))
)
])
printList(
path
.map(print, 'arguments')
.map((arg, index) => concat([node.names[index], ': ', arg])),
{ firstSeparator: options.bracketSpacing ? line : softline }
),
options.bracketSpacing ? line : softline,
'}'
])
);

const printParameters = (node, path, print) =>
group(
concat([
indent(
concat([
softline,
join(concat([',', line]), path.map(print, 'arguments'))
])
),
softline
])
);

const printArguments = (node, path, print, options) => {
if (node.names && node.names.length > 0) {
return printObject(node, path, print, options);
}
if (node.arguments && node.arguments.length > 0) {
return printParameters(node, path, print);
return printList(path.map(print, 'arguments'));
}
return '';
};
Expand Down
24 changes: 7 additions & 17 deletions src/nodes/FunctionDefinition.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
const {
doc: {
builders: { concat, dedent, group, indent, join, line, softline }
builders: { concat, dedent, group, indent, join, line }
}
} = require('prettier/standalone');

const printList = require('./print-list');

const functionName = node => {
if (node.isConstructor && !node.name) return 'constructor';
if (node.name) return `function ${node.name}`;
return 'function';
};

const parameters = (parametersType, node, path, print) => {
if (node[parametersType] && node[parametersType].length > 0) {
return group(
concat([
indent(
concat([
softline,
join(concat([',', line]), path.map(print, parametersType))
])
),
softline
])
);
}
return '';
};
const parameters = (parametersType, node, path, print) =>
node[parametersType] && node[parametersType].length > 0
? printList(path.map(print, parametersType))
: '';

const visibility = node => {
if (node.visibility && node.visibility !== 'default') {
Expand Down
Loading