@putout/printer 1.3.1 → 1.4.0
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 +4 -0
- package/README.md +1 -1
- package/lib/tokenize/comments.js +17 -0
- package/lib/tokenize/expressions/call-expression.js +5 -2
- package/lib/tokenize/expressions/functions.js +2 -1
- package/lib/tokenize/expressions/index.js +2 -2
- package/lib/tokenize/expressions/object-expression.js +3 -1
- package/lib/tokenize/expressions/unary-expressions.js +3 -0
- package/lib/tokenize/mark.js +26 -11
- package/lib/tokenize/statements/expression-statement.js +12 -4
- package/lib/tokenize/statements/for-of-statement.js +2 -2
- package/lib/tokenize/statements/variable-declaration.js +1 -1
- package/lib/tokenize/tokenize.js +8 -2
- package/package.json +2 -4
- /package/lib/tokenize/expressions/{RestElement.js → rest-element.js} +0 -0
- /package/lib/tokenize/expressions/{SpreadElement.js → spread-element.js} +0 -0
package/ChangeLog
ADDED
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/printer.svg?style=flat&longCache=true
|
|
4
4
|
[NPMURL]: https://npmjs.org/package/@putout/printer "npm"
|
|
5
5
|
|
|
6
|
-
**
|
|
6
|
+
**@putout/printer** prints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readable **JavaScript**.
|
|
7
7
|
|
|
8
8
|
- ☝️ Similar to **Recast**, but simpler and easier in maintenance, since it supports only **Babel**.
|
|
9
9
|
- ☝️ As opinionated as **Prettier**, but has more user-friendly output and works directly with **AST**.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.parseComments = (path, {write}) => {
|
|
4
|
+
const {leadingComments} = path.node;
|
|
5
|
+
|
|
6
|
+
if (leadingComments)
|
|
7
|
+
parseLeadingComments(path, {write});
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
function parseLeadingComments(path, {write}) {
|
|
11
|
+
const {leadingComments} = path.node;
|
|
12
|
+
|
|
13
|
+
for (const {value}of leadingComments) {
|
|
14
|
+
write(`//${value}`);
|
|
15
|
+
write.newline();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {
|
|
4
|
+
hasPrevNewline,
|
|
5
|
+
isMarkedParentBefore,
|
|
6
|
+
} = require('../mark');
|
|
4
7
|
|
|
5
8
|
const {entries} = Object;
|
|
6
9
|
|
|
7
10
|
module.exports.CallExpression = (path, {traverse, indent, write, incIndent, decIndent, maybeWrite}) => {
|
|
8
11
|
const isParentCall = toLong(path) && path.parentPath.isCallExpression();
|
|
9
12
|
|
|
10
|
-
if (shouldAddNewLine(path) && !hasPrevNewline(path.parentPath) && !
|
|
13
|
+
if (shouldAddNewLine(path) && !hasPrevNewline(path.parentPath) && !isMarkedParentBefore(path)) {
|
|
11
14
|
write.newline();
|
|
12
15
|
write.indent();
|
|
13
16
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {isMarkedPrevAfter} = require('../mark');
|
|
3
4
|
const isFirst = (path) => path.node === path.parentPath.node.body[0];
|
|
4
5
|
|
|
5
6
|
module.exports.ArrowFunctionExpression = (path, {write, traverse}) => {
|
|
@@ -38,7 +39,7 @@ module.exports.ObjectMethod = (path, {write, traverse}) => {
|
|
|
38
39
|
};
|
|
39
40
|
|
|
40
41
|
module.exports.FunctionDeclaration = (path, {write, traverse}) => {
|
|
41
|
-
if (!isFirst(path))
|
|
42
|
+
if (!isFirst(path) && !isMarkedPrevAfter(path))
|
|
42
43
|
write('\n');
|
|
43
44
|
|
|
44
45
|
write('function ');
|
|
@@ -13,8 +13,8 @@ const {AssignmentExpression} = require('./assignment-expression');
|
|
|
13
13
|
const {ArrayExpression} = require('./array-expression');
|
|
14
14
|
const {ArrayPattern} = require('./array-pattern');
|
|
15
15
|
const {AssignmentPattern} = require('./assignment-pattern');
|
|
16
|
-
const {RestElement} = require('./
|
|
17
|
-
const {SpreadElement} = require('./
|
|
16
|
+
const {RestElement} = require('./rest-element');
|
|
17
|
+
const {SpreadElement} = require('./spread-element');
|
|
18
18
|
|
|
19
19
|
module.exports = {
|
|
20
20
|
...functions,
|
|
@@ -11,6 +11,8 @@ const isForOf = (path) => {
|
|
|
11
11
|
return false;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
+
const isFirstMethod = (path) => path.get('properties.0').isObjectMethod();
|
|
15
|
+
|
|
14
16
|
module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
|
|
15
17
|
indent.inc();
|
|
16
18
|
|
|
@@ -18,7 +20,7 @@ module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
|
|
|
18
20
|
const parens = isBodyOfArrow(path);
|
|
19
21
|
const isCall = path.parentPath.isCallExpression();
|
|
20
22
|
const {length} = properties;
|
|
21
|
-
const isOneLine = !length ||
|
|
23
|
+
const isOneLine = !length || isCall && length < 2 && !isFirstMethod(path) || isForOf(path);
|
|
22
24
|
|
|
23
25
|
maybe.write(parens, '(');
|
|
24
26
|
write('{');
|
package/lib/tokenize/mark.js
CHANGED
|
@@ -1,25 +1,40 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const WATER_MARK_BEFORE = '__putout_newline_before';
|
|
4
|
+
const WATER_MARK_AFTER = '__putout_newline_after';
|
|
4
5
|
|
|
5
|
-
module.exports.
|
|
6
|
+
module.exports.markBefore = markBefore;
|
|
7
|
+
module.exports.markAfter = markAfter;
|
|
6
8
|
|
|
7
|
-
function
|
|
8
|
-
path[
|
|
9
|
+
function markBefore(path) {
|
|
10
|
+
path[WATER_MARK_BEFORE] = true;
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
function markAfter(path) {
|
|
14
|
+
path[WATER_MARK_AFTER] = true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports.isMarkedBefore = isMarkedBefore;
|
|
18
|
+
module.exports.isMarkedAfter = isMarkedAfter;
|
|
19
|
+
|
|
20
|
+
function isMarkedBefore(path) {
|
|
21
|
+
return path[WATER_MARK_BEFORE];
|
|
22
|
+
}
|
|
12
23
|
|
|
13
|
-
function
|
|
14
|
-
return path[
|
|
24
|
+
function isMarkedAfter(path) {
|
|
25
|
+
return path[WATER_MARK_AFTER];
|
|
15
26
|
}
|
|
16
27
|
|
|
17
28
|
module.exports.hasPrevNewline = (path) => {
|
|
18
|
-
return
|
|
29
|
+
return isMarkedAfter(path.getPrevSibling());
|
|
19
30
|
};
|
|
20
31
|
|
|
21
|
-
module.exports.
|
|
32
|
+
module.exports.maybeMarkAfter = (a, path) => a && markAfter(path);
|
|
33
|
+
|
|
34
|
+
module.exports.isMarkedParentBefore = (path) => {
|
|
35
|
+
return isMarkedBefore(path.parentPath);
|
|
36
|
+
};
|
|
22
37
|
|
|
23
|
-
module.exports.
|
|
24
|
-
return
|
|
38
|
+
module.exports.isMarkedPrevAfter = (path) => {
|
|
39
|
+
return isMarkedAfter(path.getPrevSibling());
|
|
25
40
|
};
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {
|
|
4
|
+
markBefore,
|
|
5
|
+
markAfter,
|
|
6
|
+
isMarkedPrevAfter,
|
|
7
|
+
} = require('../mark');
|
|
8
|
+
const {isNext} = require('../is');
|
|
9
|
+
|
|
4
10
|
module.exports.ExpressionStatement = (path, {write, indent, traverse}) => {
|
|
5
|
-
if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path)) {
|
|
11
|
+
if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path) && !isMarkedPrevAfter(path)) {
|
|
6
12
|
write.linebreak();
|
|
7
|
-
|
|
13
|
+
markBefore(path);
|
|
8
14
|
}
|
|
9
15
|
|
|
10
16
|
const expressionPath = path.get('expression');
|
|
@@ -14,8 +20,10 @@ module.exports.ExpressionStatement = (path, {write, indent, traverse}) => {
|
|
|
14
20
|
write(';');
|
|
15
21
|
write.newline();
|
|
16
22
|
|
|
17
|
-
if (isStrictMode(path))
|
|
23
|
+
if (isStrictMode(path) || isCoupleLinesExpression(path) && isNext(path)) {
|
|
18
24
|
write.newline();
|
|
25
|
+
markAfter(path);
|
|
26
|
+
}
|
|
19
27
|
};
|
|
20
28
|
|
|
21
29
|
function isCoupleLinesExpression(path) {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {isMarkedAfter} = require('../mark');
|
|
4
4
|
const isPrevNewLine = (path) => {
|
|
5
5
|
const prev = path.getPrevSibling();
|
|
6
6
|
|
|
7
7
|
if (!prev.node)
|
|
8
8
|
return true;
|
|
9
9
|
|
|
10
|
-
return
|
|
10
|
+
return isMarkedAfter(prev);
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
module.exports.ForOfStatement = (path, {write, indent, traverse}) => {
|
|
@@ -31,7 +31,7 @@ module.exports.VariableDeclaration = (path, {write, maybe, maybeIndent, traverse
|
|
|
31
31
|
|
|
32
32
|
maybe.indent(is);
|
|
33
33
|
maybe.write(is, '\n');
|
|
34
|
-
maybe.
|
|
34
|
+
maybe.markAfter(is, path);
|
|
35
35
|
};
|
|
36
36
|
function isCoupleLinesExpression(path) {
|
|
37
37
|
const start = path.node?.loc?.start.line;
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -6,7 +6,11 @@ const statements = require('./statements');
|
|
|
6
6
|
const literals = require('./literals');
|
|
7
7
|
const {TYPES} = require('../types');
|
|
8
8
|
const {createDebug} = require('./debug');
|
|
9
|
-
const {
|
|
9
|
+
const {
|
|
10
|
+
maybeMarkAfter,
|
|
11
|
+
maybeMarkBefore,
|
|
12
|
+
} = require('./mark');
|
|
13
|
+
const {parseComments} = require('./comments');
|
|
10
14
|
|
|
11
15
|
const {assign} = Object;
|
|
12
16
|
|
|
@@ -68,7 +72,8 @@ module.exports.tokenize = (ast) => {
|
|
|
68
72
|
const maybe = {
|
|
69
73
|
write: maybeWrite,
|
|
70
74
|
indent: maybeIndent,
|
|
71
|
-
|
|
75
|
+
markBefore: maybeMarkBefore,
|
|
76
|
+
markAfter: maybeMarkAfter,
|
|
72
77
|
};
|
|
73
78
|
|
|
74
79
|
const printer = {
|
|
@@ -101,6 +106,7 @@ module.exports.tokenize = (ast) => {
|
|
|
101
106
|
if (!currentTraverse)
|
|
102
107
|
throw Error(`Node type '${type}' is not supported yet: '${path}'`);
|
|
103
108
|
|
|
109
|
+
parseComments(path, printer);
|
|
104
110
|
currentTraverse(path, printer);
|
|
105
111
|
debug(path.type);
|
|
106
112
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
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",
|
|
7
7
|
"homepage": "https://github.com/putoutjs/printer#readme",
|
|
8
8
|
"main": "./lib/printer.js",
|
|
9
|
-
"release": false,
|
|
10
|
-
"tag": false,
|
|
11
|
-
"changelog": false,
|
|
12
9
|
"exports": {
|
|
13
10
|
".": "./lib/printer.js"
|
|
14
11
|
},
|
|
@@ -25,6 +22,7 @@
|
|
|
25
22
|
"lint:fresh": "madrun lint:fresh",
|
|
26
23
|
"fix:lint": "madrun fix:lint",
|
|
27
24
|
"coverage": "madrun coverage",
|
|
25
|
+
"coverage:html": "madrun coverage:html",
|
|
28
26
|
"report": "madrun report"
|
|
29
27
|
},
|
|
30
28
|
"dependencies": {
|
|
File without changes
|
|
File without changes
|