pure-engine
Advanced tools
Comparing version 0.9.17 to 0.9.18
{ | ||
"name": "pure-engine", | ||
"version": "0.9.17", | ||
"version": "0.9.18", | ||
"description": "Compile HTML templates into JS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -135,3 +135,3 @@ # pure-engine | ||
``` | ||
<if foo is present>{bar}</if> | ||
<if foo.length equals 0>{bar}</if> | ||
``` | ||
@@ -142,3 +142,3 @@ | ||
var __t = ""; | ||
if (__o.foo !== void 0) { | ||
if (__o.foo.length === 0) { | ||
__t += __e(__o.bar); | ||
@@ -145,0 +145,0 @@ } |
@@ -161,11 +161,4 @@ const AbstractSyntaxTree = require('abstract-syntax-tree') | ||
const localVariables = fragment.attributes | ||
let content = component.content | ||
localVariables.forEach(variable => { | ||
if (variable.value === null) { variable.value = '{true}' } | ||
if (variable.value === '') { variable.value = '{""}' } | ||
if (!isCurlyTag(variable.value)) { | ||
content = content.replace(new RegExp(`{${variable.key}}`, 'g'), variable.value) | ||
} | ||
}) | ||
const htmlTree = parse(content) | ||
const htmlTree = parse(component.content) | ||
let children = fragment.children | ||
@@ -183,2 +176,22 @@ | ||
}) | ||
if (leaf.type === 'text') { | ||
localVariables.forEach(variable => { | ||
if (variable.value === null) { variable.value = '{true}' } | ||
if (!isCurlyTag(variable.value)) { | ||
leaf.content = leaf.content.replace(new RegExp(`{${variable.key}}`, 'g'), variable.value) | ||
} | ||
}) | ||
} | ||
if (leaf.tagName === 'if') { | ||
const normalizedAttributes = normalize(leaf.attributes) | ||
leaf.attributes = normalizedAttributes.map(attr => { | ||
// TODO handle or remove words to numbers functionality | ||
if (attr.type === 'Identifier' && !isCurlyTag(attr.key)) { | ||
attr.key = `{${attr.key}}` | ||
} | ||
return attr | ||
}) | ||
} | ||
}) | ||
@@ -201,35 +214,43 @@ | ||
leaf.attributes.forEach(attr => { | ||
const { value } = attr | ||
if ( | ||
value && | ||
value.startsWith('{') && | ||
value.endsWith('}') && | ||
// TODO reuse | ||
// add occurances method to pure-utilities | ||
(value.match(/{/g) || []).length === 1 && | ||
(value.match(/}/g) || []).length === 1 | ||
) { | ||
let source = value.substr(1, value.length - 2) | ||
source = addPlaceholders(source) | ||
const ast = new AbstractSyntaxTree(source) | ||
let replaced = false | ||
ast.replace({ | ||
enter: node => { | ||
// TODO investigate | ||
// this is too optimistic | ||
// should avoid member expressions etc. | ||
if (node.type === 'Identifier') { | ||
const variable = localVariables.find(variable => variable.key === node.name || variable.key === placeholderName(node.name)) | ||
if (variable) { | ||
replaced = true | ||
return { type: 'Literal', value: variable.value } | ||
const { key, value } = attr | ||
function inlineExpression (type, attr, string) { | ||
if ( | ||
string && | ||
string.startsWith('{') && | ||
string.endsWith('}') && | ||
// TODO reuse | ||
// add occurances method to pure-utilities | ||
(string.match(/{/g) || []).length === 1 && | ||
(string.match(/}/g) || []).length === 1 | ||
) { | ||
let source = string.substr(1, string.length - 2) | ||
source = addPlaceholders(source) | ||
const ast = new AbstractSyntaxTree(source) | ||
let replaced = false | ||
ast.replace({ | ||
enter: node => { | ||
// TODO investigate | ||
// this is too optimistic | ||
// should avoid member expressions etc. | ||
if (node.type === 'Identifier') { | ||
const variable = localVariables.find(variable => variable.key === node.name || variable.key === placeholderName(node.name)) | ||
if (variable) { | ||
replaced = true | ||
if (isCurlyTag(variable.value)) { | ||
return convertToExpression(variable.value) | ||
} | ||
return { type: 'Literal', value: variable.value } | ||
} | ||
} | ||
return node | ||
} | ||
return node | ||
}) | ||
if (replaced) { | ||
attr[type] = '{' + ast.source.replace(/;\n$/, '') + '}' | ||
} | ||
}) | ||
if (replaced) { | ||
attr.value = '{' + ast.source.replace(/;$/, '') + '}' | ||
} | ||
} | ||
inlineExpression('key', attr, key) | ||
inlineExpression('value', attr, value) | ||
}) | ||
@@ -445,5 +466,7 @@ } | ||
enter: node => { | ||
const variable = localVariables.find(variable => variable.key === node.name) | ||
if (node.type === 'Identifier' && variable) { | ||
return convertText(variable.value, variables, filters, translations, languages, translationsPaths)[0] | ||
if (node.type === 'Identifier') { | ||
const variable = localVariables.find(variable => variable.key === node.name) | ||
if (variable) { | ||
return convertText(variable.value, variables, filters, translations, languages, translationsPaths)[0] | ||
} | ||
} | ||
@@ -450,0 +473,0 @@ } |
@@ -130,2 +130,5 @@ const AbstractSyntaxTree = require('abstract-syntax-tree') | ||
optimizer.optimize() | ||
if (process.env.DEBUG) { | ||
console.log(program.source) | ||
} | ||
const compiled = new Function(`return function render(${params}) {\n${program.source}}`)() // eslint-disable-line | ||
@@ -132,0 +135,0 @@ return { template: compiled, statistics, errors } |
@@ -6,3 +6,3 @@ const { OBJECT_VARIABLE, ESCAPE_VARIABLE, BOOLEAN_ATTRIBUTES, UNESCAPED_NAMES, GLOBAL_VARIABLES, RESERVED_KEYWORDS } = require('./enum') | ||
} = require('./factory') | ||
const { extract, getName } = require('./string') | ||
const { extract, getName, isCurlyTag, getExpressionFromCurlyTag } = require('./string') | ||
const { getFilterName, extractFilterName } = require('./filters') | ||
@@ -328,2 +328,5 @@ const AbstractSyntaxTree = require('abstract-syntax-tree') | ||
function convertKey (key, variables) { | ||
if (isCurlyTag(key)) { | ||
key = getExpressionFromCurlyTag(key) | ||
} | ||
const tree = convertToExpression(key) | ||
@@ -330,0 +333,0 @@ return getTemplateNode(tree, variables, true) |
@@ -1,2 +0,2 @@ | ||
const { logicalExpressionReduction, ifStatementRemoval } = require('astoptech') | ||
const { logicalExpressionReduction, binaryExpressionReduction, ifStatementRemoval } = require('astoptech') | ||
const { TEMPLATE_VARIABLE } = require('./enum') | ||
@@ -21,2 +21,3 @@ | ||
this.program.replace({ enter: logicalExpressionReduction }) | ||
this.program.replace({ enter: binaryExpressionReduction }) | ||
this.program.replace({ enter: ifStatementRemoval }) | ||
@@ -23,0 +24,0 @@ this.concatenateLiterals() |
@@ -7,2 +7,6 @@ const { string: { singlespace } } = require('pure-utilities') | ||
function getExpressionFromCurlyTag (value) { | ||
return value.substring(1, value.length - 1) | ||
} | ||
function extract (value) { | ||
@@ -52,2 +56,2 @@ let objects = [] | ||
module.exports = {extract, getName, isCurlyTag} | ||
module.exports = {extract, getName, isCurlyTag, getExpressionFromCurlyTag} |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
97514
2408
3