Skip to content

refactor: add cache for template literal parsing #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/eslint-plugin/lib/rules/indent/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
* @property {Record<string, number>} [Option2.tagChildrenIndent]
*/

const { parse } = require("@html-eslint/template-parser");
const { getCachedParseResult } = require("../utils/template-cache");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative implementation would be to make caching a part of the parse function itself so that this is completely transparent to rules. I opted to go this route to avoid making changes that might be undesirable to anyone who is using the @html-eslint/template-parser package in isolation.

const { traverse } = require("@html-eslint/template-parser/lib/traverser");
const { NODE_TYPES } = require("@html-eslint/parser");
const { RULE_CATEGORY } = require("../../constants");
const {
Expand Down Expand Up @@ -386,13 +387,18 @@ module.exports = {
TaggedTemplateExpression(node) {
if (shouldCheckTaggedTemplateExpression(node, context)) {
const base = getTemplateLiteralBaseIndentLevel(node.quasi);
parse(node.quasi, getSourceCode(context), createIndentVisitor(base));
const { ast } = getCachedParseResult(
node.quasi,
getSourceCode(context)
);
traverse(ast, createIndentVisitor(base), null);
}
},
TemplateLiteral(node) {
if (shouldCheckTemplateLiteral(node, context)) {
const base = getTemplateLiteralBaseIndentLevel(node);
parse(node, getSourceCode(context), createIndentVisitor(base));
const { ast } = getCachedParseResult(node, getSourceCode(context));
traverse(ast, createIndentVisitor(base), null);
}
},
};
Expand Down
28 changes: 21 additions & 7 deletions packages/eslint-plugin/lib/rules/no-duplicate-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* @import {RuleModule} from "../types";
*/

const { parse } = require("@html-eslint/template-parser");
const { getCachedParseResult } = require("./utils/template-cache");
const { traverse } = require("@html-eslint/template-parser/lib/traverser");
const { RULE_CATEGORY } = require("../constants");
const { findAttr } = require("./utils/node");
const {
Expand Down Expand Up @@ -90,18 +91,31 @@ module.exports = {
TaggedTemplateExpression(node) {
const idAttrsMap = new Map();
if (shouldCheckTaggedTemplateExpression(node, context)) {
parse(node.quasi, getSourceCode(context), {
Tag: createTagVisitor(idAttrsMap),
});
const { ast } = getCachedParseResult(
node.quasi,
getSourceCode(context)
);
traverse(
ast,
{
Tag: createTagVisitor(idAttrsMap),
},
null
);
}
report(idAttrsMap);
},
TemplateLiteral(node) {
const idAttrsMap = new Map();
if (shouldCheckTemplateLiteral(node, context)) {
parse(node, getSourceCode(context), {
Tag: createTagVisitor(idAttrsMap),
});
const { ast } = getCachedParseResult(node, getSourceCode(context));
traverse(
ast,
{
Tag: createTagVisitor(idAttrsMap),
},
null
);
}
report(idAttrsMap);
},
Expand Down
12 changes: 9 additions & 3 deletions packages/eslint-plugin/lib/rules/no-duplicate-in-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* @import {RuleModule} from "../types";
*/

const { parse } = require("@html-eslint/template-parser");
const { getCachedParseResult } = require("./utils/template-cache");
const { traverse } = require("@html-eslint/template-parser/lib/traverser");
const { RULE_CATEGORY } = require("../constants");
const { findAttr } = require("./utils/node");
const {
Expand Down Expand Up @@ -168,7 +169,11 @@ module.exports = {

if (shouldCheckTaggedTemplateExpression(node, context)) {
const visitor = createTagVisitor(tagsMap, headCountRef);
parse(node.quasi, getSourceCode(context), visitor);
const { ast } = getCachedParseResult(
node.quasi,
getSourceCode(context)
);
traverse(ast, visitor, null);
report(tagsMap);
}
},
Expand All @@ -179,7 +184,8 @@ module.exports = {

if (shouldCheckTemplateLiteral(node, context)) {
const visitor = createTagVisitor(tagsMap, headCountRef);
parse(node, getSourceCode(context), visitor);
const { ast } = getCachedParseResult(node, getSourceCode(context));
traverse(ast, visitor, null);
report(tagsMap);
}
},
Expand Down
12 changes: 7 additions & 5 deletions packages/eslint-plugin/lib/rules/no-multiple-empty-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @property {number} Option.max
*/

const { parse } = require("@html-eslint/template-parser");
const { getCachedParseResult } = require("./utils/template-cache");
const { RULE_CATEGORY } = require("../constants");
const {
shouldCheckTaggedTemplateExpression,
Expand Down Expand Up @@ -117,10 +117,9 @@ module.exports = {
},
TaggedTemplateExpression(node) {
if (shouldCheckTaggedTemplateExpression(node, context)) {
const { html, tokens } = parse(
const { html, tokens } = getCachedParseResult(
node.quasi,
getSourceCode(context),
{}
getSourceCode(context)
);
const lines = codeToLines(html);
check(
Expand All @@ -133,7 +132,10 @@ module.exports = {
},
TemplateLiteral(node) {
if (shouldCheckTemplateLiteral(node, context)) {
const { html, tokens } = parse(node, getSourceCode(context), {});
const { html, tokens } = getCachedParseResult(
node,
getSourceCode(context)
);
const lines = codeToLines(html);
check(
lines,
Expand Down
12 changes: 7 additions & 5 deletions packages/eslint-plugin/lib/rules/no-trailing-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @import {CommentContent, Text} from "@html-eslint/types";
*/

const { parse } = require("@html-eslint/template-parser");
const { getCachedParseResult } = require("./utils/template-cache");
const { RULE_CATEGORY } = require("../constants");
const {
getTemplateTokens,
Expand Down Expand Up @@ -107,10 +107,9 @@ module.exports = {
},
TaggedTemplateExpression(node) {
if (shouldCheckTaggedTemplateExpression(node, context)) {
const { html, tokens } = parse(
const { html, tokens } = getCachedParseResult(
node.quasi,
getSourceCode(context),
{}
getSourceCode(context)
);
const lines = codeToLines(html);
check(
Expand All @@ -127,7 +126,10 @@ module.exports = {
},
TemplateLiteral(node) {
if (shouldCheckTemplateLiteral(node, context)) {
const { html, tokens } = parse(node, getSourceCode(context), {});
const { html, tokens } = getCachedParseResult(
node,
getSourceCode(context)
);
const lines = codeToLines(html);
check(
html,
Expand Down
36 changes: 36 additions & 0 deletions packages/eslint-plugin/lib/rules/utils/template-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @import {TemplateLiteral} from "@html-eslint/types";
* @import {DocumentNode, AnyToken} from "es-html-parser";
* @import {SourceCode} from "eslint";
*/

const { parse } = require("@html-eslint/template-parser");

/**
* Cache for parsed template literals to avoid re-parsing the same template multiple times.
* Uses WeakMap for automatic garbage collection when nodes are no longer referenced.
* @type {WeakMap<TemplateLiteral, {ast: any, html: string, tokens: any[]}>}
*/
const templateCache = new WeakMap();

/**
* Get or create cached parse result for a template literal.
* @param {TemplateLiteral} node
* @param {SourceCode} sourceCode
* @returns {{ast: DocumentNode, html: string, tokens: AnyToken[]}}
*/
function getCachedParseResult(node, sourceCode) {
// Check if we already have a cached result for this node
const cachedResult = templateCache.get(node);
if (cachedResult) {
return cachedResult;
}

// Parse and cache the result
const result = parse(node, sourceCode, {});
templateCache.set(node, result);

return result;
}

module.exports = { getCachedParseResult };
12 changes: 9 additions & 3 deletions packages/eslint-plugin/lib/rules/utils/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const {
shouldCheckTaggedTemplateExpression,
shouldCheckTemplateLiteral,
} = require("./settings");
const { parse } = require("@html-eslint/template-parser");
const { getCachedParseResult } = require("./template-cache");
const { traverse } = require("@html-eslint/template-parser/lib/traverser");
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The traverse import and usage pattern is duplicated across multiple rule files. Consider centralizing a helper that wraps parse + traverse to reduce boilerplate and ease future updates.

Copilot uses AI. Check for mistakes.

const { getSourceCode } = require("./source-code");

/**
Expand All @@ -18,12 +19,17 @@ function createTemplateVisitors(context, visitors) {
return {
TaggedTemplateExpression(node) {
if (shouldCheckTaggedTemplateExpression(node, context)) {
parse(node.quasi, getSourceCode(context), visitors);
const { ast } = getCachedParseResult(
node.quasi,
getSourceCode(context)
);
traverse(ast, visitors, null);
}
},
TemplateLiteral(node) {
if (shouldCheckTemplateLiteral(node, context)) {
parse(node, getSourceCode(context), visitors);
const { ast } = getCachedParseResult(node, getSourceCode(context));
traverse(ast, visitors, null);
}
},
};
Expand Down
Loading