From 6b0d5c87ec87adacc71728f96c2c3d1ea4ac164f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Vlas=C3=A1k?= Date: Fri, 17 Jan 2020 15:56:49 +0100 Subject: [PATCH] feat: implement fix for EsLintWithoutErrors --- .../JavaScript/ESLintWithoutErrorsPractice.ts | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/practices/JavaScript/ESLintWithoutErrorsPractice.ts b/src/practices/JavaScript/ESLintWithoutErrorsPractice.ts index 10bdada35..3e98fbcb6 100644 --- a/src/practices/JavaScript/ESLintWithoutErrorsPractice.ts +++ b/src/practices/JavaScript/ESLintWithoutErrorsPractice.ts @@ -7,6 +7,7 @@ import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '. import { DxPractice } from '../DxPracticeDecorator'; import { IPractice } from '../IPractice'; import * as nodePath from 'path'; +import { FixerContext } from '../../contexts/fixer/FixerContext'; @DxPractice({ id: 'JavaScript.ESLintWithoutErrorsPractice', @@ -24,13 +25,12 @@ export class ESLintWithoutErrorsPractice implements IPractice { ); } - async evaluate(ctx: PracticeContext): Promise { + private async runEslint(ctx: PracticeContext, { fix } = { fix: false }) { if (!ctx.fileInspector) { - return PracticeEvaluationResult.unknown; + return; } - let options: CLIEngine.Options = { - fix: false, // Use auto-fixer. + fix, // Use auto-fixer. useEslintrc: false, // Set to false so the project doesn't take the eslint config from home folder. rules: { semi: 2, @@ -78,7 +78,14 @@ export class ESLintWithoutErrorsPractice implements IPractice { } const cli = new CLIEngine(options); - const report = cli.executeOnFiles([ctx.projectComponent.path]); + return cli.executeOnFiles([ctx.projectComponent.path]); + } + + async evaluate(ctx: PracticeContext): Promise { + const report = await this.runEslint(ctx); + if (!report) { + return PracticeEvaluationResult.unknown; + } if (report['errorCount'] === 0) { return PracticeEvaluationResult.practicing; @@ -86,4 +93,8 @@ export class ESLintWithoutErrorsPractice implements IPractice { return PracticeEvaluationResult.notPracticing; } + + async fix(ctx: FixerContext) { + await this.runEslint(ctx, { fix: true }); + } }