apotheke 0.1.2 → 0.1.3

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;
@@ -237,7 +237,7 @@ function findAttachedComment(source, importStart, comments) {
237
237
  const commentLines = source.slice(0, comment.end).split("\n");
238
238
  const commentLine = commentLines.length - 1;
239
239
  if (commentLine === importLine - 1) {
240
- return `//${comment.value}`;
240
+ return comment.start;
241
241
  }
242
242
  }
243
243
  return void 0;
@@ -317,15 +317,9 @@ function collectOrphanSegments(source, imports) {
317
317
  const gapStart = imports[i].end;
318
318
  const gapEnd = imports[i + 1].start;
319
319
  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();
320
+ const commentStart = imports[i + 1].attachedCommentStart;
321
+ const cut = commentStart !== void 0 && commentStart >= gapStart && commentStart < gapEnd ? commentStart : gapEnd;
322
+ const trimmed = source.slice(gapStart, cut).trim();
329
323
  if (trimmed) orphans.push(trimmed);
330
324
  }
331
325
  return orphans;
@@ -363,7 +357,7 @@ function formatImports(source, config, options = {}) {
363
357
  const imports = parseImports(source);
364
358
  if (imports.length === 0) return source;
365
359
  const orphans = collectOrphanSegments(source, imports);
366
- const clean = imports.map((n) => ({ ...n, attachedComment: void 0 }));
360
+ const clean = imports.map((n) => ({ ...n, attachedCommentStart: void 0 }));
367
361
  const deduped = deduplicateImports(clean);
368
362
  const sorted = deduped.map(sortNamedImports);
369
363
  const blocks = buildBlocks(sorted, config, options);
@@ -372,10 +366,8 @@ function formatImports(source, config, options = {}) {
372
366
  const firstImport = imports[0];
373
367
  const lastImport = imports[imports.length - 1];
374
368
  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;
369
+ if (firstImport.attachedCommentStart !== void 0) {
370
+ regionStart = source.lastIndexOf("\n", firstImport.attachedCommentStart) + 1;
379
371
  }
380
372
  let regionEnd = lastImport.end;
381
373
  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;
@@ -234,7 +234,7 @@ function findAttachedComment(source, importStart, comments) {
234
234
  const commentLines = source.slice(0, comment.end).split("\n");
235
235
  const commentLine = commentLines.length - 1;
236
236
  if (commentLine === importLine - 1) {
237
- return `//${comment.value}`;
237
+ return comment.start;
238
238
  }
239
239
  }
240
240
  return void 0;
@@ -314,15 +314,9 @@ function collectOrphanSegments(source, imports) {
314
314
  const gapStart = imports[i].end;
315
315
  const gapEnd = imports[i + 1].start;
316
316
  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();
317
+ const commentStart = imports[i + 1].attachedCommentStart;
318
+ const cut = commentStart !== void 0 && commentStart >= gapStart && commentStart < gapEnd ? commentStart : gapEnd;
319
+ const trimmed = source.slice(gapStart, cut).trim();
326
320
  if (trimmed) orphans.push(trimmed);
327
321
  }
328
322
  return orphans;
@@ -360,7 +354,7 @@ function formatImports(source, config, options = {}) {
360
354
  const imports = parseImports(source);
361
355
  if (imports.length === 0) return source;
362
356
  const orphans = collectOrphanSegments(source, imports);
363
- const clean = imports.map((n) => ({ ...n, attachedComment: void 0 }));
357
+ const clean = imports.map((n) => ({ ...n, attachedCommentStart: void 0 }));
364
358
  const deduped = deduplicateImports(clean);
365
359
  const sorted = deduped.map(sortNamedImports);
366
360
  const blocks = buildBlocks(sorted, config, options);
@@ -369,10 +363,8 @@ function formatImports(source, config, options = {}) {
369
363
  const firstImport = imports[0];
370
364
  const lastImport = imports[imports.length - 1];
371
365
  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;
366
+ if (firstImport.attachedCommentStart !== void 0) {
367
+ regionStart = source.lastIndexOf("\n", firstImport.attachedCommentStart) + 1;
376
368
  }
377
369
  let regionEnd = lastImport.end;
378
370
  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.3",
4
4
  "description": "Prettier for imports — deterministic import organization as a Prettier plugin or CLI",
5
5
  "type": "module",
6
6
  "license": "MIT",