Skip to content

Commit

Permalink
fix: Loading yaml .eslintrc file
Browse files Browse the repository at this point in the history
  • Loading branch information
adelkahomolova committed Oct 23, 2019
1 parent bf88525 commit 938e178
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
33 changes: 27 additions & 6 deletions src/practices/JavaScript/ESLintWithoutErrorsPractice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '.
import { DxPractice } from '../DxPracticeDecorator';
import { IPractice } from '../IPractice';
import _ from 'lodash';
import yaml from 'js-yaml';
import { IFileInspector } from '../../inspectors/IFileInspector';
import { inject } from 'inversify';
import { Types } from '../../types';
import { fs } from 'memfs';
import debug from 'debug';

@DxPractice({
id: 'JavaScript.ESLintCorrectlyUsedPractice',
Expand All @@ -15,6 +21,11 @@ import _ from 'lodash';
dependsOn: { practicing: ['JavaScript.ESLintUsed'] },
})
export class ESLintWithoutErrorsPractice implements IPractice {
// private readonly fileInspector: IFileInspector;

// constructor(@inject(Types.IFileInspector) fileInspector: IFileInspector) {
// this.fileInspector = fileInspector;
// }
async isApplicable(ctx: PracticeContext): Promise<boolean> {
return (
ctx.projectComponent.language === ProgrammingLanguage.JavaScript || ctx.projectComponent.language === ProgrammingLanguage.TypeScript
Expand All @@ -38,12 +49,22 @@ export class ESLintWithoutErrorsPractice implements IPractice {
const eslintConfig = await ctx.fileInspector.scanFor(/\.eslintrc/, '/', { shallow: true });

if (eslintConfig.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const baseConfig = require(eslintConfig[0].path);
const plugins = _.clone(baseConfig.plugins);
_.unset(baseConfig, 'plugins');
_.unset(baseConfig, 'extends');
options = { ...options, baseConfig, plugins };
let baseConfig, content;

try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
baseConfig = require(eslintConfig[0].path);

const plugins = _.clone(baseConfig.plugins);
_.unset(baseConfig, 'plugins');
_.unset(baseConfig, 'extends');
options = { ...options, baseConfig, plugins };
} catch (error) {
debug(`Loading .eslintrc file failed with this error:\n${error}`);

content = await ctx.fileInspector.readFile(eslintConfig[0].path);
baseConfig = yaml.safeLoad(content);
}
}

let eslintIgnore;
Expand Down
9 changes: 8 additions & 1 deletion src/scanner/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,14 @@ export class Scanner {
const isFulfilled = ScannerUtils.isFulfilled(practice, practicesWithContextFromComponent);

if (!isFulfilled) continue;
const evaluation = await practice.evaluate({ ...practiceContext, config: practiceConfig });

let evaluation;
try {
evaluation = await practice.evaluate({ ...practiceContext, config: practiceConfig });
} catch (error) {
evaluation = PracticeEvaluationResult.unknown;
debug(`The ${practice.getMetadata().name} practice failed with this error:\n${error}`);
}

const practiceWithContext = {
practice,
Expand Down

0 comments on commit 938e178

Please sign in to comment.