lit-nunjucks
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -36,3 +36,3 @@ const nunjucks = require("nunjucks"); | ||
function parse(source) { | ||
function parseNunjucks(source) { | ||
try { | ||
@@ -49,2 +49,6 @@ return nunjucks.parser.parse(source); | ||
function parse(source, opts) { | ||
return new Parser(opts).parse(source); | ||
} | ||
class Parser { | ||
@@ -55,10 +59,14 @@ constructor(opts) { | ||
compile(source) { | ||
const ast = this.parse(source); | ||
return generate(ast).code; | ||
} | ||
parse(source) { | ||
this.parsedPartials = | ||
this.opts && this.opts.partials | ||
? mapValues(this.opts.partials, parse) | ||
? mapValues(this.opts.partials, parseNunjucks) | ||
: {}; | ||
const root = parse(source); | ||
const root = parseNunjucks(source); | ||
try { | ||
return generate(this.transform(root)).code; | ||
return this.transform(root); | ||
} catch (err) { | ||
@@ -288,2 +296,3 @@ throw err; | ||
t.identifier(`_F.${node.name.value}`), | ||
// this.wrap(), | ||
[].concat(this.wrap(node.args, false)) | ||
@@ -344,6 +353,7 @@ ); | ||
if (node instanceof n.For) { | ||
const array = this.wrap(node.arr); | ||
const repeatCallExpression = t.callExpression( | ||
t.identifier("repeat"), | ||
[ | ||
this.wrap(node.arr), | ||
array, | ||
t.arrowFunctionExpression( | ||
@@ -369,12 +379,22 @@ [ | ||
); | ||
return node.else_ | ||
? t.conditionalExpression( | ||
t.memberExpression( | ||
this.wrap(node.arr), | ||
t.identifier("length") | ||
), | ||
repeatCallExpression, | ||
this.wrapTemplate(node.else_) | ||
) | ||
: repeatCallExpression; | ||
const isArray = t.callExpression(t.identifier("Array.isArray"), [ | ||
array, | ||
]); | ||
return t.conditionalExpression( | ||
node.else_ | ||
? t.logicalExpression( | ||
"&&", | ||
isArray, | ||
t.memberExpression( | ||
this.wrap(node.arr), | ||
t.identifier("length") | ||
) | ||
) | ||
: isArray, | ||
repeatCallExpression, | ||
node.else_ ? this.wrapTemplate(node.else_) : t.stringLiteral('') | ||
); | ||
} | ||
@@ -386,2 +406,2 @@ | ||
module.exports = { compile }; | ||
module.exports = { compile, parse, Parser }; |
{ | ||
"name": "lit-nunjucks", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Nunjucks compiler to lit-html", | ||
@@ -5,0 +5,0 @@ "main": "lit-nunjucks.js", |
@@ -77,3 +77,3 @@ const { compile } = require("../lit-nunjucks"); | ||
expect(compile(`{%for item in list %}{{item}}{% endfor%}`)).toEqual( | ||
toCode("{ return repeat(list, (item, index) => item); }", "list") | ||
toCode('{ return Array.isArray(list) ? repeat(list, (item, index) => item) : "" }', "list") | ||
); | ||
@@ -86,3 +86,3 @@ }); | ||
toCode( | ||
"{ return repeat(list, ({name, value}, index) => name); }", | ||
"{ return Array.isArray(list) ? repeat(list, ({name, value}, index) => name) : \"\"; }", | ||
"list" | ||
@@ -97,3 +97,3 @@ ) | ||
toCode( | ||
"{ return repeat(list, (item, index) => html`<div>${item}</div>`); }", | ||
"{ return Array.isArray(list) ? repeat(list, (item, index) => html`<div>${item}</div>`) : \"\"; }", | ||
"list" | ||
@@ -108,3 +108,3 @@ ) | ||
toCode( | ||
'{return list.length ? repeat(list, (item, index) => item) : "nothing"; }', | ||
'{return Array.isArray(list) && list.length ? repeat(list, (item, index) => item) : "nothing"; }', | ||
"list" | ||
@@ -119,3 +119,3 @@ ) | ||
toCode( | ||
'{return repeat(_F.list("foobar"), (i, index) => html`${i},`);}', | ||
'{return Array.isArray(_F.list("foobar")) ? repeat(_F.list("foobar"), (i, index) => html`${i},`):"";}', | ||
"_F" | ||
@@ -122,0 +122,0 @@ ) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51089
1254