1- import { isBlockComment } from './slang-utils/is-comment.js' ;
2- import { locEnd , locStart } from './slang-utils/loc.js' ;
3-
41import type { AstPath , Doc , ParserOptions } from 'prettier' ;
52import type {
63 AstNode ,
@@ -9,72 +6,17 @@ import type {
96} from './slang-nodes/types.d.ts' ;
107import type { PrintFunction } from './types.d.ts' ;
118
12- function hasNodeIgnoreComment ( { comments } : StrictAstNode ) : boolean {
13- // Prettier sets SourceUnit's comments to undefined after assigning comments
14- // to each node.
15- return Boolean (
16- comments ?. some (
17- ( comment ) =>
18- comment . value
19- . slice ( 2 , isBlockComment ( comment ) ? - 2 : undefined )
20- . trim ( ) === 'prettier-ignore'
21- )
22- ) ;
23- }
24-
25- function ignoreComments ( path : AstPath < StrictAstNode > ) : void {
26- const node = path . node ;
27- // We ignore anything that is not an object
28- if ( node === null || typeof node !== 'object' ) return ;
29-
30- let key : keyof StrictAstNode ;
31- for ( key in node ) {
32- switch ( key ) {
33- // We ignore `kind` and `loc` since these are added by the parser.
34- // `updateMetadata` is an internal function.
35- case 'kind' :
36- case 'loc' :
37- case 'updateMetadata' :
38- break ;
39- // The key `comments` will contain every comment for this node.
40- case 'comments' :
41- if ( node . comments !== undefined ) {
42- path . each ( ( { node } ) => ( node . printed = true ) , key ) ;
43- }
44- break ;
45- default :
46- // If the value for that key is an Array or an Object we go deeper.
47- const childNode = node [ key ] ;
48- if ( typeof childNode === 'object' ) {
49- if ( Array . isArray ( childNode ) ) {
50- path . each ( ignoreComments , key ) ;
51- break ;
52- }
53- path . call ( ignoreComments , key ) ;
54- }
55- }
56- }
57- }
58-
599// Nodes take care of undefined and string properties so we can restrict path
6010// to AstPath<StrictAstNode>
6111function genericPrint (
6212 path : AstPath < Exclude < StrictAstNode , StrictPolymorphicNode > > ,
6313 options : ParserOptions < AstNode > ,
6414 print : PrintFunction
6515) : Doc {
66- const node = path . node ;
67-
68- if ( hasNodeIgnoreComment ( node ) ) {
69- ignoreComments ( path ) ;
70-
71- return options . originalText . slice ( locStart ( node ) , locEnd ( node ) ) ;
72- }
73-
7416 // Since each node has a print function with a specific AstPath, the union of
7517 // all nodes into AstNode creates a print function with an AstPath of the
7618 // intersection of all nodes. This forces us to cast this with a never type.
77- return node . print ( path as AstPath < never > , print , options ) ;
19+ return path . node . print ( path as AstPath < never > , print , options ) ;
7820}
7921
8022export default genericPrint ;
0 commit comments