apotheke 0.1.3 → 0.1.5

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
@@ -234,6 +234,7 @@ 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) {
@@ -311,7 +312,29 @@ function sortNamedImports(node) {
311
312
  }
312
313
 
313
314
  // src/format.ts
314
- function collectOrphanSegments(source, imports) {
315
+ function printedHeaders(config) {
316
+ return /* @__PURE__ */ new Set([...config.groups.map((group) => `// ${group.name}`), `// ${OTHERS_GROUP}`]);
317
+ }
318
+ function isStrandedHeaders(segment, headers) {
319
+ const lines = segment.split("\n").filter((line) => line.trim() !== "");
320
+ return lines.length > 0 && lines.every((line) => headers.has(line.trim()));
321
+ }
322
+ function strandedHeaderStart(source, importStart, headers) {
323
+ let start;
324
+ let lineEnd = source.lastIndexOf("\n", importStart - 1);
325
+ while (lineEnd > -1) {
326
+ const lineStart = source.lastIndexOf("\n", lineEnd - 1) + 1;
327
+ const line = source.slice(lineStart, lineEnd).trim();
328
+ if (line !== "") {
329
+ if (!headers.has(line)) break;
330
+ start = lineStart;
331
+ }
332
+ if (lineStart === 0) break;
333
+ lineEnd = lineStart - 1;
334
+ }
335
+ return start;
336
+ }
337
+ function collectOrphanSegments(source, imports, headers) {
315
338
  const orphans = [];
316
339
  for (let i = 0; i < imports.length - 1; i++) {
317
340
  const gapStart = imports[i].end;
@@ -320,7 +343,7 @@ function collectOrphanSegments(source, imports) {
320
343
  const commentStart = imports[i + 1].attachedCommentStart;
321
344
  const cut = commentStart !== void 0 && commentStart >= gapStart && commentStart < gapEnd ? commentStart : gapEnd;
322
345
  const trimmed = source.slice(gapStart, cut).trim();
323
- if (trimmed) orphans.push(trimmed);
346
+ if (trimmed && !isStrandedHeaders(trimmed, headers)) orphans.push(trimmed);
324
347
  }
325
348
  return orphans;
326
349
  }
@@ -356,7 +379,8 @@ function buildBlocks(imports, config, options) {
356
379
  function formatImports(source, config, options = {}) {
357
380
  const imports = parseImports(source);
358
381
  if (imports.length === 0) return source;
359
- const orphans = collectOrphanSegments(source, imports);
382
+ const headers = printedHeaders(config);
383
+ const orphans = collectOrphanSegments(source, imports, headers);
360
384
  const clean = imports.map((n) => ({ ...n, attachedCommentStart: void 0 }));
361
385
  const deduped = deduplicateImports(clean);
362
386
  const sorted = deduped.map(sortNamedImports);
@@ -368,6 +392,8 @@ function formatImports(source, config, options = {}) {
368
392
  let regionStart = firstImport.start;
369
393
  if (firstImport.attachedCommentStart !== void 0) {
370
394
  regionStart = source.lastIndexOf("\n", firstImport.attachedCommentStart) + 1;
395
+ } else {
396
+ regionStart = strandedHeaderStart(source, firstImport.start, headers) ?? regionStart;
371
397
  }
372
398
  let regionEnd = lastImport.end;
373
399
  if (source[regionEnd] === "\n") regionEnd++;
package/dist/index.js CHANGED
@@ -231,6 +231,7 @@ 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) {
@@ -308,7 +309,29 @@ function sortNamedImports(node) {
308
309
  }
309
310
 
310
311
  // src/format.ts
311
- function collectOrphanSegments(source, imports) {
312
+ function printedHeaders(config) {
313
+ return /* @__PURE__ */ new Set([...config.groups.map((group) => `// ${group.name}`), `// ${OTHERS_GROUP}`]);
314
+ }
315
+ function isStrandedHeaders(segment, headers) {
316
+ const lines = segment.split("\n").filter((line) => line.trim() !== "");
317
+ return lines.length > 0 && lines.every((line) => headers.has(line.trim()));
318
+ }
319
+ function strandedHeaderStart(source, importStart, headers) {
320
+ let start;
321
+ let lineEnd = source.lastIndexOf("\n", importStart - 1);
322
+ while (lineEnd > -1) {
323
+ const lineStart = source.lastIndexOf("\n", lineEnd - 1) + 1;
324
+ const line = source.slice(lineStart, lineEnd).trim();
325
+ if (line !== "") {
326
+ if (!headers.has(line)) break;
327
+ start = lineStart;
328
+ }
329
+ if (lineStart === 0) break;
330
+ lineEnd = lineStart - 1;
331
+ }
332
+ return start;
333
+ }
334
+ function collectOrphanSegments(source, imports, headers) {
312
335
  const orphans = [];
313
336
  for (let i = 0; i < imports.length - 1; i++) {
314
337
  const gapStart = imports[i].end;
@@ -317,7 +340,7 @@ function collectOrphanSegments(source, imports) {
317
340
  const commentStart = imports[i + 1].attachedCommentStart;
318
341
  const cut = commentStart !== void 0 && commentStart >= gapStart && commentStart < gapEnd ? commentStart : gapEnd;
319
342
  const trimmed = source.slice(gapStart, cut).trim();
320
- if (trimmed) orphans.push(trimmed);
343
+ if (trimmed && !isStrandedHeaders(trimmed, headers)) orphans.push(trimmed);
321
344
  }
322
345
  return orphans;
323
346
  }
@@ -353,7 +376,8 @@ function buildBlocks(imports, config, options) {
353
376
  function formatImports(source, config, options = {}) {
354
377
  const imports = parseImports(source);
355
378
  if (imports.length === 0) return source;
356
- const orphans = collectOrphanSegments(source, imports);
379
+ const headers = printedHeaders(config);
380
+ const orphans = collectOrphanSegments(source, imports, headers);
357
381
  const clean = imports.map((n) => ({ ...n, attachedCommentStart: void 0 }));
358
382
  const deduped = deduplicateImports(clean);
359
383
  const sorted = deduped.map(sortNamedImports);
@@ -365,6 +389,8 @@ function formatImports(source, config, options = {}) {
365
389
  let regionStart = firstImport.start;
366
390
  if (firstImport.attachedCommentStart !== void 0) {
367
391
  regionStart = source.lastIndexOf("\n", firstImport.attachedCommentStart) + 1;
392
+ } else {
393
+ regionStart = strandedHeaderStart(source, firstImport.start, headers) ?? regionStart;
368
394
  }
369
395
  let regionEnd = lastImport.end;
370
396
  if (source[regionEnd] === "\n") regionEnd++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apotheke",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Prettier for imports — deterministic import organization as a Prettier plugin or CLI",
5
5
  "type": "module",
6
6
  "license": "MIT",