@putout/printer 1.6.1 → 1.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ChangeLog +10 -0
- package/README.md +3 -1
- package/lib/tokenize/expressions/member-expressions.js +12 -15
- package/lib/tokenize/expressions/new-expression.js +7 -7
- package/lib/tokenize/expressions/object-expression.js +16 -16
- package/lib/tokenize/expressions/rest-element.js +3 -3
- package/lib/tokenize/expressions/sequence-expression.js +4 -4
- package/lib/tokenize/expressions/spread-element.js +4 -3
- package/lib/tokenize/expressions/unary-expressions.js +18 -21
- package/lib/tokenize/literals/index.js +7 -8
- package/lib/tokenize/tokenize.js +20 -12
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/README.md
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
module.exports.MemberExpression = (path, {
|
|
3
|
+
module.exports.MemberExpression = (path, {print}) => {
|
|
4
4
|
const {computed} = path.node;
|
|
5
|
-
const propertyPath = path.get('property');
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
print('__object');
|
|
8
7
|
|
|
9
8
|
if (computed) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
print('[');
|
|
10
|
+
print('__property');
|
|
11
|
+
print(']');
|
|
13
12
|
|
|
14
13
|
return;
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
print('.');
|
|
17
|
+
print('__property');
|
|
19
18
|
};
|
|
20
19
|
|
|
21
|
-
module.exports.OptionalMemberExpression = (path, {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
write('?.');
|
|
27
|
-
traverse(propertyPath);
|
|
20
|
+
module.exports.OptionalMemberExpression = (path, {print}) => {
|
|
21
|
+
print('__object');
|
|
22
|
+
print('?.');
|
|
23
|
+
print('__property');
|
|
28
24
|
};
|
|
25
|
+
|
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
const {entries} = Object;
|
|
4
4
|
|
|
5
|
-
module.exports.NewExpression = (path, {
|
|
5
|
+
module.exports.NewExpression = (path, {indent, print, maybe}) => {
|
|
6
6
|
indent();
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
print('new ');
|
|
8
|
+
print('__callee');
|
|
9
9
|
|
|
10
10
|
const args = path.get('arguments');
|
|
11
|
-
|
|
11
|
+
print('(');
|
|
12
12
|
|
|
13
13
|
const n = args.length - 1;
|
|
14
14
|
|
|
15
15
|
for (const [i, arg] of entries(args)) {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
print(arg);
|
|
17
|
+
maybe.print(i < n, ', ');
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
print(')');
|
|
21
21
|
};
|
|
22
22
|
|
|
@@ -9,7 +9,7 @@ const isForOf = (path) => {
|
|
|
9
9
|
return path.parentPath?.parentPath?.isForOfStatement();
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
module.exports.ObjectExpression = (path, {
|
|
12
|
+
module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
|
|
13
13
|
indent.inc();
|
|
14
14
|
|
|
15
15
|
const properties = path.get('properties');
|
|
@@ -17,19 +17,19 @@ module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
|
|
|
17
17
|
const parens = isBodyOfArrow(path);
|
|
18
18
|
const manyLines = !isOneLine(path);
|
|
19
19
|
|
|
20
|
-
maybe.
|
|
21
|
-
|
|
20
|
+
maybe.print(parens, '(');
|
|
21
|
+
print('{');
|
|
22
22
|
|
|
23
|
-
maybe.
|
|
23
|
+
maybe.print(manyLines, '\n');
|
|
24
24
|
|
|
25
25
|
for (const property of properties) {
|
|
26
26
|
if (property.isSpreadElement()) {
|
|
27
27
|
maybe.indent(length > 1);
|
|
28
|
-
|
|
28
|
+
print(property);
|
|
29
29
|
|
|
30
30
|
if (length > 1) {
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
print(',');
|
|
32
|
+
print.newline();
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
continue;
|
|
@@ -40,27 +40,27 @@ module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
|
|
|
40
40
|
maybe.indent(manyLines);
|
|
41
41
|
|
|
42
42
|
if (property.isObjectMethod()) {
|
|
43
|
-
|
|
43
|
+
print(property);
|
|
44
44
|
continue;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
maybe.
|
|
48
|
-
|
|
49
|
-
maybe.
|
|
47
|
+
maybe.print(computed, '[');
|
|
48
|
+
print(property.get('key'));
|
|
49
|
+
maybe.print(computed, ']');
|
|
50
50
|
|
|
51
51
|
if (!shorthand) {
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
print(': ');
|
|
53
|
+
print(property.get('value'));
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
maybe.
|
|
56
|
+
maybe.print(manyLines, ',\n');
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
indent.dec();
|
|
60
60
|
|
|
61
61
|
maybe.indent(manyLines);
|
|
62
|
-
|
|
63
|
-
maybe.
|
|
62
|
+
print('}');
|
|
63
|
+
maybe.print(parens, ')');
|
|
64
64
|
};
|
|
65
65
|
function isOneLine(path) {
|
|
66
66
|
const isCall = path.parentPath.isCallExpression();
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
const {entries} = Object;
|
|
4
4
|
|
|
5
|
-
module.exports.SequenceExpression = (path, {maybe,
|
|
5
|
+
module.exports.SequenceExpression = (path, {maybe, print}) => {
|
|
6
6
|
const expressions = path.get('expressions');
|
|
7
7
|
const n = expressions.length - 1;
|
|
8
8
|
|
|
9
9
|
for (const [index, expression] of entries(expressions)) {
|
|
10
|
-
|
|
11
|
-
maybe.
|
|
12
|
-
maybe.
|
|
10
|
+
print(expression);
|
|
11
|
+
maybe.print(index < n, ',');
|
|
12
|
+
maybe.print(index < n, ' ');
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
15
|
|
|
@@ -1,49 +1,46 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
module.exports.UnaryExpression =
|
|
4
|
-
module.exports.UpdateExpression =
|
|
5
|
-
module.exports.AwaitExpression = (path, {
|
|
3
|
+
module.exports.UnaryExpression = unaryExpression;
|
|
4
|
+
module.exports.UpdateExpression = unaryExpression;
|
|
5
|
+
module.exports.AwaitExpression = (path, {print}) => {
|
|
6
6
|
printUnary(path, 'await', {
|
|
7
|
-
|
|
8
|
-
traverse,
|
|
7
|
+
print,
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
module.exports.YieldExpression = (path, {
|
|
10
|
+
module.exports.YieldExpression = (path, {print}) => {
|
|
12
11
|
printUnary(path, 'yield', {
|
|
13
|
-
|
|
14
|
-
traverse,
|
|
12
|
+
print,
|
|
15
13
|
});
|
|
16
14
|
};
|
|
17
15
|
|
|
18
|
-
module.exports.ThrowStatement = (path, {
|
|
16
|
+
module.exports.ThrowStatement = (path, {print, indent}) => {
|
|
19
17
|
indent();
|
|
20
18
|
|
|
21
19
|
printUnary(path, 'throw', {
|
|
22
|
-
|
|
23
|
-
traverse,
|
|
20
|
+
print,
|
|
24
21
|
});
|
|
25
22
|
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
print(';');
|
|
24
|
+
print.newline();
|
|
28
25
|
};
|
|
29
26
|
|
|
30
|
-
function printUnary(path, name, {
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
function printUnary(path, name, {print}) {
|
|
28
|
+
print(`${name} `);
|
|
29
|
+
print('__argument');
|
|
33
30
|
}
|
|
34
31
|
|
|
35
32
|
const isWord = (a) => /delete|typeof/.test(a);
|
|
36
33
|
|
|
37
|
-
function
|
|
34
|
+
function unaryExpression(path, {print, maybe}) {
|
|
38
35
|
const {prefix, operator} = path.node;
|
|
39
36
|
|
|
40
37
|
if (prefix)
|
|
41
|
-
|
|
38
|
+
print(operator);
|
|
42
39
|
|
|
43
|
-
maybe.
|
|
44
|
-
|
|
40
|
+
maybe.print(isWord(operator), ' ');
|
|
41
|
+
print('__argument');
|
|
45
42
|
|
|
46
43
|
if (!prefix)
|
|
47
|
-
|
|
44
|
+
print(operator);
|
|
48
45
|
}
|
|
49
46
|
|
|
@@ -4,17 +4,16 @@ const {TemplateLiteral} = require('./template-literal');
|
|
|
4
4
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
TemplateLiteral,
|
|
7
|
-
NumericLiteral(path, {
|
|
8
|
-
const {raw} = path.node;
|
|
9
|
-
|
|
10
|
-
//write(raw || extra.raw);
|
|
7
|
+
NumericLiteral(path, {print}) {
|
|
8
|
+
const {raw, extra} = path.node;
|
|
9
|
+
print(raw || extra.raw);
|
|
11
10
|
},
|
|
12
|
-
BooleanLiteral(path, {
|
|
13
|
-
|
|
11
|
+
BooleanLiteral(path, {print}) {
|
|
12
|
+
print(path.node.value);
|
|
14
13
|
},
|
|
15
|
-
StringLiteral(path, {
|
|
14
|
+
StringLiteral(path, {print}) {
|
|
16
15
|
const {value} = path.node;
|
|
17
|
-
|
|
16
|
+
print(`'${value}'`);
|
|
18
17
|
},
|
|
19
18
|
Identifier(path, {write}) {
|
|
20
19
|
write(path.node.name);
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const isObject = (a) => a && typeof a === 'object';
|
|
4
|
+
|
|
3
5
|
const babelTraverse = require('@babel/traverse').default;
|
|
4
6
|
const expressions = require('./expressions');
|
|
5
7
|
const statements = require('./statements');
|
|
@@ -25,7 +27,15 @@ const traversers = {
|
|
|
25
27
|
const GET = '__';
|
|
26
28
|
const get = (path, command) => path.get(command.replace(GET, ''));
|
|
27
29
|
|
|
30
|
+
function initFormat(format) {
|
|
31
|
+
return {
|
|
32
|
+
...format,
|
|
33
|
+
indent: ' ',
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
28
37
|
module.exports.tokenize = (ast, overrides = {}) => {
|
|
38
|
+
const format = initFormat(overrides.format);
|
|
29
39
|
const tokens = [];
|
|
30
40
|
const debug = createDebug(tokens);
|
|
31
41
|
const write = (value) => {
|
|
@@ -35,7 +45,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
35
45
|
});
|
|
36
46
|
};
|
|
37
47
|
|
|
38
|
-
const maybeWrite = (a, b) => a && write(b);
|
|
39
48
|
const maybeIndent = (a) => a && indent();
|
|
40
49
|
const maybeIndentInc = (a) => a && indent.inc();
|
|
41
50
|
const maybeIndentDec = (a) => a && indent.dec();
|
|
@@ -48,7 +57,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
48
57
|
const indent = () => {
|
|
49
58
|
tokens.push({
|
|
50
59
|
type: TYPES.INDENT,
|
|
51
|
-
value: printIndent(i),
|
|
60
|
+
value: printIndent(i, format.indent),
|
|
52
61
|
});
|
|
53
62
|
};
|
|
54
63
|
|
|
@@ -60,14 +69,14 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
60
69
|
const linebreak = () => {
|
|
61
70
|
tokens.push({
|
|
62
71
|
type: TYPES.NEWLINE,
|
|
63
|
-
value: `${printIndent(i)}\n`,
|
|
72
|
+
value: `${printIndent(i, format.indent)}\n`,
|
|
64
73
|
});
|
|
65
74
|
};
|
|
66
75
|
|
|
67
76
|
const breakline = () => {
|
|
68
77
|
tokens.push({
|
|
69
78
|
type: TYPES.NEWLINE,
|
|
70
|
-
value: `\n${printIndent(i)}`,
|
|
79
|
+
value: `\n${printIndent(i, format.indent)}`,
|
|
71
80
|
});
|
|
72
81
|
};
|
|
73
82
|
|
|
@@ -86,7 +95,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
86
95
|
});
|
|
87
96
|
|
|
88
97
|
const maybe = {
|
|
89
|
-
write: maybeWrite,
|
|
90
98
|
indent: maybeIndent,
|
|
91
99
|
markBefore: maybeMarkBefore,
|
|
92
100
|
markAfter: maybeMarkAfter,
|
|
@@ -102,7 +110,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
102
110
|
decIndent,
|
|
103
111
|
indent,
|
|
104
112
|
write,
|
|
105
|
-
maybeWrite,
|
|
106
113
|
debug,
|
|
107
114
|
maybeIndent,
|
|
108
115
|
traverse,
|
|
@@ -155,12 +162,12 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
155
162
|
return tokens;
|
|
156
163
|
};
|
|
157
164
|
|
|
158
|
-
function printIndent(i) {
|
|
165
|
+
function printIndent(i, indent) {
|
|
159
166
|
let result = '';
|
|
160
167
|
++i;
|
|
161
168
|
|
|
162
169
|
while (--i) {
|
|
163
|
-
result +=
|
|
170
|
+
result += indent;
|
|
164
171
|
}
|
|
165
172
|
|
|
166
173
|
return result;
|
|
@@ -170,11 +177,12 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
|
|
|
170
177
|
if (maybeLine === path)
|
|
171
178
|
return null;
|
|
172
179
|
|
|
173
|
-
if (
|
|
174
|
-
return traverse(maybeLine);
|
|
175
|
-
|
|
176
|
-
if (maybeLine.startsWith(GET))
|
|
180
|
+
if (isString(maybeLine) && maybeLine.startsWith(GET))
|
|
177
181
|
return traverse(get(path, maybeLine));
|
|
178
182
|
|
|
183
|
+
if (isObject(maybeLine))
|
|
184
|
+
return traverse(maybeLine);
|
|
185
|
+
|
|
179
186
|
return write(maybeLine);
|
|
180
187
|
};
|
|
188
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.3",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",
|