Skip to content

Commit 2d6f34d

Browse files
Franco Victoriomattiaerre
authored andcommitted
Add support for ThrowStatement (#86)
* Add support for ThrowStatement * Handle inheritance with args, multiple vars, modifiers without parentheses * Update snapshots
1 parent 47eaac2 commit 2d6f34d

4 files changed

Lines changed: 1323 additions & 20 deletions

File tree

src/printer.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ function genericPrint(path, options, print) {
7272
'}',
7373
line
7474
]);
75-
case 'InheritanceSpecifier':
76-
return path.call(print, 'baseName');
75+
case 'InheritanceSpecifier': {
76+
let parts = [path.call(print, 'baseName')];
77+
78+
if (node.arguments && node.arguments.length) {
79+
parts.push('(');
80+
parts = parts.concat(path.map(print, 'arguments'));
81+
parts.push(')');
82+
}
83+
84+
return concat(parts);
85+
}
7786
case 'UsingForDeclaration':
7887
if (node.typeName) {
7988
return concat([
@@ -253,6 +262,9 @@ function genericPrint(path, options, print) {
253262
case 'EmitStatement':
254263
return concat(['emit ', path.call(print, 'eventCall'), ';']);
255264
case 'VariableDeclarationStatement': {
265+
const startsWithVar =
266+
node.variables.filter(x => x && x.typeName).length === 0;
267+
256268
doc = join(
257269
', ',
258270
path.map(statementPath => {
@@ -263,14 +275,18 @@ function genericPrint(path, options, print) {
263275
}, 'variables')
264276
);
265277

266-
if (node.variables.length > 1) {
278+
if (node.variables.length > 1 || startsWithVar) {
267279
doc = concat(['(', doc, ')']);
268280
}
269281

270282
if (node.initialValue) {
271283
doc = concat([doc, ' = ', path.call(print, 'initialValue')]);
272284
}
273-
return concat([doc, node.omitSemicolon ? '' : ';']);
285+
return concat([
286+
startsWithVar ? 'var ' : '',
287+
doc,
288+
node.omitSemicolon ? '' : ';'
289+
]);
274290
}
275291
case 'StateVariableDeclaration':
276292
doc = concat(
@@ -446,24 +462,26 @@ function genericPrint(path, options, print) {
446462
doc = join(' ', [doc, path.call(print, 'expression')]);
447463
}
448464
return concat([doc, ';']);
449-
case 'ModifierDefinition':
450-
if (
451-
node.parameters &&
452-
node.parameters.parameters &&
453-
node.parameters.parameters.length > 0
454-
) {
455-
doc = path.call(print, 'parameters');
465+
case 'ModifierDefinition': {
466+
let parts = ['modifier ', node.name];
467+
468+
if (node.parameters && node.parameters.parameters) {
469+
// if node.paremeters is an object, print parameter list
470+
parts.push('(');
471+
parts = parts.concat(path.call(print, 'parameters'));
472+
parts.push(') ');
473+
} else if (node.parameters && node.parameters.length === 0) {
474+
// if node.paremeters is an empty array, don't print parentheses
475+
parts.push(' ');
456476
} else {
457-
doc = '';
477+
// otherwise, throw a not implemented error
478+
throw new Error('[ModifierDefinition] Scenario not implemented');
458479
}
459-
return concat([
460-
'modifier ',
461-
node.name,
462-
'(',
463-
doc,
464-
') ',
465-
path.call(print, 'body')
466-
]);
480+
481+
parts.push(path.call(print, 'body'));
482+
483+
return concat(parts);
484+
}
467485
case 'InlineAssemblyStatement':
468486
// @TODO: add support for assembly language specifier
469487
return concat(['assembly ', path.call(print, 'body')]);
@@ -548,6 +566,8 @@ function genericPrint(path, options, print) {
548566
].filter(element => element)
549567
);
550568
}
569+
case 'ThrowStatement':
570+
return 'throw;';
551571
default:
552572
throw new Error(`Unknown type: ${JSON.stringify(node.type)}`);
553573
}

0 commit comments

Comments
 (0)