Skip to content

Commit ae15b6d

Browse files
committed
perf: cache ESLint instance for config
1 parent 8e39441 commit ae15b6d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/textlint-rule-eslint.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ export type Options = {
2626
configFile: string;
2727
langs: string[];
2828
};
29+
const createESLint = () => {
30+
const cache = new Map<string, ESLint>();
31+
return function getESLint(esLintConfigFilePath: string) {
32+
const cachedESLint = cache.get(esLintConfigFilePath);
33+
if (cachedESLint) {
34+
return cachedESLint;
35+
}
36+
const engine = new ESLint({
37+
overrideConfigFile: esLintConfigFilePath,
38+
ignore: false
39+
});
40+
cache.set(esLintConfigFilePath, engine);
41+
return engine;
42+
};
43+
};
44+
const getESLint = createESLint();
2945
const reporter: TextlintRuleModule<Options> = (context, options) => {
3046
const { Syntax, RuleError, report, fixer, getSource, locator } = context;
3147
if (!options) {
@@ -37,10 +53,7 @@ const reporter: TextlintRuleModule<Options> = (context, options) => {
3753
const availableLang = options.langs || defaultOptions.langs;
3854
const textlintRCDir = getConfigBaseDir(context);
3955
const esLintConfigFilePath = textlintRCDir ? path.resolve(textlintRCDir, options.configFile) : options.configFile;
40-
const engine = new ESLint({
41-
overrideConfigFile: esLintConfigFilePath,
42-
ignore: false
43-
});
56+
const engine = getESLint(esLintConfigFilePath);
4457
return {
4558
async [Syntax.CodeBlock](node) {
4659
if (!node.lang) {

0 commit comments

Comments
 (0)