@putout/printer 1.39.0 → 1.40.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 +5 -0
- package/lib/tokenize/expressions/functions.js +0 -1
- package/lib/tokenize/maybe.js +13 -1
- package/lib/tokenize/statements/export-default-declaration.js +2 -1
- package/lib/tokenize/tokenize.js +11 -2
- package/lib/tokenize/typescript/index.js +3 -1
- package/lib/tokenize/typescript/ts-type-alias-declaration.js +16 -4
- package/lib/tokenize/typescript/ts-type-literal.js +6 -1
- package/package.json +3 -2
package/ChangeLog
CHANGED
package/lib/tokenize/maybe.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const rendy = require('rendy');
|
|
4
4
|
|
|
5
5
|
const {
|
|
6
6
|
isProgram,
|
|
@@ -10,6 +10,18 @@ const {
|
|
|
10
10
|
Program,
|
|
11
11
|
} = require('@babel/types');
|
|
12
12
|
|
|
13
|
+
const isFn = (a) => typeof a === 'function';
|
|
14
|
+
|
|
15
|
+
module.exports.maybeThrow = (a, path, b) => {
|
|
16
|
+
if (!a)
|
|
17
|
+
return;
|
|
18
|
+
|
|
19
|
+
throw Error(rendy(b, {
|
|
20
|
+
path,
|
|
21
|
+
type: path.type,
|
|
22
|
+
}));
|
|
23
|
+
};
|
|
24
|
+
|
|
13
25
|
const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
|
|
14
26
|
ExpressionStatement(ast),
|
|
15
27
|
]);
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
module.exports.ExportDefaultDeclaration = {
|
|
4
|
+
/*
|
|
4
5
|
beforeIf(path) {
|
|
5
6
|
return path.getPrevSibling().isTSTypeAliasDeclaration();
|
|
6
7
|
},
|
|
7
8
|
before(path, {print}) {
|
|
8
9
|
print.breakline();
|
|
9
10
|
},
|
|
10
|
-
|
|
11
|
+
*/print(path, {print, traverse, maybe}) {
|
|
11
12
|
const declaration = path.get('declaration');
|
|
12
13
|
print('export default ');
|
|
13
14
|
traverse(declaration);
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -14,6 +14,7 @@ const {TYPES} = require('../types');
|
|
|
14
14
|
const {
|
|
15
15
|
maybeFile,
|
|
16
16
|
maybePlugin,
|
|
17
|
+
maybeThrow,
|
|
17
18
|
} = require('./maybe');
|
|
18
19
|
|
|
19
20
|
const {
|
|
@@ -138,6 +139,13 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
138
139
|
space,
|
|
139
140
|
});
|
|
140
141
|
|
|
142
|
+
assign(maybeWrite, {
|
|
143
|
+
newline: maybeNewline,
|
|
144
|
+
breakline: maybeBreakline,
|
|
145
|
+
linebreak: maybeLinebreak,
|
|
146
|
+
space: maybeSpace,
|
|
147
|
+
});
|
|
148
|
+
|
|
141
149
|
const maybe = {
|
|
142
150
|
indent: maybeIndent,
|
|
143
151
|
markBefore: maybeMarkBefore,
|
|
@@ -206,12 +214,13 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
206
214
|
print: maybePrint,
|
|
207
215
|
});
|
|
208
216
|
|
|
209
|
-
|
|
210
|
-
throw Error(`Node type '${type}' is not supported yet: '${path}'`);
|
|
217
|
+
maybeThrow(!currentTraverse, path, `Node type '{{ type }}' is not supported yet: '{{ path }}'`);
|
|
211
218
|
|
|
219
|
+
const currentIndent = i;
|
|
212
220
|
parseLeadingComments(path, printer);
|
|
213
221
|
maybePlugin(currentTraverse, path, printer);
|
|
214
222
|
parseTrailingComments(path, printer);
|
|
223
|
+
maybeThrow(i !== currentIndent, path, `☝️Looks like indent level changed after token visitor: '{{ type }}', for code: '{{ path }}'`);
|
|
215
224
|
debug(path.type);
|
|
216
225
|
}
|
|
217
226
|
|
|
@@ -34,6 +34,9 @@ module.exports = {
|
|
|
34
34
|
TSNumberKeyword(path, {write}) {
|
|
35
35
|
write('number');
|
|
36
36
|
},
|
|
37
|
+
TSStringKeyword(path, {write}) {
|
|
38
|
+
write('string');
|
|
39
|
+
},
|
|
37
40
|
TSInstantiationExpression(path, {print}) {
|
|
38
41
|
print('__expression');
|
|
39
42
|
print('__typeParameters');
|
|
@@ -88,4 +91,3 @@ module.exports = {
|
|
|
88
91
|
}
|
|
89
92
|
},
|
|
90
93
|
};
|
|
91
|
-
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {isLast} = require('../is');
|
|
4
|
+
const isNextType = (a) => a.getNextSibling().isTSTypeAliasDeclaration();
|
|
5
|
+
|
|
4
6
|
module.exports.TSTypeAliasDeclaration = {
|
|
5
|
-
print(path, {print}) {
|
|
7
|
+
print(path, {print, maybe, store}) {
|
|
6
8
|
print('type ');
|
|
7
9
|
print('__id');
|
|
8
10
|
print.space();
|
|
@@ -10,12 +12,22 @@ module.exports.TSTypeAliasDeclaration = {
|
|
|
10
12
|
print.space();
|
|
11
13
|
print('__typeAnnotation');
|
|
12
14
|
print(';');
|
|
15
|
+
|
|
16
|
+
const is = store(isLast(path));
|
|
17
|
+
maybe.print.newline(!is);
|
|
13
18
|
},
|
|
14
|
-
afterIf(path) {
|
|
15
|
-
|
|
19
|
+
afterIf(path, {store}) {
|
|
20
|
+
const last = store();
|
|
21
|
+
|
|
22
|
+
if (last)
|
|
23
|
+
return false;
|
|
24
|
+
|
|
25
|
+
if (isNextType(path))
|
|
26
|
+
return false;
|
|
27
|
+
|
|
28
|
+
return true;
|
|
16
29
|
},
|
|
17
30
|
after(path, {print}) {
|
|
18
31
|
print.newline();
|
|
19
32
|
},
|
|
20
33
|
};
|
|
21
|
-
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {isNext} = require('../is');
|
|
4
|
+
|
|
3
5
|
module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
|
|
4
6
|
const members = path.get('members');
|
|
5
7
|
write('{');
|
|
@@ -15,11 +17,14 @@ module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
|
|
|
15
17
|
indent();
|
|
16
18
|
traverse(member);
|
|
17
19
|
maybe.write(is, ';');
|
|
20
|
+
|
|
21
|
+
if (is && isNext(member))
|
|
22
|
+
write.newline(is);
|
|
18
23
|
}
|
|
19
24
|
|
|
20
25
|
if (is) {
|
|
21
26
|
write.newline();
|
|
22
|
-
indent.
|
|
27
|
+
indent.dec();
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
write('}');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.40.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",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"@babel/types": "^7.21.3",
|
|
33
33
|
"@putout/compare": "^9.13.0",
|
|
34
34
|
"fullstore": "^3.0.0",
|
|
35
|
-
"just-snake-case": "^3.2.0"
|
|
35
|
+
"just-snake-case": "^3.2.0",
|
|
36
|
+
"rendy": "^3.1.1"
|
|
36
37
|
},
|
|
37
38
|
"keywords": [
|
|
38
39
|
"putout",
|