|
| 1 | +import * as replacements from 'module-replacements'; |
| 2 | +import {Message, PackageJsonLike} from '../types.js'; |
| 3 | +import type {FileSystem} from '../file-system.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Generates a standard URL to the docs of a given rule |
| 7 | + * @param {string} name Rule name |
| 8 | + * @return {string} |
| 9 | + */ |
| 10 | +export function getDocsUrl(name: string): string { |
| 11 | + return `https://github.com/es-tooling/eslint-plugin-depend/blob/main/docs/rules/${name}.md`; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Generates a URL for the given path on MDN |
| 16 | + * @param {string} path Docs path |
| 17 | + * @return {string} |
| 18 | + */ |
| 19 | +export function getMdnUrl(path: string): string { |
| 20 | + return `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/${path}`; |
| 21 | +} |
| 22 | + |
| 23 | +export async function runReplacements(fileSystem: FileSystem) { |
| 24 | + const messages: Message[] = []; |
| 25 | + |
| 26 | + let packageJsonText: string; |
| 27 | + |
| 28 | + try { |
| 29 | + packageJsonText = await fileSystem.readFile('/package.json'); |
| 30 | + } catch { |
| 31 | + // No package.json found |
| 32 | + return messages; |
| 33 | + } |
| 34 | + |
| 35 | + let packageJson: PackageJsonLike; |
| 36 | + |
| 37 | + try { |
| 38 | + packageJson = JSON.parse(packageJsonText); |
| 39 | + } catch { |
| 40 | + // Not parseable |
| 41 | + return messages; |
| 42 | + } |
| 43 | + |
| 44 | + if (!packageJson.dependencies) { |
| 45 | + // No dependencies |
| 46 | + return messages; |
| 47 | + } |
| 48 | + |
| 49 | + for (const name of Object.keys(packageJson.dependencies)) { |
| 50 | + const replacement = replacements.all.moduleReplacements.find( |
| 51 | + (replacement) => replacement.moduleName === name |
| 52 | + ); |
| 53 | + |
| 54 | + if (!replacement) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + if (replacement.type === 'none') { |
| 59 | + messages.push({ |
| 60 | + severity: 'warning', |
| 61 | + score: 0, |
| 62 | + message: `Module "${name}" can be removed, and native functionality used instead` |
| 63 | + }); |
| 64 | + } else if (replacement.type === 'simple') { |
| 65 | + messages.push({ |
| 66 | + severity: 'warning', |
| 67 | + score: 0, |
| 68 | + message: `Module "${name}" can be replaced. ${replacement.replacement}.` |
| 69 | + }); |
| 70 | + } else if (replacement.type === 'native') { |
| 71 | + const mdnPath = getMdnUrl(replacement.mdnPath); |
| 72 | + // TODO (43081j): support `nodeVersion` here, check it against the |
| 73 | + // packageJson.engines field, if there is one. |
| 74 | + messages.push({ |
| 75 | + severity: 'warning', |
| 76 | + score: 0, |
| 77 | + message: `Module "${name}" can be replaced with native functionality. Use "${replacement.replacement}" instead. You can read more at ${mdnPath}.` |
| 78 | + }); |
| 79 | + } else if (replacement.type === 'documented') { |
| 80 | + const docUrl = getDocsUrl(replacement.docPath); |
| 81 | + messages.push({ |
| 82 | + severity: 'warning', |
| 83 | + score: 0, |
| 84 | + message: `Module "${name}" can be replaced with a more performant alternative. See the list of available alternatives at ${docUrl}.` |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return messages; |
| 90 | +} |
0 commit comments