-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesign the reporter to include links to rules, locations, autofixes
- Loading branch information
Showing
19 changed files
with
528 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@steiger/pretty-reporter': minor | ||
'steiger': minor | ||
--- | ||
|
||
Redesign the diagnostic reporter to include links to rules, locations, and auto-fix information |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Test it with `pnpm run --silent example`. Adjust `example/index.ts` accordingly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { reportPretty } from '../src/index.js' | ||
|
||
function getRuleDescriptionUrl(ruleName: string) { | ||
return new URL(`https://github.com/feature-sliced/steiger/tree/master/packages/steiger-plugin-fsd/src/${ruleName}`) | ||
} | ||
|
||
reportPretty( | ||
[ | ||
{ | ||
message: 'Inconsistent pluralization of slice names. Prefer all plural names', | ||
ruleName: 'inconsistent-naming', | ||
fixes: [ | ||
{ | ||
type: 'rename', | ||
path: '/home/user/project/src/entities/user', | ||
newName: 'users', | ||
}, | ||
], | ||
location: { path: '/home/user/project/src/entities' }, | ||
getRuleDescriptionUrl, | ||
}, | ||
{ | ||
message: 'This slice has no references. Consider removing it.', | ||
ruleName: 'insignificant-slice', | ||
location: { path: '/home/user/project/src/entities/users' }, | ||
getRuleDescriptionUrl, | ||
}, | ||
{ | ||
message: 'This slice has no references. Consider removing it.', | ||
ruleName: 'insignificant-slice', | ||
location: { path: '/home/user/project/src/entities/user' }, | ||
getRuleDescriptionUrl, | ||
}, | ||
{ | ||
message: | ||
'Having a folder with the name "api" inside a segment could be confusing because that name is commonly used for segments. Consider renaming it.', | ||
ruleName: 'no-reserved-folder-names', | ||
location: { path: '/home/user/project/src/entities/user/ui/api' }, | ||
getRuleDescriptionUrl, | ||
}, | ||
{ | ||
message: 'This slice has no segments. Consider dividing the code inside into segments.', | ||
ruleName: 'no-segmentless-slices', | ||
location: { path: '/home/user/project/src/pages/home' }, | ||
getRuleDescriptionUrl, | ||
}, | ||
{ | ||
message: 'Layer "processes" is deprecated, avoid using it', | ||
ruleName: 'no-processes', | ||
location: { path: '/home/user/project/src/processes' }, | ||
getRuleDescriptionUrl, | ||
}, | ||
], | ||
'/home/user/project', | ||
) | ||
|
||
// reportPretty([], '/home/user/project') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { relative } from 'node:path' | ||
import figures from 'figures' | ||
import terminalLink from 'terminal-link' | ||
import chalk from 'chalk' | ||
|
||
import type { AugmentedDiagnostic } from './types.js' | ||
|
||
export function formatSingleDiagnostic(d: AugmentedDiagnostic, cwd: string): string { | ||
const x = chalk.red(figures.cross) | ||
const s = chalk.reset(figures.lineDownRight) | ||
const bar = chalk.reset(figures.lineVertical) | ||
const e = chalk.reset(figures.lineUpRight) | ||
const message = chalk.reset(d.message) | ||
const autofixable = d.fixes !== undefined && d.fixes.length > 0 ? chalk.green(`${figures.tick} Auto-fixable`) : null | ||
const location = chalk.gray(formatLocation(d.location, cwd)) | ||
const ruleName = chalk.blue(terminalLink(d.ruleName, d.getRuleDescriptionUrl(d.ruleName).toString())) | ||
|
||
return ` | ||
${s} ${location} | ||
${x} ${message} | ||
${autofixable ? autofixable + `\n${bar}` : bar} | ||
${e} ${ruleName} | ||
`.trim() | ||
} | ||
|
||
function formatLocation(location: AugmentedDiagnostic['location'], cwd: string) { | ||
let path = relative(cwd, location.path) | ||
if (location.line !== undefined) { | ||
path += `:${location.line}` | ||
|
||
if (location.column !== undefined) { | ||
path += `:${location.column}` | ||
} | ||
} | ||
|
||
return path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,40 @@ | ||
import chalk from 'chalk' | ||
import figures from 'figures' | ||
|
||
import type { AugmentedDiagnostic } from './types.js' | ||
import { formatSingleDiagnostic } from './format-single-diagnostic.js' | ||
import { s } from './pluralization.js' | ||
|
||
export function formatPretty(diagnostics: Array<AugmentedDiagnostic>) { | ||
export function formatPretty(diagnostics: Array<AugmentedDiagnostic>, cwd: string) { | ||
if (diagnostics.length === 0) { | ||
return chalk.green(`${figures.tick} No problems found!`) | ||
} | ||
|
||
const footer = chalk.red.bold(`Found ${diagnostics.length} problem${diagnostics.length > 1 ? 's' : ''}`) | ||
let footer = chalk.red.bold(`Found ${diagnostics.length} problem${s(diagnostics.length)}`) | ||
|
||
// TODO: enable when we have auto-fixes | ||
// const autofixable = diagnostics.filter((d) => (d.fixes?.length ?? 0) > 0) | ||
// if (autofixable.length === diagnostics.length) { | ||
// footer += ' (all can be fixed automatically)' | ||
// } else if (autofixable.length > 0) { | ||
// footer += ` (${autofixable.length} can be fixed automatically)` | ||
// } else { | ||
// footer += ' (none can be fixed automatically)' | ||
// } | ||
const autofixable = diagnostics.filter((d) => (d.fixes?.length ?? 0) > 0) | ||
if (autofixable.length === diagnostics.length) { | ||
footer += ` (all can be fixed automatically with ${chalk.green.bold('--fix')})` | ||
} else if (autofixable.length > 0) { | ||
footer += ` (${autofixable.length} can be fixed automatically with ${chalk.green.bold('--fix')})` | ||
} else { | ||
footer += ' (none can be fixed automatically)' | ||
} | ||
|
||
return ( | ||
'\n' + | ||
diagnostics | ||
.map((d) => { | ||
const message = ` ${chalk.red(figures.cross)} ${chalk.reset(d.message)} ${chalk.gray(`// ${d.ruleName}`)}` | ||
|
||
// TODO: enable when we have auto-fixes | ||
// if ((d.fixes?.length ?? 0) > 0) { | ||
// message += chalk.green(`\n (${figures.tick} auto-fix available)`) | ||
// } | ||
|
||
return message | ||
}) | ||
.join('\n\n') + | ||
diagnostics.map((d) => formatSingleDiagnostic(d, cwd)).join('\n\n') + | ||
'\n\n' + | ||
// Due to formatting characters, it won't be exactly the size of the footer, that is okay | ||
chalk.gray(figures.line.repeat(footer.length)) + | ||
'\n ' + | ||
footer + | ||
'\n' | ||
) | ||
} | ||
|
||
export function reportPretty(diagnostics: Array<AugmentedDiagnostic>) { | ||
console.error(formatPretty(diagnostics)) | ||
export function reportPretty(diagnostics: Array<AugmentedDiagnostic>, cwd: string) { | ||
console.error(formatPretty(diagnostics, cwd)) | ||
} | ||
|
||
export type { AugmentedDiagnostic } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Returns 's' if the amount is not 1. | ||
* | ||
* @example | ||
* `apple${s(1)}` // 'apple' | ||
* `apple${s(2)}` // 'apples' | ||
*/ | ||
export function s(amount: number) { | ||
return amount === 1 ? '' : 's' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Source: https://github.com/evanw/esbuild/issues/1921#issuecomment-1898197331 | ||
|
||
import { createRequire } from 'node:module' | ||
import path from 'node:path' | ||
import url from 'node:url' | ||
|
||
globalThis.require = createRequire(import.meta.url) | ||
globalThis.__filename = url.fileURLToPath(import.meta.url) | ||
globalThis.__dirname = path.dirname(__filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,6 @@ | |
], | ||
"devDependencies": { | ||
"@steiger/tsconfig": "workspace:*", | ||
"typescript": "^5.4.5" | ||
"typescript": "^5.5.3" | ||
} | ||
} |
Oops, something went wrong.