Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- `codegraph sync` and the file watcher now link a Shopify section or snippet to the files that name it when it is added after them, or comes back after being deleted, whether they name it in a `{% render %}`, `{% include %}` or `{% section %}` tag or as a section `type` in a JSON template. Before, the link appeared only once a file naming it changed or the project was re-indexed, even in a repository with a single theme, so the new section or snippet looked unused and `codegraph affected` missed the files that use it. Re-index Shopify themes after upgrading to pick up links an earlier version missed.
- A Shopify JSON template or section group saved by the theme editor now links the sections it lists. The editor writes a comment at the top of every file it saves, and a file with that comment, or with a trailing comma, both of which Shopify accepts, linked none of its sections, so in an editor-saved theme most sections looked unused. A layout's `{% sections 'header-group' %}` tag now links to the section group it renders, each section a JSON file lists is linked from the line that names it rather than from the top of the file, and a `{% schema %}` written inside `{% comment %}` or `{% raw %}` is no longer indexed as the section's own. Re-index Shopify themes after upgrading. Thanks @Puma7 for the report and the pull request. (#2494)
- In Liquid, a variable declared with `{% capture %}`, like `{% capture title %}…{% endcapture %}`, a quoted `{% capture 'title' %}` or a `capture title` line inside `{% liquid %}`, is now indexed like one declared with `{% assign %}`, so search and `codegraph_explore` can find it. Before, only assigned variables were indexed, so a captured one could not be found, and `codegraph_explore` said its name was not in the index. Re-index Liquid projects after upgrading. Thanks @Puma7 for the report and the pull request. (#2498)
- In Liquid, a variable whose name has a hyphen in it, like `{% assign form-field-label = … %}` or `{% capture icon-cart %}`, is now indexed under its whole name, so search and `codegraph_explore` find it. Before, such a name was skipped, as Liquid allows hyphens in names but only letters, digits and underscores were read. A name that goes on with a dot still gets no node rather than a cut-short one, and in `{% capture title-%}` the hyphen still belongs to the tag's whitespace control. Re-index Liquid projects after upgrading. Thanks @Puma7. (#2498)
- `codegraph sync` and the file watcher now link a route to the page or layout it loads lazily when that file is added after the router was indexed, comes back after being deleted, or gets the component the route renders in a later edit, whether or not the import names the file's extension: React Router's `lazy: () => import('./pages/Team')`, Vue Router's `component: () => import('@/views/Login')` and Angular's `loadComponent: () => import('./home/home.component')`. Before, the route stayed unlinked until the router file changed or the project was re-indexed, so the page looked unused and `codegraph affected` missed the tests that reach it through the router. Re-index after upgrading to pick up links an earlier version missed.
- A file named the way Google names its tests, like `wire_format_unittest.cc` in protocolbuffers/protobuf, `logging_unittest.cc` in google/glog or a Python `foo_unittest.py`, now counts as a test, as `wire_format_test.cc` already did. Before, it was taken for production code: search and `codegraph_explore` ranked it alongside the code it tests, `codegraph affected` never listed it, and calls in production code could link to a class or type a unittest declares for itself, as google/breakpad's `stack_frame_entries_.size()` did to a unittest's `StackHelper`. Re-index projects that have such files after upgrading.
- A file in a `unittests/` folder, where llvm/llvm-project and google/breakpad keep their unit tests, or in a folder named like a unittest, such as `dcheck_unittest/` in google/glog, now counts as a test whatever its own name says, like LLVM's `CountCopyAndMove.h` helper. Before, such a file was taken for production code unless its name said test, and none of them were kept apart from the program the way a `tests/` folder is: search and `codegraph_explore` ranked them alongside production code, `codegraph affected` missed them, and production code linked to a unit test's own declarations, as LLVM's `Ctx` and `OpInfo` variables did to ones its unit tests declare. A folder named just `unittest/`, like CPython's `Lib/unittest/`, is still production code. Re-index projects that have such folders after upgrading.
Expand Down
31 changes: 27 additions & 4 deletions __tests__/extraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5337,6 +5337,12 @@ import 'package:flutter/material.dart';
['{% liquid\n capture bare\n echo "Hi"\n endcapture\n%}', 'bare', 2, 2],
['{% liquid\r\n capture crlf # a note\r\n endcapture\r\n%}', 'crlf', 2, 2],
["{%- liquid\n capture 'quoted_line'\n endcapture\n-%}", 'quoted_line', 2, 2],
// A name with `-` in it is one name; the `-` of `-%}` trims whitespace.
['{% capture title-text %}Hi{% endcapture %}', 'title-text', 1, 0],
['{% capture title-text-%}Hi{% endcapture %}', 'title-text', 1, 0],
['{%- capture title-text -%}Hi{%- endcapture -%}', 'title-text', 1, 0],
["{% capture 'hero-alt' %}Hi{% endcapture %}", 'hero-alt', 1, 0],
['{% liquid\n capture hero-alt\n endcapture\n%}', 'hero-alt', 2, 2],
] as const)('reads a capture declaration from %j', (code, name, line, column) => {
const result = extractFromSource('sections/card.liquid', code);
const variables = result.nodes.filter((n) => n.kind === 'variable');
Expand All @@ -5360,10 +5366,27 @@ import 'package:flutter/material.dart';
expect(result.nodes.filter((n) => n.kind === 'variable').map((n) => n.name)).toEqual(['text']);
});

