Skip to content

Improved code coverage by removing dead code, extracting shared utilities, and added tests#723

Merged
troyciesco merged 1 commit into
mainfrom
test-updates-260311
Mar 11, 2026
Merged

Improved code coverage by removing dead code, extracting shared utilities, and added tests#723
troyciesco merged 1 commit into
mainfrom
test-updates-260311

Conversation

@troyciesco
Copy link
Copy Markdown
Contributor

@troyciesco troyciesco commented Mar 11, 2026

no ref

These updates are to prep for moving from mocha to vitest.

  • Removed dead if guards in lib/ast-linter/linter.js (scanner context properties are always initialized)
  • Removed redundant params.length checks in lint-no-unknown-custom-theme-select-value-in-match.js
  • Removed unreachable theme.helpers = theme.helpers || {} in 120-no-unknown-globals.js
  • Extracted duplicated getRules/getLogger/applyRule/parseWithAST from checks 100 and 110 into lib/utils/check-utils.js
  • Extracted labsEnabledHelpers from lib/checker.js into lib/utils/labs-enabled-helpers.js
  • Added unit tests for check-utils (12 tests covering logger, applyRule lifecycle, parseWithAST)
  • Added AST linter tests for nested async helpers and prev/next post outside post context rules
  • Added checker tests for labs-enabled-helpers integration
  • Added test fixtures: nested-async-helpers.hbs, prev-next-outside-post.hbs

Note

Low Risk
Low risk refactor that mainly deduplicates check execution code and moves labs-helper configuration, with behavior guarded by new unit tests. Minor linter/rule guard removals could surface latent assumptions but should be covered by existing parsing defaults.

Overview
Refactors theme checks by extracting duplicated getRules/getLogger/applyRule/parseWithAST logic from checks 100 and 110 into a shared lib/utils/check-utils.js, and moves the labs-flagged helper mapping out of checker.js into lib/utils/labs-enabled-helpers.js.

Simplifies AST-linter internals by removing dead guards around scanner context properties and tightening the match-select-value rule to only handle the 3-param form; also removes an unreachable theme.helpers initialization in 120-no-unknown-globals.

Adds targeted tests for the new utilities, labs-enabled helper injection, and additional AST-linter rule coverage (with new .hbs fixtures for nested async helpers and prev_post context validation).

Written by Cursor Bugbot for commit 322aa29. This will update automatically on new commits. Configure here.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 11, 2026

Walkthrough

This pull request refactors the codebase to centralize rule-based linting logic into a shared utility module. A new file lib/utils/check-utils.js exports four utility functions (getRules, getLogger, applyRule, parseWithAST) for orchestrating rule application workflows. Two check modules (100-custom-template-settings-usage.js and 110-page-builder-usage.js) are refactored to use these utilities instead of implementing similar logic locally. Additionally, lib/utils/labs-enabled-helpers.js is introduced as a scaffold for mapping labs-flagged helpers, lib/checker.js is updated to load labs helpers from this module, and several AST linting simplifications are made. New tests are added to validate the new utilities and verify specific AST linting behaviors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~28 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main changes: removing dead code, extracting shared utilities, and adding tests, which aligns with the changeset.
Description check ✅ Passed The pull request description clearly describes the changeset, including code cleanup (removing dead guards), refactoring (extracting shared utilities), and tests added, all aligned with the actual changes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch test-updates-260311

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (4)
lib/checks/100-custom-template-settings-usage.js (1)

42-48: Missing options property in applyRule call.

The lib/checks/110-page-builder-usage.js passes options to applyRule, but this file does not. While the current isEnabled function for GS100 doesn't use options, the inconsistency could cause issues if future rules need access to options.checkVersion or other settings.

