@putout/printer 1.6.2 → 1.6.4
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/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/statements/for-statement.js +36 -0
- package/lib/tokenize/statements/index.js +2 -0
- package/lib/tokenize/tokenize.js +7 -7
- package/package.json +1 -1
package/ChangeLog
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);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.ForStatement = (path, {print, maybe, indent}) => {
|
|
4
|
+
const {
|
|
5
|
+
test,
|
|
6
|
+
update,
|
|
7
|
+
body,
|
|
8
|
+
} = path.node;
|
|
9
|
+
|
|
10
|
+
print('for (');
|
|
11
|
+
print('__init');
|
|
12
|
+
print(';');
|
|
13
|
+
maybe.print(test, ' ');
|
|
14
|
+
print('__test');
|
|
15
|
+
print(';');
|
|
16
|
+
maybe.print(update, ' ');
|
|
17
|
+
print('__update');
|
|
18
|
+
print(')');
|
|
19
|
+
|
|
20
|
+
if (body.body) {
|
|
21
|
+
print(' ');
|
|
22
|
+
print('__body');
|
|
23
|
+
print.newline();
|
|
24
|
+
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!body.body) {
|
|
29
|
+
print.newline();
|
|
30
|
+
indent.inc();
|
|
31
|
+
print('__body');
|
|
32
|
+
indent.dec();
|
|
33
|
+
print.newline();
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
@@ -8,12 +8,14 @@ const {BlockStatement} = require('./block-statement');
|
|
|
8
8
|
const {ReturnStatement} = require('./return-statement');
|
|
9
9
|
const TryStatements = require('./try-statements');
|
|
10
10
|
const {DebuggerStatement} = require('./debugger-statement');
|
|
11
|
+
const {ForStatement} = require('./for-statement');
|
|
11
12
|
|
|
12
13
|
module.exports = {
|
|
13
14
|
BlockStatement,
|
|
14
15
|
ExpressionStatement,
|
|
15
16
|
VariableDeclaration,
|
|
16
17
|
IfStatement,
|
|
18
|
+
ForStatement,
|
|
17
19
|
ForOfStatement,
|
|
18
20
|
ReturnStatement,
|
|
19
21
|
DebuggerStatement,
|
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');
|
|
@@ -43,7 +45,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
43
45
|
});
|
|
44
46
|
};
|
|
45
47
|
|
|
46
|
-
const maybeWrite = (a, b) => a && write(b);
|
|
47
48
|
const maybeIndent = (a) => a && indent();
|
|
48
49
|
const maybeIndentInc = (a) => a && indent.inc();
|
|
49
50
|
const maybeIndentDec = (a) => a && indent.dec();
|
|
@@ -94,7 +95,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
94
95
|
});
|
|
95
96
|
|
|
96
97
|
const maybe = {
|
|
97
|
-
write: maybeWrite,
|
|
98
98
|
indent: maybeIndent,
|
|
99
99
|
markBefore: maybeMarkBefore,
|
|
100
100
|
markAfter: maybeMarkAfter,
|
|
@@ -110,7 +110,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
110
110
|
decIndent,
|
|
111
111
|
indent,
|
|
112
112
|
write,
|
|
113
|
-
maybeWrite,
|
|
114
113
|
debug,
|
|
115
114
|
maybeIndent,
|
|
116
115
|
traverse,
|
|
@@ -178,11 +177,12 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
|
|
|
178
177
|
if (maybeLine === path)
|
|
179
178
|
return null;
|
|
180
179
|
|
|
181
|
-
if (
|
|
182
|
-
return traverse(maybeLine);
|
|
183
|
-
|
|
184
|
-
if (maybeLine.startsWith(GET))
|
|
180
|
+
if (isString(maybeLine) && maybeLine.startsWith(GET))
|
|
185
181
|
return traverse(get(path, maybeLine));
|
|
186
182
|
|
|
183
|
+
if (isObject(maybeLine))
|
|
184
|
+
return traverse(maybeLine);
|
|
185
|
+
|
|
187
186
|
return write(maybeLine);
|
|
188
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.4",
|
|
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",
|