prettier-plugin-solidity
Advanced tools
Comparing version 1.0.0-alpha.22 to 1.0.0-alpha.23
{ | ||
"name": "prettier-plugin-solidity", | ||
"version": "1.0.0-alpha.22", | ||
"version": "1.0.0-alpha.23", | ||
"description": "prettier plugin for solidity", | ||
@@ -5,0 +5,0 @@ "main": "src", |
/* eslint-disable implicit-arrow-linebreak */ | ||
const { | ||
doc: { | ||
builders: { join } | ||
builders: { concat, group, indent, line } | ||
} | ||
@@ -10,11 +10,15 @@ } = require('prettier'); | ||
print: ({ path, print }) => | ||
join(' ', [ | ||
path.call(print, 'condition'), | ||
'?', | ||
path.call(print, 'trueExpression'), | ||
':', | ||
path.call(print, 'falseExpression') | ||
]) | ||
group( | ||
concat([ | ||
path.call(print, 'condition'), | ||
indent(line), | ||
'? ', | ||
path.call(print, 'trueExpression'), | ||
indent(line), | ||
': ', | ||
path.call(print, 'falseExpression') | ||
]) | ||
) | ||
}; | ||
module.exports = Conditional; |
@@ -0,4 +1,5 @@ | ||
/* eslint-disable implicit-arrow-linebreak */ | ||
const { | ||
doc: { | ||
builders: { concat, indent, join, line } | ||
builders: { concat, group, indent, join, line } | ||
} | ||
@@ -8,27 +9,46 @@ } = require('prettier'); | ||
const ContractDefinition = { | ||
print: ({ node, options, path, print }) => { | ||
let parts = [node.kind, ' ', node.name]; | ||
const inheritance = (node, path, print) => { | ||
if (node.baseContracts.length > 0) { | ||
return concat([ | ||
' is', | ||
indent( | ||
concat([ | ||
line, | ||
join(concat([',', line]), path.map(print, 'baseContracts')) | ||
]) | ||
) | ||
]); | ||
} | ||
return ''; | ||
}; | ||
if (node.baseContracts.length > 0) { | ||
parts = parts.concat([ | ||
' is ', | ||
join(', ', path.map(print, 'baseContracts')) | ||
]); | ||
} | ||
parts.push(' {'); | ||
if (node.subNodes.length > 0) { | ||
parts = parts.concat([ | ||
indent(line), | ||
indent(printPreservingEmptyLines(path, 'subNodes', options, print)), | ||
line | ||
]); | ||
} | ||
parts.push('}'); | ||
return concat(parts); | ||
const body = (node, path, options, print) => { | ||
if (node.subNodes.length > 0) { | ||
return concat([ | ||
indent(line), | ||
indent(printPreservingEmptyLines(path, 'subNodes', options, print)), | ||
line | ||
]); | ||
} | ||
return ''; | ||
}; | ||
const ContractDefinition = { | ||
print: ({ node, options, path, print }) => | ||
concat([ | ||
group( | ||
concat([ | ||
node.kind, | ||
' ', | ||
node.name, | ||
inheritance(node, path, print), | ||
line, | ||
'{' | ||
]) | ||
), | ||
body(node, path, options, print), | ||
'}' | ||
]) | ||
}; | ||
module.exports = ContractDefinition; |
@@ -0,68 +1,79 @@ | ||
/* eslint-disable implicit-arrow-linebreak */ | ||
const { | ||
doc: { | ||
builders: { concat, group, indent, join, line } | ||
builders: { concat, dedent, group, indent, join, line } | ||
} | ||
} = require('prettier'); | ||
const FunctionDefinition = { | ||
print: ({ node, path, print }) => { | ||
let parts = []; | ||
const functionName = node => { | ||
if (node.isConstructor && !node.name) return 'constructor'; | ||
if (node.name) return `function ${node.name}`; | ||
return 'function'; | ||
}; | ||
if (node.isConstructor) { | ||
if (node.name) { | ||
parts.push(`function ${node.name}`); | ||
} else { | ||
parts.push('constructor'); | ||
} | ||
} else if (node.name === '') { | ||
parts.push('function'); | ||
} else { | ||
parts = parts.concat(['function ', node.name]); | ||
} | ||
const visibility = node => { | ||
if (node.visibility && node.visibility !== 'default') { | ||
return concat([line, node.visibility]); | ||
} | ||
return ''; | ||
}; | ||
parts = parts.concat(['(', path.call(print, 'parameters'), ')']); | ||
const stateMutability = node => { | ||
if (node.stateMutability && node.stateMutability !== 'default') { | ||
return concat([line, node.stateMutability]); | ||
} | ||
return ''; | ||
}; | ||
let modifiers = []; | ||
if (node.visibility && node.visibility !== 'default') { | ||
modifiers.push(node.visibility); | ||
} | ||
// @TODO: check stateMutability null vs default | ||
if (node.stateMutability && node.stateMutability !== 'default') { | ||
modifiers.push(node.stateMutability); | ||
} | ||
if (node.modifiers.length > 0) { | ||
modifiers = modifiers.concat(path.map(print, 'modifiers')); | ||
} | ||
if (node.returnParameters) { | ||
modifiers.push( | ||
concat(['returns (', path.call(print, 'returnParameters'), ')']) | ||
); | ||
} | ||
const modifiers = (node, path, print) => { | ||
if (node.modifiers.length > 0) { | ||
return concat([line, join(line, path.map(print, 'modifiers'))]); | ||
} | ||
return ''; | ||
}; | ||
if (modifiers.length > 0) { | ||
parts.push( | ||
group( | ||
concat( | ||
[ | ||
indent(line), | ||
join(indent(line), modifiers), | ||
node.body ? line : null | ||
].filter(x => x) | ||
) | ||
) | ||
); | ||
} else if (node.body) { | ||
parts.push(' '); | ||
} | ||
const returnParameters = (node, path, print) => { | ||
if (node.returnParameters) { | ||
return concat([ | ||
line, | ||
'returns (', | ||
path.call(print, 'returnParameters'), | ||
')' | ||
]); | ||
} | ||
return ''; | ||
}; | ||
if (node.body) { | ||
parts.push(path.call(print, 'body')); | ||
} else { | ||
parts.push(';'); | ||
} | ||
const signatureEnd = node => { | ||
if (node.body) return dedent(line); | ||
return ';'; | ||
}; | ||
return concat(parts); | ||
} | ||
const body = (node, path, print) => { | ||
if (node.body) return path.call(print, 'body'); | ||
return ''; | ||
}; | ||
const FunctionDefinition = { | ||
print: ({ node, path, print }) => | ||
concat([ | ||
functionName(node), | ||
'(', | ||
path.call(print, 'parameters'), | ||
')', | ||
indent( | ||
group( | ||
concat([ | ||
visibility(node), | ||
stateMutability(node), | ||
modifiers(node, path, print), | ||
returnParameters(node, path, print), | ||
signatureEnd(node) | ||
]) | ||
) | ||
), | ||
body(node, path, print) | ||
]) | ||
}; | ||
module.exports = FunctionDefinition; |
@@ -0,36 +1,65 @@ | ||
/* eslint-disable implicit-arrow-linebreak */ | ||
const { | ||
doc: { | ||
builders: { concat, join } | ||
builders: { concat, group, indent, join, line, softline } | ||
} | ||
} = require('prettier'); | ||
const FunctionTypeName = { | ||
print: ({ node, path, print }) => { | ||
const returns = returnTypes => { | ||
if (returnTypes.length > 0) { | ||
return concat([ | ||
'returns (', | ||
join(', ', path.map(print, 'returnTypes')), | ||
')' | ||
]); | ||
} | ||
return null; | ||
}; | ||
const parameterTypes = (node, path, print) => | ||
group( | ||
concat([ | ||
indent( | ||
concat([ | ||
softline, | ||
join(concat([',', line]), path.map(print, 'parameterTypes')) | ||
]) | ||
), | ||
softline | ||
]) | ||
); | ||
return join( | ||
' ', | ||
[ | ||
concat([ | ||
'function(', | ||
join(', ', path.map(print, 'parameterTypes')), | ||
')' | ||
]), | ||
returns(node.returnTypes), | ||
node.visibility === 'default' ? null : node.visibility, | ||
node.stateMutability | ||
].filter(element => element) | ||
); | ||
const returnTypes = (node, path, print) => { | ||
if (node.returnTypes.length > 0) { | ||
return concat([ | ||
line, | ||
'returns (', | ||
join(', ', path.map(print, 'returnTypes')), | ||
')' | ||
]); | ||
} | ||
return ''; | ||
}; | ||
const visibility = node => { | ||
if (node.visibility && node.visibility !== 'default') { | ||
return concat([line, node.visibility]); | ||
} | ||
return ''; | ||
}; | ||
const stateMutability = node => { | ||
if (node.stateMutability && node.stateMutability !== 'default') { | ||
return concat([line, node.stateMutability]); | ||
} | ||
return ''; | ||
}; | ||
const FunctionTypeName = { | ||
print: ({ node, path, print }) => | ||
concat([ | ||
'function(', | ||
parameterTypes(node, path, print), | ||
')', | ||
indent( | ||
group( | ||
concat([ | ||
returnTypes(node, path, print), | ||
visibility(node), | ||
stateMutability(node) | ||
]) | ||
) | ||
) | ||
]) | ||
}; | ||
module.exports = FunctionTypeName; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
298364
150
2767