Proposed fix for consistency
     _.each(rules, function (check, ruleCode) {
         applyRule({
             code: ruleCode,
             ...check,
-            ...ruleImplementations[ruleCode]
+            ...ruleImplementations[ruleCode],
+            options
         }, theme);
     });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/checks/100-custom-template-settings-usage.js` around lines 42 - 48, The
loop calling applyRule over rules omits the options property, causing
inconsistency with other checks (e.g., 110-page-builder-usage) and potential
future breakage; update the applyRule invocation inside _.each so it passes
options (the same options object used elsewhere) alongside code, check, and
ruleImplementations[ruleCode] — e.g., include options: options — so that
applyRule, and any isEnabled implementations like GS100, receive
options.checkVersion and other settings.
lib/utils/check-utils.js (2)

6-18: Consider removing unnecessary g flag from regex.

The g (global) flag on line 10 is unnecessary when using .match() purely to check if a string matches a pattern. Since ruleCode.match(ruleRegex) is used as a boolean check, the g flag adds no value and could cause subtle issues if the regex were reused (due to lastIndex state).

Suggested simplification
-    const ruleRegex = new RegExp('^' + id + '-.*', 'g');
+    const ruleRegex = new RegExp('^' + id + '-');
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/utils/check-utils.js` around lines 6 - 18, In getRules, the RegExp for
ruleRegex uses the unnecessary 'g' flag which can cause stateful behavior;
change the pattern creation in getRules (ruleRegex) to omit the 'g' flag (e.g.,
new RegExp('^' + id + '-.*')) and/or use RegExp.prototype.test(ruleCode) or
String.prototype.match without the global flag when checking
ruleCode.match(ruleRegex) so the match is a simple boolean and not affected by
lastIndex.

75-82: Mutating the caller's rules object may cause unintended side effects.

Line 81 modifies the rules object passed by the caller. If a caller reuses the same rules object across multiple calls, it will accumulate the mark-used-partials entry. While currently benign (the condition checks for existence), this pattern could mask bugs or cause confusion.

Consider creating a shallow copy before mutation:

Defensive copy suggestion
 function parseWithAST({theme, log, file, rules, callback, partialVerificationCache}) {
     const linter = new ASTLinter();
+    
+    // Avoid mutating the caller's rules object
+    const effectiveRules = {...rules};

     // This rule is needed to find partials
     // Partials are needed for a full parsing
-    if (!rules['mark-used-partials']) {
-        rules['mark-used-partials'] = require(`../ast-linter/rules/mark-used-partials`);
+    if (!effectiveRules['mark-used-partials']) {
+        effectiveRules['mark-used-partials'] = require(`../ast-linter/rules/mark-used-partials`);
     }

Then use effectiveRules in the linter.verify() call on line 100.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/utils/check-utils.js` around lines 75 - 82, In parseWithAST, avoid
mutating the caller's rules object: create a shallow copy (e.g., effectiveRules
= {...rules}) and, if mark-used-partials is missing, add it to effectiveRules
instead of rules; then pass effectiveRules to ASTLinter.verify (the
linter.verify call) so callers' rules remain unchanged and you still ensure the
mark-used-partials rule is present for parsing.
test/check-utils.test.js (1)

153-196: Consider adding a test for getRules.

The module exports getRules, getLogger, applyRule, and parseWithAST, but tests only cover the latter three. Adding coverage for getRules would ensure the regex-based rule filtering and version resolution work as expected.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/check-utils.test.js` around lines 153 - 196, Add a new test case
exercising getRules: call getRules with a mock rules object containing multiple
rule entries (some matching and some not matching a provided regex pattern) and
with different version metadata (e.g., rule.version or rule.from version
ranges), assert that the returned set only includes rules whose keys match the
regex filter and that the resolved rule versions follow the expected resolution
logic (pick the highest/compatible version or fallback as getRules specifies).
Use the existing test harness style (create a fake theme/results if needed and
reuse getLogger patterns) and include at least two assertions: one for regex
filtering and one for correct version selection.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/ast-linter/rules/lint-no-unknown-custom-theme-select-value-in-match.js`:
- Around line 19-22: The current check for match only handles the 3-argument
form and misses the 2-argument shorthand; update the logic in the match handling
block so it supports both 2- and 3-parameter forms (i.e., treat
node.params.length === 2 or === 3), compute the secondary index accordingly (use
indexSecondParameter = node.params.length === 3 ? 2 : 1), and then use
getCustomSettingName(...) and getCustomSettingValue(...) against node.params[0]
and node.params[indexSecondParameter] so shorthand equality forms like {{`#match`
`@custom.typography` "comic sans"}} are validated again.

