@putout/printer 1.6.0 → 1.6.2
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 +8 -3
- package/lib/tokenize/tokenize.js +15 -18
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/README.md
CHANGED
|
@@ -48,9 +48,14 @@ Here is how you can override `AssignmentPattern`:
|
|
|
48
48
|
const ast = parse('const {a = 5} = b');
|
|
49
49
|
|
|
50
50
|
print(ast, {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
format: {
|
|
52
|
+
indent: ' ',
|
|
53
|
+
},
|
|
54
|
+
visitors: {
|
|
55
|
+
AssignmentPattern(path, {print}) {
|
|
56
|
+
print(' /* [hello world] */= ');
|
|
57
|
+
print('__right');
|
|
58
|
+
},
|
|
54
59
|
},
|
|
55
60
|
});
|
|
56
61
|
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -25,7 +25,15 @@ const traversers = {
|
|
|
25
25
|
const GET = '__';
|
|
26
26
|
const get = (path, command) => path.get(command.replace(GET, ''));
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
function initFormat(format) {
|
|
29
|
+
return {
|
|
30
|
+
...format,
|
|
31
|
+
indent: ' ',
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports.tokenize = (ast, overrides = {}) => {
|
|
36
|
+
const format = initFormat(overrides.format);
|
|
29
37
|
const tokens = [];
|
|
30
38
|
const debug = createDebug(tokens);
|
|
31
39
|
const write = (value) => {
|
|
@@ -48,7 +56,7 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
48
56
|
const indent = () => {
|
|
49
57
|
tokens.push({
|
|
50
58
|
type: TYPES.INDENT,
|
|
51
|
-
value: printIndent(i),
|
|
59
|
+
value: printIndent(i, format.indent),
|
|
52
60
|
});
|
|
53
61
|
};
|
|
54
62
|
|
|
@@ -60,14 +68,14 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
60
68
|
const linebreak = () => {
|
|
61
69
|
tokens.push({
|
|
62
70
|
type: TYPES.NEWLINE,
|
|
63
|
-
value: `${printIndent(i)}\n`,
|
|
71
|
+
value: `${printIndent(i, format.indent)}\n`,
|
|
64
72
|
});
|
|
65
73
|
};
|
|
66
74
|
|
|
67
75
|
const breakline = () => {
|
|
68
76
|
tokens.push({
|
|
69
77
|
type: TYPES.NEWLINE,
|
|
70
|
-
value: `\n${printIndent(i)}`,
|
|
78
|
+
value: `\n${printIndent(i, format.indent)}`,
|
|
71
79
|
});
|
|
72
80
|
};
|
|
73
81
|
|
|
@@ -85,14 +93,11 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
85
93
|
breakline,
|
|
86
94
|
});
|
|
87
95
|
|
|
88
|
-
//const maybePrint = (a, b) => a && print(b);
|
|
89
|
-
|
|
90
96
|
const maybe = {
|
|
91
97
|
write: maybeWrite,
|
|
92
98
|
indent: maybeIndent,
|
|
93
99
|
markBefore: maybeMarkBefore,
|
|
94
100
|
markAfter: maybeMarkAfter,
|
|
95
|
-
//print: maybePrint,
|
|
96
101
|
};
|
|
97
102
|
|
|
98
103
|
assign(maybe.indent, {
|
|
@@ -100,8 +105,6 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
100
105
|
dec: maybeIndentDec,
|
|
101
106
|
});
|
|
102
107
|
|
|
103
|
-
//assign(print, write);
|
|
104
|
-
|
|
105
108
|
const printer = {
|
|
106
109
|
incIndent,
|
|
107
110
|
decIndent,
|
|
@@ -116,7 +119,7 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
116
119
|
|
|
117
120
|
const currentTraversers = {
|
|
118
121
|
...traversers,
|
|
119
|
-
...overrides,
|
|
122
|
+
...overrides.visitors,
|
|
120
123
|
};
|
|
121
124
|
|
|
122
125
|
babelTraverse(ast, {
|
|
@@ -160,23 +163,17 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
160
163
|
return tokens;
|
|
161
164
|
};
|
|
162
165
|
|
|
163
|
-
function printIndent(i) {
|
|
166
|
+
function printIndent(i, indent) {
|
|
164
167
|
let result = '';
|
|
165
168
|
++i;
|
|
166
169
|
|
|
167
170
|
while (--i) {
|
|
168
|
-
result +=
|
|
171
|
+
result += indent;
|
|
169
172
|
}
|
|
170
173
|
|
|
171
174
|
return result;
|
|
172
175
|
}
|
|
173
176
|
|
|
174
|
-
/*
|
|
175
|
-
const createCompute = (path, {traverse}) => (maybeLine) => {
|
|
176
|
-
if (maybeLine.startsWith(GET))
|
|
177
|
-
return traverse(get(path, maybeLine));
|
|
178
|
-
};
|
|
179
|
-
*/
|
|
180
177
|
const createPrint = (path, {traverse, write}) => (maybeLine) => {
|
|
181
178
|
if (maybeLine === path)
|
|
182
179
|
return null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.2",
|
|
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",
|