Skip to content

Commit 592ce9a

Browse files
Janthermattiaerre
authored andcommitted
Manageable printing Rules (#121)
* separate node types into files * s/reduce/map * map reduce no more * s/const/function * s/try...catch/if...else * 🤦 * __dirname * fix bad merge * moar nodes * update dir-to-object * update nodes * The logic of each rule has remained unchanged. It was only split in smaller and more manageable files. * Following linting rules * removing unused vars * disabling operator-linebreak and implicit-arrow-linebreak that conflicted with prettier.js * bump version
1 parent f80a9a8 commit 592ce9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1248
-643
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`nodes list to match snapshot 1`] = `
4+
Array [
5+
"ArrayTypeName",
6+
"AssemblyAssignment",
7+
"AssemblyBlock",
8+
"AssemblyCall",
9+
"AssemblyCase",
10+
"AssemblyFor",
11+
"AssemblyIf",
12+
"AssemblyLocalDefinition",
13+
"AssemblySwitch",
14+
"BinaryOperation",
15+
"Block",
16+
"BooleanLiteral",
17+
"BreakStatement",
18+
"Conditional",
19+
"ContinueStatement",
20+
"ContractDefinition",
21+
"DecimalNumber",
22+
"ElementaryTypeName",
23+
"ElementaryTypeNameExpression",
24+
"EmitStatement",
25+
"EnumDefinition",
26+
"EnumValue",
27+
"EventDefinition",
28+
"ExpressionStatement",
29+
"ForStatement",
30+
"FunctionCall",
31+
"FunctionDefinition",
32+
"FunctionTypeName",
33+
"HexLiteral",
34+
"HexNumber",
35+
"Identifier",
36+
"IfStatement",
37+
"ImportDirective",
38+
"IndexAccess",
39+
"InheritanceSpecifier",
40+
"InlineAssemblyStatement",
41+
"LabelDefinition",
42+
"Mapping",
43+
"MemberAccess",
44+
"ModifierDefinition",
45+
"ModifierInvocation",
46+
"NewExpression",
47+
"NumberLiteral",
48+
"Parameter",
49+
"ParameterList",
50+
"PragmaDirective",
51+
"ReturnStatement",
52+
"SourceUnit",
53+
"StateVariableDeclaration",
54+
"StringLiteral",
55+
"StructDefinition",
56+
"ThrowStatement",
57+
"TupleExpression",
58+
"UnaryOperation",
59+
"UserDefinedTypeName",
60+
"UsingForDeclaration",
61+
"VariableDeclaration",
62+
"VariableDeclarationStatement",
63+
"WhileStatement",
64+
]
65+
`;

__tests__/nodes/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const nodes = require('../../src/nodes');
2+
3+
test('nodes list to match snapshot', () => {
4+
expect(Object.keys(nodes)).toMatchSnapshot();
5+
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prettier-plugin-solidity",
3-
"version": "1.0.0-alpha.18",
3+
"version": "1.0.0-alpha.19",
44
"description": "prettier plugin for solidity",
55
"main": "src",
66
"scripts": {
@@ -59,6 +59,7 @@
5959
"jest-watch-typeahead": "^0.2.0"
6060
},
6161
"dependencies": {
62+
"dir-to-object": "^2.0.0",
6263
"emoji-regex": "^7.0.3",
6364
"escape-string-regexp": "^1.0.5",
6465
"extract-comments": "^1.1.0",

src/clean.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// eslint-disable-next-line no-unused-vars
2-
const clean = (ast, newObj, parent) => {
2+
function clean(ast, newObj, parent) {
33
['code', 'codeStart', 'loc', 'range'].forEach(name => {
44
delete newObj[name]; // eslint-disable-line no-param-reassign
55
});
6-
};
6+
}
77

88
module.exports = clean;

src/loc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// see: https://github.com/prettier/prettier/blob/master/src/language-js/loc.js
22

3-
const getRange = (index, node) => {
3+
function getRange(index, node) {
44
if (node.range) {
55
return node.range[index];
66
}
77
if (node.expression.range) {
88
return node.expression.range[index];
99
}
1010
return null;
11-
};
11+
}
1212

1313
module.exports = {
1414
locEnd: node => getRange(1, node),

src/nodes/ArrayTypeName.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable operator-linebreak */
2+
const {
3+
doc: {
4+
builders: { concat }
5+
}
6+
} = require('prettier');
7+
8+
const ArrayTypeName = {
9+
print: ({ node, path, print }) => {
10+
let stateMutability = '';
11+
if (
12+
node.baseTypeName.name === 'address' &&
13+
node.baseTypeName.stateMutability
14+
) {
15+
stateMutability = concat([' ', node.baseTypeName.stateMutability]);
16+
}
17+
return concat([
18+
path.call(print, 'baseTypeName'),
19+
stateMutability,
20+
'[',
21+
node.length ? path.call(print, 'length') : '',
22+
']'
23+
]);
24+
}
25+
};
26+
27+
module.exports = ArrayTypeName;

src/nodes/AssemblyAssignment.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable implicit-arrow-linebreak */
2+
const {
3+
doc: {
4+
builders: { join }
5+
}
6+
} = require('prettier');
7+
8+
const AssemblyAssignment = {
9+
print: ({ path, print }) =>
10+
join(' ', [
11+
join(', ', path.map(print, 'names')),
12+
':=',
13+
path.call(print, 'expression')
14+
])
15+
};
16+
17+
module.exports = AssemblyAssignment;

src/nodes/AssemblyBlock.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable implicit-arrow-linebreak */
2+
const {
3+
doc: {
4+
builders: { concat, hardline, indent }
5+
}
6+
} = require('prettier');
7+
8+
const printPreservingEmptyLines = require('./print-preserving-empty-lines');
9+
10+
const AssemblyBlock = {
11+
print: ({ options, path, print }) =>
12+
concat([
13+
'{',
14+
indent(hardline),
15+
indent(printPreservingEmptyLines(path, 'operations', options, print)),
16+
hardline,
17+
'}'
18+
])
19+
};
20+
21+
module.exports = AssemblyBlock;

src/nodes/AssemblyCall.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const {
2+
doc: {
3+
builders: { concat, group, indent, join, line, softline }
4+
}
5+
} = require('prettier');
6+
7+
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
24+
])
25+
),
26+
')'
27+
]);
28+
}
29+
};
30+
31+
module.exports = AssemblyCall;

src/nodes/AssemblyCase.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const {
2+
doc: {
3+
builders: { concat, join }
4+
}
5+
} = require('prettier');
6+
7+
const AssemblyCase = {
8+
print: ({ node, path, print }) => {
9+
let doc;
10+
11+
if (node.default) {
12+
doc = concat(['default']);
13+
} else {
14+
doc = concat(['case ', path.call(print, 'value')]);
15+
}
16+
return join(' ', [doc, path.call(print, 'block')]);
17+
}
18+
};
19+
20+
module.exports = AssemblyCase;

0 commit comments

Comments
 (0)