In `@lib/checker.js`:
- Around line 9-10: The check() function currently mutates the shared spec by
pushing labs-enabled helper names into spec.knownHelpers and uses _.has to test
flags, causing helpers to remain enabled across calls and treating
{labs:{flag:false}} as enabled; fix by computing the list of extra helpers
inside check() per invocation using labsEnabledHelpers and truthy checks (e.g.,
Boolean(spec.labs && spec.labs[flagName])) instead of mutating spec.knownHelpers
or using _.has, then merge that transient list with the existing known helpers
only for the duration of the check call (do not write back to
spec.knownHelpers); add a regression test that calls check() twice in one
process and a test where spec.labs.testFlag is explicitly false to assert the
helper is not enabled.

---

Nitpick comments:
In `@lib/checks/100-custom-template-settings-usage.js`:
- Around line 42-48: The loop calling applyRule over rules omits the options
property, causing inconsistency with other checks (e.g., 110-page-builder-usage)
and potential future breakage; update the applyRule invocation inside _.each so
it passes options (the same options object used elsewhere) alongside code,
check, and ruleImplementations[ruleCode] — e.g., include options: options — so
that applyRule, and any isEnabled implementations like GS100, receive
options.checkVersion and other settings.

In `@lib/utils/check-utils.js`:
- Around line 6-18: In getRules, the RegExp for ruleRegex uses the unnecessary
'g' flag which can cause stateful behavior; change the pattern creation in
getRules (ruleRegex) to omit the 'g' flag (e.g., new RegExp('^' + id + '-.*'))
and/or use RegExp.prototype.test(ruleCode) or String.prototype.match without the
global flag when checking ruleCode.match(ruleRegex) so the match is a simple
boolean and not affected by lastIndex.
- Around line 75-82: In parseWithAST, avoid mutating the caller's rules object:
create a shallow copy (e.g., effectiveRules = {...rules}) and, if
mark-used-partials is missing, add it to effectiveRules instead of rules; then
pass effectiveRules to ASTLinter.verify (the linter.verify call) so callers'
rules remain unchanged and you still ensure the mark-used-partials rule is
present for parsing.

In `@test/check-utils.test.js`:
- Around line 153-196: Add a new test case exercising getRules: call getRules
with a mock rules object containing multiple rule entries (some matching and
some not matching a provided regex pattern) and with different version metadata
(e.g., rule.version or rule.from version ranges), assert that the returned set
only includes rules whose keys match the regex filter and that the resolved rule
versions follow the expected resolution logic (pick the highest/compatible
version or fallback as getRules specifies). Use the existing test harness style
(create a fake theme/results if needed and reuse getLogger patterns) and include
at least two assertions: one for regex filtering and one for correct version
selection.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0c270686-f137-4681-b5a6-b72758dcdb09

📥 Commits

Reviewing files that changed from the base of the PR and between 555e19a and 322aa29.

📒 Files selected for processing (13)
  • lib/ast-linter/linter.js
  • lib/ast-linter/rules/lint-no-unknown-custom-theme-select-value-in-match.js
  • lib/checker.js
  • lib/checks/100-custom-template-settings-usage.js
  • lib/checks/110-page-builder-usage.js
  • lib/checks/120-no-unknown-globals.js
  • lib/utils/check-utils.js
  • lib/utils/labs-enabled-helpers.js
  • test/ast-linter.test.js
  • test/check-utils.test.js
  • test/checker.test.js
  • test/fixtures/ast-linter/nested-async-helpers.hbs
  • test/fixtures/ast-linter/prev-next-outside-post.hbs
💤 Files with no reviewable changes (1)
  • lib/checks/120-no-unknown-globals.js

Comment thread lib/checker.js
@troyciesco troyciesco merged commit 73dfb87 into main Mar 11, 2026
5 checks passed
@troyciesco troyciesco deleted the test-updates-260311 branch March 11, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant