@storm-software/eslint-plugin-bun 0.0.6 → 0.0.8

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.
Files changed (34) hide show
  1. package/README.md +1 -1
  2. package/dist/configs/index.d.mts +5 -0
  3. package/dist/configs/index.mjs +4 -943
  4. package/dist/configs/recommended.d.mts +5 -0
  5. package/dist/configs/recommended.mjs +33 -939
  6. package/dist/helpers/bun-workspace.d.mts +49 -0
  7. package/dist/helpers/bun-workspace.mjs +197 -244
  8. package/dist/helpers/{create-rule.d.ts → create-rule.d.mts} +6 -6
  9. package/dist/helpers/create-rule.mjs +37 -38
  10. package/dist/helpers/{iterate-dependencies.d.ts → iterate-dependencies.d.mts} +7 -7
  11. package/dist/helpers/iterate-dependencies.mjs +36 -56
  12. package/dist/index.d.mts +52 -0
  13. package/dist/index.mjs +29 -951
  14. package/dist/plugin-DfH7W_YX.mjs +20 -0
  15. package/dist/plugin.d.mts +31 -0
  16. package/dist/plugin.mjs +1 -807
  17. package/dist/rules/json-enforce-catalog.d.mts +14 -0
  18. package/dist/rules/json-enforce-catalog.mjs +102 -444
  19. package/dist/rules/json-no-duplicate-catalog-item.d.mts +8 -0
  20. package/dist/rules/json-no-duplicate-catalog-item.mjs +72 -386
  21. package/dist/rules/json-no-unused-catalog-item.d.mts +5 -0
  22. package/dist/rules/json-no-unused-catalog-item.mjs +70 -423
  23. package/dist/rules/json-valid-catalog.d.mts +11 -0
  24. package/dist/rules/json-valid-catalog.mjs +80 -422
  25. package/package.json +17 -23
  26. package/dist/configs/index.d.ts +0 -5
  27. package/dist/configs/recommended.d.ts +0 -5
  28. package/dist/helpers/bun-workspace.d.ts +0 -48
  29. package/dist/index.d.ts +0 -58
  30. package/dist/plugin.d.ts +0 -33
  31. package/dist/rules/json-enforce-catalog.d.ts +0 -18
  32. package/dist/rules/json-no-duplicate-catalog-item.d.ts +0 -12
  33. package/dist/rules/json-no-unused-catalog-item.d.ts +0 -7
  34. package/dist/rules/json-valid-catalog.d.ts +0 -15
