@putout/printer 1.1.1 → 1.1.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/lib/tokenize/expressions/call-expression.js +4 -1
- package/lib/tokenize/is.js +3 -0
- package/lib/tokenize/mark.js +21 -0
- package/lib/tokenize/statements/block-statement.js +3 -0
- package/lib/tokenize/statements/if-statement.js +3 -1
- package/lib/tokenize/statements/index.js +2 -19
- package/lib/tokenize/statements/return-statement.js +25 -0
- package/lib/tokenize/statements/variable-declaration.js +1 -0
- package/lib/tokenize/tokenize.js +2 -0
- package/package.json +1 -1
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {hasPrevNewline} = require('../mark');
|
|
4
|
+
|
|
3
5
|
const {entries} = Object;
|
|
4
6
|
|
|
5
7
|
module.exports.CallExpression = (path, {traverse, indent, write, incIndent, decIndent, maybeWrite}) => {
|
|
6
8
|
const isParentCall = toLong(path) && path.parentPath.isCallExpression();
|
|
7
9
|
|
|
8
|
-
if (shouldAddNewLine(path)) {
|
|
10
|
+
if (shouldAddNewLine(path) && !hasPrevNewline(path.parentPath)) {
|
|
9
11
|
write.newline();
|
|
10
12
|
write.indent();
|
|
11
13
|
}
|
|
@@ -75,3 +77,4 @@ function toLong(path) {
|
|
|
75
77
|
|
|
76
78
|
return false;
|
|
77
79
|
}
|
|
80
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const WATER_MARK = '__putout_newline';
|
|
4
|
+
|
|
5
|
+
module.exports.mark = mark;
|
|
6
|
+
|
|
7
|
+
function mark(path) {
|
|
8
|
+
path[WATER_MARK] = true;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports.isMarked = isMarked;
|
|
12
|
+
|
|
13
|
+
function isMarked(path) {
|
|
14
|
+
return path[WATER_MARK];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports.hasPrevNewline = (path) => {
|
|
18
|
+
return isMarked(path.getPrevSibling());
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
module.exports.maybeMark = (a, path) => a && mark(path);
|
|
@@ -5,6 +5,9 @@ const isFirstStatement = (path) => path.get('body.0')?.isStatement();
|
|
|
5
5
|
module.exports.BlockStatement = (path, {write, indent, traverse}) => {
|
|
6
6
|
const body = path.get('body');
|
|
7
7
|
|
|
8
|
+
if (path.parentPath.isBlockStatement())
|
|
9
|
+
indent();
|
|
10
|
+
|
|
8
11
|
indent.inc();
|
|
9
12
|
write('{');
|
|
10
13
|
|
|
@@ -5,6 +5,7 @@ const {VariableDeclaration} = require('./variable-declaration');
|
|
|
5
5
|
const {IfStatement} = require('./if-statement');
|
|
6
6
|
const {ForOfStatement} = require('./for-of-statement');
|
|
7
7
|
const {BlockStatement} = require('./block-statement');
|
|
8
|
+
const {ReturnStatement} = require('./return-statement');
|
|
8
9
|
|
|
9
10
|
module.exports = {
|
|
10
11
|
BlockStatement,
|
|
@@ -12,6 +13,7 @@ module.exports = {
|
|
|
12
13
|
VariableDeclaration,
|
|
13
14
|
IfStatement,
|
|
14
15
|
ForOfStatement,
|
|
16
|
+
ReturnStatement,
|
|
15
17
|
Program(path, {traverse}) {
|
|
16
18
|
path.get('body').forEach(traverse);
|
|
17
19
|
},
|
|
@@ -19,24 +21,5 @@ module.exports = {
|
|
|
19
21
|
indent();
|
|
20
22
|
write('continue;\n');
|
|
21
23
|
},
|
|
22
|
-
ReturnStatement(path, {indent, write, traverse}) {
|
|
23
|
-
if (path?.parentPath?.node?.body?.length > 2) {
|
|
24
|
-
write.indent();
|
|
25
|
-
write.newline();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
indent();
|
|
29
|
-
write('return');
|
|
30
|
-
|
|
31
|
-
const argPath = path.get('argument');
|
|
32
|
-
|
|
33
|
-
if (argPath.node) {
|
|
34
|
-
write(' ');
|
|
35
|
-
traverse(argPath);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
write(';');
|
|
39
|
-
write.newline();
|
|
40
|
-
},
|
|
41
24
|
};
|
|
42
25
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {isPrevBody} = require('../is');
|
|
4
|
+
const isBodyLength = ({parentPath}) => parentPath.node?.body?.length > 2;
|
|
5
|
+
|
|
6
|
+
module.exports.ReturnStatement = (path, {indent, write, traverse}) => {
|
|
7
|
+
if (isBodyLength(path) || isPrevBody(path)) {
|
|
8
|
+
write.indent();
|
|
9
|
+
write.newline();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
indent();
|
|
13
|
+
write('return');
|
|
14
|
+
|
|
15
|
+
const argPath = path.get('argument');
|
|
16
|
+
|
|
17
|
+
if (argPath.node) {
|
|
18
|
+
write(' ');
|
|
19
|
+
traverse(argPath);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
write(';');
|
|
23
|
+
write.newline();
|
|
24
|
+
};
|
|
25
|
+
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -6,6 +6,7 @@ const statements = require('./statements');
|
|
|
6
6
|
const literals = require('./literals');
|
|
7
7
|
const {TYPES} = require('../types');
|
|
8
8
|
const {createDebug} = require('./debug');
|
|
9
|
+
const {maybeMark} = require('./mark');
|
|
9
10
|
|
|
10
11
|
const {assign} = Object;
|
|
11
12
|
|
|
@@ -67,6 +68,7 @@ module.exports.tokenize = (ast) => {
|
|
|
67
68
|
const maybe = {
|
|
68
69
|
write: maybeWrite,
|
|
69
70
|
indent: maybeIndent,
|
|
71
|
+
mark: maybeMark,
|
|
70
72
|
};
|
|
71
73
|
|
|
72
74
|
const printer = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.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",
|