esrap 2.1.3 → 2.2.0

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/README.md CHANGED
@@ -55,7 +55,12 @@ const { code, map } = print(
55
55
  quotes: 'single',
56
56
 
57
57
  // an array of `{ type: 'Line' | 'Block', value: string, loc: { start, end } }` objects
58
- comments: []
58
+ comments: [],
59
+
60
+ // a pair of functions for inserting additional comments before or after a given node.
61
+ // returns `Array<{ type: 'Line' | 'Block', value: string }>` or `undefined`
62
+ getLeadingComments: (node) => [{ type: 'Line', value: ' a comment before the node' }],
63
+ getTrailingComments: (node) => [{ type: 'Block', value: ' a comment after the node' }]
59
64
  })
60
65
  );
61
66
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esrap",
3
- "version": "2.1.3",
3
+ "version": "2.2.0",
4
4
  "description": "Parse in reverse",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,6 +1,6 @@
1
1
  /** @import { TSESTree } from '@typescript-eslint/types' */
2
2
  /** @import { Visitors } from '../../types.js' */
3
- /** @import { TSOptions, Comment } from '../types.js' */
3
+ /** @import { TSOptions, BaseComment } from '../types.js' */
4
4
  import { Context } from 'esrap';
5
5
 
6
6
  /** @typedef {TSESTree.Node} Node */
@@ -74,7 +74,7 @@ const OPERATOR_PRECEDENCE = {
74
74
  };
75
75
 
76
76
  /**
77
- * @param {Comment} comment
77
+ * @param {BaseComment} comment
78
78
  * @param {Context} context
79
79
  */
80
80
  function write_comment(comment, context) {
@@ -90,6 +90,7 @@ function write_comment(comment, context) {
90
90
  }
91
91
 
92
92
  context.write('*/');
93
+ if (lines.length > 1) context.newline();
93
94
  }
94
95
  }
95
96
 
@@ -104,6 +105,36 @@ export default (options = {}) => {
104
105
 
105
106
  let comment_index = 0;
106
107
 
108
+ /**
109
+ * Write additional comments for a node
110
+ * @param {Context} context
111
+ * @param {BaseComment[] | undefined} comments
112
+ * @param {('leading' | 'trailing')} position
113
+ */
114
+ function write_additional_comments(context, comments, position) {
115
+ if (!comments) {
116
+ return;
117
+ }
118
+
119
+ for (let i = 0; i < comments.length; i += 1) {
120
+ const comment = comments[i];
121
+
122
+ if (position === 'trailing' && i === 0) {
123
+ context.write(' ');
124
+ }
125
+
126
+ write_comment(comment, context);
127
+
128
+ if (position === 'leading') {
129
+ if (comment.type === 'Line') {
130
+ context.newline();
131
+ } else if (comment.type === 'Block' && !comment.value.includes('\n')) {
132
+ context.write(' ');
133
+ }
134
+ }
135
+ }
136
+ }
137
+
107
138
  /**
108
139
  * Set `comment_index` to be the first comment after `start`.
109
140
  * Most of the time this is already correct, but if nodes
@@ -775,11 +806,15 @@ export default (options = {}) => {
775
806
 
776
807
  return {
777
808
  _(node, context, visit) {
809
+ write_additional_comments(context, options.getLeadingComments?.(node), 'leading');
810
+
778
811
  if (node.loc) {
779
812
  flush_comments_until(context, null, node.loc.start, true);
780
813
  }
781
814
 
782
815
  visit(node);
816
+
817
+ write_additional_comments(context, options.getTrailingComments?.(node), 'trailing');
783
818
  },
784
819
 
785
820
  AccessorProperty:
@@ -1,2 +1,2 @@
1
1
  export * from './index';
2
- export { Comment } from '../types';
2
+ export { BaseComment, Comment } from '../types';
@@ -1,2 +1,2 @@
1
1
  export * from './index';
2
- export { Comment } from '../types';
2
+ export { BaseComment, Comment } from '../types';
@@ -1,6 +1,10 @@
1
+ import { TSESTree } from '@typescript-eslint/types';
2
+
1
3
  export type TSOptions = {
2
4
  quotes?: 'double' | 'single';
3
5
  comments?: Comment[];
6
+ getLeadingComments?: (node: TSESTree.Node) => BaseComment[] | undefined;
7
+ getTrailingComments?: (node: TSESTree.Node) => BaseComment[] | undefined;
4
8
  };
5
9
 
6
10
  interface Position {
@@ -10,11 +14,14 @@ interface Position {
10
14
 
11
15
  // this exists in TSESTree but because of the inanity around enums
12
16
  // it's easier to do this ourselves
13
- export interface Comment {
17
+ export interface BaseComment {
14
18
  type: 'Line' | 'Block';
15
19
  value: string;
16
20
  start?: number;
17
21
  end?: number;
22
+ }
23
+
24
+ export interface Comment extends BaseComment {
18
25
  loc: {
19
26
  start: Position;
20
27
  end: Position;
package/types/index.d.ts CHANGED
@@ -77,6 +77,8 @@ declare module 'esrap/languages/ts' {
77
77
  type TSOptions = {
78
78
  quotes?: 'double' | 'single';
79
79
  comments?: Comment[];
80
+ getLeadingComments?: (node: TSESTree.Node) => BaseComment[] | undefined;
81
+ getTrailingComments?: (node: TSESTree.Node) => BaseComment[] | undefined;
80
82
  };
81
83
 
82
84
  interface Position {
@@ -86,11 +88,14 @@ declare module 'esrap/languages/ts' {
86
88
 
87
89
  // this exists in TSESTree but because of the inanity around enums
88
90
  // it's easier to do this ourselves
89
- export interface Comment {
91
+ export interface BaseComment {
90
92
  type: 'Line' | 'Block';
91
93
  value: string;
92
94
  start?: number;
93
95
  end?: number;
96
+ }
97
+
98
+ export interface Comment extends BaseComment {
94
99
  loc: {
95
100
  start: Position;
96
101
  end: Position;
@@ -127,6 +132,8 @@ declare module 'esrap/languages/tsx' {
127
132
  type TSOptions = {
128
133
  quotes?: 'double' | 'single';
129
134
  comments?: Comment[];
135
+ getLeadingComments?: (node: TSESTree.Node) => BaseComment[] | undefined;
136
+ getTrailingComments?: (node: TSESTree.Node) => BaseComment[] | undefined;
130
137
  };
131
138
 
132
139
  interface Position {
@@ -136,11 +143,14 @@ declare module 'esrap/languages/tsx' {
136
143
 
137
144
  // this exists in TSESTree but because of the inanity around enums
138
145
  // it's easier to do this ourselves
139
- export interface Comment {
146
+ export interface BaseComment {
140
147
  type: 'Line' | 'Block';
141
148
  value: string;
142
149
  start?: number;
143
150
  end?: number;
151
+ }
152
+
153
+ export interface Comment extends BaseComment {
144
154
  loc: {
145
155
  start: Position;
146
156
  end: Position;
@@ -16,6 +16,7 @@
16
16
  "Node",
17
17
  "TSOptions",
18
18
  "Position",
19
+ "BaseComment",
19
20
  "Comment"
20
21
  ],
21
22
  "sources": [
@@ -34,6 +35,6 @@
34
35
  null,
35
36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