Repository navigation
Add a jQuery usage check - #423
adamsilverstein wants to merge 10 commits into
Conversation
Bring the four-year-old branch up to date with trunk before addressing review feedback, so the new check is written against current conventions. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XymJGmRuNKwf8srrtsrcLR
The check was copied from the comment-reply check, where the notice fires when the script is missing. Here the opposite is true: the notice should fire when jQuery is found, so drop the negation. As written the check warned every theme that does not use jQuery and stayed silent for the themes it is meant to flag. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XymJGmRuNKwf8srrtsrcLR
Most themes never enqueue jQuery directly; they list it in the $deps array of their own script, which loads jQuery all the same. Matching only wp_enqueue_script( 'jquery' ) therefore missed the common case. Add a second pattern for the dependency array of wp_enqueue_script() and wp_register_script(), covering both array() and short array syntax, any position in the list, and arbitrary whitespace. Both patterns accept the jQuery handles WordPress registers - jquery, jquery-core, jquery-migrate and the jquery-ui-* family - since each of them either is jQuery or depends on it. Report per file so the notice names the file and shows the matching lines, as the other checks do, and correct the copy: jQuery is spelled with a lowercase j, the wording now covers the dependency case, and the lead uses tc-recommended to match its RECOMMENDED label. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XymJGmRuNKwf8srrtsrcLR
|
A six-case focused check of
The other four cases behaved as expected: no jQuery and Could the check distinguish registration from enqueueing and ignore commented-out calls? |
Good suggestion, I'll give it a try @jamesbregenzer |
wp_register_script() makes a script available without loading it, so a registration that lists jQuery as a dependency does not put jQuery on the page by itself. Reporting it warned themes that register a script and never use it. Collect every wp_enqueue_script() and wp_register_script() call across the theme, follow the dependencies of the enqueued scripts, and report a registration only when its handle is reached that way - enqueued by handle, in the same file or another one, or listed as a dependency of an enqueued script. Build the reported lines from the matched calls instead of grepping the file for "jquery". The grep read the file from disk, so it also listed commented-out calls and unrelated lines that merely mention jQuery next to a real match. Multi-line calls are shown collapsed onto one line. Match the argument list up to the parenthesis that closes the call, so an array later in the same statement or template is no longer read as the dependency list, and skip array access such as $urls['jquery']. Claude-Session: https://claude.ai/code/session_018vwYjVmRVUpGNNkapNPb8Y
jquery-masonry, jquery-color, jquery-form, the jquery-effects-* family and the other jquery-prefixed scripts in core all depend on jQuery, so enqueueing one loads it. Twenty Thirteen and Twenty Fourteen enqueue jquery-masonry by handle, which the check missed. List the handles explicitly so a theme's own jquery-* handle is still not mistaken for a core one. Claude-Session: https://claude.ai/code/session_018vwYjVmRVUpGNNkapNPb8Y
The check is about the front end, yet a script enqueued for the admin, the Customizer, the block editor or the login screen was reported like any other. Twenty Twenty-One loads no jQuery on the front end and still got three notices, all for Customizer scripts. Leave out calls made inside a function, method or closure that is hooked only to admin_enqueue_scripts, customize_controls_enqueue_scripts, customize_preview_init, enqueue_block_editor_assets or login_enqueue_scripts. Methods are matched by name, so a name that is also hooked to a front end action is still reported. Claude-Session: https://claude.ai/code/session_018vwYjVmRVUpGNNkapNPb8Y
One notice per file repeated the whole sentence and the article link for every file that loads jQuery. Emit one notice for the theme and list the calls under each file name instead. Reword it to say what was found and that the theme may not need it, as issue 402 asked for a notice that reads as an invitation, not a warning. Claude-Session: https://claude.ai/code/session_018vwYjVmRVUpGNNkapNPb8Y
|
@jamesbregenzer thanks for the focused testing, I pushed an update for the registration case. I had Claude work through both cases, here is what changed:
Can you run your six cases again with comments stripped first and see if they all pass? |
|
Thanks for the update. Six reconstructed cases were rerun in an isolated PHP fixture against You were right about the commented-out case. The earlier harness passed raw PHP directly to A WordPress 7.1.4 smoke test then ran the unchanged plugin through its nonce-protected admin form on two small classic test themes. The control theme produced no jQuery notice. The positive theme produced one grouped |
|
Thanks @jamesbregenzer - let me know if any additional changes are needed here. |
Addresses #402
Claude Code wrote up where the check stands after the review rounds:
AI Use
The code after the first two commits and this description came out of a few rounds with 🤖 Claude Code. I will review and test.