diff --git a/compiler/ast/Vars.js b/compiler/ast/Vars.js index 99349f551..6b0d79ae4 100644 --- a/compiler/ast/Vars.js +++ b/compiler/ast/Vars.js @@ -8,6 +8,7 @@ class Vars extends Node { super('Vars'); this.kind = def.kind || 'var'; this.declarations = def.declarations; + this.body = this.makeContainer(def.body); } generateCode(generator) { diff --git a/test/fixtures/parser/autotest/var-body/expected.json b/test/fixtures/parser/autotest/var-body/expected.json new file mode 100644 index 000000000..3de03766c --- /dev/null +++ b/test/fixtures/parser/autotest/var-body/expected.json @@ -0,0 +1,74 @@ +{ + "type": "TemplateRoot", + "body": [ + { + "type": "Vars", + "kind": "var", + "declarations": [ + { + "name": "x", + "value": { + "type": "Literal", + "value": 1 + } + }, + { + "name": "y", + "value": { + "type": "Literal", + "value": 7 + } + }, + { + "name": "z", + "value": "x+10" + }, + { + "name": "unusedvar" + } + ], + "body": [ + { + "type": "Text", + "argument": { + "type": "Literal", + "value": "\n " + } + }, + { + "type": "Text", + "argument": "x" + }, + { + "type": "Text", + "argument": { + "type": "Literal", + "value": "\n " + } + }, + { + "type": "Text", + "argument": "y" + }, + { + "type": "Text", + "argument": { + "type": "Literal", + "value": "\n " + } + }, + { + "type": "Text", + "argument": "z" + }, + { + "type": "Text", + "argument": { + "type": "Literal", + "value": "\nHELLO WORLD\n" + } + } + ] + } + ] +} \ No newline at end of file diff --git a/test/fixtures/parser/autotest/var-body/template.marko b/test/fixtures/parser/autotest/var-body/template.marko new file mode 100644 index 000000000..10fa5a550 --- /dev/null +++ b/test/fixtures/parser/autotest/var-body/template.marko @@ -0,0 +1,6 @@ + + ${x} + ${y} + ${z} +HELLO WORLD + \ No newline at end of file diff --git a/test/fixtures/pretty-print/autotest/marko-template/expected.json b/test/fixtures/pretty-print/autotest/marko-template/expected.json index ea0de91b6..91807633b 100644 --- a/test/fixtures/pretty-print/autotest/marko-template/expected.json +++ b/test/fixtures/pretty-print/autotest/marko-template/expected.json @@ -24,7 +24,8 @@ "id": "notEmpty", "init": "__helpers.ne" } - ] + ], + "body": [] }, { "type": "Return", diff --git a/test/parser-test.js b/test/parser-test.js new file mode 100644 index 000000000..85d4d58d6 --- /dev/null +++ b/test/parser-test.js @@ -0,0 +1,31 @@ +'use strict'; + +var chai = require('chai'); +chai.config.includeStack = true; +var path = require('path'); +var compiler = require('../compiler'); +var builder = compiler.createBuilder(); +var autotest = require('./autotest'); +var fs = require('fs'); +var CompileContext = require('../compiler/CompileContext'); +var HtmlJsParser = require('../compiler/HtmlJsParser'); +var Parser = require('../compiler/Parser'); +var parser = new Parser(new HtmlJsParser()); + +describe('compiler/parser', function() { + var autoTestDir = path.join(__dirname, 'fixtures/parser/autotest'); + + autotest.scanDir( + autoTestDir, + function run(dir) { + var templatePath = path.join(dir, 'template.marko'); + var src = fs.readFileSync(templatePath, {encoding: 'utf8'}); + var context = new CompileContext(src, templatePath, builder); + var ast = parser.parse(src, context); + return ast; + }, + { + deepEqual: true, + compareExtension: '.json' + }); +}); \ No newline at end of file