|
| 1 | +import { Audit, Group } from '@code-pushup/models'; |
| 2 | + |
| 3 | +export const KNIP_PLUGIN_SLUG = 'knip'; |
| 4 | +export const KNIP_RAW_REPORT_NAME = 'knip-raw-report.json'; |
| 5 | +export const KNIP_REPORT_NAME = 'knip-code-pushup-report.json'; |
| 6 | + |
| 7 | +const audits = [ |
| 8 | + { |
| 9 | + slug: 'files', |
| 10 | + title: 'Unused Files', |
| 11 | + description: 'Unable to find a reference to this file', |
| 12 | + }, |
| 13 | + { |
| 14 | + slug: 'dependencies', |
| 15 | + title: 'Unused Dependencies', |
| 16 | + description: 'Unable to find a reference to this dependency', |
| 17 | + }, |
| 18 | + { |
| 19 | + slug: 'devdependencies', |
| 20 | + title: 'Unused Development Dependencies', |
| 21 | + description: 'Unable to find a reference to this devDependency', |
| 22 | + }, |
| 23 | + { |
| 24 | + slug: 'optionalpeerdependencies', |
| 25 | + title: 'Referenced optional peerDependencies', |
| 26 | + description: 'Optional peer dependency is referenced', |
| 27 | + }, |
| 28 | + { |
| 29 | + slug: 'unlisted', |
| 30 | + title: 'Unlisted dependencies', |
| 31 | + description: 'Used dependencies not listed in package.json', |
| 32 | + }, |
| 33 | + { |
| 34 | + slug: 'binaries', |
| 35 | + title: 'Unlisted binaries', |
| 36 | + description: 'Binaries from dependencies not listed in package.json', |
| 37 | + }, |
| 38 | + { |
| 39 | + slug: 'unresolved', |
| 40 | + title: 'Unresolved imports', |
| 41 | + description: 'Unable to resolve this (import) specifier', |
| 42 | + }, |
| 43 | + { |
| 44 | + slug: 'exports', |
| 45 | + title: 'Unused exports', |
| 46 | + description: 'Unable to find a reference to this export', |
| 47 | + }, |
| 48 | + { |
| 49 | + slug: 'types', |
| 50 | + title: 'Unused exported types', |
| 51 | + description: 'Unable to find a reference to this exported type', |
| 52 | + }, |
| 53 | + { |
| 54 | + slug: 'nsexports', |
| 55 | + title: 'Exports in used namespace', |
| 56 | + description: 'Namespace with export is referenced, but not export itself', |
| 57 | + }, |
| 58 | + { |
| 59 | + slug: 'nstypes', |
| 60 | + title: 'Exported types in used namespace', |
| 61 | + description: 'Namespace with type is referenced, but not type itself', |
| 62 | + }, |
| 63 | + { |
| 64 | + slug: 'enummembers', |
| 65 | + title: 'Unused exported enum members', |
| 66 | + description: 'Unable to find a reference to this enum member', |
| 67 | + }, |
| 68 | + { |
| 69 | + slug: 'classmembers', |
| 70 | + title: 'Unused exported class members', |
| 71 | + description: 'Unable to find a reference to this class member', |
| 72 | + }, |
| 73 | + { |
| 74 | + slug: 'duplicates', |
| 75 | + title: 'Duplicate exports', |
| 76 | + description: 'This is exported more than once', |
| 77 | + }, |
| 78 | +] as const satisfies Audit[]; // we use `as const satisfies` to get strict slug typing |
| 79 | + |
| 80 | +export type KnipAudits = (typeof audits)[number]['slug']; |
| 81 | + |
| 82 | +function docsLink(slug: KnipAudits): string { |
| 83 | + let anchor = '#'; |
| 84 | + const base = 'https://knip.dev/guides/handling-issues'; |
| 85 | + |
| 86 | + switch (slug) { |
| 87 | + case 'files': |
| 88 | + anchor = '#unused-files'; |
| 89 | + break; |
| 90 | + case 'dependencies': |
| 91 | + case 'devdependencies': |
| 92 | + anchor = '#unused-dependencies'; |
| 93 | + break; |
| 94 | + case 'unlisted': |
| 95 | + anchor = '#unlisted-dependencies'; |
| 96 | + break; |
| 97 | + case 'optionalpeerdependencies': |
| 98 | + anchor = '#referenced-optional-peerDependencies'; |
| 99 | + break; |
| 100 | + case 'unresolved': |
| 101 | + anchor = '#unresolved-imports'; |
| 102 | + break; |
| 103 | + case 'exports': |
| 104 | + case 'types': |
| 105 | + case 'nsexports': |
| 106 | + case 'nstypes': |
| 107 | + anchor = '#unused-exports'; |
| 108 | + break; |
| 109 | + case 'enummembers': |
| 110 | + anchor = '#enum-members'; |
| 111 | + break; |
| 112 | + case 'classmembers': |
| 113 | + anchor = '#class-members'; |
| 114 | + break; |
| 115 | + // following cases also default: |
| 116 | + // - case 'binaries': |
| 117 | + // - case 'duplicates': |
| 118 | + default: |
| 119 | + return base; |
| 120 | + } |
| 121 | + |
| 122 | + return `${base}${anchor}`; |
| 123 | +} |
| 124 | + |
| 125 | +export const KNIP_AUDITS = audits.map((audit) => ({ |
| 126 | + ...audit, |
| 127 | + docsUrl: docsLink(audit.slug), |
| 128 | +})); |
| 129 | + |
| 130 | +export const KNIP_GROUP_FILES = { |
| 131 | + slug: 'files', |
| 132 | + title: 'All file audits', |
| 133 | + description: 'Groups all file related audits', |
| 134 | + refs: [{ slug: 'files', weight: 1 }], |
| 135 | +} as const satisfies Group; |
| 136 | + |
| 137 | +export const KNIP_GROUP_DEPENDENCIES = { |
| 138 | + slug: 'dependencies', |
| 139 | + title: 'All dependency audits', |
| 140 | + description: 'Groups all dependency related audits', |
| 141 | + refs: [ |
| 142 | + { slug: 'dependencies', weight: 1 }, |
| 143 | + { slug: 'devdependencies', weight: 1 }, |
| 144 | + { slug: 'binaries', weight: 1 }, |
| 145 | + // critical as potentially breaking |
| 146 | + { slug: 'optionalpeerdependencies', weight: 2 }, |
| 147 | + { slug: 'unlisted', weight: 2 }, |
| 148 | + ], |
| 149 | +} as const satisfies Group; |
| 150 | + |
| 151 | +export const KNIP_GROUP_EXPORTS = { |
| 152 | + slug: 'exports', |
| 153 | + title: 'All exports related audits', |
| 154 | + description: 'Groups all dependency related knip audits', |
| 155 | + refs: [ |
| 156 | + { slug: 'unresolved', weight: 10 }, |
| 157 | + { slug: 'exports', weight: 10 }, |
| 158 | + { slug: 'types', weight: 10 }, |
| 159 | + { slug: 'nsexports', weight: 10 }, |
| 160 | + { slug: 'nstypes', weight: 10 }, |
| 161 | + { slug: 'enummembers', weight: 10 }, |
| 162 | + { slug: 'classmembers', weight: 10 }, |
| 163 | + { slug: 'duplicates', weight: 2 }, |
| 164 | + ], |
| 165 | +} as const satisfies Group; |
| 166 | + |
| 167 | +export const KNIP_GROUP_ALL = { |
| 168 | + slug: 'all', |
| 169 | + title: 'All knip audits', |
| 170 | + description: 'Groups all knip audits into a group for easy use', |
| 171 | + refs: [ |
| 172 | + ...KNIP_GROUP_FILES.refs, |
| 173 | + ...KNIP_GROUP_EXPORTS.refs, |
| 174 | + ...KNIP_GROUP_DEPENDENCIES.refs, |
| 175 | + ], |
| 176 | +} as const satisfies Group; |
| 177 | + |
| 178 | +export const KNIP_GROUPS = [ |
| 179 | + KNIP_GROUP_FILES, |
| 180 | + KNIP_GROUP_EXPORTS, |
| 181 | + KNIP_GROUP_DEPENDENCIES, |
| 182 | + KNIP_GROUP_ALL, |
| 183 | +] as const satisfies Group[]; // we use `as const satisfies` to get strict slug typing; |
| 184 | + |
| 185 | +export type KnipGroups = (typeof KNIP_GROUPS)[number]['slug']; |
| 186 | + |
1 | 187 | import { IssueType as KnipIssueType } from 'knip/dist/types/issues';
|
2 | 188 |
|
3 | 189 | /**
|
|
0 commit comments