Skip to content

Commit

Permalink
feature: @putout/printer: comments: improve
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 24, 2023
1 parent c359fc9 commit 66a867a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
5 changes: 5 additions & 0 deletions lib/tokenize/comment/comment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ test('printer: tokenize: comments: top', (t) => {
t.end();
});

test('printer: tokenize: comments: only', (t) => {
t.print(fixture.commentsOnly);
t.end();
});

test('printer: comments: after', (t) => {
t.print(fixture.commentsAfter);
t.end();
Expand Down
5 changes: 5 additions & 0 deletions lib/tokenize/comment/fixture/comments-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// const a = () => (
// <div>
// <a>&nbsp;</a>
// </div>
// );
17 changes: 14 additions & 3 deletions lib/tokenize/comment/parse-comments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

module.exports.parseComments = (path, {write}, semantics) => {
const {isNext} = require('../is');

module.exports.parseComments = (path, {write, maybe}, semantics) => {
if (!semantics.comments)
return;

Expand All @@ -9,11 +11,20 @@ module.exports.parseComments = (path, {write}, semantics) => {
if (!comments)
return;

for (const {type, value} of comments) {
const n = comments.length - 1;
const program = path.isProgram();

for (const [i, {type, value}] of comments.entries()) {
if (type === 'CommentLine') {
write.breakline();
maybe.write.breakline(isNext(path) || !program);
write('//');
write(value);

if (program) {
maybe.write.newline(i < n);
continue;
}

write.newline();
continue;
}
Expand Down
10 changes: 8 additions & 2 deletions lib/tokenize/expressions/object-expression/object-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ const isMemberExpressionCallee = ({parentPath}) => {
return likeChain(callee);
};

module.exports.ObjectExpression = (path, {print, maybe, indent, write}, semantics) => {
module.exports.ObjectExpression = (path, printer, semantics) => {
const {trailingComma} = semantics;
const {
print,
maybe,
indent,
} = printer;

indent.inc();

const properties = path.get('properties');
Expand All @@ -42,7 +48,7 @@ module.exports.ObjectExpression = (path, {print, maybe, indent, write}, semantic

maybe.print(parens, '(');
print('{');
parseComments(path, {write}, semantics);
parseComments(path, printer, semantics);
maybe.print.newline(manyLines);

const n = properties.length - 1;
Expand Down
10 changes: 8 additions & 2 deletions lib/tokenize/statements/block-statement/block-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ const parentIfWithoutElse = ({parentPath}) => {
};

module.exports.BlockStatement = {
print(path, {indent, maybe, write, traverse}, semantics) {
print(path, printer, semantics) {
const {
indent,
maybe,
write,
traverse,
} = printer;
const body = path.get('body');
const directives = getDirectives(path);

Expand All @@ -50,7 +56,7 @@ module.exports.BlockStatement = {
traverse(element);
}

parseComments(path, {write}, semantics);
parseComments(path, printer, semantics);

indent.dec();
maybe.indent(body.length);
Expand Down
10 changes: 8 additions & 2 deletions lib/tokenize/statements/program/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
const {parseComments} = require('../../comment/comment');
const {getDirectives} = require('../block-statement/get-directives');

module.exports.Program = (path, {print, write, traverse, maybe}, semantics) => {
module.exports.Program = (path, printer, semantics) => {
const {body} = path.node;
const {
print,
traverse,
maybe,
} = printer;

print('__interpreter');
parseComments(path, {write}, semantics);
parseComments(path, printer, semantics);

const directives = getDirectives(path);

Expand Down

0 comments on commit 66a867a

Please sign in to comment.