Skip to content

Commit

Permalink
#84 Add ESLint support and configure it to use Drupal standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
hkirsman committed Oct 28, 2022
1 parent 6791e43 commit c81d7ed
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ composer require wunderio/code-quality --dev
cp vendor/wunderio/code-quality/config/grumphp.yml ./grumphp.yml
cp vendor/wunderio/code-quality/config/phpstan.neon ./phpstan.neon
cp vendor/wunderio/code-quality/config/psalm.xml ./psalm.xml
npm init # If you don't have package.json does not exist yet in your project.
npm i eslint@^8.9.0 eslint-config-airbnb@^19.0.4 eslint-config-prettier@^8.4.0 eslint-plugin-import@^2.25.4 eslint-plugin-jquery@^1.5.1 eslint-plugin-jsx-a11y@^6.5.1 eslint-plugin-prettier@^4.0.0 eslint-plugin-react@^7.28.0 eslint-plugin-yml@^0.14.0 prettier@^2.5.1
```

The commit hook for GrumPHP is automatically installed on composer require.
Expand Down
2 changes: 2 additions & 0 deletions config/grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ grumphp:
yaml_lint: ~
json_lint: ~
psalm: ~
eslint: ~
extensions:
- Wunderio\GrumPHP\Task\PhpCompatibility\PhpCompatibilityExtensionLoader
- Wunderio\GrumPHP\Task\PhpCheckSyntax\PhpCheckSyntaxExtensionLoader
Expand All @@ -23,3 +24,4 @@ grumphp:
- Wunderio\GrumPHP\Task\YamlLint\YamlLintExtensionLoader
- Wunderio\GrumPHP\Task\JsonLint\JsonLintExtensionLoader
- Wunderio\GrumPHP\Task\Psalm\PsalmExtensionLoader
- Wunderio\GrumPHP\Task\ESLint\EslintExtensionLoader
14 changes: 14 additions & 0 deletions src/Task/ESLint/EslintExtensionLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Wunderio\GrumPHP\Task\ESLint;

use Wunderio\GrumPHP\Task\AbstractExternalExtensionLoader;

/**
* Class PhpCompatibilityExtensionLoader.
*
* Register Eslint task in the service container of GrumPHP.
*
* @package Wunderio\GrumPHP\Task
*/
class EslintExtensionLoader extends AbstractExternalExtensionLoader {}
46 changes: 46 additions & 0 deletions src/Task/ESLint/EslintTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types = 1);

namespace Wunderio\GrumPHP\Task\ESLint;

use GrumPHP\Collection\ProcessArgumentsCollection;
use Wunderio\GrumPHP\Task\AbstractMultiPathProcessingTask;

/**
* Class ESLint2Task.
*
* Eslint task.
*
* @package Wunderio\GrumPHP\Task
*/
class EslintTask extends AbstractMultiPathProcessingTask {

/**
* {@inheritdoc}
*/
public function buildArguments(iterable $files): ProcessArgumentsCollection {
$config = $this->getConfig()->getOptions();
$arguments = ProcessArgumentsCollection::forExecutable($config['bin']);

$arguments->add('--config=' . $config['config']);
$arguments->addOptionalArgument('--debug', $config['debug']);
$arguments->addOptionalCommaSeparatedArgument('--ext=%s', (array) $config['extensions']);
$arguments->addOptionalArgument('--format=%s', $config['format']);
// @todo Not sure if this works.
$arguments->addOptionalIntegerArgument('--max-warnings=%d', $config['max_warnings']);
$arguments->addOptionalBooleanArgument('--no-eslintrc=%s', $config['no_eslintrc'], 'true', 'false');
$arguments->addOptionalBooleanArgument('--quiet=%s', $config['quiet'], 'true', 'false');
foreach ($config['ignore_patterns'] as $ignore_pattern) {
$arguments->add('--ignore-pattern=' . $ignore_pattern);
}

foreach ($files as $file) {
$files_new[] = $file;
$arguments->add($file);
}

return $arguments;
}

}
29 changes: 29 additions & 0 deletions src/Task/ESLint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ESLint

Check js code against coding standards and security standards. By default Drupal rules are loaded.

### grumphp.yml (with current defaults):
````yml
parameters:
tasks:
eslint:
ignore_patterns:
- '**/vendor/**'
- '**/node_modules/**'
- '**/core/**'
- '**/libraries/**'
- '**/contrib/**'
extensions: ['js', 'jsx', 'ts', 'tsx', 'vue']
run_on: [ 'web/modules/custom', 'web/themes/custom' ]
bin: 'node_modules/.bin/eslint'
config: 'web/core/.eslintrc.passing.json'
debug: false
format: ~
max_warnings: ~
no_eslintrc: false
quiet: ~
extensions:
- Wunderio\GrumPHP\Task\PhpCompatibilityTask\PhpCompatibilityExtensionLoader
````


41 changes: 39 additions & 2 deletions src/Task/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,50 @@ Wunderio\GrumPHP\Task\Psalm\PsalmTask:
allowed_types: ['bool']
report:
defaults: ~
allowed_types: ['string', 'null' ]
allowed_types: ['string', 'null']
output_format:
defaults: null
allowed_types: ['string', 'null' ]
allowed_types: ['string', 'null']
threads:
defaults: 6
allowed_types: ['int']
show_info:
defaults: false
allowed_types: ['bool']
Wunderio\GrumPHP\Task\ESLint\EslintTask:
options:
ignore_patterns:
defaults:
- '**/vendor/**'
- '**/node_modules/**'
- '**/core/**'
- '**/libraries/**'
- '**/contrib/**'
allowed_types: ['array']
extensions:
defaults: ['js', 'jsx', 'ts', 'tsx', 'vue'],
allowed_types: ['array']
run_on:
defaults: ['web/modules/custom', 'web/themes/custom']
allowed_types: ['array']
bin:
defaults: 'node_modules/.bin/eslint'
allowed_types: ['string']
config:
defaults: 'web/core/.eslintrc.passing.json'
allowed_types: ['string']
debug:
defaults: false
allowed_types: ['bool']
format:
defaults: null
allowed_types: ['string', 'null']
max_warnings:
defaults: null
allowed_types: ['int', 'null']
no_eslintrc:
defaults: false
allowed_types: ['bool']
quiet:
defaults: null
allowed_types: ['bool', 'null']

0 comments on commit c81d7ed

Please sign in to comment.