Skip to content

Commit

Permalink
Refactor (#841)
Browse files Browse the repository at this point in the history
* Refactor Mapping with named parameters

* Refactor StructDefinition

* Refactor UsingForDeclaration with User-Defined Operators

* using array instead of template literals
  • Loading branch information
Janther committed Apr 25, 2023
1 parent a63cd3e commit b5826b8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 61 deletions.
31 changes: 16 additions & 15 deletions src/nodes/Mapping.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const namedParameter = (prefix, node, path, print) =>
node[`${prefix}Name`]
? [
path.call(print, `${prefix}Type`),
' ',
path.call(print, `${prefix}Name`)
]
: path.call(print, `${prefix}Type`);

const Mapping = {
print: ({ path, print }) => {
const keyType = path.call(print, 'keyType');
const keyName = path.call(print, 'keyName');
const valueType = path.call(print, 'valueType');
const valueName = path.call(print, 'valueName');
return [
'mapping(',
keyType,
keyName ? ` ${keyName}` : '',
' => ',
valueType,
valueName ? ` ${valueName}` : '',
')'
];
}
print: ({ node, path, print }) => [
'mapping(',
namedParameter('key', node, path, print),
' => ',
namedParameter('value', node, path, print),
')'
]
};

module.exports = Mapping;
22 changes: 9 additions & 13 deletions src/nodes/StructDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ const {
const { printSeparatedList } = require('../common/printer-helpers');

const StructDefinition = {
print: ({ node, path, print }) => {
const parts = ['struct ', node.name, ' {'];

if (node.members.length > 0) {
parts.push(
printSeparatedList(path.map(print, 'members'), {
print: ({ node, path, print }) => [
'struct ',
node.name,
' {',
node.members.length > 0
? printSeparatedList(path.map(print, 'members'), {
firstSeparator: hardline,
separator: [';', hardline],
lastSeparator: [';', hardline]
})
);
}

parts.push('}');

return parts;
}
: '',
'}'
]
};

module.exports = StructDefinition;
55 changes: 22 additions & 33 deletions src/nodes/UsingForDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,28 @@ const {
const { printSeparatedList } = require('../common/printer-helpers');

const UsingForDeclaration = {
print: ({ node, path, print, options }) => {
const parts = ['using '];

if (node.functions && node.functions.length) {
const importedFunctions = [];
for (let i = 0; i < node.functions.length; i += 1) {
const fun = node.functions[i];
const operator = node.operators[i];

if (operator) {
importedFunctions.push(`${fun} as ${operator}`);
} else {
importedFunctions.push(fun);
}
}

parts.push('{');
parts.push(
printSeparatedList(importedFunctions, {
firstSeparator: options.bracketSpacing ? line : softline
})
);
parts.push('}');
} else {
parts.push(node.libraryName);
}

parts.push(' for ');
parts.push(node.typeName ? path.call(print, 'typeName') : '*');
parts.push(node.isGlobal ? ' global;' : ';');

return parts;
}
print: ({ node, path, print, options }) => [
'using ',
node.functions && node.functions.length
? [
'{',
printSeparatedList(
node.functions.map((functionName, i) =>
node.operators[i]
? [functionName, ' as ', node.operators[i]]
: functionName
),
{
firstSeparator: options.bracketSpacing ? line : softline
}
),
'}'
]
: node.libraryName,
' for ',
node.typeName ? path.call(print, 'typeName') : '*',
node.isGlobal ? ' global;' : ';'
]
};

module.exports = UsingForDeclaration;

0 comments on commit b5826b8

Please sign in to comment.