@putout/printer 1.143.0 → 1.145.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 CHANGED
@@ -1,3 +1,14 @@
1
+ 2023.06.08, v1.145.0
2
+
3
+ feature:
4
+ - 9d33542 @putout/printer: ArrayExpression: tuple: Identifier and StringLiteral
5
+
6
+ 2023.06.07, v1.144.0
7
+
8
+ feature:
9
+ - 1a0881b @putout/printer: semantics: add
10
+ - 21298d2 @putout/printer: add ability to configurate semantics
11
+
1
12
  2023.06.07, v1.143.0
2
13
 
3
14
  feature:
package/README.md CHANGED
@@ -85,6 +85,10 @@ write(ast, {
85
85
  roundBraceOpen: '(',
86
86
  roundBraceClose: ')',
87
87
  },
88
+ semantics: {
89
+ maxSpecifiersInOneLine: 2,
90
+ maxElementsInOneLine: 3,
91
+ },
88
92
  visitors: {
89
93
  AssignmentPattern(path, {write}) {
90
94
  write('/* [hello world] */= ');
@@ -18,6 +18,8 @@ const {isSimple} = require('@putout/operate');
18
18
  const {
19
19
  isCoupleLines,
20
20
  isStringAndIdentifier,
21
+ isIdentifierAndString,
22
+
21
23
  } = require('../is');
22
24
 
23
25
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
@@ -261,6 +263,9 @@ function isNewlineBetweenElements(path, {elements}) {
261
263
  if (isStringAndIdentifier(elements))
262
264
  return false;
263
265
 
266
+ if (isIdentifierAndString(elements))
267
+ return false;
268
+
264
269
  if (isSimpleAndObject(elements))
265
270
  return false;
266
271
 
@@ -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 > 2;
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');
@@ -43,6 +43,7 @@ function isCoupleLines(path) {
43
43
 
44
44
  module.exports.exists = (a) => a.node;
45
45
  module.exports.isStringAndIdentifier = ([a, b]) => isStringLiteral(a) && isIdentifier(b);
46
+ module.exports.isIdentifierAndString = ([a, b]) => isIdentifier(a) && isStringLiteral(b);
46
47
 
47
48
  const isIfOrStatement = (a) => isIfStatement(a) || isStatement(a);
48
49
  const isForOfOrStatement = (a) => isForOfStatement(a) || isStatement(a);
@@ -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
+ }
@@ -9,18 +9,12 @@ const {
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 {maxOneLineSpecifiers} = options.imports;
17
+ const {maxSpecifiersInOneLine} = options;
24
18
 
25
19
  print('import');
26
20
  maybe.print(isType, ' type');
@@ -61,7 +55,7 @@ module.exports.ImportDeclaration = {
61
55
  indent.inc();
62
56
 
63
57
  maybe.write(!wasSpecifier, '{');
64
- maybe.write.breakline(importsCount > 2);
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 <= maxOneLineSpecifiers && notLast) {
68
+ if (importsCount <= maxSpecifiersInOneLine && notLast) {
75
69
  maybe.write(n, ',');
76
70
  maybe.write.space(n);
77
71
  }
78
72
 
79
- if (importsCount > maxOneLineSpecifiers) {
73
+ if (importsCount > maxSpecifiersInOneLine) {
80
74
  maybe.write(n, ',');
81
75
  maybe.write.newline(index === n);
82
76
  }
@@ -32,6 +32,8 @@ const {
32
32
  parseTrailingComments,
33
33
  } = require('./comments');
34
34
 
35
+ const {parseOverrides} = require('./overrides');
36
+
35
37
  const isString = (a) => typeof a === 'string';
36
38
  const {assign} = Object;
37
39
  const callWith = (fn, a) => () => fn(a);
@@ -47,19 +49,6 @@ const traversers = {
47
49
  const GET = '__';
48
50
  const get = (path, command) => path.get(command.replace(GET, ''));
49
51
 
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
52
  const createAddToken = (tokens) => {
64
53
  const log = createLog();
65
54
 
@@ -69,8 +58,13 @@ const createAddToken = (tokens) => {
69
58
  };
70
59
  };
71
60
 
72
- module.exports.tokenize = (ast, overrides = {}) => {
73
- const format = initFormat(overrides.format);
61
+ module.exports.tokenize = (ast, overrides) => {
62
+ const {
63
+ visitors,
64
+ format,
65
+ semantics,
66
+ } = parseOverrides(overrides);
67
+
74
68
  const tokens = [];
75
69
  const addToken = createAddToken(tokens);
76
70
  const debug = createDebug(tokens);
@@ -187,7 +181,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
187
181
 
188
182
  const currentTraversers = {
189
183
  ...traversers,
190
- ...overrides.visitors,
184
+ ...visitors,
191
185
  };
192
186
 
193
187
  if (ast.parentPath)
@@ -241,10 +235,10 @@ module.exports.tokenize = (ast, overrides = {}) => {
241
235
  const currentIndent = i;
242
236
  parseLeadingComments(path, printer, format);
243
237
  // this is main thing
244
- maybePlugin(currentTraverse, path, printer);
245
-
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.143.0",
3
+ "version": "1.145.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",