Skip to content

Commit

Permalink
Merge pull request #28 from innocenzi/feat/eslint-formatter
Browse files Browse the repository at this point in the history
feat: add `eslint` formatter
  • Loading branch information
rubenvanassche authored Apr 29, 2022
2 parents 3eaba0e + de8cb0c commit 5fef8ea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/laravel/executing-the-transform-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ php artisan typescript:transform --output=types.d.ts

This file will be stored in the resource's path of your Laravel application.

It is also possible to automatically format the generated TypeScript with prettier:
It is also possible to automatically format the generated TypeScript with Prettier, ESLint, or a custom formatter:

```bash
php artisan typescript:transform --format
Expand Down
6 changes: 3 additions & 3 deletions docs/laravel/installation-and-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ return [
'writer' => Spatie\TypeScriptTransformer\Writers\TypeDefinitionWriter::class,

/*
* The generated TypeScript file can be formatted. We ship a Prettier formatter
* out of the box: `PrettierFormatter` but you can also implement your own one.
* The generated TypeScript will not be formatted when no formatter was set.
* The generated TypeScript file can be formatted. We ship two formatters by
* default: a Prettier and an ESLint one. You can also implement your own.
* The generated TypeScript will not be formatted if none is configured.
*/

'formatter' => null,
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/formatters.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Formatters
weight: 7
---

This output file with all the transformed types can be formatted using tools like Prettier. We ship a Prettier formatter by default with the package, which will run Prettier after the output file is generated. you can configure it as such:
This output file with all the transformed types can be formatted using tools like Prettier. We ship an ESLint and a Prettier formatter, which will run after the output file is generated. For instance, you can configure the Prettier formatter as such:

```php
$config = TypeScriptTransformerConfig::create()
Expand Down
20 changes: 20 additions & 0 deletions src/Formatters/EslintFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Spatie\TypeScriptTransformer\Formatters;

use Spatie\TypeScriptTransformer\Formatters\Formatter;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class EslintFormatter implements Formatter
{
public function format(string $file): void
{
$process = new Process(['npx', '--yes', 'eslint', '--fix', '--no-ignore', $file]);
$process->run();

if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
}
}

0 comments on commit 5fef8ea

Please sign in to comment.