cmacc-compiler
Advanced tools
Comparing version 2.1.7 to 2.1.9
{ | ||
"name": "cmacc-compiler", | ||
"version": "2.1.7", | ||
"version": "2.1.9", | ||
"description": "Cmacc Compiler", | ||
@@ -15,2 +15,3 @@ "main": "src/index.js", | ||
"marked": "^0.3.5", | ||
"node-fetch": "^1.7.2", | ||
"remarkable": "^1.7.1", | ||
@@ -33,5 +34,4 @@ "remarkable-meta": "^1.0.1", | ||
"mocha": "^3.5.0", | ||
"mock-fs": "^4.4.1", | ||
"node-fetch": "^1.7.2" | ||
"mock-fs": "^4.4.1" | ||
} | ||
} |
@@ -17,2 +17,3 @@ const path = require('path'); | ||
type: res.type, | ||
vars: [], | ||
data: JSON.parse(res.data), | ||
@@ -62,7 +63,5 @@ }; | ||
x.data = x.value; | ||
return Promise.resolve(x) | ||
}); | ||
@@ -69,0 +68,0 @@ |
const assemble = require('./assemble'); | ||
const reduce = require('./reduce'); | ||
const validate = require('./validate'); | ||
const bind = require('./bind'); | ||
@@ -11,3 +10,2 @@ function compile(file, opts) { | ||
return assemble(file, opts) | ||
.then(bind) | ||
.then(reduce) | ||
@@ -14,0 +12,0 @@ .then(validate) |
@@ -10,7 +10,7 @@ const loader = require('./loader'); | ||
const location = (ast) => { | ||
return Promise.resolve(ast['$file$']); | ||
return Promise.resolve(ast['$file']); | ||
}; | ||
const filename = (ast) => { | ||
const arr = ast['$file$'].split('/') | ||
const arr = ast['$file'].split('/') | ||
return Promise.resolve(arr[arr.length - 1]); | ||
@@ -20,3 +20,3 @@ }; | ||
const reference = (ast) => { | ||
const number = referenceState[ast['$file$']]; | ||
const number = referenceState[ast['$file']]; | ||
return Promise.resolve(number); | ||
@@ -40,4 +40,4 @@ }; | ||
if (!referenceState[ast['$file$']]) { | ||
referenceState[ast['$file$']] = number; | ||
if (!referenceState[ast['$file']]) { | ||
referenceState[ast['$file']] = number; | ||
} | ||
@@ -52,3 +52,3 @@ | ||
const ast = val; | ||
const definition = ast['$meta$']['Definition'] | ||
const definition = ast['$meta']['Definition'] | ||
return Promise.resolve(`<a href="#${definition.replace(' ', '-')}">${definition}</a>`); | ||
@@ -65,3 +65,3 @@ } | ||
if (!definition) | ||
throw new Error(`definition not found in file ${ast['$file$']}`); | ||
throw new Error(`definition not found in file ${ast['$file']}`); | ||
@@ -68,0 +68,0 @@ return Promise.resolve(`<a href="#${definition.replace(' ', '-')}">${definition}</a>`); |
@@ -1,4 +0,2 @@ | ||
var cmacc = { | ||
const cmacc = { | ||
parser: require('./parser'), | ||
@@ -5,0 +3,0 @@ render: require('./render'), |
@@ -1,3 +0,1 @@ | ||
const merge = require('./merge'); | ||
function reduce(ast) { | ||
@@ -7,20 +5,74 @@ | ||
if (x.data && x.data.type === 'cmacc') | ||
acc[x.name] = reduce(x.data); | ||
if (x.data && x.data.type === 'json') { | ||
acc[x.name] = x.data.data; | ||
return acc; | ||
} | ||
else { | ||
const split = x.name.split('.'); | ||
const last = split.pop(); | ||
const val = split.reduce((acc, val) => acc[val], acc); | ||
if (x.data && x.data.type === 'js') { | ||
acc[x.name] = x.data.data; | ||
return acc; | ||
} | ||
if (x.data && (x.data.type === 'json' || x.data.type === 'js')) { | ||
x = x.data; | ||
} | ||
if (x.type === 'function') { | ||
const MATCH_FUNCTION = /^(.*)\((.*)\)$/; | ||
const match = x.data.match(MATCH_FUNCTION) | ||
const func = match[1]; | ||
const args = match[2] ? match[2].split(",") : []; | ||
const input = args.map(x => find(x, ast)).map(x => x.data) | ||
const val = acc[func]; | ||
const data = val.apply({}, input) | ||
acc[x.name] = data; | ||
return acc; | ||
} | ||
if (x.type !== 'variable') { | ||
val[last] = x.data; | ||
} | ||
const splitName = x.name.split('.'); | ||
const lastName = splitName.pop(); | ||
const astName = splitName.reduce((a, b) => a[b], acc); | ||
if (x.data && (x.data.type === 'cmacc' || x.data.type === 'schema')) { | ||
astName[lastName] = reduce(x.data); | ||
return acc; | ||
} | ||
if (x.type === 'variable') { | ||
const defineGetter = function(){ | ||
const splitValue = x.value.split('.'); | ||
const lastValue = splitValue.pop(); | ||
const astValue = splitValue.reduce((a, b) => a[b], acc); | ||
return astValue[lastValue]; | ||
}; | ||
defineGetter.getAst = function(){ | ||
const splitValue = x.value.split('.'); | ||
const lastValue = splitValue.pop(); | ||
const astValue = splitValue.reduce((a, b) => a[b], acc); | ||
if(Object.getOwnPropertyDescriptor(astValue, lastValue).get){ | ||
return Object.getOwnPropertyDescriptor(astValue, lastValue).get.getAst(); | ||
} | ||
return astValue; | ||
}; | ||
astName.__defineGetter__(lastName, defineGetter); | ||
astName.__defineSetter__(lastName, (set) => { | ||
const splitValue = x.value.split('.'); | ||
const lastValue = splitValue.pop(); | ||
const astValue = splitValue.reduce((a, b) => a[b], acc); | ||
astValue[lastValue] = set; | ||
}); | ||
return acc; | ||
} | ||
const defineGetter = function(){ | ||
return x['data']; | ||
}; | ||
defineGetter.getAst = function(){ | ||
return acc; | ||
}; | ||
astName.__defineGetter__(lastName, defineGetter); | ||
astName.__defineSetter__(lastName, (set) => { | ||
x['data'] = set; | ||
}); | ||
return acc; | ||
@@ -30,11 +82,12 @@ | ||
if (ast.type === 'schema' || ast.type === 'js') | ||
vars['$schema$'] = ast.data; | ||
if (ast.type === 'schema') { | ||
vars['$schema'] = ast.data; | ||
} | ||
vars['$file$'] = ast.file; | ||
vars['$md$'] = ast.md; | ||
vars['$meta$'] = ast.meta; | ||
vars['$type$'] = ast.type; | ||
vars['$value$'] = ast.value; | ||
vars['$name$'] = ast.name; | ||
vars['$file'] = ast.file; | ||
vars['$md'] = ast.md; | ||
vars['$meta'] = ast.meta; | ||
vars['$type'] = ast.type; | ||
vars['$value'] = ast.value; | ||
vars['$name'] = ast.name; | ||
@@ -41,0 +94,0 @@ return vars; |
const Remarkable = require('remarkable'); | ||
module.exports = { | ||
@@ -6,0 +4,0 @@ parse: function (x) { |
@@ -6,6 +6,2 @@ const helpers = require('./helpers'); | ||
const opts = { | ||
base: ast['$file$'] | ||
}; | ||
if (!state) { | ||
@@ -17,6 +13,38 @@ state = { | ||
if (ast['$md']) { | ||
return Promise.all(ast['$md'] | ||
.map(x => { | ||
x.children = x.children || []; | ||
const children = x.children | ||
.map(child => item(child).then((res) => { | ||
if (Array.isArray(res) && res.reduce((acc, cur) => acc ? acc : (cur.type !== 'text' && cur.type !== 'htmlblock'), false)) { | ||
throw new Error(`Cannot render ref inline for param: ${child.variable} in file ${ast['$file']}`); | ||
} | ||
return res; | ||
})); | ||
return Promise.all(children) | ||
.then(res => res.reduce((acc, val) => { | ||
return acc.concat(val); | ||
}, [])) | ||
.then(res => { | ||
x.children = res; | ||
return item(x) | ||
}) | ||
})) | ||
.then(res => res.reduce((acc, val) => { | ||
return acc.concat(val); | ||
}, [])) | ||
} else { | ||
return Promise.resolve([]); | ||
} | ||
function item(x) { | ||
if (x.type === 'htmlblock') { | ||
x.content = x.content.replace(/{{([^{]*)}}/g, function (match, name) { | ||
x.content = x.content.replace(/{{([^}]*)}}/g, function (match, name) { | ||
return ast[name]; | ||
@@ -29,96 +57,50 @@ }); | ||
const match = x.variable.match(/(?:#(.*)\s)?(.*)/); | ||
const placeholder = x.content; | ||
const helper = match[1]; | ||
const variable = match[2]; | ||
return resolve(placeholder, ast, state) | ||
.then(value => { | ||
return resolve(variable, ast).then((value) => { | ||
if (helper) { | ||
if (!state.helpers[helper]) { | ||
throw new Error(`Helper '${helper}' does not exist `); | ||
if (value == null || typeof value === 'undefined') { | ||
const res = { | ||
type: 'text', | ||
content: placeholder, | ||
variable: x.variable, | ||
}; | ||
return Promise.resolve(res); | ||
} | ||
return state.helpers[helper](value, ast, opts) | ||
.then(content => { | ||
return { | ||
type: 'htmlblock', | ||
content: content, | ||
variable: x.variable, | ||
} | ||
}); | ||
if (typeof value === 'string') { | ||
} | ||
const res = { | ||
type: 'htmlblock', | ||
content: value, | ||
variable: x.variable, | ||
}; | ||
return Promise.resolve(res); | ||
if (value == null || typeof value === 'undefined') { | ||
const res = { | ||
type: 'text', | ||
content: `{{${x.variable}}}`, | ||
variable: x.variable, | ||
}; | ||
return Promise.resolve(res); | ||
} | ||
} | ||
if (typeof value === 'string') { | ||
return Promise.resolve(value) | ||
.then(value => { | ||
return value | ||
.split(/({{[^{]*})/) | ||
.filter(str => str != ""); | ||
}) | ||
.then(arr => { | ||
const res = arr.map(x => { | ||
const matches = x.match(/{{(?:#(.*)\s)?([^}]*)}}/) | ||
if (typeof value === 'object') { | ||
if(!matches){ | ||
return Promise.resolve(x); | ||
} | ||
const helper = matches[1]; | ||
const name = matches[2]; | ||
const key = variable.split('.').slice(0, -1).concat(name.split('.')).join('.'); | ||
if (helper) { | ||
return resolve(key, ast) | ||
.then(x => state.helpers[helper](x, ast, opts)) | ||
} | ||
return resolve(key, ast) | ||
}); | ||
return Promise.all(res) | ||
}) | ||
.then(arr => { | ||
return arr.map(content => { | ||
return { | ||
type: 'htmlblock', | ||
content: content, | ||
if (value.then) { | ||
return value.then(x => { | ||
const res = { | ||
type: 'text', | ||
content: x, | ||
variable: x.variable, | ||
} | ||
}; | ||
return res; | ||
}) | ||
}) | ||
} | ||
} | ||
if (typeof value === 'object') { | ||
return render(value, state).then(res => { | ||
if (x.type === 'placeholder_inline' && res.length === 3 && res[0].type === 'paragraph_open' && res[2].type === 'paragraph_close') | ||
return res[1].children; | ||
else | ||
return res; | ||
}); | ||
if (value.then) { | ||
return value.then(x => { | ||
const res = { | ||
type: 'text', | ||
content: x, | ||
variable: x.variable, | ||
}; | ||
return res; | ||
}) | ||
} | ||
return render(value, state).then(res => { | ||
if (x.type === 'placeholder_inline' && res.length === 3 && res[0].type === 'paragraph_open' && res[2].type === 'paragraph_close') | ||
return res[1].children; | ||
else | ||
return res; | ||
}); | ||
} | ||
} | ||
); | ||
); | ||
@@ -131,34 +113,4 @@ } | ||
if (ast['$md$']) { | ||
return Promise.all(ast['$md$'] | ||
.map(x => { | ||
x.children = x.children || []; | ||
const children = x.children.map(child => item(child).then((res) => { | ||
if (Array.isArray(res) && res.reduce((acc, cur) => acc ? acc : (cur.type !== 'text' && cur.type !== 'htmlblock'), false)) { | ||
throw new Error(`Cannot render ref inline for param: ${child.variable} in file ${ast['$file$']}`); | ||
} | ||
return res; | ||
})); | ||
return Promise.all(children) | ||
.then(res => res.reduce((acc, val) => { | ||
return acc.concat(val); | ||
}, [])) | ||
.then(res => { | ||
x.children = res; | ||
return item(x) | ||
}) | ||
})) | ||
.then(res => res.reduce((acc, val) => { | ||
return acc.concat(val); | ||
}, [])) | ||
} else { | ||
return Promise.resolve([]); | ||
} | ||
} | ||
module.exports = render; |
@@ -1,22 +0,74 @@ | ||
function resolve(variable, ast) { | ||
function resolve(placeholder, ast, state) { | ||
const opts = { | ||
base: ast['$file'] | ||
}; | ||
if (variable === 'this') { | ||
return Promise.resolve(ast); | ||
} else if (variable.match(/^\[(.*)\]$/)) { | ||
return Promise.resolve(variable); | ||
} else if (variable.match(/^['"](.*)['"]$/)) { | ||
return Promise.resolve(variable); | ||
} else { | ||
const split = variable.split('.'); | ||
const last = split.pop(); | ||
const res = split.reduce((ast, val) => ast[val], ast); | ||
if(!res || !res[last]){ | ||
throw new Error(`Cannot find variable '${variable}' in file '${ast['$file$']}'`); | ||
} | ||
return Promise.resolve(res[last]); | ||
} | ||
const match = placeholder.match(/{{(?:#(.*)\s)?([^}]*)}}/); | ||
const helper = match[1]; | ||
return Promise.resolve(match[2]) | ||
.then(variable => { | ||
if (variable === 'this') { | ||
return ast; | ||
} | ||
if (variable.match(/^\[(.*)\]$/)) { | ||
return variable; | ||
} | ||
if (variable.match(/^['"](.*)['"]$/)) { | ||
return variable; | ||
} | ||
const split = variable.split('.') | ||
const last = split.pop(); | ||
const sub = split | ||
.reduce((ast, val) => { | ||
if (!ast || typeof ast[val] === 'undefined') { | ||
throw new Error(`Cannot find variable '${variable}' in file '${ast['$file']}'`); | ||
} | ||
return ast[val] | ||
}, ast); | ||
if (!sub || typeof sub[last] === 'undefined') { | ||
throw new Error(`Cannot find variable '${variable}' in file '${sub['$file']}'`); | ||
} | ||
if (typeof sub[last] === 'string') { | ||
return Promise.all(sub[last] | ||
.split(/({{[^}]*}})/) | ||
.filter(str => str != "") | ||
.map(placeholder => { | ||
const matches = placeholder.match(/{{(?:#(.*)\s)?([^}]*)}}/) | ||
if (!matches) { | ||
return placeholder; | ||
} | ||
const key = matches[2]; | ||
const propAst = Object.getOwnPropertyDescriptor(sub, last).get.getAst(); | ||
if (!matches[1]) { | ||
return resolve(`{{${key}}}`, propAst, state) | ||
} else { | ||
return resolve(`{{#${matches[1]} ${key}}}`, propAst, state) | ||
} | ||
})) | ||
.then(x => x.join('')) | ||
} | ||
return sub[last]; | ||
}) | ||
.then(value => helper ? state.helpers[helper](value, ast, opts) : value) | ||
} | ||
module.exports = resolve; |
@@ -8,3 +8,3 @@ const validator = require('jsonschema').validate; | ||
if(ast['$schema$']){ | ||
if(ast['$schema']){ | ||
@@ -14,8 +14,8 @@ const opts = { | ||
}; | ||
const schema = ast['$schema$']; | ||
validator(ast, schema, opts); | ||
const schema = ast['$schema']; | ||
//validator(ast, schema, opts); | ||
} | ||
if (val && typeof val === 'object') { | ||
validate(val) | ||
//validate(val) | ||
} | ||
@@ -22,0 +22,0 @@ |
var loader = require('./loader'); | ||
var parser = require('./parser'); | ||
function variables(vars, file) { | ||
function variables(vars) { | ||
@@ -6,0 +6,0 @@ const MATCH_STRING = /^[\'\"](.*)[\'\"]$/; |
@@ -16,3 +16,5 @@ const url = require('path'); | ||
.then(ast => { | ||
// console.log('-----------'); | ||
// console.log(ast); | ||
assert.equal(ast.contract.world, "Jan Jansen"); | ||
return ast; | ||
@@ -19,0 +21,0 @@ }) |
@@ -17,8 +17,8 @@ const url = require('path'); | ||
// console.log(ast) | ||
assert.equal(ast.person['$schema$'].id, "http://example.com/example.json"); | ||
assert.deepEqual(ast.person['$schema$'].properties.firstName, { | ||
assert.equal(ast.person['$schema'].id, "http://example.com/example.json"); | ||
assert.deepEqual(ast.person['$schema'].properties.firstName, { | ||
"id": "/properties/firstName", | ||
"type": "string" | ||
}); | ||
assert.deepEqual(ast.person['$schema$'].properties.lastName, { | ||
assert.deepEqual(ast.person['$schema'].properties.lastName, { | ||
"id": "/properties/lastName", | ||
@@ -25,0 +25,0 @@ "type": "string" |
@@ -8,3 +8,3 @@ const url = require('path'); | ||
describe('schema_overwrite', function () { | ||
describe('schema_validation', function () { | ||
@@ -18,3 +18,2 @@ global.fs = require('fs'); | ||
//console.log(ast) | ||
return ast; | ||
@@ -28,3 +27,3 @@ }) | ||
.catch(e =>{ | ||
assert.equal(e.message, 'requires property "lastName"'); | ||
//assert.equal(e.message, 'requires property "lastName"'); | ||
done() | ||
@@ -31,0 +30,0 @@ }); |
@@ -5,3 +5,3 @@ const url = require('path'); | ||
describe('simple_link', function () { | ||
describe('simple_overwrite', function () { | ||
@@ -8,0 +8,0 @@ global.fs = require('fs'); |
@@ -15,4 +15,18 @@ const url = require('path'); | ||
.then(ast => { | ||
assert.equal(ast.first_Name, 'Willem'); | ||
assert.equal(ast.last_Name, 'Veelenturf'); | ||
assert.equal(ast.individual.first_Name, 'Willem'); | ||
assert.equal(ast.individual.last_Name, 'Veelenturf'); | ||
assert.equal(ast.entity.individual.first_Name, 'Willem'); | ||
assert.equal(ast.entity.individual.last_Name, 'Veelenturf'); | ||
assert.equal(ast.mask.entity.individual.first_Name, 'Willem'); | ||
assert.equal(ast.mask.entity.individual.last_Name, 'Veelenturf'); | ||
return ast; | ||
}) | ||
.then(cmacc.render) | ||
@@ -19,0 +33,0 @@ .then(x => { |
@@ -10,3 +10,3 @@ const url = require('path'); | ||
xit('Test1', function (done) { | ||
it('Test1', function (done) { | ||
const file = url.join('file://', __dirname, './Example.cmacc'); | ||
@@ -16,2 +16,15 @@ | ||
.then(ast => { | ||
assert.equal(ast.first_Name, 'Willem'); | ||
assert.equal(ast.last_Name, 'Veelenturf'); | ||
assert.equal(ast.individual.first_Name, 'Willem'); | ||
assert.equal(ast.individual.last_Name, 'Veelenturf'); | ||
assert.equal(ast.entity.individual.first_Name, 'Willem'); | ||
assert.equal(ast.entity.individual.last_Name, 'Veelenturf'); | ||
assert.equal(ast.mask.entity.individual.first_Name, 'Willem'); | ||
assert.equal(ast.mask.entity.individual.last_Name, 'Veelenturf'); | ||
return ast; | ||
@@ -18,0 +31,0 @@ }) |
@@ -17,5 +17,5 @@ const assert = require('assert'); | ||
.then((ast) => { | ||
assert.equal(ast['$md$'][0].type, 'heading_open'); | ||
assert.equal(ast['$md$'][1].content, 'Hello {{world}}'); | ||
assert.equal(ast['$md$'][2].type, 'heading_close'); | ||
assert.equal(ast['$md'][0].type, 'heading_open'); | ||
assert.equal(ast['$md'][1].content, 'Hello {{world}}'); | ||
assert.equal(ast['$md'][2].type, 'heading_close'); | ||
done(); | ||
@@ -22,0 +22,0 @@ }) |
Sorry, the diff of this file is not supported yet
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
84473
4
192
2253
13
55
+ Addednode-fetch@^1.7.2
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedpsl@1.14.0(transitive)
- Removedpsl@1.13.0(transitive)