Skip to content

Commit 57a1097

Browse files
authored
PrintableNode (#1498)
* Using `PrintableNode` as a more correct type for `StrictAstNode` * A specialized comment handler is not needed as prettier was always taking care of this * replacing all `AstNode` references for the more accurate `PrintableNode` * updating `hasPrettierIgnore` type
1 parent 68d920f commit 57a1097

189 files changed

Lines changed: 498 additions & 473 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
RequiredOptions,
1818
SupportLanguage
1919
} from 'prettier';
20-
import type { AstNode } from './slang-nodes/types.d.ts';
20+
import type { PrintableNode } from './slang-nodes/types.d.ts';
2121

2222
const slangParserId = 'slang';
2323
const antlrParserId = 'antlr';
@@ -40,7 +40,7 @@ const languages: SupportLanguage[] = [
4040

4141
// https://prettier.io/docs/en/plugins.html#parsers
4242
const antlrParser = { astFormat: antlrAstId, parse: antlrParse, ...loc };
43-
const slangParser: Parser<AstNode> = {
43+
const slangParser: Parser<PrintableNode> = {
4444
astFormat: slangAstId,
4545
parse: slangParse,
4646
locStart,
@@ -54,8 +54,7 @@ const parsers = {
5454

5555
const antlrCanAttachComment = ({ type }: { type: string }): boolean =>
5656
typeof type === 'string' && type !== 'BlockComment' && type !== 'LineComment';
57-
const canAttachComment = (node: AstNode): boolean =>
58-
typeof node !== 'string' &&
57+
const canAttachComment = (node: PrintableNode | undefined): boolean =>
5958
node !== undefined &&
6059
node.kind && // Make sure it's not Location
6160
!isComment(node);
@@ -73,7 +72,7 @@ const antlrPrinter = {
7372
print: antlrPrint,
7473
printComment: comments.printComment
7574
};
76-
const slangPrinter: Printer<AstNode> = {
75+
const slangPrinter: Printer<PrintableNode | undefined> = {
7776
canAttachComment,
7877
handleComments,
7978
isBlockComment,

src/slang-comments/handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { Comment } from '../slang-nodes/types.d.ts';
55
function handler(
66
comment: Comment,
77
text: string
8-
// options: ParserOptions<AstNode>,
9-
// ast: AstNode,
8+
// options: ParserOptions<PrintableNode>,
9+
// ast: PrintableNode,
1010
// isLastComment: boolean
1111
): boolean {
1212
const { precedingNode, enclosingNode, followingNode } = comment;

src/slang-comments/handlers/handle-storage-layout-specifier-comments.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/slang-comments/handlers/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import handleModifierInvocationComments from './handle-modifier-invocation-comme
1010
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
1111
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
1212
import handleSourceUnitMembersComments from './handle-source-unit-members-comments.js';
13-
import handleStorageLayoutSpecifierComments from './handle-storage-layout-specifier-comments.js';
1413
import handleStructDefinitionComments from './handle-struct-definition-comments.js';
1514
import handleWhileStatementComments from './handle-while-statement-comments.js';
1615
import handleYulBlockComments from './handle-yul-block-comments.js';
@@ -28,7 +27,6 @@ export default [
2827
handleParametersDeclarationComments,
2928
handlePositionalArgumentsDeclarationComments,
3029
handleSourceUnitMembersComments,
31-
handleStorageLayoutSpecifierComments,
3230
handleStructDefinitionComments,
3331
handleWhileStatementComments,
3432
handleYulBlockComments
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { Comment, StrictAstNode } from '../../slang-nodes/types.d.ts';
1+
import type { Comment, PrintableNode } from '../../slang-nodes/types.d.ts';
22

33
interface HandlerParams {
44
text: string;
5-
precedingNode?: StrictAstNode;
6-
enclosingNode?: StrictAstNode;
7-
followingNode?: StrictAstNode;
5+
precedingNode?: PrintableNode;
6+
enclosingNode?: PrintableNode;
7+
followingNode?: PrintableNode;
88
comment: Comment;
99
}

src/slang-comments/printer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { isComment } from '../slang-utils/is-comment.js';
22

33
import type { AstPath, Doc } from 'prettier';
4-
import type { AstNode } from '../slang-nodes/types.d.ts';
4+
import type { PrintableNode } from '../slang-nodes/types.d.ts';
55

6-
export function printComment({ node: comment }: AstPath<AstNode>): Doc {
6+
export function printComment({
7+
node: comment
8+
}: AstPath<PrintableNode | undefined>): Doc {
79
if (isComment(comment)) {
810
return comment.print();
911
}

src/slang-nodes/AdditiveExpression.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Expression } from './Expression.js';
99
import type * as ast from '@nomicfoundation/slang/ast';
1010
import type { AstPath, Doc, ParserOptions } from 'prettier';
1111
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
12-
import type { AstNode } from './types.d.ts';
12+
import type { PrintableNode } from './types.d.ts';
1313

1414
const tryToHug = createHugFunction(['%']);
1515

@@ -38,7 +38,7 @@ export class AdditiveExpression extends SlangNode {
3838
constructor(
3939
ast: ast.AdditiveExpression,
4040
collected: CollectedMetadata,
41-
options: ParserOptions<AstNode>
41+
options: ParserOptions<PrintableNode>
4242
) {
4343
super(ast, collected);
4444

@@ -59,7 +59,7 @@ export class AdditiveExpression extends SlangNode {
5959
print(
6060
path: AstPath<AdditiveExpression>,
6161
print: PrintFunction,
62-
options: ParserOptions<AstNode>
62+
options: ParserOptions<PrintableNode>
6363
): Doc {
6464
return printAdditiveExpression(this, path, print, options);
6565
}

src/slang-nodes/AndExpression.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Expression } from './Expression.js';
77
import type * as ast from '@nomicfoundation/slang/ast';
88
import type { AstPath, Doc, ParserOptions } from 'prettier';
99
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
10-
import type { AstNode } from './types.d.ts';
10+
import type { PrintableNode } from './types.d.ts';
1111

1212
export class AndExpression extends SlangNode {
1313
readonly kind = NonterminalKind.AndExpression;
@@ -21,7 +21,7 @@ export class AndExpression extends SlangNode {
2121
constructor(
2222
ast: ast.AndExpression,
2323
collected: CollectedMetadata,
24-
options: ParserOptions<AstNode>
24+
options: ParserOptions<PrintableNode>
2525
) {
2626
super(ast, collected);
2727

@@ -39,7 +39,7 @@ export class AndExpression extends SlangNode {
3939
print(
4040
path: AstPath<AndExpression>,
4141
print: PrintFunction,
42-
options: ParserOptions<AstNode>
42+
options: ParserOptions<PrintableNode>
4343
): Doc {
4444
return printLogicalOperation(this, path, print, options);
4545
}

src/slang-nodes/ArgumentsDeclaration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NamedArgumentsDeclaration } from './NamedArgumentsDeclaration.js';
77

88
import type { ParserOptions } from 'prettier';
99
import type { CollectedMetadata } from '../types.d.ts';
10-
import type { AstNode } from './types.d.ts';
10+
import type { PrintableNode } from './types.d.ts';
1111

1212
const createNonterminalVariant = createNonterminalVariantSimpleCreator<
1313
ast.ArgumentsDeclaration,
@@ -25,7 +25,7 @@ export class ArgumentsDeclaration extends SlangNode {
2525
constructor(
2626
ast: ast.ArgumentsDeclaration,
2727
collected: CollectedMetadata,
28-
options: ParserOptions<AstNode>
28+
options: ParserOptions<PrintableNode>
2929
) {
3030
super(ast, collected);
3131

src/slang-nodes/ArrayExpression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ArrayValues } from './ArrayValues.js';
55
import type * as ast from '@nomicfoundation/slang/ast';
66
import type { AstPath, Doc, ParserOptions } from 'prettier';
77
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
8-
import type { AstNode } from './types.d.ts';
8+
import type { PrintableNode } from './types.d.ts';
99

1010
export class ArrayExpression extends SlangNode {
1111
readonly kind = NonterminalKind.ArrayExpression;
@@ -15,7 +15,7 @@ export class ArrayExpression extends SlangNode {
1515
constructor(
1616
ast: ast.ArrayExpression,
1717
collected: CollectedMetadata,
18-
options: ParserOptions<AstNode>
18+
options: ParserOptions<PrintableNode>
1919
) {
2020
super(ast, collected);
2121

0 commit comments

Comments
 (0)