ast.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { parse, parseWithoutProcessing } from '../dist/esm';
  2. import { equals } from './utils';
  3. describe('ast', function() {
  4. describe('whitespace control', function() {
  5. describe('parse', function() {
  6. it('mustache', function() {
  7. let ast = parse(' {{~comment~}} ');
  8. equals(ast.body[0].value, '');
  9. equals(ast.body[2].value, '');
  10. });
  11. it('block statements', function() {
  12. let ast = parse(' {{# comment~}} \nfoo\n {{~/comment}}');
  13. equals(ast.body[0].value, '');
  14. equals(ast.body[1].program.body[0].value, 'foo');
  15. });
  16. });
  17. describe('parseWithoutProcessing', function() {
  18. it('mustache', function() {
  19. let ast = parseWithoutProcessing(' {{~comment~}} ');
  20. equals(ast.body[0].value, ' ');
  21. equals(ast.body[2].value, ' ');
  22. });
  23. it('block statements', function() {
  24. let ast = parseWithoutProcessing(
  25. ' {{# comment~}} \nfoo\n {{~/comment}}'
  26. );
  27. equals(ast.body[0].value, ' ');
  28. equals(ast.body[1].program.body[0].value, ' \nfoo\n ');
  29. });
  30. });
  31. });
  32. describe('standalone flags', function() {
  33. describe('mustache', function() {
  34. it('does not mark mustaches as standalone', function() {
  35. let ast = parse(' {{comment}} ');
  36. equals(!!ast.body[0].value, true);
  37. equals(!!ast.body[2].value, true);
  38. });
  39. });
  40. describe('blocks - parseWithoutProcessing', function() {
  41. it('block mustaches', function() {
  42. let ast = parseWithoutProcessing(
  43. ' {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '
  44. ),
  45. block = ast.body[1];
  46. equals(ast.body[0].value, ' ');
  47. equals(block.program.body[0].value, ' \nfoo\n ');
  48. equals(block.inverse.body[0].value, ' \n bar \n ');
  49. equals(ast.body[2].value, ' ');
  50. });
  51. it('initial block mustaches', function() {
  52. let ast = parseWithoutProcessing(
  53. '{{# comment}} \nfoo\n {{/comment}}'
  54. ),
  55. block = ast.body[0];
  56. equals(block.program.body[0].value, ' \nfoo\n ');
  57. });
  58. it('mustaches with children', function() {
  59. let ast = parseWithoutProcessing(
  60. '{{# comment}} \n{{foo}}\n {{/comment}}'
  61. ),
  62. block = ast.body[0];
  63. equals(block.program.body[0].value, ' \n');
  64. equals(block.program.body[1].path.original, 'foo');
  65. equals(block.program.body[2].value, '\n ');
  66. });
  67. it('nested block mustaches', function() {
  68. let ast = parseWithoutProcessing(
  69. '{{#foo}} \n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} \n{{/foo}}'
  70. ),
  71. body = ast.body[0].program.body,
  72. block = body[1];
  73. equals(body[0].value, ' \n');
  74. equals(block.program.body[0].value, ' \nfoo\n ');
  75. equals(block.inverse.body[0].value, ' \n bar \n ');
  76. });
  77. it('column 0 block mustaches', function() {
  78. let ast = parseWithoutProcessing(
  79. 'test\n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '
  80. ),
  81. block = ast.body[1];
  82. equals(ast.body[0].omit, undefined);
  83. equals(block.program.body[0].value, ' \nfoo\n ');
  84. equals(block.inverse.body[0].value, ' \n bar \n ');
  85. equals(ast.body[2].value, ' ');
  86. });
  87. });
  88. describe('blocks', function() {
  89. it('marks block mustaches as standalone', function() {
  90. let ast = parse(
  91. ' {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '
  92. ),
  93. block = ast.body[1];
  94. equals(ast.body[0].value, '');
  95. equals(block.program.body[0].value, 'foo\n');
  96. equals(block.inverse.body[0].value, ' bar \n');
  97. equals(ast.body[2].value, '');
  98. });
  99. it('marks initial block mustaches as standalone', function() {
  100. let ast = parse('{{# comment}} \nfoo\n {{/comment}}'),
  101. block = ast.body[0];
  102. equals(block.program.body[0].value, 'foo\n');
  103. });
  104. it('marks mustaches with children as standalone', function() {
  105. let ast = parse('{{# comment}} \n{{foo}}\n {{/comment}}'),
  106. block = ast.body[0];
  107. equals(block.program.body[0].value, '');
  108. equals(block.program.body[1].path.original, 'foo');
  109. equals(block.program.body[2].value, '\n');
  110. });
  111. it('marks nested block mustaches as standalone', function() {
  112. let ast = parse(
  113. '{{#foo}} \n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} \n{{/foo}}'
  114. ),
  115. body = ast.body[0].program.body,
  116. block = body[1];
  117. equals(body[0].value, '');
  118. equals(block.program.body[0].value, 'foo\n');
  119. equals(block.inverse.body[0].value, ' bar \n');
  120. equals(body[0].value, '');
  121. });
  122. it('does not mark nested block mustaches as standalone', function() {
  123. let ast = parse(
  124. '{{#foo}} {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} {{/foo}}'
  125. ),
  126. body = ast.body[0].program.body,
  127. block = body[1];
  128. equals(body[0].omit, undefined);
  129. equals(block.program.body[0].value, ' \nfoo\n');
  130. equals(block.inverse.body[0].value, ' bar \n ');
  131. equals(body[0].omit, undefined);
  132. });
  133. it('does not mark nested initial block mustaches as standalone', function() {
  134. let ast = parse(
  135. '{{#foo}}{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}}{{/foo}}'
  136. ),
  137. body = ast.body[0].program.body,
  138. block = body[0];
  139. equals(block.program.body[0].value, ' \nfoo\n');
  140. equals(block.inverse.body[0].value, ' bar \n ');
  141. equals(body[0].omit, undefined);
  142. });
  143. it('marks column 0 block mustaches as standalone', function() {
  144. let ast = parse(
  145. 'test\n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '
  146. ),
  147. block = ast.body[1];
  148. equals(ast.body[0].omit, undefined);
  149. equals(block.program.body[0].value, 'foo\n');
  150. equals(block.inverse.body[0].value, ' bar \n');
  151. equals(ast.body[2].value, '');
  152. });
  153. });
  154. describe('partials - parseWithoutProcessing', function() {
  155. it('simple partial', function() {
  156. let ast = parseWithoutProcessing('{{> partial }} ');
  157. equals(ast.body[1].value, ' ');
  158. });
  159. it('indented partial', function() {
  160. let ast = parseWithoutProcessing(' {{> partial }} ');
  161. equals(ast.body[0].value, ' ');
  162. equals(ast.body[1].indent, '');
  163. equals(ast.body[2].value, ' ');
  164. });
  165. });
  166. describe('partials', function() {
  167. it('marks partial as standalone', function() {
  168. let ast = parse('{{> partial }} ');
  169. equals(ast.body[1].value, '');
  170. });
  171. it('marks indented partial as standalone', function() {
  172. let ast = parse(' {{> partial }} ');
  173. equals(ast.body[0].value, '');
  174. equals(ast.body[1].indent, ' ');
  175. equals(ast.body[2].value, '');
  176. });
  177. it('marks those around content as not standalone', function() {
  178. let ast = parse('a{{> partial }}');
  179. equals(ast.body[0].omit, undefined);
  180. ast = parse('{{> partial }}a');
  181. equals(ast.body[1].omit, undefined);
  182. });
  183. });
  184. describe('comments - parseWithoutProcessing', function() {
  185. it('simple comment', function() {
  186. let ast = parseWithoutProcessing('{{! comment }} ');
  187. equals(ast.body[1].value, ' ');
  188. });
  189. it('indented comment', function() {
  190. let ast = parseWithoutProcessing(' {{! comment }} ');
  191. equals(ast.body[0].value, ' ');
  192. equals(ast.body[2].value, ' ');
  193. });
  194. });
  195. describe('comments', function() {
  196. it('marks comment as standalone', function() {
  197. let ast = parse('{{! comment }} ');
  198. equals(ast.body[1].value, '');
  199. });
  200. it('marks indented comment as standalone', function() {
  201. let ast = parse(' {{! comment }} ');
  202. equals(ast.body[0].value, '');
  203. equals(ast.body[2].value, '');
  204. });
  205. it('marks those around content as not standalone', function() {
  206. let ast = parse('a{{! comment }}');
  207. equals(ast.body[0].omit, undefined);
  208. ast = parse('{{! comment }}a');
  209. equals(ast.body[1].omit, undefined);
  210. });
  211. });
  212. });
  213. });