Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { describe, it, expect } from 'vitest';
import { extractUndefinedVariables } from './extract-undefined-variables';

describe('extractUndefinedVariables', () => {
it('should return variables used but not defined', () => {
const source = `{% liquid
assign b = a
%}
{{ b }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal(['a']);
});

it('should not include assigned variables', () => {
const source = `{% assign x = 1 %}{{ x }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should not include captured variables', () => {
const source = `{% capture x %}hello{% endcapture %}{{ x }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should not include for loop variables', () => {
const source = `{% for item in items %}{{ item }}{% endfor %}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal(['items']);
});

it('should not include forloop variable', () => {
const source = `{% for item in items %}{{ forloop.index }}{% endfor %}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal(['items']);
});

it('should handle function result variables', () => {
const source = `{% function res = 'my_partial' %}{{ res }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should handle graphql result variables', () => {
const source = `{% graphql res = 'my_query' %}{{ res }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should handle inline graphql result variables', () => {
const source = `{% graphql res %}{ users { id } }{% endgraphql %}{{ res }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should handle parse_json result variables', () => {
const source = `{% parse_json data %}{"a":1}{% endparse_json %}{{ data }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should not include global objects', () => {
const source = `{{ context.session }}`;
const result = extractUndefinedVariables(source, [
'context',
'null',
'true',
'false',
'blank',
'empty',
]);
expect(result).to.deep.equal([]);
});

it('should deduplicate results', () => {
const source = `{{ a }}{{ a }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal(['a']);
});

it('should return empty array if source fails to parse', () => {
const source = `{% invalid unclosed`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should handle increment/decrement as definitions', () => {
const source = `{% increment counter %}{{ counter }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should handle background file-based result variables', () => {
const source = `{% background my_job = 'some_partial' %}{{ my_job }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal([]);
});

it('should handle inline background tag without job_id', () => {
const source = `{% background source_name: 'my_task' %}echo "hello"{% endbackground %}{{ my_job }}`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal(['my_job']);
});

it('should not include doc param names', () => {
const source = `
{% doc %}
@param {String} name - a name
{% enddoc %}
{{ name }}
`;
const result = extractUndefinedVariables(source);
expect(result).to.deep.equal(['name']);
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we can also add a test for inline graphql aka {% graphql res %}...{% endgraphql %}, for background tag - both as a function and inline

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

});
Loading
Loading