|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import type { Linter } from 'eslint'; |
| 5 | + |
| 6 | +interface IPackageJson { |
| 7 | + [key: string]: unknown; |
| 8 | + name?: string; |
| 9 | + version?: string; |
| 10 | + description?: string; |
| 11 | + homepage?: string; |
| 12 | + keywords?: string[]; |
| 13 | + categories?: string[]; |
| 14 | + license?: string; |
| 15 | + licenses?: unknown; |
| 16 | + author?: unknown; |
| 17 | + publisher?: unknown; |
| 18 | + publishConfig?: unknown; |
| 19 | + private?: boolean; |
| 20 | + repository?: unknown; |
| 21 | + experimental?: unknown; |
| 22 | + extensionKind?: unknown; |
| 23 | + activationEvents?: unknown; |
| 24 | + contributes?: unknown; |
| 25 | + enabledApiProposals?: unknown; |
| 26 | + bin?: Record<string, string>; |
| 27 | + tsdoc?: unknown; |
| 28 | + engines?: Record<string, string>; |
| 29 | + scripts?: Record<string, string>; |
| 30 | + type?: string; |
| 31 | + format?: unknown; |
| 32 | + main?: string; |
| 33 | + module?: string; |
| 34 | + types?: string; |
| 35 | + typings?: string; |
| 36 | + browser?: unknown; |
| 37 | + imports?: unknown; |
| 38 | + exports?: unknown; |
| 39 | + typesVersions?: unknown; |
| 40 | + dependencies?: Record<string, string>; |
| 41 | + peerDependencies?: Record<string, string>; |
| 42 | + peerDependenciesMeta?: Record<string, unknown>; |
| 43 | + devDependencies?: Record<string, string>; |
| 44 | + optionalDependencies?: Record<string, string>; |
| 45 | + files?: string[]; |
| 46 | + directories?: unknown; |
| 47 | + sideEffects?: unknown; |
| 48 | + pnpm?: unknown; |
| 49 | +} |
| 50 | + |
| 51 | +function sortObjectByKeysRecursive<T>(obj: Record<string, T> | undefined): Record<string, T> | undefined { |
| 52 | + if (obj && typeof obj === 'object' && !Array.isArray(obj)) { |
| 53 | + const result: Record<string, T> = {}; |
| 54 | + const sortedKeys: string[] = Object.keys(obj).sort(); |
| 55 | + for (const key of sortedKeys) { |
| 56 | + const value: T = obj[key]; |
| 57 | + if (value && typeof value === 'object' && !Array.isArray(value)) { |
| 58 | + result[key] = sortObjectByKeysRecursive(value as Record<string, T>) as T; |
| 59 | + } else { |
| 60 | + result[key] = value; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return result; |
| 65 | + } else { |
| 66 | + return obj; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function sortPackageJsonScripts( |
| 71 | + scripts: Record<string, string> | undefined |
| 72 | +): Record<string, string> | undefined { |
| 73 | + if (scripts) { |
| 74 | + const underscorePrefixScripts: Record<string, string> = {}; |
| 75 | + const otherScripts: Record<string, string> = {}; |
| 76 | + |
| 77 | + const sortedKeys: string[] = Object.keys(scripts).sort(); |
| 78 | + for (const key of sortedKeys) { |
| 79 | + if (key.startsWith('_')) { |
| 80 | + underscorePrefixScripts[key] = scripts[key]; |
| 81 | + } else { |
| 82 | + otherScripts[key] = scripts[key]; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return { |
| 87 | + ...otherScripts, |
| 88 | + ...underscorePrefixScripts |
| 89 | + }; |
| 90 | + } else { |
| 91 | + return scripts; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Compute the sorted package.json content from the parsed object. |
| 97 | + */ |
| 98 | +function computeSortedPackageJson(parsed: IPackageJson): string { |
| 99 | + const { |
| 100 | + name, |
| 101 | + version, |
| 102 | + description, |
| 103 | + homepage, |
| 104 | + keywords, |
| 105 | + categories, |
| 106 | + license, |
| 107 | + licenses, |
| 108 | + author, |
| 109 | + publisher, |
| 110 | + publishConfig, |
| 111 | + private: privateValue, |
| 112 | + repository, |
| 113 | + experimental, |
| 114 | + extensionKind, |
| 115 | + activationEvents, |
| 116 | + contributes, |
| 117 | + enabledApiProposals, |
| 118 | + bin, |
| 119 | + tsdoc, |
| 120 | + engines, |
| 121 | + scripts, |
| 122 | + type, |
| 123 | + format, |
| 124 | + main, |
| 125 | + module: moduleValue, |
| 126 | + types, |
| 127 | + typings, |
| 128 | + browser, |
| 129 | + imports, |
| 130 | + exports: exportsValue, |
| 131 | + typesVersions, |
| 132 | + dependencies, |
| 133 | + peerDependencies, |
| 134 | + peerDependenciesMeta, |
| 135 | + devDependencies, |
| 136 | + optionalDependencies, |
| 137 | + files, |
| 138 | + directories, |
| 139 | + sideEffects, |
| 140 | + pnpm, |
| 141 | + ...extraFields |
| 142 | + } = parsed; |
| 143 | + |
| 144 | + const newPackageJson: {} = { |
| 145 | + name, |
| 146 | + version, |
| 147 | + description, |
| 148 | + homepage, |
| 149 | + keywords, |
| 150 | + categories, |
| 151 | + license, |
| 152 | + licenses, |
| 153 | + author, |
| 154 | + publisher, |
| 155 | + publishConfig, |
| 156 | + private: privateValue, |
| 157 | + repository, |
| 158 | + experimental, |
| 159 | + extensionKind, |
| 160 | + activationEvents, |
| 161 | + contributes, |
| 162 | + enabledApiProposals, |
| 163 | + bin: sortObjectByKeysRecursive(bin as Record<string, string> | undefined), |
| 164 | + tsdoc, |
| 165 | + engines: sortObjectByKeysRecursive(engines as Record<string, string> | undefined), |
| 166 | + scripts: sortPackageJsonScripts(scripts), |
| 167 | + type, |
| 168 | + format, |
| 169 | + main, |
| 170 | + module: moduleValue, |
| 171 | + types, |
| 172 | + typings, |
| 173 | + browser, |
| 174 | + imports, |
| 175 | + exports: exportsValue, |
| 176 | + typesVersions, |
| 177 | + dependencies: sortObjectByKeysRecursive(dependencies), |
| 178 | + peerDependencies: sortObjectByKeysRecursive(peerDependencies), |
| 179 | + peerDependenciesMeta: sortObjectByKeysRecursive( |
| 180 | + peerDependenciesMeta as Record<string, Record<string, unknown>> | undefined |
| 181 | + ), |
| 182 | + devDependencies: sortObjectByKeysRecursive(devDependencies), |
| 183 | + optionalDependencies: sortObjectByKeysRecursive(optionalDependencies), |
| 184 | + files, |
| 185 | + directories, |
| 186 | + sideEffects, |
| 187 | + pnpm, |
| 188 | + ...extraFields |
| 189 | + }; |
| 190 | + |
| 191 | + return JSON.stringify(newPackageJson, undefined, 2) + '\n'; |
| 192 | +} |
| 193 | + |
| 194 | +// Store original content keyed by filename for use in postprocess |
| 195 | +const originalContentMap: Map<string, string> = new Map(); |
| 196 | + |
| 197 | +const sortPackageJsonProcessor: Linter.Processor = { |
| 198 | + meta: { |
| 199 | + name: '@rushstack/sort-package-json', |
| 200 | + version: '0.0.0' |
| 201 | + }, |
| 202 | + |
| 203 | + supportsAutofix: true, |
| 204 | + |
| 205 | + preprocess(text: string, filename: string): Linter.ProcessorFile[] { |
| 206 | + originalContentMap.set(filename, text); |
| 207 | + // Return a minimal valid JS file so ESLint doesn't report parse errors |
| 208 | + return [{ text: '', filename: '0.js' }]; |
| 209 | + }, |
| 210 | + |
| 211 | + postprocess(messages: Linter.LintMessage[][], filename: string): Linter.LintMessage[] { |
| 212 | + const originalContent: string | undefined = originalContentMap.get(filename); |
| 213 | + originalContentMap.delete(filename); |
| 214 | + |
| 215 | + if (!originalContent) { |
| 216 | + return []; |
| 217 | + } |
| 218 | + |
| 219 | + let parsed: IPackageJson; |
| 220 | + try { |
| 221 | + parsed = JSON.parse(originalContent); |
| 222 | + } catch { |
| 223 | + // If the JSON is invalid, don't report any sorting issues |
| 224 | + return []; |
| 225 | + } |
| 226 | + |
| 227 | + const sortedContent: string = computeSortedPackageJson(parsed); |
| 228 | + |
| 229 | + if (originalContent !== sortedContent) { |
| 230 | + return [ |
| 231 | + { |
| 232 | + ruleId: '@rushstack/sort-package-json', |
| 233 | + message: 'package.json properties are not sorted correctly.', |
| 234 | + line: 1, |
| 235 | + column: 1, |
| 236 | + severity: 1, |
| 237 | + nodeType: null as never, |
| 238 | + fix: { |
| 239 | + range: [0, originalContent.length] as [number, number], |
| 240 | + text: sortedContent |
| 241 | + } |
| 242 | + } |
| 243 | + ]; |
| 244 | + } |
| 245 | + |
| 246 | + return []; |
| 247 | + } |
| 248 | +}; |
| 249 | + |
| 250 | +export { sortPackageJsonProcessor, computeSortedPackageJson }; |
0 commit comments