@putout/printer 1.51.0 → 1.52.1

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,13 @@
1
+ 2023.04.17, v1.52.1
2
+
3
+ feature:
4
+ - cf83bcb @putout/printer: move out satisfy
5
+
6
+ 2023.04.17, v1.52.0
7
+
8
+ feature:
9
+ - bf2b9a3 @putout/printer: add support of afterSatisfy
10
+
1
11
  2023.04.17, v1.51.0
2
12
 
3
13
  feature:
@@ -76,3 +76,12 @@ module.exports.isNewlineBetweenStatements = (path) => {
76
76
 
77
77
  return startNext - endCurrent > 1;
78
78
  };
79
+
80
+ module.exports.satisfy = (conditions) => (path) => {
81
+ for (const condition of conditions) {
82
+ if (condition(path))
83
+ return true;
84
+ }
85
+
86
+ return false;
87
+ };
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ const {satisfy} = require('../is');
4
+ module.exports = (plugin) => {
5
+ if (!plugin.afterSatisfy && !plugin.beforeSatisfy)
6
+ return plugin;
7
+
8
+ return {
9
+ afterIf: createIf(plugin.afterSatisfy),
10
+ beforeIf: createIf(plugin.beforeSatisfy),
11
+ ...plugin,
12
+ };
13
+ };
14
+
15
+ const createIf = (getConditions) => {
16
+ const conditions = getConditions?.() || [];
17
+ return satisfy(conditions);
18
+ };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const rendy = require('rendy');
4
+ const maybeSatisfy = require('./maybe/satisfy');
4
5
 
5
6
  const {
6
7
  isProgram,
@@ -22,9 +23,7 @@ module.exports.maybeThrow = (a, path, b) => {
22
23
  }));
23
24
  };
24
25
 
25
- const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
26
- ExpressionStatement(ast),
27
- ]);
26
+ const maybeProgram = (ast) => isProgram(ast) ? ast : Program([ExpressionStatement(ast)]);
28
27
 
29
28
  module.exports.maybeFile = (ast) => isFile(ast) ? ast : File(maybeProgram(ast));
30
29
 
@@ -44,7 +43,7 @@ function objectPlugin(plugin, path, printer) {
44
43
  beforeIf = condition,
45
44
  after = split,
46
45
  afterIf = condition,
47
- } = plugin;
46
+ } = maybeSatisfy(plugin);
48
47
 
49
48
  if (beforeIf?.(path, printer)) {
50
49
  before(path, printer);
@@ -2,7 +2,6 @@
2
2
 
3
3
  const {
4
4
  isNext,
5
- isParentProgram,
6
5
  isLast,
7
6
  isParentBlock,
8
7
  isParentLast,
@@ -21,9 +20,12 @@ module.exports.ExpressionStatement = {
21
20
  store(true);
22
21
  }
23
22
  },
24
- afterIf(path) {
25
- return shouldAddNewLineAfter(path);
26
- },
23
+ afterSatisfy: () => [
24
+ isParentOrNotLastORParentLast,
25
+ isParentBlock,
26
+ isNext,
27
+ isNextUp,
28
+ ],
27
29
  after(path, {print, maybe, store}) {
28
30
  print.newline();
29
31
  maybe.markAfter(store(), path);
@@ -49,16 +51,11 @@ function shouldBreakline(path) {
49
51
  return false;
50
52
  }
51
53
 
52
- function shouldAddNewLineAfter(path) {
53
- if (!isParentBlock(path) && (isLast(path) || isParentLast(path)))
54
- return false;
55
-
56
- if (isParentBlock(path) && !isParentProgram(path))
57
- return true;
58
-
59
- if (isNext(path))
60
- return true;
61
-
54
+ function isParentOrNotLastORParentLast(path) {
55
+ return isParentBlock(path) || !(isLast(path) || isParentLast(path));
56
+ }
57
+
58
+ function isNextUp(path) {
62
59
  return path.findParent(isNext);
63
60
  }
64
61
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.51.0",
3
+ "version": "1.52.1",
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",