Skip to content

Commit

Permalink
Prefer to use user workspace ESLint.
Browse files Browse the repository at this point in the history
  • Loading branch information
daidodo committed Mar 31, 2022
1 parent d8badd4 commit c54d27d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/lib/format/config/eslint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
} from 'eslint';
import { type PromiseType } from 'utility-types';

import findUp from '@dozerg/find-up';

import { logger } from '../../../common';
import { type Configuration } from '../../../config';
import { apply } from './helper';
Expand Down Expand Up @@ -44,6 +46,7 @@ export async function enhanceWithEslint(
async function loadESLintConfig(fileName: string, configFile?: string) {
const log = logger('format-imports.loadESLintConfig');
log.debug('Loading ESLint config for fileName:', fileName, 'from', configFile ?? 'default');
const ESLint = findESLint(fileName);
log.debug('ESLint API version:', ESLint.version);
try {
const eslint = new ESLint({ overrideConfigFile: configFile });
Expand All @@ -58,3 +61,27 @@ async function loadESLintConfig(fileName: string, configFile?: string) {
return undefined;
}
}

declare const __webpack_require__: typeof require;
declare const __non_webpack_require__: typeof require;
const req = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;

function findESLint(fromPath: string) {
const log = logger('format-imports.findESLint');
const userESLint = findUserESLint(fromPath);
if (userESLint) return userESLint;
log.warn('Cannot find eslint module from path:', fromPath, ', use pre-packed');
return ESLint;
}

function findUserESLint(fromPath: string) {
const log = logger('format-imports.findUserESLint');
const [eslintPath] = findUp.sync('node_modules/eslint', {
cwd: fromPath,
stopAtLimit: 1,
type: 'directory',
});
if (!eslintPath) return undefined;
log.debug('Found eslint in', eslintPath);
return req(eslintPath).ESLint;
}

0 comments on commit c54d27d

Please sign in to comment.