apotheke 0.1.1 → 0.1.2
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 +38 -11
- package/dist/index.js +38 -11
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -102,8 +102,12 @@ function deduplicateImports(imports) {
|
|
|
102
102
|
|
|
103
103
|
// src/grouper.ts
|
|
104
104
|
import path2 from "path";
|
|
105
|
-
|
|
105
|
+
|
|
106
|
+
// src/types.ts
|
|
106
107
|
var OTHERS_GROUP = "Others";
|
|
108
|
+
var SIDE_EFFECTS_BLOCK = "SideEffects";
|
|
109
|
+
|
|
110
|
+
// src/grouper.ts
|
|
107
111
|
function groupImports(imports, config, options = {}) {
|
|
108
112
|
const buckets = /* @__PURE__ */ new Map();
|
|
109
113
|
for (const node of imports) {
|
|
@@ -112,9 +116,6 @@ function groupImports(imports, config, options = {}) {
|
|
|
112
116
|
buckets.get(groupName).push(node);
|
|
113
117
|
}
|
|
114
118
|
const result = [];
|
|
115
|
-
if (buckets.has(SIDE_EFFECTS_GROUP)) {
|
|
116
|
-
result.push({ name: SIDE_EFFECTS_GROUP, imports: buckets.get(SIDE_EFFECTS_GROUP) });
|
|
117
|
-
}
|
|
118
119
|
for (const group of config.groups) {
|
|
119
120
|
if (buckets.has(group.name)) {
|
|
120
121
|
result.push({ name: group.name, imports: buckets.get(group.name) });
|
|
@@ -126,7 +127,6 @@ function groupImports(imports, config, options = {}) {
|
|
|
126
127
|
return result;
|
|
127
128
|
}
|
|
128
129
|
function assignGroup(node, config, options) {
|
|
129
|
-
if (node.isSideEffect) return SIDE_EFFECTS_GROUP;
|
|
130
130
|
const canonicalPath = resolveCanonicalPath(node.specifier, config, options);
|
|
131
131
|
for (const group of config.groups) {
|
|
132
132
|
for (const pattern of group.match) {
|
|
@@ -257,9 +257,10 @@ function printGroups(groups, config, q = "'") {
|
|
|
257
257
|
const parts = [];
|
|
258
258
|
for (const group of groups) {
|
|
259
259
|
const importLines = [];
|
|
260
|
+
const headed = useComments && group.name !== SIDE_EFFECTS_BLOCK;
|
|
260
261
|
for (let i = 0; i < group.imports.length; i++) {
|
|
261
262
|
const node = group.imports[i];
|
|
262
|
-
if (i === 0 &&
|
|
263
|
+
if (i === 0 && headed) {
|
|
263
264
|
importLines.push(`// ${group.name}
|
|
264
265
|
${buildImportStatement(node, q)}`);
|
|
265
266
|
continue;
|
|
@@ -329,6 +330,35 @@ function collectOrphanSegments(source, imports) {
|
|
|
329
330
|
}
|
|
330
331
|
return orphans;
|
|
331
332
|
}
|
|
333
|
+
function buildBlocks(imports, config, options) {
|
|
334
|
+
const blocks = [];
|
|
335
|
+
let values = [];
|
|
336
|
+
let sideEffects = [];
|
|
337
|
+
function flushValues() {
|
|
338
|
+
if (values.length === 0) return;
|
|
339
|
+
for (const group of groupImports(values, config, options)) {
|
|
340
|
+
blocks.push({ ...group, imports: sortGroup(group.imports) });
|
|
341
|
+
}
|
|
342
|
+
values = [];
|
|
343
|
+
}
|
|
344
|
+
function flushSideEffects() {
|
|
345
|
+
if (sideEffects.length === 0) return;
|
|
346
|
+
blocks.push({ name: SIDE_EFFECTS_BLOCK, imports: sideEffects });
|
|
347
|
+
sideEffects = [];
|
|
348
|
+
}
|
|
349
|
+
for (const node of imports) {
|
|
350
|
+
if (node.isSideEffect) {
|
|
351
|
+
flushValues();
|
|
352
|
+
sideEffects.push(node);
|
|
353
|
+
} else {
|
|
354
|
+
flushSideEffects();
|
|
355
|
+
values.push(node);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
flushSideEffects();
|
|
359
|
+
flushValues();
|
|
360
|
+
return blocks;
|
|
361
|
+
}
|
|
332
362
|
function formatImports(source, config, options = {}) {
|
|
333
363
|
const imports = parseImports(source);
|
|
334
364
|
if (imports.length === 0) return source;
|
|
@@ -336,12 +366,9 @@ function formatImports(source, config, options = {}) {
|
|
|
336
366
|
const clean = imports.map((n) => ({ ...n, attachedComment: void 0 }));
|
|
337
367
|
const deduped = deduplicateImports(clean);
|
|
338
368
|
const sorted = deduped.map(sortNamedImports);
|
|
339
|
-
const
|
|
340
|
-
const sortedGroups = grouped.map(
|
|
341
|
-
(g) => g.name === SIDE_EFFECTS_GROUP ? g : { ...g, imports: sortGroup(g.imports) }
|
|
342
|
-
);
|
|
369
|
+
const blocks = buildBlocks(sorted, config, options);
|
|
343
370
|
const q = detectQuoteChar(source);
|
|
344
|
-
const newImportBlock = printGroups(
|
|
371
|
+
const newImportBlock = printGroups(blocks, config, q);
|
|
345
372
|
const firstImport = imports[0];
|
|
346
373
|
const lastImport = imports[imports.length - 1];
|
|
347
374
|
let regionStart = firstImport.start;
|
package/dist/index.js
CHANGED
|
@@ -99,8 +99,12 @@ function deduplicateImports(imports) {
|
|
|
99
99
|
|
|
100
100
|
// src/grouper.ts
|
|
101
101
|
import path2 from "path";
|
|
102
|
-
|
|
102
|
+
|
|
103
|
+
// src/types.ts
|
|
103
104
|
var OTHERS_GROUP = "Others";
|
|
105
|
+
var SIDE_EFFECTS_BLOCK = "SideEffects";
|
|
106
|
+
|
|
107
|
+
// src/grouper.ts
|
|
104
108
|
function groupImports(imports, config, options = {}) {
|
|
105
109
|
const buckets = /* @__PURE__ */ new Map();
|
|
106
110
|
for (const node of imports) {
|
|
@@ -109,9 +113,6 @@ function groupImports(imports, config, options = {}) {
|
|
|
109
113
|
buckets.get(groupName).push(node);
|
|
110
114
|
}
|
|
111
115
|
const result = [];
|
|
112
|
-
if (buckets.has(SIDE_EFFECTS_GROUP)) {
|
|
113
|
-
result.push({ name: SIDE_EFFECTS_GROUP, imports: buckets.get(SIDE_EFFECTS_GROUP) });
|
|
114
|
-
}
|
|
115
116
|
for (const group of config.groups) {
|
|
116
117
|
if (buckets.has(group.name)) {
|
|
117
118
|
result.push({ name: group.name, imports: buckets.get(group.name) });
|
|
@@ -123,7 +124,6 @@ function groupImports(imports, config, options = {}) {
|
|
|
123
124
|
return result;
|
|
124
125
|
}
|
|
125
126
|
function assignGroup(node, config, options) {
|
|
126
|
-
if (node.isSideEffect) return SIDE_EFFECTS_GROUP;
|
|
127
127
|
const canonicalPath = resolveCanonicalPath(node.specifier, config, options);
|
|
128
128
|
for (const group of config.groups) {
|
|
129
129
|
for (const pattern of group.match) {
|
|
@@ -254,9 +254,10 @@ function printGroups(groups, config, q = "'") {
|
|
|
254
254
|
const parts = [];
|
|
255
255
|
for (const group of groups) {
|
|
256
256
|
const importLines = [];
|
|
257
|
+
const headed = useComments && group.name !== SIDE_EFFECTS_BLOCK;
|
|
257
258
|
for (let i = 0; i < group.imports.length; i++) {
|
|
258
259
|
const node = group.imports[i];
|
|
259
|
-
if (i === 0 &&
|
|
260
|
+
if (i === 0 && headed) {
|
|
260
261
|
importLines.push(`// ${group.name}
|
|
261
262
|
${buildImportStatement(node, q)}`);
|
|
262
263
|
continue;
|
|
@@ -326,6 +327,35 @@ function collectOrphanSegments(source, imports) {
|
|
|
326
327
|
}
|
|
327
328
|
return orphans;
|
|
328
329
|
}
|
|
330
|
+
function buildBlocks(imports, config, options) {
|
|
331
|
+
const blocks = [];
|
|
332
|
+
let values = [];
|
|
333
|
+
let sideEffects = [];
|
|
334
|
+
function flushValues() {
|
|
335
|
+
if (values.length === 0) return;
|
|
336
|
+
for (const group of groupImports(values, config, options)) {
|
|
337
|
+
blocks.push({ ...group, imports: sortGroup(group.imports) });
|
|
338
|
+
}
|
|
339
|
+
values = [];
|
|
340
|
+
}
|
|
341
|
+
function flushSideEffects() {
|
|
342
|
+
if (sideEffects.length === 0) return;
|
|
343
|
+
blocks.push({ name: SIDE_EFFECTS_BLOCK, imports: sideEffects });
|
|
344
|
+
sideEffects = [];
|
|
345
|
+
}
|
|
346
|
+
for (const node of imports) {
|
|
347
|
+
if (node.isSideEffect) {
|
|
348
|
+
flushValues();
|
|
349
|
+
sideEffects.push(node);
|
|
350
|
+
} else {
|
|
351
|
+
flushSideEffects();
|
|
352
|
+
values.push(node);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
flushSideEffects();
|
|
356
|
+
flushValues();
|
|
357
|
+
return blocks;
|
|
358
|
+
}
|
|
329
359
|
function formatImports(source, config, options = {}) {
|
|
330
360
|
const imports = parseImports(source);
|
|
331
361
|
if (imports.length === 0) return source;
|
|
@@ -333,12 +363,9 @@ function formatImports(source, config, options = {}) {
|
|
|
333
363
|
const clean = imports.map((n) => ({ ...n, attachedComment: void 0 }));
|
|
334
364
|
const deduped = deduplicateImports(clean);
|
|
335
365
|
const sorted = deduped.map(sortNamedImports);
|
|
336
|
-
const
|
|
337
|
-
const sortedGroups = grouped.map(
|
|
338
|
-
(g) => g.name === SIDE_EFFECTS_GROUP ? g : { ...g, imports: sortGroup(g.imports) }
|
|
339
|
-
);
|
|
366
|
+
const blocks = buildBlocks(sorted, config, options);
|
|
340
367
|
const q = detectQuoteChar(source);
|
|
341
|
-
const newImportBlock = printGroups(
|
|
368
|
+
const newImportBlock = printGroups(blocks, config, q);
|
|
342
369
|
const firstImport = imports[0];
|
|
343
370
|
const lastImport = imports[imports.length - 1];
|
|
344
371
|
let regionStart = firstImport.start;
|