Skip to content

Commit

Permalink
chore: comments in function body
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonstudio committed Feb 2, 2024
1 parent 0f83e5f commit 64377fc
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/generator/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export const generate = async (ast: SyntaxNode, options: GenerateOptions = {}):
return prettier.format(JSON.stringify(ast), {
...options,
parser: PrettierGenerator.name,
plugins: [generatorPlugin],
plugins: [...(options.plugins ?? []), generatorPlugin],
});
};
8 changes: 5 additions & 3 deletions src/prettier/parser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Parser, ParserOptions } from 'prettier';
import type { Parser, ParserOptions } from 'prettier';
import { SyntaxNode } from '../ast';
import { PrettierPrinter } from './printer';
import { SyntaxToken, parse, tokenizer } from '../parser';
import { WithComments, comments } from './printers/base';

export const getCommentTokens = (tokens: SyntaxToken[]) => {
return tokens.filter((token) => comments.includes(token.type));
return tokens
.filter((token) => comments.includes(token.type))
.map((c) => ({ ...c, value: c.text }));
};

export class PrettierParser implements Parser<SyntaxNode> {
Expand All @@ -16,7 +18,7 @@ export class PrettierParser implements Parser<SyntaxNode> {
public parse = (text: string, _options: ParserOptions<SyntaxNode>) => {
const ast = parse(text, { tolerant: true }) as WithComments<SyntaxNode>;
const tokens = tokenizer(text, { tolerant: true });
if (ast) (<any>ast).comments = getCommentTokens(tokens).map((c) => ({ ...c, value: c.text }));
if (ast) (<any>ast).comments = getCommentTokens(tokens);
return ast;
};
}
2 changes: 1 addition & 1 deletion src/prettier/printer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ast from '../ast';
import { AstPath, Doc, ParserOptions, Printer } from 'prettier';
import type { AstPath, Doc, ParserOptions, Printer } from 'prettier';
import {
printComment,
isBlockComment,
Expand Down
9 changes: 7 additions & 2 deletions src/prettier/printers/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as ast from '../../ast';
import { type AstPath, type Doc, type ParserOptions, doc, util } from 'prettier';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import * as doc from 'prettier/doc';
import * as prettier from 'prettier/standalone';
import { SyntaxTokenType, SyntaxToken } from '../../parser';
import { printComment } from './comment';

Expand Down Expand Up @@ -55,7 +57,10 @@ export class BasePrinter {

// The pangu space
pangu = (path: AstPath<any>) => {
return util.isNextLineEmpty(this.options.originalText, this.options.locEnd(path.node))
return (prettier as any).util.isNextLineEmpty(
this.options.originalText,
this.options.locEnd(path.node),
)
? this.builders.hardline
: '';
};
Expand Down
2 changes: 1 addition & 1 deletion src/prettier/printers/comment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { doc } from 'prettier';
import * as doc from 'prettier/doc';
import type { Printer } from 'prettier';
import { blockComments, comments, CommentToken } from './base';

Expand Down
8 changes: 6 additions & 2 deletions src/prettier/printers/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ export class PrinterStatement
return this.builders.group(parts);
};
printBlock: PrintFunc<ast.Block> = ({ node, path, print }) => {
const statements = path.map((p) => [print(p), this.pangu(p)], 'statements');
const statements: Doc[] = [];
path.map((p) => statements.push([print(p), this.pangu(p)]), 'statements');
if (node.comments?.length) {
statements.push(...this.comments(path));
}
const parts = this.block(this.builders.join(this.builders.line, statements), {
empty: !node.statements.length,
empty: !statements.length,
shouldBreak: true,
});
if (node.unchecked) {
Expand Down
11 changes: 7 additions & 4 deletions src/tests/__files__/comment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ contract Comment {
}
}

function test() pure {
// hhh11
}

abstract contract aloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongName is Foo("aloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongName", "aloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongName"), aloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongName2 {}


//////


contract Foo {
// hhh
}

0 comments on commit 64377fc

Please sign in to comment.