@putout/printer 1.8.11 → 1.9.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,16 @@
1
+ 2023.03.28, v1.9.1
2
+
3
+ feature:
4
+ - 063bb53 @putout/printer: improve support of AST witout parent File
5
+ - d6fd09d @putout/printer: add support of trailingComments
6
+ - a323b20 @putout/printer: shouldAddNewlineAfter
7
+
8
+ 2023.03.27, v1.9.0
9
+
10
+ feature:
11
+ - cdcea37 @putout/printer: add support of ExportDefaultDeclaration
12
+ - e891b21 @putout/printer: add support of ExportDefaultDeclaration
13
+
1
14
  2023.03.27, v1.8.11
2
15
 
3
16
  fix:
@@ -3,9 +3,10 @@
3
3
  module.exports.cook = (tokens) => {
4
4
  const cookedTokens = [];
5
5
 
6
- for (const [i, {value}] of tokens.entries()) {
6
+ for (const {value} of tokens) {
7
7
  cookedTokens.push(value);
8
8
  }
9
9
 
10
10
  return cookedTokens;
11
11
  };
12
+
package/lib/printer.js CHANGED
@@ -5,5 +5,7 @@ const {printTokens} = require('./print-tokens');
5
5
 
6
6
  module.exports.print = (ast, overrides) => {
7
7
  const tokens = tokenize(ast, overrides);
8
- return printTokens(tokens);
8
+ const result = printTokens(tokens);
9
+
10
+ return result;
9
11
  };
@@ -3,15 +3,11 @@
3
3
  const {isFirst} = require('./is');
4
4
  const {markBefore} = require('./mark');
5
5
 
6
- module.exports.parseComments = (path, {print, indent, maybe}) => {
6
+ module.exports.parseLeadingComments = (path, {print, indent}) => {
7
7
  const {leadingComments} = path.node;
8
8
 
9
- if (leadingComments)
10
- parseLeadingComments(path, {print, indent, maybe});
11
- };
12
-
13
- function parseLeadingComments(path, {print, indent}) {
14
- const {leadingComments} = path.node;
9
+ if (!leadingComments || !leadingComments.length)
10
+ return;
15
11
 
16
12
  if (shouldAddNewlineBefore(path))
17
13
  print.linebreak();
@@ -22,6 +18,7 @@ function parseLeadingComments(path, {print, indent}) {
22
18
  if (type === 'CommentLine') {
23
19
  print(`//${value}`);
24
20
  print.newline();
21
+
25
22
  continue;
26
23
  }
27
24
 
@@ -36,9 +33,24 @@ function parseLeadingComments(path, {print, indent}) {
36
33
  continue;
37
34
  }
38
35
  }
39
- }
36
+ };
37
+
38
+ module.exports.parseTrailingComments = (path, {print}) => {
39
+ const {trailingComments} = path.node;
40
+
41
+ if (!trailingComments || !trailingComments.length)
42
+ return;
43
+
44
+ for (const {type, value} of trailingComments) {
45
+ if (type === 'CommentLine') {
46
+ print.space();
47
+ print(`//${value}`);
48
+
49
+ continue;
50
+ }
51
+ }
52
+ };
40
53
 
41
54
  function shouldAddNewlineBefore(path) {
42
55
  return path.isStatement() && !isFirst(path);
43
56
  }
44
-
@@ -1,8 +1,15 @@
1
1
  'use strict';
2
2
 
3
+ const {stringify} = JSON;
4
+
3
5
  const {TYPES} = require('../types');
4
6
  const toSnakeCase = require('just-snake-case');
5
- const {LOG, DEBUG} = process.env;
7
+ const {
8
+ LOG,
9
+ LOG_ALL,
10
+ LOG_TOKENS,
11
+ DEBUG,
12
+ } = process.env;
6
13
  const {codeFrameColumns} = require('@babel/code-frame');
7
14
 
8
15
  module.exports.createDebug = (tokens) => (a) => {
@@ -15,18 +22,31 @@ module.exports.createDebug = (tokens) => (a) => {
15
22
  });
16
23
  };
17
24
 