it('never cuts a capture name short', () => {
// Liquid allows `-` and `.` in a name, which the assign rule doesn't read
// either; such a name gets no node rather than a wrong one.
const code = ['{% capture ghost-name %}x{% endcapture %}', '{% capture ghost.name %}x{% endcapture %}'].join('\n');
it('reads an assign name with `-` in it whole', () => {
const code = [
"{% assign title-text = 'Sale' %}",
'{%- assign page-title=page.title -%}',
'{% liquid',
' assign hero-image = section.settings.image',
'%}',
].join('\n');
const variables = extractFromSource('sections/card.liquid', code).nodes.filter((n) => n.kind === 'variable');
expect(variables.map((n) => [n.name, n.startLine])).toEqual([['title-text', 1], ['page-title', 2], ['hero-image', 4]]);
});

it('never cuts a name short', () => {
// Liquid allows `.` in a name too, which neither rule reads; such a name
// gets no node rather than a wrong one.
const code = [
'{% capture ghost.name %}x{% endcapture %}',
'{% capture ghost-name.x %}x{% endcapture %}',
'{% assign ghost.name = 1 %}',
'{% assign ghost-name.x = 1 %}',
].join('\n');
expect(extractFromSource('sections/card.liquid', code).nodes.filter((n) => n.kind === 'variable')).toEqual([]);
});

Expand Down
16 changes: 10 additions & 6 deletions src/extraction/liquid-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,16 +464,20 @@ export class LiquidExtractor {
* declarations as variables
*/
private extractAssignments(fileNodeId: string): void {
// A name is word characters and `-`, as Liquid reads it: `title-text` is one
// name. One that goes on with `.` gets no node rather than a cut-short one.
const name = `\\w[\\w-]*`;
// Both spellings. Most of a modern theme's assigns are the bare kind.
for (const match of this.findTagOccurrences('assign', `(\\w+)\\s*=`)) {
for (const match of this.findTagOccurrences('assign', `(${name})\\s*=`)) {
this.addVariable(fileNodeId, match.groups[1]!, match);
}
// A capture declares its name as an assign does, and Liquid reads a quoted
// name (`{% capture 'title' %}`) as the name inside the quotes. A name that
// goes on with `-` or `.`, which the assign rule doesn't read either, gets
// no node rather than a cut-short one. Captures come after assigns, so an
// assign keeps its id when a capture of the same name shares its line.
for (const match of this.findTagOccurrences('capture', `(?:'(\\w+)'|"(\\w+)"|(\\w+))(?=\\s|-?%\\}|$)`)) {
// name (`{% capture 'title' %}`) as the name inside the quotes. A bare name
// ends at the first whitespace or at the tag's end, `-%}` included: in
// `{% capture title-%}` the `-` trims whitespace, so the name is `title`.
// Captures come after assigns, so an assign keeps its id when a capture of
// the same name shares its line.
for (const match of this.findTagOccurrences('capture', `(?:'(${name})'|"(${name})"|(${name}?))(?=\\s|-?%\\}|$)`)) {
const [, singleQuoted, doubleQuoted, bare] = match.groups;
this.addVariable(fileNodeId, (singleQuoted ?? doubleQuoted ?? bare)!, match);
}
Expand Down