Skip to content

Commit eab2dc3

Browse files
committed
wip ESLintPreCommitHook.php
1 parent 20a5bf2 commit eab2dc3

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

config/git-hooks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@
192192
'config' => env('PRETTIER_CONFIG', '.prettierrc.json'),
193193
'additional_params' => env('PRETTIER_ADDITIONAL_PARAMS', ''),
194194
],
195+
'eslint' => [
196+
'path' => env('ESLINT_PATH', 'node_modules/.bin/eslint'),
197+
'config' => env('ESLINT_CONFIG', '.eslintrc.js'),
198+
'additional_params' => env('ESLINT_ADDITIONAL_PARAMS', ''),
199+
],
195200
],
196201

197202
];
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Console\Commands\Hooks;
4+
5+
use Closure;
6+
use Igorsgm\GitHooks\Contracts\CodeAnalyzerPreCommitHook;
7+
use Igorsgm\GitHooks\Git\ChangedFiles;
8+
9+
class ESLintPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
10+
{
11+
/**
12+
* @var string
13+
*/
14+
protected $configParam;
15+
16+
/**
17+
* Name of the hook
18+
*
19+
* @var string
20+
*/
21+
protected $name = 'ESLint';
22+
23+
/**
24+
* Analyze and fix committed JS files using ESLint.
25+
*
26+
* @param ChangedFiles $files The files that have been changed in the current commit.
27+
* @param Closure $next A closure that represents the next middleware in the pipeline.
28+
* @return mixed|null
29+
*/
30+
public function handle(ChangedFiles $files, Closure $next)
31+
{
32+
$this->configParam = $this->configParam();
33+
34+
return $this->setFileExtensions('/\.(jsx?|tsx?|vue)$/')
35+
->setAnalyzerExecutable(config('git-hooks.code_analyzers.eslint.path'), true)
36+
->handleCommittedFiles($files, $next);
37+
}
38+
39+
/**
40+
* Returns the command to run ESLint tester
41+
*/
42+
public function analyzerCommand(): string
43+
{
44+
return trim(implode(' ', [
45+
$this->getAnalyzerExecutable(),
46+
$this->configParam,
47+
$this->additionalParams()
48+
]));
49+
}
50+
51+
/**
52+
* Returns the command to run ESLint fixer
53+
*/
54+
public function fixerCommand(): string
55+
{
56+
return trim(
57+
sprintf('%s --fix %s %s', $this->getFixerExecutable(), $this->configParam, $this->additionalParams())
58+
);
59+
}
60+
61+
/**
62+
* Gets the command-line parameter for specifying the configuration file for ESLint.
63+
*
64+
* @return string The command-line parameter for the configuration file, or an empty string if not set.
65+
*/
66+
protected function configParam(): string
67+
{
68+
$eslintConfig = rtrim(config('git-hooks.code_analyzers.eslint.config'), '/');
69+
70+
return empty($eslintConfig) ? '' : '--config='.$eslintConfig;
71+
}
72+
73+
/**
74+
* Retrieves additional parameters for the ESLint code analyzer from the configuration file,
75+
* filters out pre-defined parameters to avoid conflicts, and returns them as a string.
76+
*/
77+
protected function additionalParams(): string
78+
{
79+
$additionalParams = config('git-hooks.code_analyzers.eslint.additional_params');
80+
81+
if (! empty($additionalParams)) {
82+
$additionalParams = preg_replace('/\s+\.(?:(\s)|$)/', '$1', $additionalParams);
83+
$additionalParams = preg_replace('/\s*--(config|c)\b(=\S*)?\s*/', '', $additionalParams);
84+
}
85+
86+
return $additionalParams;
87+
}
88+
}

0 commit comments

Comments
 (0)