Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce generic twig tags #154

Merged
merged 6 commits into from
Feb 27, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

- `[melody-parser]`: Introduce option `allowUnknownTags`, along with the `GenericTwigTag` node type

### Bug fixes

## 1.6.0
Expand Down
125 changes: 125 additions & 0 deletions packages/melody-compiler/__tests__/ParserSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,131 @@ describe('Parser', function() {
}).toThrowErrorMatchingSnapshot();
});

it('should handle unknown tags when requested', function() {
const node = parse('{% exit 404 %}', {
allowUnknownTags: true,
});

const tagNode = node.expressions[0];
expect(tagNode.type).toBe('GenericTwigTag');
expect(tagNode.parts.length).toBe(1);
expect(tagNode.parts[0].type).toBe('NumericLiteral');
expect(tagNode.parts[0].value).toBe(404);
});

it('should record the correct boundaries for unknown tags', function() {
const source = '{% exit 404 %}';
const node = parse(source, {
allowUnknownTags: true,
});

const tagNode = node.expressions[0];
const reproducedSource = source.substr(
tagNode.loc.start.index,
tagNode.loc.end.index
);
expect(reproducedSource).toEqual(source);
});

it('should parse expressions in unknown tags', function() {
const node = parse(
'{% exit a + b %}',
{
allowUnknownTags: true,
},
coreExtensions
);

const tagNode = node.expressions[0];
expect(tagNode).toMatchSnapshot();
});

it('should parse an unknown "header" tag', function() {
const node = parse(
'{% header "Cache-Control: max-age=" ~ (expiry.timestamp - now.timestamp) %}',
{
allowUnknownTags: true,
},
coreExtensions
);

const tagNode = node.expressions[0];
expect(tagNode).toMatchSnapshot();
});

it('should parse an unknown "paginate" tag', function() {
const node = parse(
"{% paginate entries.section('blog').limit(10) as pageInfo, pageEntries %}",
{
allowUnknownTags: true,
},
coreExtensions
);

const tagNode = node.expressions[0];
expect(tagNode).toMatchSnapshot();
});

it('should parse an unknown "nav" tag', function() {
const node = parse(
`{%- nav items as item %}
<li>{{ item.name }}</li>
{% endnav -%}`,
{
allowUnknownTags: true,
multiTags: { nav: ['endnav'] },
},
coreExtensions
);

const tagNode = node.expressions[0];
expect(tagNode.trimLeft).toBe(true);
expect(tagNode.trimRight).toBe(false);
expect(tagNode.sections.length).toBe(2);
expect(tagNode.sections[0].type).toBe('SequenceExpression');

const endnavTag = tagNode.sections[1];
expect(endnavTag.tagName).toBe('endnav');
expect(endnavTag.type).toBe('GenericTwigTag');
expect(endnavTag.trimLeft).toBe(false);
expect(endnavTag.trimRight).toBe(true);
});

it('should parse an unknown tag with multiple sub-tags', function() {
const node = parse(
`{% myIf someCondition %}
Output A
{%- myElseIf conditionB %}
Output B
{% myElseIf conditionC %}
Output C
{% myElse %}
Default
{% myEndif %}`,
{
allowUnknownTags: true,
multiTags: { myIf: ['myElseIf', 'myElse', 'myEndif'] },
},
coreExtensions
);

const tagNode = node.expressions[0];
expect(tagNode.tagName).toBe('myIf');
const sections = tagNode.sections;
expect(sections[0].type).toBe('SequenceExpression');
expect(sections[1].type).toBe('GenericTwigTag');
expect(sections[1].tagName).toBe('myElseIf');
expect(sections[2].type).toBe('SequenceExpression');
expect(sections[3].type).toBe('GenericTwigTag');
expect(sections[3].tagName).toBe('myElseIf');
expect(sections[4].type).toBe('SequenceExpression');
expect(sections[5].type).toBe('GenericTwigTag');
expect(sections[5].tagName).toBe('myElse');
expect(sections[6].type).toBe('SequenceExpression');
expect(sections[7].type).toBe('GenericTwigTag');
expect(sections[7].tagName).toBe('myEndif');
});

it('should preserve whitespace control information', function() {
const node = parse(
'{%- set count = 0 -%}',
Expand Down
155 changes: 155 additions & 0 deletions packages/melody-compiler/__tests__/__snapshots__/ParserSpec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,161 @@ Object {
}
`;

exports[`Parser when parsing tags should parse an unknown "header" tag 1`] = `
Object {
"parts": Array [
Object {
"left": Object {
"type": "StringLiteral",
"value": "Cache-Control: max-age=",
},
"operator": "~",
"right": Object {
"left": Object {
"computed": false,
"object": Object {
"name": "expiry",
"type": "Identifier",
},
"property": Object {
"name": "timestamp",
"type": "Identifier",
},
"type": "MemberExpression",
},
"operator": "-",
"right": Object {
"computed": false,
"object": Object {
"name": "now",
"type": "Identifier",
},
"property": Object {
"name": "timestamp",
"type": "Identifier",
},
"type": "MemberExpression",
},
"type": "BinarySubExpression",
},
"type": "BinaryConcatExpression",
"wasImplicitConcatenation": false,
},
],
"sections": Array [],
"tagName": "header",
"tagNameLoc": Object {
"column": 3,
"index": 3,
"line": 1,
},
"trimLeft": false,
"trimRight": false,
"type": "GenericTwigTag",
}
`;

exports[`Parser when parsing tags should parse an unknown "paginate" tag 1`] = `
Object {
"parts": Array [
Object {
"arguments": Array [
Object {
"type": "NumericLiteral",
"value": 10,
},
],
"callee": Object {
"computed": false,
"object": Object {
"arguments": Array [
Object {
"type": "StringLiteral",
"value": "blog",
},
],
"callee": Object {
"computed": false,
"object": Object {
"name": "entries",
"type": "Identifier",
},
"property": Object {
"name": "section",
"type": "Identifier",
},
"type": "MemberExpression",
},
"type": "CallExpression",
},
"property": Object {
"name": "limit",
"type": "Identifier",
},
"type": "MemberExpression",
},
"type": "CallExpression",
},
Object {
"name": "as",
"type": "Identifier",
},
Object {
"name": "pageInfo",
"type": "Identifier",
},
Object {
"tokenText": ",",
"tokenType": ",",
"type": "GenericToken",
},
Object {
"name": "pageEntries",
"type": "Identifier",
},
],
"sections": Array [],
"tagName": "paginate",
"tagNameLoc": Object {
"column": 3,
"index": 3,
"line": 1,
},
"trimLeft": false,
"trimRight": false,
"type": "GenericTwigTag",
}
`;

exports[`Parser when parsing tags should parse expressions in unknown tags 1`] = `
Object {
"parts": Array [
Object {
"left": Object {
"name": "a",
"type": "Identifier",
},
"operator": "+",
"right": Object {
"name": "b",
"type": "Identifier",
},
"type": "BinaryAddExpression",
},
],
"sections": Array [],
"tagName": "exit",
"tagNameLoc": Object {
"column": 3,
"index": 3,
"line": 1,
},
"trimLeft": false,
"trimRight": false,
"type": "GenericTwigTag",
}
`;

exports[`Parser when parsing tags should preserve whitespace control information 1`] = `
Object {
"expressions": Array [
Expand Down
Loading