-
Notifications
You must be signed in to change notification settings - Fork 15
chore: add eslint formatter for multiple formats #1090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 41 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
51f7a89
chore: add eslint formatter for multiple formats
BioPhoton d3e41aa
chore: stricter plugin usage
BioPhoton ee9aae1
chore: remove package
BioPhoton 4166956
refactor(eslint-multi-formatter): fix lint
BioPhoton 8208532
Update tools/eslint-multi-format/README.md
BioPhoton d37c4a3
Update tools/eslint-multi-format/src/lib/utils.ts
BioPhoton 6e0c734
Merge branch 'main' into tools/custom-eslint-formatter
BioPhoton 7b090ca
Merge remote-tracking branch 'origin/tools/custom-eslint-formatter' i…
BioPhoton 1592f62
refactor: full refactor + add tests
BioPhoton 56cf11e
refactor: use error helper
BioPhoton 53c5796
refactor: fix lint
BioPhoton 65c365e
Merge branch 'main' into tools/custom-eslint-formatter
BioPhoton 4de3d33
refactor: fix lint formatter usage
BioPhoton d3351f0
refactor: fix lint formatter lint
BioPhoton a786407
refactor: adjust lint targets
BioPhoton e1554d6
refactor: remove packages
BioPhoton 944b141
refactor: fix lint
BioPhoton 28bc74d
refactor: revert env vars
BioPhoton 4e154e1
refactor: adjust GH ci action
BioPhoton 7fa3e53
refactor: adjust GH ci action 2
BioPhoton 72b3f2c
refactor: adjust targets
BioPhoton 6e0d998
refactor: adjust targets 3
BioPhoton 689d671
refactor: adjust targets 4
BioPhoton 26b80b8
refactor: adjust targets 5
BioPhoton 6372d63
refactor: adjust targets 6
BioPhoton 3d306da
chore: revert targets
BioPhoton 914b146
Merge branch 'main' into tools/custom-eslint-formatter
BioPhoton 17f8029
chore: fix lint setup
BioPhoton 9a73ab9
refactor: fix async
BioPhoton 37ea315
refactor: copy code
BioPhoton 563c8ec
refactor: use copy code
BioPhoton fe10e39
refactor: wip
BioPhoton e0270c6
refactor: rename package from eslint-formatter-multiple-formats to es…
BioPhoton 7981046
refactor: fix targets
BioPhoton 6c499cc
refactor: fix lint
BioPhoton ccdb58d
refactor: fix lint target
BioPhoton 16a3977
refactor: use plugin to set up targets
BioPhoton 97e1e6c
chore: adjust lint target
BioPhoton 44ddced
chore: fix pkg install
BioPhoton 8eb075a
chore: fix lint
BioPhoton 28b76f0
Merge branch 'main' into tools/custom-eslint-formatter
BioPhoton f56ba9e
Update tools/eslint-formatter-multi/package.json
BioPhoton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| # ESLint Multi Formatter | ||
|
|
||
| The ESLint plugin uses a custom formatter that supports multiple output formats and destinations simultaneously. | ||
|
|
||
| ## Configuration | ||
|
|
||
| Use the `ESLINT_FORMATTER_CONFIG` environment variable to configure the formatter with JSON. | ||
|
|
||
| ### Configuration Schema | ||
|
|
||
| ```jsonc | ||
| { | ||
| "outputDir": "./reports", // Optional: Output directory (default: cwd/.eslint) | ||
| "filename": "eslint-report", // Optional: Base filename without extension (default: 'eslint-report') | ||
| "formats": ["json"], // Optional: Array of format names for file output (default: ['json']) | ||
| "terminal": "stylish", // Optional: Format for terminal output (default: 'stylish') | ||
| "verbose": true, // Optional: Enable verbose logging (default: false) | ||
| } | ||
| ``` | ||
|
|
||
| ### Supported Formats | ||
|
|
||
| The following ESLint formatters are supported: | ||
|
|
||
| - `stylish` (default terminal output) | ||
| - `json` (default file output) | ||
| - Custom formatters (fallback to stylish formatting) | ||
|
|
||
| ## Usage Examples | ||
|
|
||
| ### Basic Usage | ||
|
|
||
| ```bash | ||
| # Default behavior - JSON file output + stylish console output | ||
| npx eslint . | ||
|
|
||
| # Custom output directory and filename | ||
| ESLINT_FORMATTER_CONFIG='{"outputDir":"./ci-reports","filename":"lint-results"}' npx eslint . | ||
| # Creates: ci-reports/lint-results.json + terminal output | ||
| ``` | ||
|
|
||
| ### Multiple Output Formats | ||
|
|
||
| ```bash | ||
| # Generate JSON file | ||
| ESLINT_FORMATTER_CONFIG='{"formats":["json"],"terminal":"stylish"}' npx eslint . | ||
| # Creates: .eslint/eslint-report.json + terminal output | ||
|
|
||
| # Custom directory with JSON format | ||
| ESLINT_FORMATTER_CONFIG='{"outputDir":"./reports","filename":"eslint-results","formats":["json"]}' npx eslint . | ||
| # Creates: reports/eslint-results.json | ||
| ``` | ||
|
|
||
| ### Terminal Output Only | ||
|
|
||
| ```bash | ||
| # Only show terminal output, no files | ||
| ESLINT_FORMATTER_CONFIG='{"formats":[],"terminal":"stylish"}' npx eslint . | ||
|
|
||
| # Different terminal format | ||
| ESLINT_FORMATTER_CONFIG='{"formats":[],"terminal":"stylish"}' npx eslint . | ||
| ``` | ||
|
|
||
| ## Default Behavior | ||
|
|
||
| When no `ESLINT_FORMATTER_CONFIG` is provided, the formatter uses these defaults: | ||
|
|
||
| - **outputDir**: `./.eslint` (relative to current working directory) | ||
| - **filename**: `eslint-report` | ||
| - **formats**: `["json"]` | ||
| - **terminal**: `stylish` | ||
| - **verbose**: `false` | ||
|
|
||
| This means by default you get: | ||
|
|
||
| - A JSON file at `./.eslint/eslint-report.json` | ||
| - Stylish terminal output |
This file contains hidden or 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,21 @@ | ||
| import tseslint from 'typescript-eslint'; | ||
| import baseConfig from '../../eslint.config.js'; | ||
|
|
||
| export default tseslint.config( | ||
| ...baseConfig, | ||
| { | ||
| files: ['**/*.ts'], | ||
| languageOptions: { | ||
| parserOptions: { | ||
| projectService: true, | ||
| tsconfigRootDir: import.meta.dirname, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| files: ['**/*.json'], | ||
| rules: { | ||
| '@nx/dependency-checks': ['error'], | ||
| }, | ||
| }, | ||
| ); |
This file contains hidden or 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,41 @@ | ||
| { | ||
| "name": "eslint-formatter-multi", | ||
| "version": "0.0.1", | ||
| "private": false, | ||
| "type": "module", | ||
| "main": "./src/index.js", | ||
| "types": "./src/index.d.ts", | ||
| "engines": { | ||
| "node": ">=17.0.0" | ||
| }, | ||
| "description": "ESLint formatter that supports multiple output formats and destinations simultaneously", | ||
| "author": "Michael Hladky", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/code-pushup/cli.git", | ||
| "directory": "tools/eslint-formatter-multi" | ||
| }, | ||
| "keywords": [ | ||
| "eslint", | ||
| "eslint-formatter", | ||
| "eslintformatter", | ||
| "multi-format", | ||
| "code-quality", | ||
| "linting" | ||
| ], | ||
| "files": [ | ||
| "src/*", | ||
| "README.md" | ||
| ], | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "dependencies": { | ||
| "ansis": "^3.3.0", | ||
| "tslib": "^2.8.1" | ||
| }, | ||
| "peerDependencies": { | ||
| "eslint": "^8.0.0 || ^9.0.0" | ||
| } | ||
| } | ||
This file contains hidden or 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,12 @@ | ||
| { | ||
| "name": "eslint-formatter-multi", | ||
| "$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
| "sourceRoot": "tools/eslint-formatter-multi/src", | ||
| "projectType": "library", | ||
| "tags": ["scope:tooling", "type:util"], | ||
| "targets": { | ||
| "lint": {}, | ||
| "unit-test": {}, | ||
| "build": {} | ||
| } | ||
| } |
This file contains hidden or 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,3 @@ | ||
| export { default } from './lib/multiple-formats.js'; | ||
| export * from './lib/multiple-formats.js'; | ||
| export * from './lib/utils.js'; |
This file contains hidden or 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,83 @@ | ||
| import type { ESLint } from 'eslint'; | ||
| import * as process from 'node:process'; | ||
| import type { FormatterConfig } from './types.js'; | ||
| import { | ||
| type EslintFormat, | ||
| formatTerminalOutput, | ||
| findConfigFromEnv as getConfigFromEnv, | ||
| persistEslintReports, | ||
| } from './utils.js'; | ||
|
|
||
| export const DEFAULT_OUTPUT_DIR = '.eslint'; | ||
| export const DEFAULT_FILENAME = 'eslint-report'; | ||
| export const DEFAULT_FORMATS = ['json'] as EslintFormat[]; | ||
| export const DEFAULT_TERMINAL = 'stylish' as EslintFormat; | ||
|
|
||
| export const DEFAULT_CONFIG: Required< | ||
| Pick< | ||
| FormatterConfig, | ||
| 'outputDir' | 'filename' | 'formats' | 'terminal' | 'verbose' | ||
| > | ||
| > = { | ||
| outputDir: DEFAULT_OUTPUT_DIR, | ||
| filename: DEFAULT_FILENAME, | ||
| formats: DEFAULT_FORMATS, | ||
| terminal: DEFAULT_TERMINAL, | ||
| verbose: false, | ||
| }; | ||
|
|
||
| /** | ||
| * Format ESLint results using multiple configurable formatters | ||
| * | ||
| * @param results - The ESLint results | ||
| * @param args - The arguments passed to the formatter | ||
| * @returns The formatted results for terminal display | ||
| * | ||
| * @example | ||
| * // Basic usage: | ||
| * ESLINT_FORMATTER_CONFIG='{"filename":"lint-results","formats":["json"],"terminal":"stylish"}' npx eslint . | ||
| * // Creates: .eslint/eslint-results.json + terminal output | ||
| * | ||
| * // With custom output directory: | ||
| * ESLINT_FORMATTER_CONFIG='{"outputDir":"./ci-reports","filename":"eslint-report","formats":["json","html"],"terminal":"stylish"}' nx lint utils | ||
| * // Creates: ci-reports/eslint-report.json, ci-reports/eslint-report.html + terminal output | ||
| * | ||
| * Configuration schema: | ||
| * { | ||
| * "outputDir": "./reports", // Optional: Output directory (default: cwd/.eslint) | ||
| * "filename": "eslint-report", // Optional: Base filename without extension (default: 'eslint-report') | ||
| * "formats": ["json"], // Optional: Array of format names for file output (default: ['json']) | ||
| * "terminal": "stylish" // Optional: Format for terminal output (default: 'stylish') | ||
| * } | ||
| */ | ||
| export default async function multipleFormats( | ||
| results: ESLint.LintResult[], | ||
| _args?: unknown, | ||
| ): Promise<string> { | ||
| const config = { | ||
| ...DEFAULT_CONFIG, | ||
| ...getConfigFromEnv(process.env), | ||
| } satisfies FormatterConfig; | ||
|
|
||
| const { | ||
| outputDir = DEFAULT_OUTPUT_DIR, | ||
| filename, | ||
| formats, | ||
| terminal, | ||
| verbose = false, | ||
| } = config; | ||
|
|
||
| try { | ||
| await persistEslintReports(formats, results, { | ||
| outputDir, | ||
| filename, | ||
| verbose, | ||
| }); | ||
| } catch (error) { | ||
| if (verbose) { | ||
| console.error('Error writing ESLint reports:', error); | ||
| } | ||
| } | ||
|
|
||
| return formatTerminalOutput(terminal, results); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.