Skip to content

Commit

Permalink
fix(extension šŸ›): cd to projct cwd before making checks to avoid brakā€¦
Browse files Browse the repository at this point in the history
ā€¦ing eslintrc files (#159)

Fixes a bug where eslintrc isn't executed in the project directory, causing rulesets that use the
working directory to determine their rules to fail. Fixes a bug where eslintrc isn't executed in the
project directory, causing rulesets that use the working directory to determine their rules to fail.
Does this by changing directory to the project before executing checks

Co-authored-by: Kirk Holloway <[email protected]>
  • Loading branch information
kirkobyte and kirkobyte authored Jul 11, 2020
1 parent 9b0c8ea commit 7b05ea9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .betterer.results
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ exports[`new eslint rules`] = {
[63, 4, 71, "Promises must be handled appropriately or explicitly marked as ignored with the \`void\` operator.", "2936064845"],
[69, 6, 27, "Promises must be handled appropriately or explicitly marked as ignored with the \`void\` operator.", "3607138074"]
],
"packages/extension/src/server/validator.ts:2916623605": [
"packages/extension/src/server/validator.ts:2557653799": [
[48, 10, 90, "Promises must be handled appropriately or explicitly marked as ignored with the \`void\` operator.", "1821734533"],
[95, 12, 94, "Promises must be handled appropriately or explicitly marked as ignored with the \`void\` operator.", "2449957056"]
[96, 12, 94, "Promises must be handled appropriately or explicitly marked as ignored with the \`void\` operator.", "2449957056"]
],
"packages/typescript/src/typescript.ts:405611543": [
[20, 12, 6, "Unsafe array destructuring of a tuple element with an any value.", "1544266319"],
Expand Down
35 changes: 19 additions & 16 deletions packages/eslint/src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
BettererFileIssuesMapRaw,
BettererFileResolver
} from '@betterer/betterer';
import { CLIEngine, Linter } from 'eslint';
import { Linter, ESLint } from 'eslint';
import LinesAndColumns from 'lines-and-columns';

import { FILE_GLOB_REQUIRED, RULE_OPTIONS_REQUIRED, RULES_OPTIONS_REQUIRED } from './errors';
Expand Down Expand Up @@ -43,35 +43,38 @@ export function eslintBetterer(globs: string | ReadonlyArray<string>, rule: ESLi
// once we remove `eslintBetterer`:
function createEslintTest(rules: ESLintRulesConfig): BettererFileTest {
const resolver = new BettererFileResolver(3);
return new BettererFileTest(resolver, (files) => {
return new BettererFileTest(resolver, async (files) => {
const { cwd } = resolver;
const cli = new CLIEngine({ cwd });
const cli = new ESLint({ cwd });

const issues: BettererFileIssuesMapRaw = {};
files.forEach((filePath) => {
const linterOptions = cli.getConfigForFile(filePath);
issues[filePath] = getFileIssues(cwd, linterOptions, rules, filePath);
});
await Promise.all(
files.map(async (filePath) => {
const linterOptions = (await cli.calculateConfigForFile(filePath)) as Linter.Config;
issues[filePath] = await getFileIssues(cwd, linterOptions, rules, filePath);
})
);
return issues;
});
}

function getFileIssues(
async function getFileIssues(
cwd: string,
linterOptions: Linter.Config,
rules: ESLintRulesConfig,
filePath: string
): BettererFileIssuesRaw {
const runner = new CLIEngine({
...linterOptions,
cwd,
): Promise<BettererFileIssuesRaw> {
const runner = new ESLint({
baseConfig: {
...linterOptions,
rules
},
useEslintrc: false,
globals: Object.keys(linterOptions.globals || {}),
rules
cwd
});

const report = runner.executeOnFiles([filePath]);
const resultsWithSource = report.results.filter((result) => result.source);
const result = await runner.lintFiles([filePath]);
const resultsWithSource = result.filter((result) => result.source);
return ([] as BettererFileIssuesRaw).concat(
...resultsWithSource.map((result) => {
const { source, messages } = result;
Expand Down
5 changes: 4 additions & 1 deletion packages/extension/src/server/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ export class BettererValidator {

const loading = load(this._connection);
let status = BettererStatus.ok;

const extensionCwd = process.cwd();
try {
process.chdir(cwd);
const config = await getBettererConfig(workspace);
const runs = await betterer.single({ ...config, cwd }, filePath);

Expand Down Expand Up @@ -98,6 +99,8 @@ export class BettererValidator {
} else {
status = BettererStatus.error;
}
} finally {
process.chdir(extensionCwd);
}
await loading();
this._connection.sendNotification(BettererStatusNotification, status);
Expand Down

0 comments on commit 7b05ea9

Please sign in to comment.