From 0f0b0628c1f9f4ce6d92f3ae258402e5547d2c17 Mon Sep 17 00:00:00 2001 From: Princi Vershwal Date: Tue, 17 Sep 2024 18:35:39 +0530 Subject: [PATCH] Added caching for checking partials (#553) Ref: https://linear.app/tryghost/issue/ENG-1444/gscan-slow-checking-for-themes-with-many-files#comment-62c7724a --- lib/checks/090-template-syntax.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/checks/090-template-syntax.js b/lib/checks/090-template-syntax.js index 38dd2e4f..2eed2049 100644 --- a/lib/checks/090-template-syntax.js +++ b/lib/checks/090-template-syntax.js @@ -3,7 +3,7 @@ const spec = require('../specs'); const {versions, getPackageJSON} = require('../utils'); const ASTLinter = require('../ast-linter'); -function processFileFunction(files, failures, rules) { +function processFileFunction(files, failures, rules, partialVerificationCache) { // This rule is needed to find partials // Partials are needed for a full parsing if (!rules['mark-used-partials']) { @@ -16,6 +16,16 @@ function processFileFunction(files, failures, rules) { return; } + const fileName = themeFile.file; + + // Check if the file is a partial + const isPartial = fileName.startsWith('partials/'); + + // Skip if already cached (for partials only) + if (isPartial && partialVerificationCache.has(fileName)) { + return; + } + const astResults = linter.verify({ parsed: themeFile.parsed, rules, @@ -30,6 +40,11 @@ function processFileFunction(files, failures, rules) { }); } + // Cache the result for this partial + if (isPartial) { + partialVerificationCache.set(fileName, astResults); + } + linter.partials.forEach(({normalizedName}) => { const file = files.find(f => f.normalizedFile === `partials/${normalizedName}.hbs`); if (file) { @@ -62,15 +77,16 @@ const checkTemplateSyntax = function checkTemplateSyntax(theme, options) { }); _.each(rulesToCheck, function (check, ruleCode) { + const partialVerificationCache = new Map(); const failures = []; const processFile = processFileFunction( theme.files, failures, { [ruleCode]: require(`../ast-linter/rules`)[ruleCode] - } + }, + partialVerificationCache ); - const linter = new ASTLinter({ partials: theme.partials, inlinePartials: [],