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

feat: adding custom blocks test #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
77 changes: 69 additions & 8 deletions src/lib/HtlTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,10 @@ export class HtlTranspiler implements ITranspiler {
// else section
if (statement.inverse) {
let indent = '';
const ifContentStatement =
(
<any>statement.program.body
const ifContentStatement = (<any>statement.program.body
.concat()
.reverse()
.find(s => s.type === 'ContentStatement') || {}
).original || '';
.find(s => s.type === 'ContentStatement') || {}).original || '';

const match = /\n([\t ]*)$/gi.exec(ifContentStatement);
if (match && match[1]) {
Expand Down Expand Up @@ -161,6 +158,70 @@ export class HtlTranspiler implements ITranspiler {
break;
}

case 'unless': {
const firstCondition = statement.params[0];
this.buffer.push(
`<sly data-sly-test="\${ ${
(<hbs.AST.PathExpression>firstCondition).original
} == 'false' }">`,
);
const t = new HtlTranspiler(null, this.context, this.depth);

this.buffer.push(t.parseProgram(statement.program).toString());

this.buffer.push(`</sly>`);

break;
}

case 'is': {
const firstCondition = statement.params[0];
const secondCondition = statement.params[1];
this.buffer.push(
`<sly data-sly-test="\${ ${(<hbs.AST.PathExpression>firstCondition).original} == ${
(<hbs.AST.PathExpression>secondCondition).original
} }">`,
);
const t = new HtlTranspiler(null, this.context, this.depth);

this.buffer.push(t.parseProgram(statement.program).toString());

this.buffer.push(`</sly>`);

break;
}

case 'with': {
const condition = statement.params[0];
this.buffer.push(
`<sly data-sly-test="\${ ${(<hbs.AST.PathExpression>condition).original} }">`,
);
const t = new HtlTranspiler(null, this.context, this.depth);

this.buffer.push(t.parseProgram(statement.program).toString());

this.buffer.push('</sly>');

break;
}

case 'gt': {
const firstCondition = statement.params[0];
const secondCondition = statement.params[1];
this.buffer.push(
`<sly data-sly-test="\${ ${
(<hbs.AST.PathExpression>firstCondition).original
} &gt; ${(<hbs.AST.PathExpression>secondCondition).original} }">`,
);
const t = new HtlTranspiler(null, this.context, this.depth);

this.buffer.push(t.parseProgram(statement.program).toString());

this.buffer.push('</sly>');

break;
}

case 'each': {
const condition = this.context.getScopedVariable(statement
.params[0] as hbs.AST.PathExpression);
Expand Down Expand Up @@ -274,9 +335,9 @@ export class HtlTranspiler implements ITranspiler {
.map((pair: hbs.AST.HashPair) => {
const key = `${pair.key}=`;
if (pair.value.type === 'PathExpression') {
return `${key}${this.context.getScopedVariable(
<hbs.AST.PathExpression>pair.value,
)}`;
return `${key}${this.context.getScopedVariable(<hbs.AST.PathExpression>(
pair.value
))}`;
}
if (pair.value.type === 'StringLiteral') {
return `${key}'${(<hbs.AST.StringLiteral>pair.value).value}'`;
Expand Down
4 changes: 4 additions & 0 deletions test/HtlTranspiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('convert from hbs to htl', () => {
const tests = [
'comment/comment',
'condition/condition',
'condition/condition-unless',
'condition/condition-with',
'condition/condition-greater-than',
'condition/condition-is',
'variable/variable',
'variable/upper-context-variables',
'raw/raw',
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/condition/condition-greater-than.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#gt noListItems 1}}<h2>Greater</h2>{{/gt}}
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a dedicated custom helper for this?
In Muban we have https://github.com/mediamonks/muban/blob/master/build-tools/handlebars-helpers/condition.js, where you can use all condition types. Does > work in htl in a -test check?

1 change: 1 addition & 0 deletions test/fixtures/condition/condition-greater-than.htl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<sly data-sly-test="${ noListItems &gt; 1 }"><h2>Greater</h2></sly>
1 change: 1 addition & 0 deletions test/fixtures/condition/condition-is.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#is noListItems 1}}Foo{{/is}}
Copy link
Member

Choose a reason for hiding this comment

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

1 change: 1 addition & 0 deletions test/fixtures/condition/condition-is.htl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<sly data-sly-test="${ noListItems == 1 }">Foo</sly>
1 change: 1 addition & 0 deletions test/fixtures/condition/condition-unless.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#unless foo }}Foo{{/unless}}
1 change: 1 addition & 0 deletions test/fixtures/condition/condition-unless.htl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<sly data-sly-test="${ foo == 'false' }">Foo</sly>
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be false as a boolean, currently you're comparing it to a string?

1 change: 1 addition & 0 deletions test/fixtures/condition/condition-with.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#with author}}<h2>By {{firstName}} {{lastName}}</h2>{{/with}}
1 change: 1 addition & 0 deletions test/fixtures/condition/condition-with.htl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<sly data-sly-test="${ author }"><h2>By ${ firstName } ${ lastName }</h2></sly>
Copy link
Member

Choose a reason for hiding this comment

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

It this correct? In hbs the name fields are nested within the with block, so without changing scope in htl, shouldn't it be author.firstName ?