@openrewrite/rewrite 8.65.0-20251025-011321 → 8.65.0-20251025-111616

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.
@@ -98,81 +98,45 @@ function applyRemovedElementPrefix(removedElement, nextElement, preserveRemovedC
98
98
  }
99
99
  const removedPrefix = removedElement.prefix;
100
100
  const currentPrefix = nextElement.prefix;
101
- const currentComments = currentPrefix.comments || [];
102
- // If the next element has no comments, apply appropriate formatting
103
- if (currentComments.length === 0) {
104
- if (currentPrefix === removedPrefix) {
105
- return nextElement;
106
- }
107
- // If preserving comments, transfer file header comments and use removed element's whitespace
108
- if (preserveRemovedComments) {
109
- const removedComments = removedPrefix.comments || [];
110
- // Check if removed element has leading comments (not inline trailing comments)
111
- // These are likely file headers and should be preserved
112
- const removedWhitespace = removedPrefix.whitespace || '';
113
- const hasLeadingNewlineBeforeComments = removedComments.length === 0 || /[\r\n]/.test(removedWhitespace);
114
- // Transfer file header comments from removed element (non-inline comments only)
115
- const commentsToTransfer = hasLeadingNewlineBeforeComments ? removedComments : [];
116
- return (0, immer_1.produce)(nextElement, draft => {
117
- draft.prefix = Object.assign(Object.assign({}, removedPrefix), { comments: commentsToTransfer.length > 0 ? commentsToTransfer : [] });
118
- });
119
- }
120
- // When not preserving comments, preserve blank lines from the current prefix
121
- const removedWhitespace = removedPrefix.whitespace || '';
122
- const currentWhitespace = currentPrefix.whitespace || '';
123
- const removedNewlines = (removedWhitespace.match(/\r?\n/g) || []).length;
124
- const currentNewlines = (currentWhitespace.match(/\r?\n/g) || []).length;
125
- // If the current element has more newlines (i.e., blank lines), preserve them
126
- if (currentNewlines > removedNewlines) {
127
- return nextElement;
128
- }
129
- // Use removed element's whitespace but don't transfer comments from removed middle elements
130
- return (0, immer_1.produce)(nextElement, draft => {
131
- draft.prefix = {
132
- kind: removedPrefix.kind,
133
- whitespace: removedWhitespace,
134
- comments: []
135
- };
136
- });
101
+ if (currentPrefix === removedPrefix) {
102
+ return nextElement;
137
103
  }
138
- // The next element has comments - check if we need to remove inline/trailing line comments
139
- const currentWhitespace = currentPrefix.whitespace || '';
140
- const hasLeadingNewline = /[\r\n]/.test(currentWhitespace);
104
+ // Helper to count newlines in whitespace
105
+ const countNewlines = (ws) => ((ws === null || ws === void 0 ? void 0 : ws.match(/\r?\n/g)) || []).length;
106
+ // Helper to check if whitespace has leading newline (comments are on their own line, not inline)
107
+ const hasLeadingNewline = (ws) => /[\r\n]/.test(ws || '');
108
+ const removedWs = removedPrefix.whitespace || '';
109
+ const currentWs = currentPrefix.whitespace || '';
110
+ const removedComments = removedPrefix.comments || [];
111
+ const currentComments = currentPrefix.comments || [];
112
+ // Filter out inline trailing line comments from current element
141
113
  let commentsToKeep = currentComments;
142
- // Check for truly inline trailing line comments that should be removed
143
- if (currentComments.length > 0) {
114
+ if (currentComments.length > 0 && !hasLeadingNewline(currentWs)) {
144
115
  const firstComment = currentComments[0];
145
116
  const commentText = firstComment.text || firstComment.message || '';
146
117
  const isLineComment = commentText.includes('//') || firstComment.multiline === false;
147
- if (isLineComment && !hasLeadingNewline) {
148
- // Only remove comments that are truly inline (no newline before them)
149
- // Comments on their own line (with leading newline) are preserved as they're
150
- // likely leading comments for the next element, not trailing comments from removed element
118
+ if (isLineComment) {
151
119
  commentsToKeep = currentComments.slice(1);
152
120
  }
153
121
  }
154
- // If preserving comments, preserve leading comments from removed element
155
- if (preserveRemovedComments) {
156
- const removedComments = removedPrefix.comments || [];
157
- // Only transfer leading comments (not inline trailing comments)
158
- const removedWhitespace = removedPrefix.whitespace || '';
159
- const hasLeadingComments = removedComments.length === 0 || /[\r\n]/.test(removedWhitespace);
160
- // Combine leading comments from removed element with filtered comments from current element
161
- const allComments = hasLeadingComments ? [...removedComments, ...commentsToKeep] : commentsToKeep;
162
- // When preserving comments, always use the removed element's whitespace
163
- return (0, immer_1.produce)(nextElement, draft => {
164
- draft.prefix = Object.assign(Object.assign({}, removedPrefix), { comments: allComments.length > 0 ? allComments : [] });
165
- });
122
+ // Determine which comments to include in final prefix
123
+ let finalComments;
124
+ if (preserveRemovedComments && hasLeadingNewline(removedWs)) {
125
+ // Transfer leading comments from removed element
126
+ finalComments = [...removedComments, ...commentsToKeep];
166
127
  }
167
- // If we still have comments after filtering, use removed element's whitespace with filtered comments
168
- if (commentsToKeep.length > 0) {
169
- return (0, immer_1.produce)(nextElement, draft => {
170
- draft.prefix = Object.assign(Object.assign({}, removedPrefix), { comments: commentsToKeep });
171
- });
128
+ else {
129
+ finalComments = commentsToKeep;
172
130
  }
173
- // No comments left after filtering, use removed element's prefix entirely
131
+ // Determine which whitespace to use: preserve current if it has more blank lines
132
+ const shouldPreserveCurrentWhitespace = !preserveRemovedComments &&
133
+ countNewlines(currentWs) > countNewlines(removedWs);
174
134
  return (0, immer_1.produce)(nextElement, draft => {
175
- draft.prefix = removedPrefix;
135
+ draft.prefix = {
136
+ kind: shouldPreserveCurrentWhitespace ? currentPrefix.kind : removedPrefix.kind,
137
+ whitespace: shouldPreserveCurrentWhitespace ? currentWs : removedWs,
138
+ comments: finalComments.length > 0 ? finalComments : []
139
+ };
176
140
  });
177
141
  }
178
142
  //# sourceMappingURL=formatting-utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatting-utils.js","sourceRoot":"","sources":["../../src/java/formatting-utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAGH,iCAA8B;AAE9B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,uBAAuB;IAKhC;;;OAGG;IACH,YAA6B,+BAAwC,KAAK;QAA7C,iCAA4B,GAA5B,4BAA4B,CAAiB;QAPlE,cAAS,GAAG,CAAC,CAAC;QACd,iBAAY,GAAG,CAAC,CAAC;IAMoD,CAAC;IAE9E;;OAEG;IACH,IAAI,WAAW;QACX,OAAO,IAAI,CAAC,YAAY,GAAG,CAAC,CAAC;IACjC,CAAC;IAED;;OAEG;IACH,WAAW,CAAC,IAAO;;QACf,MAAA,IAA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CAAC,aAAa,CAAC,CAAC,CAAC,EAAE;SAC1D,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC"}
package/dist/version.txt CHANGED
@@ -1 +1 @@
1
- 8.65.0-20251025-010219
1
+ 8.65.0-20251025-110017
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openrewrite/rewrite",
3
- "version": "8.65.0-20251025-011321",
3
+ "version": "8.65.0-20251025-111616",
4
4
  "license": "Moderne Source Available License",
5
5
  "description": "OpenRewrite JavaScript.",
6
6
  "homepage": "https://github.com/openrewrite/rewrite",
@@ -102,107 +102,52 @@ function applyRemovedElementPrefix<T extends J>(removedElement: J, nextElement:
102
102
 
103
103
  const removedPrefix = removedElement.prefix;
104
104
  const currentPrefix = nextElement.prefix;
105
- const currentComments = currentPrefix.comments || [];
106
-
107
- // If the next element has no comments, apply appropriate formatting
108
- if (currentComments.length === 0) {
109
- if (currentPrefix === removedPrefix) {
110
- return nextElement;
111
- }
112
-
113
- // If preserving comments, transfer file header comments and use removed element's whitespace
114
- if (preserveRemovedComments) {
115
- const removedComments = removedPrefix.comments || [];
116
-
117
- // Check if removed element has leading comments (not inline trailing comments)
118
- // These are likely file headers and should be preserved
119
- const removedWhitespace = removedPrefix.whitespace || '';
120
- const hasLeadingNewlineBeforeComments = removedComments.length === 0 || /[\r\n]/.test(removedWhitespace);
121
105
 
122
- // Transfer file header comments from removed element (non-inline comments only)
123
- const commentsToTransfer = hasLeadingNewlineBeforeComments ? removedComments : [];
124
-
125
- return produce(nextElement, draft => {
126
- draft.prefix = {
127
- ...removedPrefix,
128
- comments: commentsToTransfer.length > 0 ? commentsToTransfer : []
129
- };
130
- });
131
- }
106
+ if (currentPrefix === removedPrefix) {
107
+ return nextElement;
108
+ }
132
109
 
133
- // When not preserving comments, preserve blank lines from the current prefix
134
- const removedWhitespace = removedPrefix.whitespace || '';
135
- const currentWhitespace = currentPrefix.whitespace || '';
136
- const removedNewlines = (removedWhitespace.match(/\r?\n/g) || []).length;
137
- const currentNewlines = (currentWhitespace.match(/\r?\n/g) || []).length;
110
+ // Helper to count newlines in whitespace
111
+ const countNewlines = (ws: string | undefined) => (ws?.match(/\r?\n/g) || []).length;
138
112
 
139
- // If the current element has more newlines (i.e., blank lines), preserve them
140
- if (currentNewlines > removedNewlines) {
141
- return nextElement;
142
- }
113
+ // Helper to check if whitespace has leading newline (comments are on their own line, not inline)
114
+ const hasLeadingNewline = (ws: string | undefined) => /[\r\n]/.test(ws || '');
143
115
 
144
- // Use removed element's whitespace but don't transfer comments from removed middle elements
145
- return produce(nextElement, draft => {
146
- draft.prefix = {
147
- kind: removedPrefix.kind,
148
- whitespace: removedWhitespace,
149
- comments: []
150
- };
151
- });
152
- }
153
-
154
- // The next element has comments - check if we need to remove inline/trailing line comments
155
- const currentWhitespace = currentPrefix.whitespace || '';
156
- const hasLeadingNewline = /[\r\n]/.test(currentWhitespace);
116
+ const removedWs = removedPrefix.whitespace || '';
117
+ const currentWs = currentPrefix.whitespace || '';
118
+ const removedComments = removedPrefix.comments || [];
119
+ const currentComments = currentPrefix.comments || [];
157
120
 
121
+ // Filter out inline trailing line comments from current element
158
122
  let commentsToKeep = currentComments;
159
-
160
- // Check for truly inline trailing line comments that should be removed
161
- if (currentComments.length > 0) {
123
+ if (currentComments.length > 0 && !hasLeadingNewline(currentWs)) {
162
124
  const firstComment: any = currentComments[0];
163
125
  const commentText = firstComment.text || firstComment.message || '';
164
126
  const isLineComment = commentText.includes('//') || firstComment.multiline === false;
165
-
166
- if (isLineComment && !hasLeadingNewline) {
167
- // Only remove comments that are truly inline (no newline before them)
168
- // Comments on their own line (with leading newline) are preserved as they're
169
- // likely leading comments for the next element, not trailing comments from removed element
127
+ if (isLineComment) {
170
128
  commentsToKeep = currentComments.slice(1);
171
129
  }
172
130
  }
173
131
 
174
- // If preserving comments, preserve leading comments from removed element
175
- if (preserveRemovedComments) {
176
- const removedComments = removedPrefix.comments || [];
177
-
178
- // Only transfer leading comments (not inline trailing comments)
179
- const removedWhitespace = removedPrefix.whitespace || '';
180
- const hasLeadingComments = removedComments.length === 0 || /[\r\n]/.test(removedWhitespace);
181
-
182
- // Combine leading comments from removed element with filtered comments from current element
183
- const allComments = hasLeadingComments ? [...removedComments, ...commentsToKeep] : commentsToKeep;
184
-
185
- // When preserving comments, always use the removed element's whitespace
186
- return produce(nextElement, draft => {
187
- draft.prefix = {
188
- ...removedPrefix,
189
- comments: allComments.length > 0 ? allComments : []
190
- };
191
- });
132
+ // Determine which comments to include in final prefix
133
+ let finalComments: any[];
134
+ if (preserveRemovedComments && hasLeadingNewline(removedWs)) {
135
+ // Transfer leading comments from removed element
136
+ finalComments = [...removedComments, ...commentsToKeep];
137
+ } else {
138
+ finalComments = commentsToKeep;
192
139
  }
193
140
 
194
- // If we still have comments after filtering, use removed element's whitespace with filtered comments
195
- if (commentsToKeep.length > 0) {
196
- return produce(nextElement, draft => {
197
- draft.prefix = {
198
- ...removedPrefix,
199
- comments: commentsToKeep
200
- };
201
- });
202
- }
141
+ // Determine which whitespace to use: preserve current if it has more blank lines
142
+ const shouldPreserveCurrentWhitespace =
143
+ !preserveRemovedComments &&
144
+ countNewlines(currentWs) > countNewlines(removedWs);
203
145
 
204
- // No comments left after filtering, use removed element's prefix entirely
205
146
  return produce(nextElement, draft => {
206
- draft.prefix = removedPrefix;
147
+ draft.prefix = {
148
+ kind: shouldPreserveCurrentWhitespace ? currentPrefix.kind : removedPrefix.kind,
149
+ whitespace: shouldPreserveCurrentWhitespace ? currentWs : removedWs,
150
+ comments: finalComments.length > 0 ? finalComments : []
151
+ };
207
152
  });
208
153
  }