@putout/printer 1.142.0 → 1.144.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 +11 -0
- package/README.md +4 -0
- package/lib/tokenize/expressions/array-pattern.js +5 -2
- package/lib/tokenize/maybe/index.js +5 -5
- package/lib/tokenize/overrides.js +36 -0
- package/lib/tokenize/statements/import-declaration/import-declaration.js +7 -13
- package/lib/tokenize/tokenize.js +12 -18
- package/package.json +2 -1
- package/lib/tokenize/statements/import-declaration/parse-specifiers.js +0 -30
package/ChangeLog
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
2023.06.07, v1.144.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 1a0881b @putout/printer: semantics: add
|
|
5
|
+
- 21298d2 @putout/printer: add ability to configurate semantics
|
|
6
|
+
|
|
7
|
+
2023.06.07, v1.143.0
|
|
8
|
+
|
|
9
|
+
feature:
|
|
10
|
+
- 89bb884 @putout/printer: move out parseImportSpeciviers
|
|
11
|
+
|
|
1
12
|
2023.06.06, v1.142.0
|
|
2
13
|
|
|
3
14
|
feature:
|
package/README.md
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
const isForOf = ({parentPath}) => parentPath.parentPath.parentPath?.isForOfStatement();
|
|
4
4
|
|
|
5
|
-
module.exports.ArrayPattern = (path, {indent, maybe, print}) => {
|
|
5
|
+
module.exports.ArrayPattern = (path, {indent, maybe, print}, options) => {
|
|
6
|
+
const {maxElementsInOneLine} = options;
|
|
7
|
+
|
|
6
8
|
print('[');
|
|
7
9
|
|
|
8
10
|
const elements = path.get('elements');
|
|
9
11
|
indent.inc();
|
|
10
12
|
|
|
11
|
-
const isNewLine = !isForOf(path) && elements.length >
|
|
13
|
+
const isNewLine = !isForOf(path) && elements.length > maxElementsInOneLine;
|
|
12
14
|
const n = elements.length - 1;
|
|
13
15
|
|
|
14
16
|
maybe.print(isNewLine && elements.length, '\n');
|
|
@@ -29,3 +31,4 @@ module.exports.ArrayPattern = (path, {indent, maybe, print}) => {
|
|
|
29
31
|
maybe.indent(elements.length && isNewLine);
|
|
30
32
|
print(']');
|
|
31
33
|
};
|
|
34
|
+
|
|
@@ -29,14 +29,14 @@ const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
|
|
|
29
29
|
|
|
30
30
|
module.exports.maybeFile = (ast) => isFile(ast) ? ast : File(maybeProgram(ast));
|
|
31
31
|
|
|
32
|
-
module.exports.maybePlugin = (plugin, path, printer) => {
|
|
32
|
+
module.exports.maybePlugin = (plugin, path, printer, options) => {
|
|
33
33
|
if (isFn(plugin))
|
|
34
|
-
return plugin(path, printer);
|
|
34
|
+
return plugin(path, printer, options);
|
|
35
35
|
|
|
36
|
-
return objectPlugin(plugin, path, printer);
|
|
36
|
+
return objectPlugin(plugin, path, printer, options);
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
function objectPlugin(plugin, path, printer) {
|
|
39
|
+
function objectPlugin(plugin, path, printer, options) {
|
|
40
40
|
const {
|
|
41
41
|
print,
|
|
42
42
|
split,
|
|
@@ -51,7 +51,7 @@ function objectPlugin(plugin, path, printer) {
|
|
|
51
51
|
before(path, printer);
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
print(path, printer);
|
|
54
|
+
print(path, printer, options);
|
|
55
55
|
|
|
56
56
|
if (afterIf?.(path, printer)) {
|
|
57
57
|
after(path, printer);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.parseOverrides = (overrides = {}) => {
|
|
4
|
+
const {
|
|
5
|
+
format,
|
|
6
|
+
semantics,
|
|
7
|
+
visitors,
|
|
8
|
+
} = overrides;
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
format: initFormat(format),
|
|
12
|
+
semantics: initSemantics(semantics),
|
|
13
|
+
visitors,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function initFormat(format) {
|
|
18
|
+
return {
|
|
19
|
+
indent: ' ',
|
|
20
|
+
newline: '\n',
|
|
21
|
+
space: ' ',
|
|
22
|
+
comments: true,
|
|
23
|
+
splitter: '\n',
|
|
24
|
+
roundBraceOpen: '(',
|
|
25
|
+
roundBraceClose: ')',
|
|
26
|
+
...format,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function initSemantics(semantics = {}) {
|
|
31
|
+
return {
|
|
32
|
+
maxSpecifiersInOneLine: 2,
|
|
33
|
+
maxElementsInOneLine: 5,
|
|
34
|
+
...semantics,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -2,25 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
const {markAfter} = require('../../mark');
|
|
4
4
|
const {isLast} = require('../../is');
|
|
5
|
-
const {
|
|
5
|
+
const {parseImportSpecifiers} = require('parse-import-specifiers');
|
|
6
6
|
|
|
7
7
|
const {
|
|
8
8
|
maybePrintAttributes,
|
|
9
9
|
ImportAttribute,
|
|
10
10
|
} = require('./import-attribute');
|
|
11
11
|
|
|
12
|
-
const options = {
|
|
13
|
-
imports: {
|
|
14
|
-
maxOneLineSpecifiers: 2,
|
|
15
|
-
},
|
|
16
|
-
};
|
|
17
|
-
|
|
18
12
|
module.exports.ImportAttribute = ImportAttribute;
|
|
19
13
|
module.exports.ImportDeclaration = {
|
|
20
|
-
print(path, {print, maybe, write, traverse, indent}) {
|
|
14
|
+
print(path, {print, maybe, write, traverse, indent}, options) {
|
|
21
15
|
const isType = path.node.importKind === 'type';
|
|
22
16
|
const specifiers = path.get('specifiers');
|
|
23
|
-
const {
|
|
17
|
+
const {maxSpecifiersInOneLine} = options;
|
|
24
18
|
|
|
25
19
|
print('import');
|
|
26
20
|
maybe.print(isType, ' type');
|
|
@@ -32,7 +26,7 @@ module.exports.ImportDeclaration = {
|
|
|
32
26
|
defaults,
|
|
33
27
|
namespaces,
|
|
34
28
|
imports,
|
|
35
|
-
} =
|
|
29
|
+
} = parseImportSpecifiers(specifiers);
|
|
36
30
|
|
|
37
31
|
maybe.print(specifiers.length, ' ');
|
|
38
32
|
|
|
@@ -61,7 +55,7 @@ module.exports.ImportDeclaration = {
|
|
|
61
55
|
indent.inc();
|
|
62
56
|
|
|
63
57
|
maybe.write(!wasSpecifier, '{');
|
|
64
|
-
maybe.write.breakline(importsCount >
|
|
58
|
+
maybe.write.breakline(importsCount > maxSpecifiersInOneLine);
|
|
65
59
|
|
|
66
60
|
wasSpecifier = true;
|
|
67
61
|
write(imported.name);
|
|
@@ -71,12 +65,12 @@ module.exports.ImportDeclaration = {
|
|
|
71
65
|
write(spec.node.local.name);
|
|
72
66
|
}
|
|
73
67
|
|
|
74
|
-
if (importsCount <=
|
|
68
|
+
if (importsCount <= maxSpecifiersInOneLine && notLast) {
|
|
75
69
|
maybe.write(n, ',');
|
|
76
70
|
maybe.write.space(n);
|
|
77
71
|
}
|
|
78
72
|
|
|
79
|
-
if (importsCount >
|
|
73
|
+
if (importsCount > maxSpecifiersInOneLine) {
|
|
80
74
|
maybe.write(n, ',');
|
|
81
75
|
maybe.write.newline(index === n);
|
|
82
76
|
}
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -31,6 +31,7 @@ const {
|
|
|
31
31
|
parseLeadingComments,
|
|
32
32
|
parseTrailingComments,
|
|
33
33
|
} = require('./comments');
|
|
34
|
+
const {parseOverrides} = require('./overrides');
|
|
34
35
|
|
|
35
36
|
const isString = (a) => typeof a === 'string';
|
|
36
37
|
const {assign} = Object;
|
|
@@ -47,19 +48,6 @@ const traversers = {
|
|
|
47
48
|
const GET = '__';
|
|
48
49
|
const get = (path, command) => path.get(command.replace(GET, ''));
|
|
49
50
|
|
|
50
|
-
function initFormat(format) {
|
|
51
|
-
return {
|
|
52
|
-
indent: ' ',
|
|
53
|
-
newline: '\n',
|
|
54
|
-
space: ' ',
|
|
55
|
-
comments: true,
|
|
56
|
-
splitter: '\n',
|
|
57
|
-
roundBraceOpen: '(',
|
|
58
|
-
roundBraceClose: ')',
|
|
59
|
-
...format,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
51
|
const createAddToken = (tokens) => {
|
|
64
52
|
const log = createLog();
|
|
65
53
|
|
|
@@ -69,8 +57,13 @@ const createAddToken = (tokens) => {
|
|
|
69
57
|
};
|
|
70
58
|
};
|
|
71
59
|
|
|
72
|
-
module.exports.tokenize = (ast, overrides
|
|
73
|
-
const
|
|
60
|
+
module.exports.tokenize = (ast, overrides) => {
|
|
61
|
+
const {
|
|
62
|
+
visitors,
|
|
63
|
+
format,
|
|
64
|
+
semantics,
|
|
65
|
+
} = parseOverrides(overrides);
|
|
66
|
+
|
|
74
67
|
const tokens = [];
|
|
75
68
|
const addToken = createAddToken(tokens);
|
|
76
69
|
const debug = createDebug(tokens);
|
|
@@ -187,7 +180,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
187
180
|
|
|
188
181
|
const currentTraversers = {
|
|
189
182
|
...traversers,
|
|
190
|
-
...
|
|
183
|
+
...visitors,
|
|
191
184
|
};
|
|
192
185
|
|
|
193
186
|
if (ast.parentPath)
|
|
@@ -240,11 +233,12 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
240
233
|
|
|
241
234
|
const currentIndent = i;
|
|
242
235
|
parseLeadingComments(path, printer, format);
|
|
243
|
-
// this is main thing
|
|
244
|
-
maybePlugin(currentTraverse, path, printer);
|
|
245
236
|
|
|
237
|
+
// this is main thing
|
|
238
|
+
maybePlugin(currentTraverse, path, printer, semantics);
|
|
246
239
|
parseTrailingComments(path, printer, format);
|
|
247
240
|
maybeThrow(i !== currentIndent, path, `☝️Looks like indent level changed after token visitor: '{{ type }}', for code: '{{ path }}'`);
|
|
241
|
+
|
|
248
242
|
debug(path.type);
|
|
249
243
|
}
|
|
250
244
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.144.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "Simplest possible opinionated Babel AST printer for 🐊Putout",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"@putout/operate": "^8.11.0",
|
|
35
35
|
"fullstore": "^3.0.0",
|
|
36
36
|
"just-snake-case": "^3.2.0",
|
|
37
|
+
"parse-import-specifiers": "^1.0.1",
|
|
37
38
|
"rendy": "^3.1.1"
|
|
38
39
|
},
|
|
39
40
|
"keywords": [
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports.parseSpecifiers = (specifiers) => {
|
|
4
|
-
const defaults = [];
|
|
5
|
-
const namespaces = [];
|
|
6
|
-
const imports = [];
|
|
7
|
-
|
|
8
|
-
for (const spec of specifiers) {
|
|
9
|
-
if (spec.isImportDefaultSpecifier()) {
|
|
10
|
-
defaults.push(spec);
|
|
11
|
-
continue;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (spec.isImportNamespaceSpecifier()) {
|
|
15
|
-
namespaces.push(spec);
|
|
16
|
-
continue;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (spec.isImportSpecifier()) {
|
|
20
|
-
imports.push(spec);
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
defaults,
|
|
27
|
-
namespaces,
|
|
28
|
-
imports,
|
|
29
|
-
};
|
|
30
|
-
};
|