@code-pushup/eslint-plugin 0.1.0

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/index.js ADDED
@@ -0,0 +1,874 @@
1
+ // packages/plugin-eslint/src/lib/eslint-plugin.ts
2
+ import { mkdir, writeFile } from "fs/promises";
3
+ import { dirname as dirname2, join as join3 } from "path";
4
+ import { fileURLToPath } from "url";
5
+
6
+ // packages/plugin-eslint/package.json
7
+ var name = "@code-pushup/eslint-plugin";
8
+ var version = "0.1.0";
9
+
10
+ // packages/plugin-eslint/src/lib/config.ts
11
+ import { z } from "zod";
12
+ var eslintPluginConfigSchema = z.object({
13
+ eslintrc: z.union(
14
+ [
15
+ z.string({ description: "Path to ESLint config file" }),
16
+ z.record(z.string(), z.unknown(), {
17
+ description: "ESLint config object"
18
+ })
19
+ ],
20
+ { description: "ESLint config as file path or inline object" }
21
+ ),
22
+ patterns: z.union([z.string(), z.array(z.string()).min(1)], {
23
+ description: "Lint target files. May contain file paths, directory paths or glob patterns"
24
+ })
25
+ });
26
+
27
+ // packages/models/src/lib/category-config.ts
28
+ import { z as z3 } from "zod";
29
+
30
+ // packages/models/src/lib/implementation/schemas.ts
31
+ import { z as z2 } from "zod";
32
+ import { MATERIAL_ICONS } from "@code-pushup/portal-client";
33
+
34
+ // packages/models/src/lib/implementation/utils.ts
35
+ var slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
36
+ var filenameRegex = /^(?!.*[ \\/:*?"<>|]).+$/;
37
+ function hasDuplicateStrings(strings) {
38
+ const uniqueStrings = Array.from(new Set(strings));
39
+ const duplicatedStrings = strings.filter(
40
+ /* @__PURE__ */ ((i) => (v) => uniqueStrings[i] !== v || !++i)(0)
41
+ );
42
+ return duplicatedStrings.length === 0 ? false : duplicatedStrings;
43
+ }
44
+ function hasMissingStrings(toCheck, existing) {
45
+ const nonExisting = toCheck.filter((s) => !existing.includes(s));
46
+ return nonExisting.length === 0 ? false : nonExisting;
47
+ }
48
+ function errorItems(items, transform = (items2) => items2.join(", ")) {
49
+ const paredItems = items ? items : [];
50
+ return transform(paredItems);
51
+ }
52
+ function exists(value) {
53
+ return value != null;
54
+ }
55
+
56
+ // packages/models/src/lib/implementation/schemas.ts
57
+ function executionMetaSchema(options = {
58
+ descriptionDate: "Execution start date and time",
59
+ descriptionDuration: "Execution duration in ms"
60
+ }) {
61
+ return z2.object({
62
+ date: z2.string({ description: options.descriptionDate }),
63
+ duration: z2.number({ description: options.descriptionDuration })
64
+ });
65
+ }
66
+ function slugSchema(description = "Unique ID (human-readable, URL-safe)") {
67
+ return z2.string({ description }).regex(slugRegex, {
68
+ message: "The slug has to follow the pattern [0-9a-z] followed by multiple optional groups of -[0-9a-z]. e.g. my-slug"
69
+ }).max(128, {
70
+ message: "slug can be max 128 characters long"
71
+ });
72
+ }
73
+ function descriptionSchema(description = "Description (markdown)") {
74
+ return z2.string({ description }).max(65536).optional();
75
+ }
76
+ function docsUrlSchema(description = "Documentation site") {
77
+ return urlSchema(description).optional().or(z2.string().max(0));
78
+ }
79
+ function urlSchema(description) {
80
+ return z2.string({ description }).url();
81
+ }
82
+ function titleSchema(description = "Descriptive name") {
83
+ return z2.string({ description }).max(256);
84
+ }
85
+ function metaSchema(options) {
86
+ const {
87
+ descriptionDescription,
88
+ titleDescription,
89
+ docsUrlDescription,
90
+ description
91
+ } = options || {};
92
+ return z2.object(
93
+ {
94
+ title: titleSchema(titleDescription),
95
+ description: descriptionSchema(descriptionDescription),
96
+ docsUrl: docsUrlSchema(docsUrlDescription)
97
+ },
98
+ { description }
99
+ );
100
+ }
101
+ function filePathSchema(description) {
102
+ return z2.string({ description }).trim().min(1, { message: "path is invalid" });
103
+ }
104
+ function fileNameSchema(description) {
105
+ return z2.string({ description }).trim().regex(filenameRegex, {
106
+ message: `The filename has to be valid`
107
+ }).min(1, { message: "file name is invalid" });
108
+ }
109
+ function positiveIntSchema(description) {
110
+ return z2.number({ description }).int().nonnegative();
111
+ }
112
+ function packageVersionSchema(options) {
113
+ let { versionDescription, optional } = options || {};
114
+ versionDescription = versionDescription || "NPM version of the package";
115
+ optional = !!optional;
116
+ const packageSchema = z2.string({ description: "NPM package name" });
117
+ const versionSchema = z2.string({ description: versionDescription });
118
+ return z2.object(
119
+ {
120
+ packageName: optional ? packageSchema.optional() : packageSchema,
121
+ version: optional ? versionSchema.optional() : versionSchema
122
+ },
123
+ { description: "NPM package name and version of a published package" }
124
+ );
125
+ }
126
+ function weightSchema(description = "Coefficient for the given score (use weight 0 if only for display)") {
127
+ return positiveIntSchema(description);
128
+ }
129
+ function weightedRefSchema(description, slugDescription) {
130
+ return z2.object(
131
+ {
132
+ slug: slugSchema(slugDescription),
133
+ weight: weightSchema("Weight used to calculate score")
134
+ },
135
+ { description }
136
+ );
137
+ }
138
+ function scorableSchema(description, refSchema, duplicateCheckFn, duplicateMessageFn) {
139
+ return z2.object(
140
+ {
141
+ slug: slugSchema('Human-readable unique ID, e.g. "performance"'),
142
+ refs: z2.array(refSchema).refine(
143
+ (refs) => !duplicateCheckFn(refs),
144
+ (refs) => ({
145
+ message: duplicateMessageFn(refs)
146
+ })
147
+ )
148
+ },
149
+ { description }
150
+ );
151
+ }
152
+ var materialIconSchema = z2.enum(
153
+ MATERIAL_ICONS,
154
+ { description: "Icon from VSCode Material Icons extension" }
155
+ );
156
+
157
+ // packages/models/src/lib/category-config.ts
158
+ var categoryRefSchema = weightedRefSchema(
159
+ "Weighted references to audits and/or groups for the category",
160
+ "Slug of an audit or group (depending on `type`)"
161
+ ).merge(
162
+ z3.object({
163
+ type: z3.enum(["audit", "group"], {
164
+ description: "Discriminant for reference kind, affects where `slug` is looked up"
165
+ }),
166
+ plugin: slugSchema(
167
+ "Plugin slug (plugin should contain referenced audit or group)"
168
+ )
169
+ })
170
+ );
171
+ var categoryConfigSchema = scorableSchema(
172
+ "Category with a score calculated from audits and groups from various plugins",
173
+ categoryRefSchema,
174
+ getDuplicateRefsInCategoryMetrics,
175
+ duplicateRefsInCategoryMetricsErrorMsg
176
+ ).merge(
177
+ metaSchema({
178
+ titleDescription: "Category Title",
179
+ docsUrlDescription: "Category docs URL",
180
+ descriptionDescription: "Category description",
181
+ description: "Meta info for category"
182
+ })
183
+ ).merge(
184
+ z3.object({
185
+ isBinary: z3.boolean({
186
+ description: 'Is this a binary category (i.e. only a perfect score considered a "pass")?'
187
+ }).optional()
188
+ })
189
+ );
190
+ function duplicateRefsInCategoryMetricsErrorMsg(metrics) {
191
+ const duplicateRefs = getDuplicateRefsInCategoryMetrics(metrics);
192
+ return `In the categories, the following audit or group refs are duplicates: ${errorItems(
193
+ duplicateRefs
194
+ )}`;
195
+ }
196
+ function getDuplicateRefsInCategoryMetrics(metrics) {
197
+ return hasDuplicateStrings(
198
+ metrics.map(({ slug, type, plugin }) => `${type} :: ${plugin} / ${slug}`)
199
+ );
200
+ }
201
+
202
+ // packages/models/src/lib/core-config.ts
203
+ import { z as z12 } from "zod";
204
+
205
+ // packages/models/src/lib/persist-config.ts
206
+ import { z as z4 } from "zod";
207
+ var formatSchema = z4.enum(["json", "md"]);
208
+ var persistConfigSchema = z4.object({
209
+ outputDir: filePathSchema("Artifacts folder"),
210
+ filename: fileNameSchema("Artifacts file name (without extension)").default(
211
+ "report"
212
+ ),
213
+ format: z4.array(formatSchema).default(["json"]).optional()
214
+ // @TODO remove default or optional value and otherwise it will not set defaults.
215
+ });
216
+
217
+ // packages/models/src/lib/plugin-config.ts
218
+ import { z as z10 } from "zod";
219
+
220
+ // packages/models/src/lib/plugin-config-audits.ts
221
+ import { z as z5 } from "zod";
222
+ var auditSchema = z5.object({
223
+ slug: slugSchema("ID (unique within plugin)")
224
+ }).merge(
225
+ metaSchema({
226
+ titleDescription: "Descriptive name",
227
+ descriptionDescription: "Description (markdown)",
228
+ docsUrlDescription: "Link to documentation (rationale)",
229
+ description: "List of scorable metrics for the given plugin"
230
+ })
231
+ );
232
+ var pluginAuditsSchema = z5.array(auditSchema, {
233
+ description: "List of audits maintained in a plugin"
234
+ }).refine(
235
+ (auditMetadata) => !getDuplicateSlugsInAudits(auditMetadata),
236
+ (auditMetadata) => ({
237
+ message: duplicateSlugsInAuditsErrorMsg(auditMetadata)
238
+ })
239
+ );
240
+ function duplicateSlugsInAuditsErrorMsg(audits) {
241
+ const duplicateRefs = getDuplicateSlugsInAudits(audits);
242
+ return `In plugin audits the slugs are not unique: ${errorItems(
243
+ duplicateRefs
244
+ )}`;
245
+ }
246
+ function getDuplicateSlugsInAudits(audits) {
247
+ return hasDuplicateStrings(audits.map(({ slug }) => slug));
248
+ }
249
+
250
+ // packages/models/src/lib/plugin-config-groups.ts
251
+ import { z as z6 } from "zod";
252
+ var auditGroupRefSchema = weightedRefSchema(
253
+ "Weighted references to audits",
254
+ "Reference slug to an audit within this plugin (e.g. 'max-lines')"
255
+ );
256
+ var auditGroupSchema = scorableSchema(
257
+ 'An audit group aggregates a set of audits into a single score which can be referenced from a category. E.g. the group slug "performance" groups audits and can be referenced in a category',
258
+ auditGroupRefSchema,
259
+ getDuplicateRefsInGroups,
260
+ duplicateRefsInGroupsErrorMsg
261
+ ).merge(
262
+ metaSchema({
263
+ titleDescription: "Descriptive name for the group",
264
+ descriptionDescription: "Description of the group (markdown)",
265
+ docsUrlDescription: "Group documentation site",
266
+ description: "Group metadata"
267
+ })
268
+ );
269
+ var auditGroupsSchema = z6.array(auditGroupSchema, {
270
+ description: "List of groups"
271
+ }).optional().refine(
272
+ (groups) => !getDuplicateSlugsInGroups(groups),
273
+ (groups) => ({
274
+ message: duplicateSlugsInGroupsErrorMsg(groups)
275
+ })
276
+ );
277
+ function duplicateRefsInGroupsErrorMsg(groupAudits) {
278
+ const duplicateRefs = getDuplicateRefsInGroups(groupAudits);
279
+ return `In plugin groups the audit refs are not unique: ${errorItems(
280
+ duplicateRefs
281
+ )}`;
282
+ }
283
+ function getDuplicateRefsInGroups(groupAudits) {
284
+ return hasDuplicateStrings(
285
+ groupAudits.map(({ slug: ref }) => ref).filter(exists)
286
+ );
287
+ }
288
+ function duplicateSlugsInGroupsErrorMsg(groups) {
289
+ const duplicateRefs = getDuplicateSlugsInGroups(groups);
290
+ return `In groups the slugs are not unique: ${errorItems(duplicateRefs)}`;
291
+ }
292
+ function getDuplicateSlugsInGroups(groups) {
293
+ return Array.isArray(groups) ? hasDuplicateStrings(groups.map(({ slug }) => slug)) : false;
294
+ }
295
+
296
+ // packages/models/src/lib/plugin-config-runner.ts
297
+ import { z as z9 } from "zod";
298
+
299
+ // packages/models/src/lib/plugin-process-output.ts
300
+ import { z as z8 } from "zod";
301
+
302
+ // packages/models/src/lib/plugin-process-output-audit-issue.ts
303
+ import { z as z7 } from "zod";
304
+ var sourceFileLocationSchema = z7.object(
305
+ {
306
+ file: filePathSchema("Relative path to source file in Git repo"),
307
+ position: z7.object(
308
+ {
309
+ startLine: positiveIntSchema("Start line"),
310
+ startColumn: positiveIntSchema("Start column").optional(),
311
+ endLine: positiveIntSchema("End line").optional(),
312
+ endColumn: positiveIntSchema("End column").optional()
313
+ },
314
+ { description: "Location in file" }
315
+ ).optional()
316
+ },
317
+ { description: "Source file location" }
318
+ );
319
+ var issueSeveritySchema = z7.enum(["info", "warning", "error"], {
320
+ description: "Severity level"
321
+ });
322
+ var issueSchema = z7.object(
323
+ {
324
+ message: z7.string({ description: "Descriptive error message" }).max(512),
325
+ severity: issueSeveritySchema,
326
+ source: sourceFileLocationSchema.optional()
327
+ },
328
+ { description: "Issue information" }
329
+ );
330
+
331
+ // packages/models/src/lib/plugin-process-output.ts
332
+ var auditOutputSchema = z8.object(
333
+ {
334
+ slug: slugSchema("Reference to audit"),
335
+ displayValue: z8.string({ description: "Formatted value (e.g. '0.9 s', '2.1 MB')" }).optional(),
336
+ value: positiveIntSchema("Raw numeric value"),
337
+ score: z8.number({
338
+ description: "Value between 0 and 1"
339
+ }).min(0).max(1),
340
+ details: z8.object(
341
+ {
342
+ issues: z8.array(issueSchema, { description: "List of findings" })
343
+ },
344
+ { description: "Detailed information" }
345
+ ).optional()
346
+ },
347
+ { description: "Audit information" }
348
+ );
349
+ var auditOutputsSchema = z8.array(auditOutputSchema, {
350
+ description: "List of JSON formatted audit output emitted by the runner process of a plugin"
351
+ }).refine(
352
+ (audits) => !getDuplicateSlugsInAudits2(audits),
353
+ (audits) => ({ message: duplicateSlugsInAuditsErrorMsg2(audits) })
354
+ );
355
+ function duplicateSlugsInAuditsErrorMsg2(audits) {
356
+ const duplicateRefs = getDuplicateSlugsInAudits2(audits);
357
+ return `In plugin audits the slugs are not unique: ${errorItems(
358
+ duplicateRefs
359
+ )}`;
360
+ }
361
+ function getDuplicateSlugsInAudits2(audits) {
362
+ return hasDuplicateStrings(audits.map(({ slug }) => slug));
363
+ }
364
+
365
+ // packages/models/src/lib/plugin-config-runner.ts
366
+ var outputTransformSchema = z9.function().args(z9.unknown()).returns(z9.union([auditOutputsSchema, z9.promise(auditOutputsSchema)]));
367
+ var runnerConfigSchema = z9.object(
368
+ {
369
+ command: z9.string({
370
+ description: "Shell command to execute"
371
+ }),
372
+ args: z9.array(z9.string({ description: "Command arguments" })).optional(),
373
+ outputFile: filePathSchema("Output path"),
374
+ outputTransform: outputTransformSchema.optional()
375
+ },
376
+ {
377
+ description: "How to execute runner"
378
+ }
379
+ );
380
+ var onProgressSchema = z9.function().args(z9.unknown()).returns(z9.void());
381
+ var runnerFunctionSchema = z9.function().args(onProgressSchema.optional()).returns(z9.union([auditOutputsSchema, z9.promise(auditOutputsSchema)]));
382
+
383
+ // packages/models/src/lib/plugin-config.ts
384
+ var pluginMetaSchema = packageVersionSchema({
385
+ optional: true
386
+ }).merge(
387
+ metaSchema({
388
+ titleDescription: "Descriptive name",
389
+ descriptionDescription: "Description (markdown)",
390
+ docsUrlDescription: "Plugin documentation site",
391
+ description: "Plugin metadata"
392
+ })
393
+ ).merge(
394
+ z10.object({
395
+ slug: slugSchema("References plugin. ID (unique within core config)"),
396
+ icon: materialIconSchema
397
+ })
398
+ );
399
+ var pluginDataSchema = z10.object({
400
+ runner: z10.union([runnerConfigSchema, runnerFunctionSchema]),
401
+ audits: pluginAuditsSchema,
402
+ groups: auditGroupsSchema
403
+ });
404
+ var pluginConfigSchema = pluginMetaSchema.merge(pluginDataSchema).refine(
405
+ (pluginCfg) => !getMissingRefsFromGroups(pluginCfg),
406
+ (pluginCfg) => ({
407
+ message: missingRefsFromGroupsErrorMsg(pluginCfg)
408
+ })
409
+ );
410
+ function missingRefsFromGroupsErrorMsg(pluginCfg) {
411
+ const missingRefs = getMissingRefsFromGroups(pluginCfg);
412
+ return `In the groups, the following audit ref's needs to point to a audit in this plugin config: ${errorItems(
413
+ missingRefs
414
+ )}`;
415
+ }
416
+ function getMissingRefsFromGroups(pluginCfg) {
417
+ if (pluginCfg?.groups?.length && pluginCfg?.audits?.length) {
418
+ const groups = pluginCfg?.groups || [];
419
+ const audits = pluginCfg?.audits || [];
420
+ return hasMissingStrings(
421
+ groups.flatMap(({ refs: audits2 }) => audits2.map(({ slug: ref }) => ref)),
422
+ audits.map(({ slug }) => slug)
423
+ );
424
+ }
425
+ return false;
426
+ }
427
+
428
+ // packages/models/src/lib/upload-config.ts
429
+ import { z as z11 } from "zod";
430
+ var uploadConfigSchema = z11.object({
431
+ server: urlSchema("URL of deployed portal API"),
432
+ apiKey: z11.string({
433
+ description: "API key with write access to portal (use `process.env` for security)"
434
+ }),
435
+ organization: z11.string({
436
+ description: "Organization in code versioning system"
437
+ }),
438
+ project: z11.string({
439
+ description: "Project in code versioning system"
440
+ })
441
+ });
442
+
443
+ // packages/models/src/lib/core-config.ts
444
+ var unrefinedCoreConfigSchema = z12.object({
445
+ plugins: z12.array(pluginConfigSchema, {
446
+ description: "List of plugins to be used (official, community-provided, or custom)"
447
+ }),
448
+ /** portal configuration for persisting results */
449
+ persist: persistConfigSchema,
450
+ /** portal configuration for uploading results */
451
+ upload: uploadConfigSchema.optional(),
452
+ categories: z12.array(categoryConfigSchema, {
453
+ description: "Categorization of individual audits"
454
+ }).refine(
455
+ (categoryCfg) => !getDuplicateSlugCategories(categoryCfg),
456
+ (categoryCfg) => ({
457
+ message: duplicateSlugCategoriesErrorMsg(categoryCfg)
458
+ })
459
+ )
460
+ });
461
+ var coreConfigSchema = refineCoreConfig(unrefinedCoreConfigSchema);
462
+ function refineCoreConfig(schema) {
463
+ return schema.refine(
464
+ (coreCfg) => !getMissingRefsForCategories(coreCfg),
465
+ (coreCfg) => ({
466
+ message: missingRefsForCategoriesErrorMsg(coreCfg)
467
+ })
468
+ );
469
+ }
470
+ function missingRefsForCategoriesErrorMsg(coreCfg) {
471
+ const missingRefs = getMissingRefsForCategories(coreCfg);
472
+ return `In the categories, the following plugin refs do not exist in the provided plugins: ${errorItems(
473
+ missingRefs
474
+ )}`;
475
+ }
476
+ function getMissingRefsForCategories(coreCfg) {
477
+ const missingRefs = [];
478
+ const auditRefsFromCategory = coreCfg.categories.flatMap(
479
+ ({ refs }) => refs.filter(({ type }) => type === "audit").map(({ plugin, slug }) => `${plugin}/${slug}`)
480
+ );
481
+ const auditRefsFromPlugins = coreCfg.plugins.flatMap(
482
+ ({ audits, slug: pluginSlug }) => {
483
+ return audits.map(({ slug }) => `${pluginSlug}/${slug}`);
484
+ }
485
+ );
486
+ const missingAuditRefs = hasMissingStrings(
487
+ auditRefsFromCategory,
488
+ auditRefsFromPlugins
489
+ );
490
+ if (Array.isArray(missingAuditRefs) && missingAuditRefs.length > 0) {
491
+ missingRefs.push(...missingAuditRefs);
492
+ }
493
+ const groupRefsFromCategory = coreCfg.categories.flatMap(
494
+ ({ refs }) => refs.filter(({ type }) => type === "group").map(({ plugin, slug }) => `${plugin}#${slug} (group)`)
495
+ );
496
+ const groupRefsFromPlugins = coreCfg.plugins.flatMap(
497
+ ({ groups, slug: pluginSlug }) => {
498
+ return Array.isArray(groups) ? groups.map(({ slug }) => `${pluginSlug}#${slug} (group)`) : [];
499
+ }
500
+ );
501
+ const missingGroupRefs = hasMissingStrings(
502
+ groupRefsFromCategory,
503
+ groupRefsFromPlugins
504
+ );
505
+ if (Array.isArray(missingGroupRefs) && missingGroupRefs.length > 0) {
506
+ missingRefs.push(...missingGroupRefs);
507
+ }
508
+ return missingRefs.length ? missingRefs : false;
509
+ }
510
+ function duplicateSlugCategoriesErrorMsg(categories) {
511
+ const duplicateStringSlugs = getDuplicateSlugCategories(categories);
512
+ return `In the categories, the following slugs are duplicated: ${errorItems(
513
+ duplicateStringSlugs
514
+ )}`;
515
+ }
516
+ function getDuplicateSlugCategories(categories) {
517
+ return hasDuplicateStrings(categories.map(({ slug }) => slug));
518
+ }
519
+
520
+ // packages/models/src/lib/report.ts
521
+ import { z as z13 } from "zod";
522
+ var auditReportSchema = auditSchema.merge(auditOutputSchema);
523
+ var pluginReportSchema = pluginMetaSchema.merge(
524
+ executionMetaSchema({
525
+ descriptionDate: "Start date and time of plugin run",
526
+ descriptionDuration: "Duration of the plugin run in ms"
527
+ })
528
+ ).merge(
529
+ z13.object({
530
+ audits: z13.array(auditReportSchema),
531
+ groups: z13.array(auditGroupSchema).optional()
532
+ })
533
+ );
534
+ var reportSchema = packageVersionSchema({
535
+ versionDescription: "NPM version of the CLI"
536
+ }).merge(
537
+ executionMetaSchema({
538
+ descriptionDate: "Start date and time of the collect run",
539
+ descriptionDuration: "Duration of the collect run in ms"
540
+ })
541
+ ).merge(
542
+ z13.object(
543
+ {
544
+ categories: z13.array(categoryConfigSchema),
545
+ plugins: z13.array(pluginReportSchema)
546
+ },
547
+ { description: "Collect output data" }
548
+ )
549
+ );
550
+
551
+ // packages/utils/src/lib/file-system.ts
552
+ import { bundleRequire } from "bundle-require";
553
+ import chalk from "chalk";
554
+ import { join } from "path";
555
+
556
+ // packages/utils/src/lib/formatting.ts
557
+ function slugify(text) {
558
+ return text.trim().toLowerCase().replace(/\s+|\//g, "-").replace(/[^a-z0-9-]/g, "");
559
+ }
560
+
561
+ // packages/utils/src/lib/file-system.ts
562
+ function pluginWorkDir(slug) {
563
+ return join("node_modules", ".code-pushup", slug);
564
+ }
565
+
566
+ // packages/utils/src/lib/execute-process.ts
567
+ function objectToCliArgs(params) {
568
+ if (!params) {
569
+ return [];
570
+ }
571
+ return Object.entries(params).flatMap(([key, value]) => {
572
+ if (key === "_") {
573
+ if (Array.isArray(value)) {
574
+ return value;
575
+ } else {
576
+ return [value + ""];
577
+ }
578
+ }
579
+ const prefix = key.length === 1 ? "-" : "--";
580
+ if (Array.isArray(value)) {
581
+ return value.map((v) => `${prefix}${key}="${v}"`);
582
+ }
583
+ if (Array.isArray(value)) {
584
+ return value.map((v) => `${prefix}${key}="${v}"`);
585
+ }
586
+ if (typeof value === "string") {
587
+ return [`${prefix}${key}="${value}"`];
588
+ }
589
+ if (typeof value === "number") {
590
+ return [`${prefix}${key}=${value}`];
591
+ }
592
+ if (typeof value === "boolean") {
593
+ return [`${prefix}${value ? "" : "no-"}${key}`];
594
+ }
595
+ throw new Error(`Unsupported type ${typeof value} for key ${key}`);
596
+ });
597
+ }
598
+ objectToCliArgs({ z: 5 });
599
+
600
+ // packages/utils/src/lib/git.ts
601
+ import simpleGit from "simple-git";
602
+ var git = simpleGit();
603
+
604
+ // packages/utils/src/lib/progress.ts
605
+ import chalk2 from "chalk";
606
+ import { MultiProgressBars } from "multi-progress-bars";
607
+
608
+ // packages/utils/src/lib/report-to-stdout.ts
609
+ import cliui from "@isaacs/cliui";
610
+ import chalk3 from "chalk";
611
+ import Table from "cli-table3";
612
+
613
+ // packages/utils/src/lib/transformation.ts
614
+ function toArray(val) {
615
+ return Array.isArray(val) ? val : [val];
616
+ }
617
+ function objectToKeys(obj) {
618
+ return Object.keys(obj);
619
+ }
620
+ function distinct(array) {
621
+ return Array.from(new Set(array));
622
+ }
623
+
624
+ // packages/plugin-eslint/src/lib/meta/hash.ts
625
+ import { createHash } from "crypto";
626
+ function ruleIdToSlug(ruleId, options) {
627
+ const slug = slugify(ruleId);
628
+ if (!options?.length) {
629
+ return slug;
630
+ }
631
+ return `${slug}-${jsonHash(options)}`;
632
+ }
633
+ function jsonHash(data, bytes = 8) {
634
+ return createHash("shake256", { outputLength: bytes }).update(JSON.stringify(data) ?? "null").digest("hex");
635
+ }
636
+
637
+ // packages/plugin-eslint/src/lib/meta/rules.ts
638
+ async function listRules(eslint, patterns) {
639
+ const configs = await toArray(patterns).reduce(
640
+ async (acc, pattern) => [
641
+ ...await acc,
642
+ await eslint.calculateConfigForFile(pattern)
643
+ ],
644
+ Promise.resolve([])
645
+ );
646
+ const rulesIds = distinct(
647
+ configs.flatMap((config) => Object.keys(config.rules ?? {}))
648
+ );
649
+ const rulesMeta = eslint.getRulesMetaForResults([
650
+ {
651
+ messages: rulesIds.map((ruleId) => ({ ruleId })),
652
+ suppressedMessages: []
653
+ }
654
+ ]);
655
+ const rulesMap = configs.flatMap((config) => Object.entries(config.rules ?? {})).filter(([, ruleEntry]) => ruleEntry != null && !isRuleOff(ruleEntry)).reduce(
656
+ (acc, [ruleId, ruleEntry]) => {
657
+ const meta = rulesMeta[ruleId];
658
+ if (!meta) {
659
+ console.warn(`Metadata not found for ESLint rule ${ruleId}`);
660
+ return acc;
661
+ }
662
+ const options = toArray(ruleEntry).slice(1);
663
+ const optionsHash = jsonHash(options);
664
+ const ruleData = {
665
+ ruleId,
666
+ meta,
667
+ options
668
+ };
669
+ return {
670
+ ...acc,
671
+ [ruleId]: {
672
+ ...acc[ruleId],
673
+ [optionsHash]: ruleData
674
+ }
675
+ };
676
+ },
677
+ {}
678
+ );
679
+ return Object.values(rulesMap).flatMap(Object.values);
680
+ }
681
+ function isRuleOff(entry) {
682
+ const level = Array.isArray(entry) ? entry[0] : entry;
683
+ switch (level) {
684
+ case 0:
685
+ case "off":
686
+ return true;
687
+ case 1:
688
+ case 2:
689
+ case "warn":
690
+ case "error":
691
+ return false;
692
+ }
693
+ }
694
+ function parseRuleId(ruleId) {
695
+ const i = ruleId.lastIndexOf("/");
696
+ if (i < 0) {
697
+ return { name: ruleId };
698
+ }
699
+ return {
700
+ plugin: ruleId.slice(0, i),
701
+ name: ruleId.slice(i + 1)
702
+ };
703
+ }
704
+
705
+ // packages/plugin-eslint/src/lib/meta/groups.ts
706
+ var typeGroups = {
707
+ problem: {
708
+ slug: "problems",
709
+ title: "Problems",
710
+ description: "Code that either will cause an error or may cause confusing behavior. Developers should consider this a high priority to resolve."
711
+ },
712
+ suggestion: {
713
+ slug: "suggestions",
714
+ title: "Suggestions",
715
+ description: "Something that could be done in a better way but no errors will occur if the code isn't changed."
716
+ },
717
+ layout: {
718
+ slug: "formatting",
719
+ title: "Formatting",
720
+ description: "Primarily about whitespace, semicolons, commas, and parentheses, all the parts of the program that determine how the code looks rather than how it executes."
721
+ }
722
+ };
723
+ function groupsFromRuleTypes(rules) {
724
+ const allTypes = objectToKeys(typeGroups);
725
+ const auditSlugsMap = rules.reduce(
726
+ (acc, { meta: { type }, ruleId, options }) => type == null ? acc : {
727
+ ...acc,
728
+ [type]: [...acc[type] ?? [], ruleIdToSlug(ruleId, options)]
729
+ },
730
+ {}
731
+ );
732
+ return allTypes.map((type) => ({
733
+ ...typeGroups[type],
734
+ refs: auditSlugsMap[type]?.map(
735
+ (slug) => ({ slug, weight: 1 })
736
+ ) ?? []
737
+ }));
738
+ }
739
+ function groupsFromRuleCategories(rules) {
740
+ const categoriesMap = rules.reduce(
741
+ (acc, { meta: { docs }, ruleId, options }) => {
742
+ const category = docs?.category;
743
+ if (!category) {
744
+ return acc;
745
+ }
746
+ const { plugin = "" } = parseRuleId(ruleId);
747
+ return {
748
+ ...acc,
749
+ [plugin]: {
750
+ ...acc[plugin],
751
+ [category]: [
752
+ ...acc[plugin]?.[category] ?? [],
753
+ ruleIdToSlug(ruleId, options)
754
+ ]
755
+ }
756
+ };
757
+ },
758
+ {}
759
+ );
760
+ return Object.entries(categoriesMap).flatMap(
761
+ ([plugin, categories]) => Object.entries(categories).map(
762
+ ([category, slugs]) => ({
763
+ slug: `${slugify(plugin)}-${slugify(category)}`,
764
+ title: `${category} (${plugin})`,
765
+ refs: slugs.map((slug) => ({ slug, weight: 1 }))
766
+ })
767
+ )
768
+ ).sort((a, b) => a.slug.localeCompare(b.slug));
769
+ }
770
+
771
+ // packages/plugin-eslint/src/lib/meta/transform.ts
772
+ function ruleToAudit({ ruleId, meta, options }) {
773
+ const name2 = ruleId.split("/").at(-1) ?? ruleId;
774
+ const plugin = name2 === ruleId ? null : ruleId.slice(0, ruleId.lastIndexOf("/"));
775
+ const lines = [
776
+ `ESLint rule **${name2}**${plugin ? `, from _${plugin}_ plugin` : ""}.`,
777
+ ...options?.length ? ["Custom options:"] : [],
778
+ ...options?.map(
779
+ (option) => ["```json", JSON.stringify(option, null, 2), "```"].join("\n")
780
+ ) ?? []
781
+ ];
782
+ return {
783
+ slug: ruleIdToSlug(ruleId, options),
784
+ title: meta.docs?.description ?? name2,
785
+ description: lines.join("\n\n"),
786
+ ...meta.docs?.url && {
787
+ docsUrl: meta.docs.url
788
+ }
789
+ };
790
+ }
791
+
792
+ // packages/plugin-eslint/src/lib/meta/index.ts
793
+ async function listAuditsAndGroups(eslint, patterns) {
794
+ const rules = await listRules(eslint, patterns);
795
+ const audits = rules.map(ruleToAudit);
796
+ const groups = [
797
+ ...groupsFromRuleTypes(rules),
798
+ ...groupsFromRuleCategories(rules)
799
+ ];
800
+ return { audits, groups };
801
+ }
802
+
803
+ // packages/plugin-eslint/src/lib/runner/index.ts
804
+ import { platform } from "os";
805
+ import { dirname, join as join2 } from "path";
806
+
807
+ // packages/plugin-eslint/src/lib/setup.ts
808
+ import { ESLint } from "eslint";
809
+ function setupESLint(eslintrc) {
810
+ return new ESLint({
811
+ ...typeof eslintrc === "string" ? { overrideConfigFile: eslintrc } : { baseConfig: eslintrc },
812
+ useEslintrc: false,
813
+ errorOnUnmatchedPattern: false
814
+ });
815
+ }
816
+
817
+ // packages/plugin-eslint/src/lib/runner/index.ts
818
+ var WORKDIR = pluginWorkDir("eslint");
819
+ var RUNNER_OUTPUT_PATH = join2(WORKDIR, "runner-output.json");
820
+ var ESLINTRC_PATH = join2(process.cwd(), WORKDIR, ".eslintrc.json");
821
+ var AUDIT_SLUGS_SEP = ",";
822
+ function createRunnerConfig(scriptPath, audits, eslintrc, patterns) {
823
+ return {
824
+ command: "node",
825
+ args: [
826
+ scriptPath,
827
+ audits.map((audit) => audit.slug).join(AUDIT_SLUGS_SEP),
828
+ eslintrc,
829
+ ...toArray(patterns).map(
830
+ (pattern) => platform() === "win32" ? pattern : `'${pattern}'`
831
+ )
832
+ ],
833
+ outputFile: RUNNER_OUTPUT_PATH
834
+ };
835
+ }
836
+
837
+ // packages/plugin-eslint/src/lib/eslint-plugin.ts
838
+ async function eslintPlugin(config) {
839
+ const { eslintrc, patterns } = eslintPluginConfigSchema.parse(config);
840
+ const eslint = setupESLint(eslintrc);
841
+ const { audits, groups } = await listAuditsAndGroups(eslint, patterns);
842
+ if (typeof eslintrc !== "string") {
843
+ await mkdir(dirname2(ESLINTRC_PATH), { recursive: true });
844
+ await writeFile(ESLINTRC_PATH, JSON.stringify(eslintrc));
845
+ }
846
+ const eslintrcPath = typeof eslintrc === "string" ? eslintrc : ESLINTRC_PATH;
847
+ const runnerScriptPath = join3(
848
+ fileURLToPath(dirname2(import.meta.url)),
849
+ "bin.js"
850
+ );
851
+ return {
852
+ slug: "eslint",
853
+ title: "ESLint",
854
+ icon: "eslint",
855
+ description: "Official Code PushUp ESLint plugin",
856
+ // TODO: docsUrl (package README, once published)
857
+ packageName: name,
858
+ version,
859
+ audits,
860
+ groups,
861
+ runner: createRunnerConfig(
862
+ runnerScriptPath,
863
+ audits,
864
+ eslintrcPath,
865
+ patterns
866
+ )
867
+ };
868
+ }
869
+
870
+ // packages/plugin-eslint/src/index.ts
871
+ var src_default = eslintPlugin;
872
+ export {
873
+ src_default as default
874
+ };