18
- module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => (chunk) => {
19
- if (!LOG)
25
+ module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => ({type, value}) => {
26
+ if (LOG_TOKENS) {
27
+ console.log(codeFrameColumns(stringify({type, value}), {}, {
28
+ highlightCode: true,
29
+ }));
20
30
  return;
31
+ }
21
32
 
22
- if (chunk === newline) {
23
- console.log(codeFrameColumns(store(), {}, {
33
+ if (LOG_ALL) {
34
+ console.log(codeFrameColumns(value, {}, {
24
35
  highlightCode: true,
25
36
  }));
26
37
  return;
27
38
  }
28
39
 
29
- store(chunk);
40
+ if (LOG) {
41
+ if (value === newline) {
42
+ console.log(codeFrameColumns(store(), {}, {
43
+ highlightCode: true,
44
+ }));
45
+ return;
46
+ }
47
+
48
+ store(value);
49
+ }
30
50
  };
31
51
 
32
52
  function createStore() {
@@ -20,7 +20,7 @@ module.exports.CallExpression = CallExpression;
20
20
  function CallExpression(path, {indent, print, maybe}) {
21
21
  const isParentCall = toLong(path) && path.parentPath.isCallExpression();
22
22
 
23
- if (isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath)) {
23
+ if (shouldAddNewlineBefore(path)) {
24
24
  print.breakline();
25
25
  }
26
26
 
@@ -92,3 +92,7 @@ function toLong(path) {
92
92
 
93
93
  return false;
94
94
  }
95
+
96
+ function shouldAddNewlineBefore(path) {
97
+ return isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath);
98
+ }
@@ -1,14 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {entries} = Object;
4
-
5
- const isFirst = (path) => path.node === path.parentPath.node.body[0];
6
-
7
4
  module.exports.ClassDeclaration = (path, {maybe, print, indent}) => {
8
- if (!isFirst(path)) {
9
- print.breakline();
10
- }
11
-
12
5
  indent();
13
6
  print('class ');
14
7
  print('__id');
@@ -4,13 +4,19 @@ const functions = require('./functions');
4
4
  const unaryExpressions = require('./unary-expressions');
5
5
  const memberExpressions = require('./member-expressions');
6
6
  const {ClassDeclaration} = require('./class-declaration');
7
+
7
8
  const {
8
9
  CallExpression,
9
10
  OptionalCallExpression,
10
11
  } = require('./call-expression');
11
12
 
12
13
  const {NewExpression} = require('./new-expression');
13
- const {ObjectExpression} = require('./object-expression');
14
+
15
+ const {
16
+ ObjectExpression,
17
+ ObjectProperty,
18
+ } = require('./object-expression');
19
+
14
20
  const {ObjectPattern} = require('./object-pattern');
15
21
  const {AssignmentExpression} = require('./assignment-expression');
16
22
  const {ArrayExpression} = require('./array-expression');
@@ -20,10 +26,12 @@ const {RestElement} = require('./rest-element');
20
26
  const {SpreadElement} = require('./spread-element');
21
27
  const {SequenceExpression} = require('./sequence-expression');
22
28
  const {TaggedTemplateExpression} = require('./tagged-template-expression');
29
+
23
30
  const {
24
31
  BinaryExpression,
25
32
  LogicalExpression,
26
33
  } = require('./binary-expression');
34
+
27
35
  const {ConditionalExpression} = require('./conditional-expression');
28
36
 
29
37
  module.exports = {
@@ -42,6 +50,7 @@ module.exports = {
42
50
  LogicalExpression,
43
51
  OptionalCallExpression,
44
52
  ObjectExpression,
53
+ ObjectProperty,
45
54
  ObjectPattern,
46
55
  RestElement,
47
56
  SpreadElement,
@@ -3,6 +3,7 @@
3
3
  const {isCoupleLines} = require('../is');
4
4
  const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
5
5
  const isLogical = (path) => path.get('argument').isLogicalExpression();
6
+
6
7
  const isForOf = (path) => {
7
8
  if (path.parentPath.isForOfStatement())
8
9
  return true;
@@ -20,7 +21,6 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
20
21
 
21
22
  maybe.print(parens, '(');
22
23
  print('{');
23
-
24
24
  maybe.print(manyLines, '\n');
25
25
 
26
26
  for (const property of properties) {
@@ -36,8 +36,6 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
36
36
  continue;
37
37
  }
38
38
 
39
- const {shorthand, computed} = property.node;
40
-
41
39
  maybe.indent(manyLines);
42
40
 
43
41
  if (property.isObjectMethod()) {
@@ -45,25 +43,36 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
45
43
  continue;
46
44
  }
47
45
 
48
- maybe.print(computed, '[');
49
- print(property.get('key'));
50
- maybe.print(computed, ']');
51
-
52
- if (!shorthand) {
53
- print(': ');
54
- print(property.get('value'));
55
- }
56
-
57
- maybe.print(manyLines, ',\n');
46
+ print(property);
47
+ maybe.print.newline(manyLines);
58
48
  }
59
49
 
60
50
  indent.dec();
61
-
62
51
  maybe.indent(manyLines);
63
52
  print('}');
64
53
  maybe.print.newline(path.parentPath.isLogicalExpression());
65
54
  maybe.print(parens, ')');
66
55
  };
56
+
57
+ module.exports.ObjectProperty = (path, {print, maybe}) => {
58
+ const {
59
+ shorthand,
60
+ computed,
61
+ } = path.node;
62
+ const manyLines = !isOneLine(path.parentPath);
63
+
64
+ maybe.print(computed, '[');
65
+ print('__key');
66
+ maybe.print(computed, ']');
67
+
68
+ if (!shorthand) {
69
+ print(': ');
70
+ print('__value');
71
+ }
72
+
73
+ maybe.print(manyLines, ',');
74
+ };
75
+
67
76
  function isOneLine(path) {
68
77
  const isCall = path.parentPath.isCallExpression();
69
78
  const {length} = path.get('properties');
@@ -1,11 +1,15 @@
1
1
  'use strict';
2
2
 
3
+ const isParentProgram = (path) => path.parentPath?.isProgram();
4
+ const isParentBlock = (path) => path.parentPath.isBlockStatement();
5
+ const isNext = (path) => path.getNextSibling().node;
6
+
3
7
  module.exports.isFirst = (path) => path.node === path.parentPath.node.body[0];
4
8
  module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
5
- module.exports.isNext = (path) => path.getNextSibling().node;
6
- module.exports.isProgramParent = (path) => path.parentPath?.isProgram();
7
- module.exports.isNextParent = (path) => path.parentPath.getNextSibling().node;
8
- module.exports.isNextBlock = (path) => path.getNextSibling().isBlockStatement();
9
+ module.exports.isNext = isNext;
10
+ module.exports.isParentProgram = isParentProgram;
11
+ module.exports.isParentBlock = isParentBlock;
12
+ module.exports.isLast = (path) => isParentProgram(path) && !isNext(path);
9
13
 
10
14
  module.exports.isCoupleLines = (path) => {
11
15
  const start = path.node?.loc?.start?.line;
@@ -4,24 +4,24 @@ const {TemplateLiteral} = require('./template-literal');
4
4
 
5
5
  module.exports = {
6
6
  TemplateLiteral,
7
- NumericLiteral(path, {print}) {
7
+ NumericLiteral(path, {write}) {
8
8
  const {
9
9
  raw,
10
10
  extra,
11
11
  } = path.node;
12
12
 
13
- print(raw || extra.raw);
13
+ write(raw || extra.raw);
14
14
  },
15
- BooleanLiteral(path, {print}) {
16
- print(path.node.value);
15
+ BooleanLiteral(path, {write}) {
16
+ write(path.node.value);
17
17
  },
18
- StringLiteral(path, {print}) {
18
+ StringLiteral(path, {write}) {
19
19
  const {
20
20
  raw,
21
21
  value,
22
22
  } = path.node;
23
23
 
24
- print(raw || `'${value}'`);
24
+ write(raw || `'${value}'`);
25
25
  },
26
26
  Identifier(path, {write}) {
27
27
  write(path.node.name);
@@ -29,7 +29,10 @@ module.exports = {
29
29
  RegExpLiteral(path, {print}) {
30
30
  print(path.node.raw);
31
31
  },
32
- NullLiteral(path, {print}) {
33
- print('null');
32
+ NullLiteral(path, {write}) {
33
+ write('null');
34
+ },
35
+ MetaProperty(path, {write}) {
36
+ write('import.meta');
34
37
  },
35
38
  };
@@ -5,6 +5,7 @@ const WATER_MARK_AFTER = '__putout_newline_after';
5
5
 
6
6
  module.exports.markBefore = markBefore;
7
7
  module.exports.markAfter = markAfter;
8
+ module.exports.maybeMarkAfter = (a, path) => a && markAfter(path);
8
9
 
9
10
  function markBefore(path) {
10
11
  path[WATER_MARK_BEFORE] = true;
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ isProgram,
5
+ isFile,
6
+ File,
7
+ ExpressionStatement,
8
+ Program,
9
+ } = require('@babel/types');
10
+
11
+ const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
12
+ ExpressionStatement(ast),
13
+ ]);
14
+
15
+ module.exports.maybeFile = (ast) => isFile(ast) ? ast : File(maybeProgram(ast));
@@ -2,8 +2,9 @@
2
2
 
3
3
  const {
4
4
  isNext,
5
- isProgramParent,
5
+ isParentProgram,
6
6
  } = require('../is');
7
+
7
8
  const isFirstStatement = (path) => path.get('body.0')?.isStatement();
8
9
 
9
10
  module.exports.BlockStatement = (path, {indent, maybe, print}) => {
@@ -38,7 +39,7 @@ module.exports.BlockStatement = (path, {indent, maybe, print}) => {
38
39
  function shouldAddNewlineAfter(path) {
39
40
  const {parentPath} = path;
40
41
 
41
- if (!isNext(path) && !isNext(path.parentPath) && isProgramParent(path.parentPath))
42
+ if (!isNext(path) && !isNext(path.parentPath) && isParentProgram(path.parentPath))
42
43
  return false;
43
44
 
44
45
  if (parentPath.isStatement() && !path.node.body.length && !isNext(parentPath))
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ module.exports.ExportDefaultDeclaration = (path, {print}) => {
4
+ print('export default ');
5
+ print('__declaration');
6
+ print(';');
7
+ };
@@ -2,18 +2,26 @@
2
2
 
3
3
  const {
4
4
  markBefore,
5
- markAfter,
6
5
  hasPrevNewline,
7
6
  } = require('../mark');
8
7
 
9
8
  const {
10
9
  isNext,
11
- isProgramParent,
12
- isNextParent,
13
-
10
+ isParentProgram,
11
+ isLast,
12
+ isParentBlock,
14
13
  } = require('../is');
15
14
 
16
- module.exports.ExpressionStatement = (path, {indent, print}) => {
15
+ const isNextDifferent = (path) => {
16
+ const next = path.getNextSibling();
17
+
18
+ if (!next.isExpressionStatement())
19
+ return true;
20
+
21
+ return next.node.expression.type !== path.node.expression.type;
22
+ };
23
+
24
+ module.exports.ExpressionStatement = (path, {indent, print, maybe}) => {
17
25
  if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewlineBefore(path) && !hasPrevNewline(path)) {
18
26
  print.breakline();
19
27
  markBefore(path);
@@ -23,23 +31,50 @@ module.exports.ExpressionStatement = (path, {indent, print}) => {
23
31
  print('__expression');
24
32
  print(';');
25
33
 
26
- if (isNext(path) || isNextParent(path) || path.parentPath.isBlockStatement() && !isProgramParent(path)) {
34
+ let wasNewline = false;
35
+
36
+ if (shouldBreakline(path)) {
27
37
  print.newline();
38
+ maybe.indent(isNext(path));
39
+
40
+ wasNewline = true;
28
41
  }
29
42
 
30
43
  if (shouldAddNewLineAfter(path)) {
31
44
  print.newline();
32
- markAfter(path);
45
+ maybe.markAfter(wasNewline, path);
33
46
  }
34
47
  };
35
48
 
36
- function shouldAddNewLineAfter(path) {
37
- if (!isNext(path))
49
+ function shouldBreakline(path) {
50
+ if (isLast(path) || isLast(path.parentPath))
51
+ return false;
52
+
53
+ if (!isNext(path) && isParentBlock(path))
38
54
  return false;
39
55
 
56
+ if (path.parentPath.get('body') === path)
57
+ return true;
58
+
40
59
  if (isStrictMode(path) || isCoupleLinesExpression(path))
41
60
  return true;
42
61
 
62
+ if (isNext(path) && isNextDifferent(path) && path.parentPath.node.body?.length > 2)
63
+ return true;
64
+
65
+ return false;
66
+ }
67
+
68
+ function shouldAddNewLineAfter(path) {
69
+ if (isLast(path))
70
+ return false;
71
+
72
+ if (isParentBlock(path) && !isParentProgram(path))
73
+ return true;
74
+
75
+ if (isNext(path))
76
+ return true;
77
+
43
78
  return false;
44
79
  }
45
80
 
@@ -4,9 +4,8 @@ const {hasPrevNewline, markAfter} = require('../mark');
4
4
  const {isFirst} = require('../is');
5
5
 
6
6
  module.exports.IfStatement = (path, {indent, print}) => {
7
- if (!isFirst(path) && !hasPrevNewline(path)) {
8
- print.indent();
9
- print.newline();
7
+ if (shouldAddNewlineBefore(path)) {
8
+ print.linebreak();
10
9
  }
11
10
 
12
11
  indent();
@@ -31,6 +30,7 @@ module.exports.IfStatement = (path, {indent, print}) => {
31
30
  print(' else ');
32
31
  print(alternate);
33
32
  } else if (alternate.node) {
33
+ print.breakline();
34
34
  print('else');
35
35
  print.newline();
36
36
  indent.inc();
@@ -48,3 +48,6 @@ module.exports.IfStatement = (path, {indent, print}) => {
48
48
  markAfter(path);
49
49
  };
50
50
 
51
+ function shouldAddNewlineBefore(path) {
52
+ return !isFirst(path) && !hasPrevNewline(path);
53
+ }
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {markAfter} = require('../mark');
3
4
  const {entries} = Object;
4
5
 
5
6
  module.exports.ImportDeclaration = (path, {print, maybe}) => {
@@ -13,7 +14,7 @@ module.exports.ImportDeclaration = (path, {print, maybe}) => {
13
14
  }
14
15
 
15
16
  if (spec.isImportSpecifier()) {
16
- maybe.print(index, ', ');
17
+ maybe.print(Number(index), ', ');
17
18
  print('{');
18
19
  print(spec.get('imported'));
19
20
  print('}');
@@ -27,5 +28,16 @@ module.exports.ImportDeclaration = (path, {print, maybe}) => {
27
28
  print('__source');
28
29
  print(';');
29
30
  print.newline();
31
+
32
+ if (shouldAddNewlineAfter(path)) {
33
+ print.newline();
34
+ markAfter(path);
35
+ }
30
36
  };
31
37
 
38
+ function shouldAddNewlineAfter(path) {
39
+ if (path.getNextSibling().isImportDeclaration())
40
+ return false;
41
+
42
+ return true;
43
+ }
@@ -14,12 +14,14 @@ const exportDeclarations = require('./export-declarations');
14
14
  const {WhileStatement} = require('./while-statement');
15
15
  const {SwitchStatement} = require('./switch-statement');
16
16
  const {ForInStatement} = require('./for-in-statement');
17
+ const {ExportDefaultDeclaration} = require('./export-default-declaration');
17
18
 
18
19
  module.exports = {
19
20
  ...importDeclarations,
20
21
  ...exportDeclarations,
21
22
  BlockStatement,
22
23
  ExpressionStatement,
24
+ ExportDefaultDeclaration,
23
25
  VariableDeclaration,
24
26
  IfStatement,
25
27
  ForStatement,
@@ -3,9 +3,8 @@
3
3
  const {
4
4
  isNext,
5
5
  isCoupleLines,
6
- isNextBlock,
7
-
8
6
  } = require('../is');
7
+
9
8
  const {
10
9
  hasPrevNewline,
11
10
  markAfter,
@@ -26,6 +25,7 @@ module.exports.VariableDeclaration = (path, {maybe, print}) => {
26
25
  maybe.print(initPath.node, ' = ');
27
26
  print(initPath);
28
27
  maybe.print(isParentBlock(path), ';');
28
+
29
29
  let wasNewline = false;
30
30
 
31
31
  if (isParentBlock(path) && isNext(path)) {
@@ -44,9 +44,6 @@ function shouldAddNewlineAfter(path) {
44
44
  if (!isNext(path) && path.parentPath.isBlockStatement())
45
45
  return true;
46
46
 
47
- if (isNextBlock(path))
48
- return false;
49
-
50
47
  if (isLast(path))
51
48
  return false;
52
49
 
@@ -61,7 +58,6 @@ function shouldAddNewlineAfter(path) {
61
58
 
62
59
  return false;
63
60
  }
64
-
65
61
  const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
66
62
 
67
63
  function shouldAddNewlineBefore(path) {
@@ -81,15 +77,6 @@ function shouldAddNewlineBefore(path) {
81
77
 
82
78
  if (isCoupleLines(path))
83
79
  return true;
84
-
85
- const nextPath = path.getNextSibling();
86
- const nextNextPath = nextPath.getNextSibling();
87
- const twoNext = nextPath.node && nextNextPath.node;
88
-
89
- if (!twoNext)
90
- return false;
91
-
92
- return true;
93
80
  }
94
81
 
95
82
  function isFirst(path) {
@@ -103,4 +90,3 @@ const isNextAssign = (path) => {
103
90
 
104
91
  return nextPath.get('expression').isAssignmentExpression();
105
92
  };
106
-
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const {isLast} = require('../is');
4
+
3
5
  module.exports.WhileStatement = (path, {print, indent}) => {
4
6
  print('while (');
5
7
  print('__test');
@@ -15,5 +17,14 @@ module.exports.WhileStatement = (path, {print, indent}) => {
15
17
  indent.dec();
16
18
  }
17
19
 
18
- print.linebreak();
20
+ if (shouldAddNewlineAfter(path)) {
21
+ print.linebreak();
22
+ }
19
23
  };
24
+
25
+ function shouldAddNewlineAfter(path) {
26
+ if (isLast(path))
27
+ return false;
28
+
29
+ return true;
30
+ }
@@ -6,14 +6,23 @@ const expressions = require('./expressions');
6
6
  const statements = require('./statements');
7
7
  const literals = require('./literals');
8
8
  const {TYPES} = require('../types');
9
- const {createDebug, createLog} = require('./debug');
9
+ const {maybeFile} = require('./maybe');
10
+
11
+ const {
12
+ createDebug,
13
+ createLog,
14
+ } = require('./debug');
10
15
 
11
16
  const {
12
17
  maybeMarkAfter,
13
18
  maybeMarkBefore,
14
19
  } = require('./mark');
15
20
 
16
- const {parseComments} = require('./comments');
21
+ const {
22
+ parseLeadingComments,
23
+ parseTrailingComments,
24
+ } = require('./comments');
25
+
17
26
  const isString = (a) => typeof a === 'string';
18
27
  const {assign} = Object;
19
28
 
@@ -32,13 +41,11 @@ function initFormat(format) {
32
41
  indent: ' ',
33
42
  };
34
43
  }
35
-
36
44
  const createAddToken = (tokens) => {
37
45
  const log = createLog();
46
+
38
47
  return (token) => {
39
- const {value} = token;
40
-
41
- log(value);
48
+ log(token);
42
49
  tokens.push(token);
43
50
  };
44
51
  };
@@ -137,7 +144,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
137
144
  ...overrides.visitors,
138
145
  };
139
146
 
140
- babelTraverse(ast, {
147
+ babelTraverse(maybeFile(ast), {
141
148
  Program(path) {
142
149
  traverse(path);
143
150
  path.stop();
@@ -179,8 +186,9 @@ module.exports.tokenize = (ast, overrides = {}) => {
179
186
  if (!currentTraverse)
180
187
  throw Error(`Node type '${type}' is not supported yet: '${path}'`);
181
188
 
182
- parseComments(path, printer);
189
+ parseLeadingComments(path, printer);
183
190
  currentTraverse(path, printer);
191
+ parseTrailingComments(path, printer);
184
192
  debug(path.type);
185
193
  }
186
194
 
@@ -212,4 +220,3 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
212
220
 
213
221
  return write(maybeLine);
214
222
  };
215
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.8.11",
3
+ "version": "1.9.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",
@@ -49,7 +49,6 @@
49
49
  "eslint-plugin-putout": "^17.0.0",
50
50
  "estree-to-babel": "^5.0.1",
51
51
  "just-kebab-case": "^4.2.0",
52
- "lerna": "^6.0.1",
53
52
  "madrun": "^9.0.0",
54
53
  "mock-require": "^3.0.3",
55
54
  "montag": "^1.0.0",
@@ -1,11 +1,21 @@
1
1
  'use strict';
2
2
 
3
+ const {assign} = Object;
4
+
3
5
  module.exports.report = () => `Use print('__path') instead of path.get(__path)`;
4
6
 
5
7
  module.exports.replace = () => ({
6
8
  'print(path.get(__a))': ({__a}) => {
7
- __a.value = '__' + __a.value;
9
+ const {
10
+ raw,
11
+ value,
12
+ } = __a;
13
+
14
+ assign(__a, {
15
+ value: `__${value}`,
16
+ raw: raw.replace(value, `__${value}`),
17
+ });
18
+
8
19
  return 'print(__a)';
9
20
  },
10
21
  });
11
-