package/dist/plugin.mjs CHANGED
@@ -1,808 +1,2 @@
1
- import defu from 'defu';
2
- import fs from 'node:fs';
3
- import process from 'node:process';
4
- import { findUpSync } from 'find-up-simple';
5
- import { normalize, resolve, dirname } from 'pathe';
6
- import { globSync } from 'tinyglobby';
7
-
8
- // package.json
9
- var package_default = {
10
- version: "0.0.5"};
11
- function RuleCreator(urlCreator) {
12
- return function createNamedRule({
13
- name,
14
- meta,
15
- ...rule
16
- }) {
17
- return createRuleInfo({
18
- meta: {
19
- ...meta,
20
- docs: {
21
- ...meta.docs,
22
- url: urlCreator(name)
23
- }
24
- },
25
- ...rule
26
- });
27
- };
28
- }
29
- function createRuleInfo({
30
- create,
31
- defaultOptions,
32
- meta
33
- }) {
34
- return {
35
- create: ((context) => {
36
- const optionsWithDefault = context.options.map((options, index) => {
37
- return defu(defaultOptions?.[index] ?? {}, options);
38
- });
39
- return create(context, optionsWithDefault);
40
- }),
41
- defaultOptions: defaultOptions ?? {},
42
- meta
43
- };
44
- }
45
- var createRule = RuleCreator(
46
- (ruleName) => `https://docs.stormsoftware.com/projects/storm-ops/eslint-plugin-bun/rules/${ruleName}.md`
47
- );
48
- function isWorkspaceObject(workspaces) {
49
- return Boolean(workspaces) && typeof workspaces === "object" && !Array.isArray(workspaces);
50
- }
51
- function detectCatalogRoot(data) {
52
- if (data.catalog || data.catalogs) {
53
- return "top";
54
- }
55
- if (isWorkspaceObject(data.workspaces) && (data.workspaces.catalog || data.workspaces.catalogs)) {
56
- return "workspaces";
57
- }
58
- return isWorkspaceObject(data.workspaces) ? "workspaces" : "top";
59
- }
60
- function normalizeCatalogName(name) {
61
- return name.replace(/\^/g, "h").replace(/~/g, "t").replace(/\./g, "_").replace(/</g, "l").replace(/>/g, "g").replace(/=/g, "e").replace(/\*/g, "s").replace(/@/g, "a").replace(/\|/g, "p").replace(/&/g, "n").replace(/-/g, "m").replace(/\+/g, "u").replace(/\s/g, "_");
62
- }
63
- function sortObjectKeys(input) {
64
- return Object.fromEntries(
65
- Object.entries(input).sort(([left], [right]) => left.localeCompare(right))
66
- );
67
- }
68
- function isWorkspaceRootPackageJson(data) {
69
- return Boolean(
70
- data.workspaces || data.catalog || data.catalogs
71
- );
72
- }
73
- function readBunWorkspace(cwd = process.cwd()) {
74
- const filepath = findUpSync("package.json", {
75
- cwd,
76
- type: "file"
77
- });
78
- if (!filepath) {
79
- throw new Error("package.json not found");
80
- }
81
- let current = filepath;
82
- let content = fs.readFileSync(current, "utf8");
83
- let data = JSON.parse(content);
84
- while (!isWorkspaceRootPackageJson(data)) {
85
- const searchFrom = dirname(dirname(current));
86
- const next = findUpSync("package.json", {
87
- cwd: searchFrom,
88
- type: "file"
89
- });
90
- if (!next || normalize(next) === normalize(current)) {
91
- break;
92
- }
93
- current = next;
94
- content = fs.readFileSync(current, "utf8");
95
- data = JSON.parse(content);
96
- }
97
- if (!isWorkspaceRootPackageJson(data)) {
98
- throw new Error(
99
- "Bun workspace root package.json with workspaces/catalog not found"
100
- );
101
- }
102
- let hasChanged = false;
103
- let queueTimer;
104
- const queue = [];
105
- let catalogRoot = detectCatalogRoot(data);
106
- const write = () => {
107
- if (!fs.existsSync(current)) {
108
- return;
109
- }
110
- fs.writeFileSync(current, `${JSON.stringify(data, null, 2)}
111
- `);
112
- };
113
- const clearQueue = () => {
114
- if (queueTimer != null) {
115
- clearTimeout(queueTimer);
116
- queueTimer = void 0;
117
- }
118
- queue.length = 0;
119
- };
120
- const getCatalogContainer = () => {
121
- if (catalogRoot === "workspaces") {
122
- if (!isWorkspaceObject(data.workspaces)) {
123
- data.workspaces = {
124
- packages: Array.isArray(data.workspaces) ? data.workspaces : [],
125
- catalog: {},
126
- catalogs: {}
127
- };
128
- }
129
- return data.workspaces;
130
- }
131
- return data;
132
- };
133
- const getCatalogData = () => {
134
- const container = getCatalogContainer();
135
- return {
136
- catalog: container.catalog,
137
- catalogs: container.catalogs
138
- };
139
- };
140
- const setPackage = (catalogName, packageName, specifier) => {
141
- const container = getCatalogContainer();
142
- if (catalogName === "default") {
143
- container.catalog ??= {};
144
- if (container.catalog[packageName] !== specifier) {
145
- container.catalog[packageName] = specifier;
146
- container.catalog = sortObjectKeys(container.catalog);
147
- hasChanged = true;
148
- }
149
- return;
150
- }
151
- container.catalogs ??= {};
152
- container.catalogs[catalogName] ??= {};
153
- if (container.catalogs[catalogName][packageName] !== specifier) {
154
- container.catalogs[catalogName][packageName] = specifier;
155
- container.catalogs[catalogName] = sortObjectKeys(
156
- container.catalogs[catalogName]
157
- );
158
- container.catalogs = sortObjectKeys(container.catalogs);
159
- hasChanged = true;
160
- }
161
- };
162
- const hasSpecifierConflicts = (catalogName, packageName, specifier) => {
163
- const { catalog, catalogs } = getCatalogData();
164
- const existingSpecifier = catalogName === "default" ? catalog?.[packageName] : catalogs?.[catalogName]?.[packageName];
165
- if (existingSpecifier === specifier) {
166
- return {
167
- conflicts: false,
168
- newCatalogName: catalogName,
169
- existingSpecifier
170
- };
171
- }
172
- if (existingSpecifier) {
173
- const versionSuffix = normalizeCatalogName(specifier);
174
- const newCatalogName = catalogName === "default" ? `conflicts_${packageName}_${versionSuffix}` : `conflicts_${catalogName}_${versionSuffix}`;
175
- return {
176
- conflicts: true,
177
- existingSpecifier,
178
- newCatalogName
179
- };
180
- }
181
- return {
182
- conflicts: false,
183
- newCatalogName: catalogName
184
- };
185
- };
186
- const getPackageCatalogs = (packageName) => {
187
- const found = [];
188
- const { catalog, catalogs } = getCatalogData();
189
- if (catalogs) {
190
- for (const name of Object.keys(catalogs)) {
191
- if (catalogs[name]?.[packageName]) {
192
- found.push(name);
193
- }
194
- }
195
- }
196
- if (catalog?.[packageName]) {
197
- found.push("default");
198
- }
199
- return found;
200
- };
201
- const workspace2 = {
202
- filepath: current,
203
- setContent(newContent) {
204
- content = newContent;
205
- data = JSON.parse(newContent);
206
- catalogRoot = detectCatalogRoot(data);
207
- hasChanged = false;
208
- },
209
- hasChanged: () => hasChanged,
210
- hasQueue: () => queueTimer != null,
211
- clearQueue,
212
- queueChange(fn, order) {
213
- if (order === "pre") {
214
- queue.unshift(fn);
215
- } else {
216
- queue.push(fn);
217
- }
218
- if (queueTimer != null) {
219
- clearTimeout(queueTimer);
220
- }
221
- queueTimer = setTimeout(() => {
222
- queueTimer = void 0;
223
- const clone = [...queue];
224
- queue.length = 0;
225
- for (const queued of clone) {
226
- queued(workspace2);
227
- }
228
- if (hasChanged) {
229
- write();
230
- }
231
- }, 1e3);
232
- },
233
- toJSON: () => data,
234
- toString: () => `${JSON.stringify(data, null, 2)}
235
- `,
236
- setPackage,
237
- setPackageNoConflicts(catalogName, packageName, specifier) {
238
- const { newCatalogName } = hasSpecifierConflicts(
239
- catalogName,
240
- packageName,
241
- specifier
242
- );
243
- setPackage(newCatalogName, packageName, specifier);
244
- },
245
- getPackageCatalogs,
246
- hasSpecifierConflicts,
247
- getCatalogData,
248
- getWorkspacePackagePatterns() {
249
- if (Array.isArray(data.workspaces)) {
250
- return data.workspaces;
251
- }
252
- if (isWorkspaceObject(data.workspaces) && data.workspaces.packages) {
253
- return data.workspaces.packages;
254
- }
255
- return [];
256
- }
257
- };
258
- return workspace2;
259
- }
260
- var WORKSPACE_CACHE_TIME = 1e4;
261
- var workspaceLastRead;
262
- var workspace;
263
- function getBunWorkspace(cwd = process.cwd()) {
264
- if (workspaceLastRead && workspace && !workspace.hasQueue() && Date.now() - workspaceLastRead > WORKSPACE_CACHE_TIME) {
265
- workspace = void 0;
266
- }
267
- if (!workspace) {
268
- workspace = readBunWorkspace(cwd);
269
- workspaceLastRead = Date.now();
270
- }
271
- return workspace;
272
- }
273
- function isRootWorkspacePackageJson(filename, cwd = process.cwd()) {
274
- try {
275
- const root = getBunWorkspace(cwd);
276
- return normalize(root.filepath) === normalize(filename);
277
- } catch {
278
- return false;
279
- }
280
- }
281
- function collectWorkspacePackageJsonPaths(workspaceRoot) {
282
- const root = resolve(dirname(workspaceRoot.filepath));
283
- const patterns = workspaceRoot.getWorkspacePackagePatterns();
284
- const dirs = patterns.length ? globSync(patterns, {
285
- cwd: root,
286
- dot: false,
287
- ignore: [
288
- "**/node_modules/**",
289
- "**/dist/**",
290
- "**/build/**"
291
- ],
292
- absolute: true,
293
- expandDirectories: false,
294
- onlyDirectories: true
295
- }) : [];
296
- dirs.push(root);
297
- return dirs.map((dir) => resolve(dir, "package.json")).filter((path) => fs.existsSync(path)).sort();
298
- }
299
-
300
- // src/helpers/iterate-dependencies.ts
301
- function getPackageJsonRootNode(context) {
302
- if (!context.filename.endsWith("package.json")) {
303
- return void 0;
304
- }
305
- const ast = context.sourceCode.ast;
306
- const root = ast.body[0];
307
- if (root?.expression?.type === "JSONObjectExpression") {
308
- return root.expression;
309
- }
310
- return void 0;
311
- }
312
- function* iterateDependencies(context, fields) {
313
- const root = getPackageJsonRootNode(context);
314
- if (!root) {
315
- return;
316
- }
317
- for (const fieldName of fields) {
318
- const path = fieldName.split(".");
319
- let node = root;
320
- for (const segment of path) {
321
- const item = node.properties?.find(
322
- (property) => property.key.type === "JSONLiteral" && property.key.value === segment
323
- );
324
- if (!item?.value || item.value.type !== "JSONObjectExpression") {
325
- node = void 0;
326
- break;
327
- }
328
- node = item.value;
329
- }
330
- if (!node || node === root || !node.properties) {
331
- continue;
332
- }
333
- for (const property of node.properties) {
334
- if (property.value.type !== "JSONLiteral" || property.key.type !== "JSONLiteral") {
335
- continue;
336
- }
337
- if (typeof property.value.value !== "string") {
338
- continue;
339
- }
340
- yield {
341
- packageName: String(property.key.value),
342
- specifier: String(property.value.value),
343
- property
344
- };
345
- }
346
- }
347
- }
348
- function getObjectPath(obj, path) {
349
- let current = obj;
350
- for (const key of path) {
351
- if (!current || typeof current !== "object") {
352
- return void 0;
353
- }
354
- current = current[key];
355
- if (!current) {
356
- return void 0;
357
- }
358
- }
359
- return current;
360
- }
361
-
362
- // src/rules/json-enforce-catalog.ts
363
- var RULE_NAME = "json-enforce-catalog";
364
- var DEFAULT_FIELDS = ["dependencies", "devDependencies"];
365
- var IGNORED_DEPENDENCIES = ["typescript"];
366
- var json_enforce_catalog_default = createRule({
367
- name: RULE_NAME,
368
- meta: {
369
- type: "layout",
370
- docs: {
371
- description: 'Enforce using "catalog:" in `package.json` and write versions to the Bun workspace root catalog'
372
- },
373
- fixable: "code",
374
- schema: [
375
- {
376
- type: "object",
377
- properties: {
378
- allowedProtocols: {
379
- type: "array",
380
- description: "Allowed protocols in specifier to not be converted to catalog",
381
- items: { type: "string" }
382
- },
383
- autofix: {
384
- type: "boolean",
385
- description: "Whether to autofix the linting error",
386
- default: true
387
- },
388
- defaultCatalog: {
389
- type: "string",
390
- description: "Default catalog to use when moving version to catalog with autofix"
391
- },
392
- reuseExistingCatalog: {
393
- type: "boolean",
394
- description: "Whether to reuse existing catalog when moving version to catalog with autofix",
395
- default: true
396
- },
397
- conflicts: {
398
- type: "string",
399
- description: "Strategy to handle conflicts when adding packages to catalogs",
400
- enum: ["new-catalog", "overrides", "error"],
401
- default: "new-catalog"
402
- },
403
- fields: {
404
- type: "array",
405
- description: "Fields to check for catalog",
406
- items: { type: "string" },
407
- default: DEFAULT_FIELDS
408
- },
409
- ignore: {
410
- type: "array",
411
- description: "A list of dependencies to ignore",
412
- items: { type: "string" },
413
- default: IGNORED_DEPENDENCIES
414
- }
415
- },
416
- additionalProperties: false
417
- }
418
- ],
419
- messages: {
420
- expectCatalog: 'Expect to use catalog instead of plain specifier, got "{{specifier}}" for package "{{packageName}}".'
421
- }
422
- },
423
- defaultOptions: [{}],
424
- create(context, [options]) {
425
- const {
426
- allowedProtocols = ["workspace", "link", "file"],
427
- defaultCatalog = "default",
428
- autofix = true,
429
- reuseExistingCatalog = true,
430
- conflicts = "new-catalog",
431
- fields = DEFAULT_FIELDS,
432
- ignore = IGNORED_DEPENDENCIES
433
- } = options || {};
434
- for (const { packageName, specifier, property } of iterateDependencies(
435
- context,
436
- fields
437
- )) {
438
- if (ignore?.some((name) => name === packageName)) {
439
- continue;
440
- }
441
- if (specifier.startsWith("catalog:")) {
442
- continue;
443
- }
444
- if (allowedProtocols?.some((protocol) => specifier.startsWith(protocol))) {
445
- continue;
446
- }
447
- let workspace2;
448
- try {
449
- workspace2 = getBunWorkspace();
450
- } catch {
451
- return {};
452
- }
453
- let targetCatalog = reuseExistingCatalog ? workspace2.getPackageCatalogs(packageName)[0] || defaultCatalog : defaultCatalog;
454
- const resolvedConflicts = workspace2.hasSpecifierConflicts(
455
- targetCatalog,
456
- packageName,
457
- specifier
458
- );
459
- let shouldFix = autofix;
460
- if (conflicts === "error" && resolvedConflicts.conflicts) {
461
- shouldFix = false;
462
- }
463
- if (conflicts === "new-catalog" && resolvedConflicts.conflicts) {
464
- targetCatalog = resolvedConflicts.newCatalogName;
465
- }
466
- context.report({
467
- node: property.value,
468
- messageId: "expectCatalog",
469
- data: {
470
- specifier,
471
- packageName
472
- },
473
- fix: shouldFix ? (fixer) => {
474
- workspace2.queueChange(() => {
475
- workspace2.setPackage(targetCatalog, packageName, specifier);
476
- });
477
- return fixer.replaceText(
478
- property.value,
479
- targetCatalog === "default" ? JSON.stringify("catalog:") : JSON.stringify(`catalog:${targetCatalog}`)
480
- );
481
- } : void 0
482
- });
483
- }
484
- return {};
485
- }
486
- });
487
-
488
- // src/rules/json-no-duplicate-catalog-item.ts
489
- var RULE_NAME2 = "json-no-duplicate-catalog-item";
490
- function getProperty(node, key) {
491
- return node?.properties?.find(
492
- (property) => property.key.type === "JSONLiteral" && property.key.value === key
493
- );
494
- }
495
- var json_no_duplicate_catalog_item_default = createRule({
496
- name: RULE_NAME2,
497
- meta: {
498
- type: "problem",
499
- docs: {
500
- description: "Disallow duplicate catalog items in the Bun workspace root `package.json`"
501
- },
502
- schema: [
503
- {
504
- type: "object",
505
- properties: {
506
- allow: {
507
- type: "array",
508
- items: { type: "string" }
509
- }
510
- },
511
- additionalProperties: false
512
- }
513
- ],
514
- messages: {
515
- duplicateCatalogItem: 'Catalog item "{{name}}" is already defined in the "{{existingCatalog}}" catalog. You may want to remove one of them.'
516
- }
517
- },
518
- defaultOptions: [{}],
519
- create(context, [options = {}]) {
520
- if (!isRootWorkspacePackageJson(context.filename)) {
521
- return {};
522
- }
523
- let workspace2;
524
- try {
525
- workspace2 = getBunWorkspace();
526
- } catch {
527
- return {};
528
- }
529
- if (workspace2.hasChanged() || workspace2.hasQueue()) {
530
- return {};
531
- }
532
- workspace2.setContent(context.sourceCode.text);
533
- const root = getPackageJsonRootNode(context);
534
- if (!root) {
535
- return {};
536
- }
537
- const { allow = [] } = options;
538
- const topCatalog = getProperty(root, "catalog");
539
- const topCatalogs = getProperty(root, "catalogs");
540
- const workspaces = getProperty(root, "workspaces");
541
- const workspacesCatalog = workspaces?.value?.type === "JSONObjectExpression" ? getProperty(workspaces.value, "catalog") : void 0;
542
- const workspacesCatalogs = workspaces?.value?.type === "JSONObjectExpression" ? getProperty(workspaces.value, "catalogs") : void 0;
543
- const catalogNode = topCatalog ?? workspacesCatalog;
544
- const catalogsNode = topCatalogs ?? workspacesCatalogs;
545
- const exists = /* @__PURE__ */ new Map();
546
- const catalogEntries = [];
547
- if (catalogNode?.value?.type === "JSONObjectExpression") {
548
- for (const property of catalogNode.value.properties || []) {
549
- catalogEntries.push({ catalogName: "default", property });
550
- }
551
- }
552
- if (catalogsNode?.value?.type === "JSONObjectExpression") {
553
- for (const named of catalogsNode.value.properties || []) {
554
- if (named.value.type !== "JSONObjectExpression") {
555
- continue;
556
- }
557
- const catalogName = String(named.key.value);
558
- for (const property of named.value.properties || []) {
559
- catalogEntries.push({ catalogName, property });
560
- }
561
- }
562
- }
563
- for (const { catalogName, property } of catalogEntries) {
564
- const key = String(property.key.value);
565
- if (allow.includes(key)) {
566
- continue;
567
- }
568
- if (exists.has(key)) {
569
- context.report({
570
- node: property.value,
571
- messageId: "duplicateCatalogItem",
572
- data: {
573
- name: key,
574
- currentCatalog: catalogName,
575
- existingCatalog: exists.get(key)
576
- }
577
- });
578
- } else {
579
- exists.set(key, catalogName);
580
- }
581
- }
582
- return {};
583
- }
584
- });
585
- var RULE_NAME3 = "json-no-unused-catalog-item";
586
- var FIELDS = [
587
- "dependencies",
588
- "devDependencies",
589
- "peerDependencies",
590
- "optionalDependencies",
591
- "overrides",
592
- "resolutions"
593
- ];
594
- function getProperty2(node, key) {
595
- return node?.properties?.find(
596
- (property) => property.key.type === "JSONLiteral" && property.key.value === key
597
- );
598
- }
599
- var json_no_unused_catalog_item_default = createRule({
600
- name: RULE_NAME3,
601
- meta: {
602
- type: "problem",
603
- docs: {
604
- description: "Disallow unused catalog items in the Bun workspace root `package.json`"
605
- },
606
- schema: [],
607
- messages: {
608
- unusedCatalogItem: 'Catalog item "{{catalogItem}}" is not used in any package.json.'
609
- }
610
- },
611
- defaultOptions: [],
612
- create(context) {
613
- if (!isRootWorkspacePackageJson(context.filename)) {
614
- return {};
615
- }
616
- let workspace2;
617
- try {
618
- workspace2 = getBunWorkspace();
619
- } catch {
620
- return {};
621
- }
622
- if (workspace2.hasChanged() || workspace2.hasQueue()) {
623
- return {};
624
- }
625
- workspace2.setContent(context.sourceCode.text);
626
- const root = getPackageJsonRootNode(context);
627
- if (!root) {
628
- return {};
629
- }
630
- const topCatalog = getProperty2(root, "catalog");
631
- const topCatalogs = getProperty2(root, "catalogs");
632
- const workspaces = getProperty2(root, "workspaces");
633
- const workspacesCatalog = workspaces?.value?.type === "JSONObjectExpression" ? getProperty2(workspaces.value, "catalog") : void 0;
634
- const workspacesCatalogs = workspaces?.value?.type === "JSONObjectExpression" ? getProperty2(workspaces.value, "catalogs") : void 0;
635
- const catalogNode = topCatalog ?? workspacesCatalog;
636
- const catalogsNode = topCatalogs ?? workspacesCatalogs;
637
- const entries = /* @__PURE__ */ new Map();
638
- if (catalogNode?.value?.type === "JSONObjectExpression") {
639
- for (const property of catalogNode.value.properties || []) {
640
- entries.set(`${String(property.key.value)}:default`, property);
641
- }
642
- }
643
- if (catalogsNode?.value?.type === "JSONObjectExpression") {
644
- for (const named of catalogsNode.value.properties || []) {
645
- if (named.value.type !== "JSONObjectExpression") {
646
- continue;
647
- }
648
- const catalogName = String(named.key.value);
649
- for (const property of named.value.properties || []) {
650
- entries.set(`${String(property.key.value)}:${catalogName}`, property);
651
- }
652
- }
653
- }
654
- if (entries.size === 0) {
655
- return {};
656
- }
657
- const packages = collectWorkspacePackageJsonPaths(workspace2);
658
- for (const path of packages) {
659
- const pkg = JSON.parse(fs.readFileSync(path, "utf8"));
660
- for (const field of FIELDS) {
661
- const map = getObjectPath(pkg, field.split("."));
662
- if (!map) {
663
- continue;
664
- }
665
- for (const [name, value] of Object.entries(map)) {
666
- if (typeof value !== "string" || !value.startsWith("catalog:")) {
667
- continue;
668
- }
669
- const catalogName = value.slice(8) || "default";
670
- entries.delete(`${name}:${catalogName}`);
671
- }
672
- }
673
- }
674
- if (entries.size > 0) {
675
- for (const [key, property] of Array.from(entries.entries()).sort(
676
- (a, b) => a[0].localeCompare(b[0])
677
- )) {
678
- context.report({
679
- node: property.value,
680
- messageId: "unusedCatalogItem",
681
- data: { catalogItem: key }
682
- });
683
- }
684
- }
685
- return {};
686
- }
687
- });
688
-
689
- // src/rules/json-valid-catalog.ts
690
- var RULE_NAME4 = "json-valid-catalog";
691
- var DEFAULT_FIELDS2 = [
692
- "dependencies",
693
- "devDependencies",
694
- "optionalDependencies",
695
- "peerDependencies",
696
- "resolutions",
697
- "overrides"
698
- ];
699
- var json_valid_catalog_default = createRule({
700
- name: RULE_NAME4,
701
- meta: {
702
- type: "layout",
703
- docs: {
704
- description: "Enforce using valid Bun catalog references in `package.json`"
705
- },
706
- fixable: "code",
707
- schema: [
708
- {
709
- type: "object",
710
- properties: {
711
- autoInsert: {
712
- type: "boolean",
713
- description: "Whether to auto insert to catalog if missing",
714
- default: true
715
- },
716
- autoInsertDefaultSpecifier: {
717
- type: "string",
718
- description: "Default specifier to use when auto inserting to catalog",
719
- default: "^0.0.0"
720
- },
721
- autofix: {
722
- type: "boolean",
723
- description: "Whether to autofix the linting error",
724
- default: true
725
- },
726
- fields: {
727
- type: "array",
728
- description: "Fields to check for catalog",
729
- default: DEFAULT_FIELDS2
730
- }
731
- },
732
- additionalProperties: false
733
- }
734
- ],
735
- messages: {
736
- invalidCatalog: 'Catalog "{{specifier}}" for package "{{packageName}}" is not defined in the workspace root `package.json`.'
737
- }
738
- },
739
- defaultOptions: [{}],
740
- create(context, [options = {}]) {
741
- const {
742
- autoInsert = true,
743
- autofix = true,
744
- autoInsertDefaultSpecifier = "^0.0.0",
745
- fields = DEFAULT_FIELDS2
746
- } = options || {};
747
- for (const { packageName, specifier, property } of iterateDependencies(
748
- context,
749
- fields
750
- )) {
751
- if (!specifier.startsWith("catalog:")) {
752
- continue;
753
- }
754
- let workspace2;
755
- try {
756
- workspace2 = getBunWorkspace();
757
- } catch {
758
- return {};
759
- }
760
- const currentCatalog = specifier.replace(/^catalog:/, "").trim() || "default";
761
- const existingCatalogs = workspace2.getPackageCatalogs(packageName);
762
- if (!existingCatalogs.includes(currentCatalog)) {
763
- context.report({
764
- node: property.value,
765
- messageId: "invalidCatalog",
766
- data: {
767
- specifier,
768
- packageName
769
- },
770
- fix: !autofix || !autoInsert && !existingCatalogs.length ? void 0 : (fixer) => {
771
- let catalog = existingCatalogs[0];
772
- if (!catalog && autoInsert) {
773
- catalog = currentCatalog;
774
- workspace2.queueChange(() => {
775
- workspace2.setPackage(
776
- catalog,
777
- packageName,
778
- autoInsertDefaultSpecifier
779
- );
780
- }, "pre");
781
- }
782
- return fixer.replaceText(
783
- property.value,
784
- catalog === "default" ? JSON.stringify("catalog:") : JSON.stringify(`catalog:${catalog}`)
785
- );
786
- }
787
- });
788
- }
789
- }
790
- return {};
791
- }
792
- });
793
-
794
- // src/plugin.ts
795
- var plugin = {
796
- meta: {
797
- name: "bun",
798
- version: package_default.version
799
- },
800
- rules: {
801
- "json-enforce-catalog": json_enforce_catalog_default,
802
- "json-valid-catalog": json_valid_catalog_default,
803
- "json-no-unused-catalog-item": json_no_unused_catalog_item_default,
804
- "json-no-duplicate-catalog-item": json_no_duplicate_catalog_item_default
805
- }
806
- };
807
-
1
+ import { t as plugin } from "./plugin-DfH7W_YX.mjs";
808
2
  export { plugin };