typescript-eslint-language-service
Version:
TypeScript language service plugin for ESLint
70 lines • 2.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ESLintConfigProvider = void 0;
const path_1 = __importDefault(require("path"));
const eslintrc_1 = require("@eslint/eslintrc");
function getFactroyClass() {
return eslintrc_1.Legacy.CascadingConfigArrayFactory;
}
const ESLINTRC_SUFFIX_LIST = [
".eslintrc.js",
".eslintrc.yaml",
".eslintrc.yml",
".eslintrc.json",
".eslintrc",
"package.json",
];
class ESLintConfigProvider {
constructor({ host, log = () => { }, directoriesToWatch }) {
this.host = host;
this.log = log;
const eslintRecommendedPath = this.resolveESLintIntrinsicConfigPath("eslint-recommended");
const eslintAllPath = this.resolveESLintIntrinsicConfigPath("eslint-all");
//
// It requires the ESLint intrinsic configuration js file path when user config has "eslint:recommended" or "eslint:all" such as:
//
// ```yaml
// extends:
// - "eslint:recommended"
// ```
//
this.factory = new (getFactroyClass())({
eslintAllPath,
eslintRecommendedPath,
});
directoriesToWatch.forEach(directory => {
ESLINTRC_SUFFIX_LIST.map(suffix => path_1.default.resolve(directory, suffix)).forEach(eslintrcFilepath => {
this.host.watchFile(eslintrcFilepath, () => this.factory.clearCache(), 50);
});
});
}
getConfigArrayForFile(fileName) {
return this.factory.getConfigArrayForFile(fileName);
}
getConfigForFile(fileName) {
return this.factory.getConfigArrayForFile(fileName).extractConfig(fileName);
}
resolveESLintIntrinsicConfigPath(name) {
try {
// Config js files move to @eslint/js package sinse ESLint v8.35.
// https://github.com/eslint/eslint/pull/16844
return require.resolve(`@eslint/js/src/configs/${name}`);
}
catch {
try {
// For legacy ESLint < v8.35
const fragments = require.resolve("eslint").split(path_1.default.join("node_modules", "eslint"));
return path_1.default.join(...fragments.slice(0, fragments.length - 1), "node_modules", "eslint", "conf", `${name}.js`);
}
catch (e) {
this.log(String(e));
}
}
return undefined;
}
}
exports.ESLintConfigProvider = ESLintConfigProvider;
//# sourceMappingURL=eslint-config-provider.js.map