Skip to content

Commit 218c8e7

Browse files
committed
perf(kit): cache jsx config to improve performance
1 parent 32985ae commit 218c8e7

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

packages/utilities/kit/src/JsxConfig/JsxConfig.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ export function getFromContext(context: RuleContext) {
4141
};
4242
}
4343

44+
const cache = new WeakMap<RuleContext["sourceCode"], JsxConfig>();
45+
4446
/**
4547
* Get JsxConfig from annotation
4648
* @param context The RuleContext
4749
* @returns JsxConfig
4850
*/
4951
export function getFromAnnotation(context: RuleContext) {
52+
if (cache.has(context.sourceCode)) return cache.get(context.sourceCode);
5053
if (!context.sourceCode.text.includes("@jsx")) return {};
5154
let jsx, jsxFrag, jsxRuntime, jsxImportSource;
5255
for (const comment of context.sourceCode.getAllComments().reverse()) {
@@ -61,5 +64,6 @@ export function getFromAnnotation(context: RuleContext) {
6164
if (jsxFrag != null) options.jsxFragmentFactory = jsxFrag;
6265
if (jsxRuntime != null) options.jsx = jsxRuntime === "classic" ? JsxEmit.React : JsxEmit.ReactJSX;
6366
if (jsxImportSource != null) options.jsxImportSource = jsxImportSource;
67+
cache.set(context.sourceCode, options);
6468
return options;
6569
}

0 commit comments

Comments
 (0)