@putout/printer 5.19.0 → 5.21.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 +10 -0
- package/README.md +24 -0
- package/lib/printer.js +2 -0
- package/lib/tokenize/comment/parse-leading-comments.js +2 -3
- package/lib/tokenize/expressions/class/class-property.js +2 -1
- package/lib/tokenize/expressions/class/class.js +29 -32
- package/lib/tokenize/is.js +0 -7
- package/lib/tokenize/maybe/maybe-decorators.js +9 -2
- package/lib/tokenize/tokenize.js +2 -15
- package/lib/tokenize/visitors.js +15 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/README.md
CHANGED
|
@@ -174,6 +174,30 @@ Options used to configure logic of output, similar to ESLint rules:
|
|
|
174
174
|
|
|
175
175
|
When you want to improve support of existing visitor or extend **Printer** with a new ones, you need next base operations:
|
|
176
176
|
|
|
177
|
+
### override
|
|
178
|
+
|
|
179
|
+
When you need to override behavior of existing visitor use:
|
|
180
|
+
|
|
181
|
+
```js
|
|
182
|
+
import {
|
|
183
|
+
print,
|
|
184
|
+
visitors as v,
|
|
185
|
+
} from '@putout/printer';
|
|
186
|
+
|
|
187
|
+
print(ast, {
|
|
188
|
+
visitors: {
|
|
189
|
+
CallExpression(path, printer, semantics) {
|
|
190
|
+
const {print} = printer;
|
|
191
|
+
|
|
192
|
+
if (!path.node.goldstein)
|
|
193
|
+
return v.CallExpression(path, printer, semantics);
|
|
194
|
+
|
|
195
|
+
print('__goldstein');
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
177
201
|
### `print`
|
|
178
202
|
|
|
179
203
|
Used in previous example `print` can be used for a couple purposes:
|
package/lib/printer.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {hasTrailingComment} = require('../is');
|
|
4
|
-
const {isVariableDeclarator} = require('@putout/babel').types;
|
|
5
4
|
|
|
6
5
|
const {markBefore} = require('../mark');
|
|
7
6
|
const {maybeInsideFn} = require('./maybe-inside-fn');
|
|
@@ -21,7 +20,7 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
|
|
|
21
20
|
return;
|
|
22
21
|
|
|
23
22
|
const insideFn = path.parentPath.isFunction();
|
|
24
|
-
const isProperty = path.isObjectProperty() || isVariableDeclarator(path);
|
|
23
|
+
const isProperty = path.isObjectProperty() || path.isVariableDeclarator() || path.isClassProperty();
|
|
25
24
|
const isIndent = !looksLikeSwitchCase && !path.isClassMethod() && !insideFn && !isProperty;
|
|
26
25
|
|
|
27
26
|
for (const {type, value} of leadingComments) {
|
|
@@ -33,7 +32,7 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
|
|
|
33
32
|
indent,
|
|
34
33
|
});
|
|
35
34
|
|
|
36
|
-
maybe.print.space(isProperty);
|
|
35
|
+
maybe.print.space(isProperty && !path.isClassProperty());
|
|
37
36
|
print(`//${value}`);
|
|
38
37
|
|
|
39
38
|
const isParentSwitch = path.parentPath.isSwitchCase();
|
|
@@ -5,7 +5,6 @@ const {maybePrintTypeAnnotation} = require('../../maybe/maybe-type-annotation');
|
|
|
5
5
|
const {maybeDecorators} = require('../../maybe/maybe-decorators');
|
|
6
6
|
|
|
7
7
|
const processClassProperty = maybeDecorators((path, printer, semantics, {accessor} = {}) => {
|
|
8
|
-
const {print, maybe} = printer;
|
|
9
8
|
const {node} = path;
|
|
10
9
|
const {
|
|
11
10
|
accessibility,
|
|
@@ -13,6 +12,8 @@ const processClassProperty = maybeDecorators((path, printer, semantics, {accesso
|
|
|
13
12
|
optional,
|
|
14
13
|
} = node;
|
|
15
14
|
|
|
15
|
+
const {print, maybe} = printer;
|
|
16
|
+
|
|
16
17
|
maybe.print(accessor, 'accessor ');
|
|
17
18
|
|
|
18
19
|
const value = path.get('value');
|
|
@@ -2,37 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
const {isNext} = require('../../is');
|
|
4
4
|
const {markAfter} = require('../../mark');
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
//const {maybeDecorators} = require('../../maybe-get');
|
|
6
7
|
const {maybeDeclare} = require('../../maybe/maybe-declare');
|
|
7
8
|
const {parseComments} = require('../../comment/comment');
|
|
9
|
+
const {maybeDecorators} = require('../../maybe/maybe-decorators');
|
|
8
10
|
|
|
9
11
|
const isInsideExport = ({parentPath}) => parentPath.isExportDeclaration();
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
module.exports.ClassDeclaration = {
|
|
14
|
-
print: maybeDeclare((path, printer, semantics) => {
|
|
15
|
-
const {indent} = printer;
|
|
16
|
-
indent();
|
|
17
|
-
classVisitor(path, printer, semantics);
|
|
18
|
-
}),
|
|
19
|
-
afterIf(path) {
|
|
20
|
-
if (!isNext(path))
|
|
21
|
-
return false;
|
|
22
|
-
|
|
23
|
-
return !isInsideExport(path);
|
|
24
|
-
},
|
|
25
|
-
after(path, {write}) {
|
|
26
|
-
write.newline();
|
|
27
|
-
|
|
28
|
-
if (path.node.body.body.length) {
|
|
29
|
-
write.newline();
|
|
30
|
-
markAfter(path);
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
function classVisitor(path, printer, semantics) {
|
|
13
|
+
const classVisitor = maybeDecorators((path, printer, semantics) => {
|
|
36
14
|
const {id, abstract} = path.node;
|
|
37
15
|
const {
|
|
38
16
|
print,
|
|
@@ -41,11 +19,6 @@ function classVisitor(path, printer, semantics) {
|
|
|
41
19
|
traverse,
|
|
42
20
|
} = printer;
|
|
43
21
|
|
|
44
|
-
for (const decorator of maybeDecorators(path)) {
|
|
45
|
-
traverse(decorator);
|
|
46
|
-
print.breakline();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
22
|
maybe.print(abstract, 'abstract ');
|
|
50
23
|
print('class ');
|
|
51
24
|
print('__id');
|
|
@@ -86,4 +59,28 @@ function classVisitor(path, printer, semantics) {
|
|
|
86
59
|
indent.dec();
|
|
87
60
|
maybe.indent(body.length);
|
|
88
61
|
print('}');
|
|
89
|
-
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
module.exports.ClassExpression = classVisitor;
|
|
65
|
+
|
|
66
|
+
module.exports.ClassDeclaration = {
|
|
67
|
+
print: maybeDeclare((path, printer, semantics) => {
|
|
68
|
+
const {indent} = printer;
|
|
69
|
+
indent();
|
|
70
|
+
classVisitor(path, printer, semantics);
|
|
71
|
+
}),
|
|
72
|
+
afterIf(path) {
|
|
73
|
+
if (!isNext(path))
|
|
74
|
+
return false;
|
|
75
|
+
|
|
76
|
+
return !isInsideExport(path);
|
|
77
|
+
},
|
|
78
|
+
after(path, {write}) {
|
|
79
|
+
write.newline();
|
|
80
|
+
|
|
81
|
+
if (path.node.body.body.length) {
|
|
82
|
+
write.newline();
|
|
83
|
+
markAfter(path);
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
};
|
package/lib/tokenize/is.js
CHANGED
|
@@ -23,18 +23,11 @@ const isNext = (path) => {
|
|
|
23
23
|
return !next.isEmptyStatement();
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
const isPrev = (path) => {
|
|
27
|
-
const prev = path.getPrevSibling();
|
|
28
|
-
|
|
29
|
-
return prev.node;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
26
|
const isNextParent = (path) => isNext(path.parentPath);
|
|
33
27
|
const isLast = (path) => isParentProgram(path) && !isNext(path);
|
|
34
28
|
|
|
35
29
|
module.exports.isFirst = (path) => path.node === path.parentPath.node.body?.[0];
|
|
36
30
|
module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
|
|
37
|
-
module.exports.isPrev = isPrev;
|
|
38
31
|
module.exports.isNext = isNext;
|
|
39
32
|
module.exports.isNextParent = isNextParent;
|
|
40
33
|
module.exports.isParentProgram = isParentProgram;
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const isPrevClassProperty = (path) => {
|
|
4
|
+
const prev = path.getPrevSibling();
|
|
5
|
+
|
|
6
|
+
if (!prev.node)
|
|
7
|
+
return false;
|
|
8
|
+
|
|
9
|
+
return prev.isClassProperty() || prev.isClassAccessorProperty();
|
|
10
|
+
};
|
|
4
11
|
|
|
5
12
|
module.exports.maybeDecorators = (visitor) => (path, printer, semantics, options) => {
|
|
6
13
|
const {
|
|
@@ -13,7 +20,7 @@ module.exports.maybeDecorators = (visitor) => (path, printer, semantics, options
|
|
|
13
20
|
|
|
14
21
|
if (decorators) {
|
|
15
22
|
for (const decorator of path.get('decorators')) {
|
|
16
|
-
maybe.write.breakline(
|
|
23
|
+
maybe.write.breakline(isPrevClassProperty(path));
|
|
17
24
|
traverse(decorator);
|
|
18
25
|
write.breakline();
|
|
19
26
|
}
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -4,12 +4,8 @@ const {round} = Math;
|
|
|
4
4
|
const fullstore = require('fullstore');
|
|
5
5
|
const isObject = (a) => a && typeof a === 'object';
|
|
6
6
|
const babelTraverse = require('@putout/babel').traverse;
|
|
7
|
-
const expressions = require('./expressions');
|
|
8
|
-
const statements = require('./statements');
|
|
9
|
-
const literals = require('./literals');
|
|
10
|
-
const typescript = require('./typescript');
|
|
11
|
-
const jsx = require('./jsx');
|
|
12
7
|
const {TYPES} = require('../types');
|
|
8
|
+
const baseVisitors = require('./visitors');
|
|
13
9
|
|
|
14
10
|
const {
|
|
15
11
|
maybeFile,
|
|
@@ -18,7 +14,6 @@ const {
|
|
|
18
14
|
} = require('./maybe');
|
|
19
15
|
|
|
20
16
|
const {createDebug, createLog} = require('./debug');
|
|
21
|
-
|
|
22
17
|
const {maybeMarkAfter} = require('./mark');
|
|
23
18
|
|
|
24
19
|
const {
|
|
@@ -32,14 +27,6 @@ const isString = (a) => typeof a === 'string';
|
|
|
32
27
|
const {assign, freeze} = Object;
|
|
33
28
|
const callWith = (fn, a) => () => fn(a);
|
|
34
29
|
|
|
35
|
-
const traversers = {
|
|
36
|
-
...expressions,
|
|
37
|
-
...statements,
|
|
38
|
-
...literals,
|
|
39
|
-
...typescript,
|
|
40
|
-
...jsx,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
30
|
const GET = '__';
|
|
44
31
|
const get = (path, command) => path.get(command.replace(GET, ''));
|
|
45
32
|
|
|
@@ -183,7 +170,7 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
183
170
|
});
|
|
184
171
|
|
|
185
172
|
const currentTraversers = {
|
|
186
|
-
...
|
|
173
|
+
...baseVisitors,
|
|
187
174
|
...visitors,
|
|
188
175
|
};
|
|
189
176
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const expressions = require('./expressions');
|
|
4
|
+
const statements = require('./statements');
|
|
5
|
+
const literals = require('./literals');
|
|
6
|
+
const typescript = require('./typescript');
|
|
7
|
+
const jsx = require('./jsx');
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
...expressions,
|
|
11
|
+
...statements,
|
|
12
|
+
...literals,
|
|
13
|
+
...typescript,
|
|
14
|
+
...jsx,
|
|
15
|
+
};
|
package/package.json
CHANGED