Skip to content

Commit

Permalink
perf: 72 times faster to check no-unused-selector (#285)
Browse files Browse the repository at this point in the history
* 33 times faster to check no-unused-selector

* add changeset
  • Loading branch information
baseballyama authored Jul 2, 2023
1 parent d078489 commit 0e015a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-cooks-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-vue-scoped-css": patch
---

Improve `no-unused-selector` performance
15 changes: 15 additions & 0 deletions lib/styles/context/vue-components/find-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ function getVueComponentObject(
return null;
}

const vueComponentCache = new WeakMap<
RuleContext,
{ component: AST.ESLintObjectExpression | null; cachedAt: number }
>();

/**
* Find Vue component of the current file.
* @param {RuleContext} context The ESLint rule context object.
Expand All @@ -63,6 +68,11 @@ function getVueComponentObject(
function findVueComponent(
context: RuleContext
): AST.ESLintObjectExpression | null {
const cached = vueComponentCache.get(context);
if (cached !== undefined && cached.cachedAt > Date.now() - 1000) {
return cached.component;
}

const sourceCode = context.getSourceCode();
const componentComments = sourceCode
.getAllComments()
Expand Down Expand Up @@ -118,6 +128,11 @@ function findVueComponent(
// noop
},
});

vueComponentCache.set(context, {
component: result,
cachedAt: Date.now(),
});
return result;
}

Expand Down

0 comments on commit 0e015a7

Please sign in to comment.