apotheke 0.1.2 → 0.1.4

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/dist/cli.js CHANGED
@@ -224,7 +224,7 @@ function parseImports(source) {
224
224
  if (defaultImport) importNode.defaultImport = defaultImport;
225
225
  if (namespaceImport) importNode.namespaceImport = namespaceImport;
226
226
  const attached = findAttachedComment(source, node.start, comments);
227
- if (attached) importNode.attachedComment = attached;
227
+ if (attached !== void 0) importNode.attachedCommentStart = attached;
228
228
  nodes.push(importNode);
229
229
  }
230
230
  return nodes;
@@ -234,10 +234,11 @@ function findAttachedComment(source, importStart, comments) {
234
234
  const importLine = linesBefore.length - 1;
235
235
  for (const comment of comments) {
236
236
  if (comment.type !== "Line") continue;
237
+ if (comment.value.startsWith("/")) continue;
237
238
  const commentLines = source.slice(0, comment.end).split("\n");
238
239
  const commentLine = commentLines.length - 1;
239
240
  if (commentLine === importLine - 1) {
240
- return `//${comment.value}`;
241
+ return comment.start;
241
242
  }
242
243
  }
243
244
  return void 0;
@@ -317,15 +318,9 @@ function collectOrphanSegments(source, imports) {
317
318
  const gapStart = imports[i].end;
318
319
  const gapEnd = imports[i + 1].start;
319
320
  if (gapEnd <= gapStart) continue;
320
- let gap = source.slice(gapStart, gapEnd);
321
- const nextComment = imports[i + 1].attachedComment;
322
- if (nextComment) {
323
- const commentText = nextComment.startsWith("//") ? nextComment.slice(2) : nextComment;
324
- const commentPattern = `// ${commentText.trim()}`;
325
- const idx = gap.lastIndexOf(commentPattern);
326
- if (idx !== -1) gap = gap.slice(0, idx);
327
- }
328
- const trimmed = gap.trim();
321
+ const commentStart = imports[i + 1].attachedCommentStart;
322
+ const cut = commentStart !== void 0 && commentStart >= gapStart && commentStart < gapEnd ? commentStart : gapEnd;
323
+ const trimmed = source.slice(gapStart, cut).trim();
329
324
  if (trimmed) orphans.push(trimmed);
330
325
  }
331
326
  return orphans;
@@ -363,7 +358,7 @@ function formatImports(source, config, options = {}) {
363
358
  const imports = parseImports(source);
364
359
  if (imports.length === 0) return source;
365
360
  const orphans = collectOrphanSegments(source, imports);
366
- const clean = imports.map((n) => ({ ...n, attachedComment: void 0 }));
361
+ const clean = imports.map((n) => ({ ...n, attachedCommentStart: void 0 }));
367
362
  const deduped = deduplicateImports(clean);
368
363
  const sorted = deduped.map(sortNamedImports);
369
364
  const blocks = buildBlocks(sorted, config, options);
@@ -372,10 +367,8 @@ function formatImports(source, config, options = {}) {
372
367
  const firstImport = imports[0];
373
368
  const lastImport = imports[imports.length - 1];
374
369
  let regionStart = firstImport.start;
375
- if (firstImport.attachedComment) {
376
- const before2 = source.slice(0, firstImport.start);
377
- const commentLineStart = before2.lastIndexOf("\n", before2.length - 2) + 1;
378
- regionStart = commentLineStart;
370
+ if (firstImport.attachedCommentStart !== void 0) {
371
+ regionStart = source.lastIndexOf("\n", firstImport.attachedCommentStart) + 1;
379
372
  }
380
373
  let regionEnd = lastImport.end;
381
374
  if (source[regionEnd] === "\n") regionEnd++;
package/dist/index.js CHANGED
@@ -221,7 +221,7 @@ function parseImports(source) {
221
221
  if (defaultImport) importNode.defaultImport = defaultImport;
222
222
  if (namespaceImport) importNode.namespaceImport = namespaceImport;
223
223
  const attached = findAttachedComment(source, node.start, comments);
224
- if (attached) importNode.attachedComment = attached;
224
+ if (attached !== void 0) importNode.attachedCommentStart = attached;
225
225
  nodes.push(importNode);
226
226
  }
227
227
  return nodes;
@@ -231,10 +231,11 @@ function findAttachedComment(source, importStart, comments) {
231
231
  const importLine = linesBefore.length - 1;
232
232
  for (const comment of comments) {
233
233
  if (comment.type !== "Line") continue;
234
+ if (comment.value.startsWith("/")) continue;
234
235
  const commentLines = source.slice(0, comment.end).split("\n");
235
236
  const commentLine = commentLines.length - 1;
236
237
  if (commentLine === importLine - 1) {
237
- return `//${comment.value}`;
238
+ return comment.start;
238
239
  }
239
240
  }
240
241
  return void 0;
@@ -314,15 +315,9 @@ function collectOrphanSegments(source, imports) {
314
315
  const gapStart = imports[i].end;
315
316
  const gapEnd = imports[i + 1].start;
316
317
  if (gapEnd <= gapStart) continue;
317
- let gap = source.slice(gapStart, gapEnd);
318
- const nextComment = imports[i + 1].attachedComment;
319
- if (nextComment) {
320
- const commentText = nextComment.startsWith("//") ? nextComment.slice(2) : nextComment;
321
- const commentPattern = `// ${commentText.trim()}`;
322
- const idx = gap.lastIndexOf(commentPattern);
323
- if (idx !== -1) gap = gap.slice(0, idx);
324
- }
325
- const trimmed = gap.trim();
318
+ const commentStart = imports[i + 1].attachedCommentStart;
319
+ const cut = commentStart !== void 0 && commentStart >= gapStart && commentStart < gapEnd ? commentStart : gapEnd;
320
+ const trimmed = source.slice(gapStart, cut).trim();
326
321
  if (trimmed) orphans.push(trimmed);
327
322
  }
328
323
  return orphans;
@@ -360,7 +355,7 @@ function formatImports(source, config, options = {}) {
360
355
  const imports = parseImports(source);
361
356
  if (imports.length === 0) return source;
362
357
  const orphans = collectOrphanSegments(source, imports);
363
- const clean = imports.map((n) => ({ ...n, attachedComment: void 0 }));
358
+ const clean = imports.map((n) => ({ ...n, attachedCommentStart: void 0 }));
364
359
  const deduped = deduplicateImports(clean);
365
360
  const sorted = deduped.map(sortNamedImports);
366
361
  const blocks = buildBlocks(sorted, config, options);
@@ -369,10 +364,8 @@ function formatImports(source, config, options = {}) {
369
364
  const firstImport = imports[0];
370
365
  const lastImport = imports[imports.length - 1];
371
366
  let regionStart = firstImport.start;
372
- if (firstImport.attachedComment) {
373
- const before2 = source.slice(0, firstImport.start);
374
- const commentLineStart = before2.lastIndexOf("\n", before2.length - 2) + 1;
375
- regionStart = commentLineStart;
367
+ if (firstImport.attachedCommentStart !== void 0) {
368
+ regionStart = source.lastIndexOf("\n", firstImport.attachedCommentStart) + 1;
376
369
  }
377
370
  let regionEnd = lastImport.end;
378
371
  if (source[regionEnd] === "\n") regionEnd++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apotheke",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Prettier for imports — deterministic import organization as a Prettier plugin or CLI",
5
5
  "type": "module",
6
6
  "license": "MIT",