Skip to content

Commit

Permalink
perf: cache ESLint instance for config
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jan 3, 2025
1 parent 8e39441 commit ae15b6d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/textlint-rule-eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ export type Options = {
configFile: string;
langs: string[];
};
const createESLint = () => {
const cache = new Map<string, ESLint>();
return function getESLint(esLintConfigFilePath: string) {
const cachedESLint = cache.get(esLintConfigFilePath);
if (cachedESLint) {
return cachedESLint;
}
const engine = new ESLint({
overrideConfigFile: esLintConfigFilePath,
ignore: false
});
cache.set(esLintConfigFilePath, engine);
return engine;
};
};
const getESLint = createESLint();
const reporter: TextlintRuleModule<Options> = (context, options) => {
const { Syntax, RuleError, report, fixer, getSource, locator } = context;
if (!options) {
Expand All @@ -37,10 +53,7 @@ const reporter: TextlintRuleModule<Options> = (context, options) => {
const availableLang = options.langs || defaultOptions.langs;
const textlintRCDir = getConfigBaseDir(context);
const esLintConfigFilePath = textlintRCDir ? path.resolve(textlintRCDir, options.configFile) : options.configFile;
const engine = new ESLint({
overrideConfigFile: esLintConfigFilePath,
ignore: false
});
const engine = getESLint(esLintConfigFilePath);
return {
async [Syntax.CodeBlock](node) {
if (!node.lang) {
Expand Down

0 comments on commit ae15b6d

Please sign in to comment.