Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bemhtml-syntax

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bemhtml-syntax - npm Package Compare versions

Comparing version 0.0.20 to 0.0.21

lib/blabla.bemhtml.js

8

lib/api.js

@@ -1,7 +0,1 @@

var Q = require('q'),
coa = require('../bin/bemhtml-syntax'),
api = coa.api;
var api = require('./');

@@ -7,3 +7,4 @@ var syntax = exports,

BEMHTMLToJS = null,
beautify = require("js-beautify").js_beautify;
beautify = require("js-beautify").js_beautify,
kr = require("./kr");

@@ -23,2 +24,8 @@ function lazyLoad() {

// Krasota methods
syntax.kparse = kr.parse;
syntax.ktranslate = kr.translate;
syntax.kgenerate = kr.generate;
syntax.kcompile = kr.compile;
// Translate old ast to new ast

@@ -104,3 +111,11 @@ syntax.translate = function translate(ast, opts) {

quote_reserved: options.quote_reserved,
replace_this_: options.replace_this_
replace_this_: options.replace_this_,
elemMatch: options.elemMatch,
wrapPattern: options.wrapPattern,
assertHasBlock: options.assertHasBlock,
assertNoThisElem: options.assertNoThisElem,
assertNoBuf: options.assertNoBuf,
returnFromDef: options.returnFromDef,
applySetsMode: options.applySetsMode,
applyCheckFields: options.applyCheckFields
},

@@ -123,6 +138,12 @@ result;

if (options.format === 'bemhtml') {
if (options.format === 'bemhtml' ||
options.format === 'bemxjst1') {
result = syntax.compile(source, opts);
}
if (options.format === 'krasota' ||
options.format === 'bemxjst4') {
result = syntax.kcompile(source, opts);
}
output.write(beautify(result, getBeatufierOptions(options)));

@@ -129,0 +150,0 @@ output.end('\n');

{
"name": "bemhtml-syntax",
"version": "0.0.20",
"version": "0.0.21",
"description": "BEMHTML syntax converter",

@@ -28,2 +28,3 @@ "main": "lib/syntax.js",

"bemhtml-compat": "",
"krasota": "https://github.com/vkz/krasota.js.git#current",

@@ -39,6 +40,7 @@ "bh": "2.2.0",

"devDependencies": {
"bower": "1.4.x",
"escodegen": "1.6.1",
"mocha": "2.2.4",
"bower": "1.4.x"
"ze-helpers": ""
}
}

@@ -0,1 +1,2 @@

// comment here
block('logo').tag()('img')
block('logo')(
tag()('img'),
attrs()({alt: 'logo', href: 'http://...'})
tag()('img'), // end of line comment
attrs()({alt: 'logo', href: 'http://...'}) // another comment
)

@@ -0,6 +1,18 @@

/*
*
long winded comment here with code inside
block('logo')(
tag()('img'),
attrs()({alt: 'logo', href: 'http://...'})
)
*
*/
block('b-bla')(
tag()('span'),
mod('0-mode', 'v2').tag()('a'),
// mod 0-mode v2, tag:'a'
mix()([{ elemMods: { m2: 'v2' } }]),
js()(true)
)
var syntax = require(".."),
assert = require("assert"),
bemxjst = require("bem-xjst"),
// TODO pull proper latest i-bem.bemhtml from bem-core
ibem = require('fs')

@@ -11,5 +10,9 @@ .readFileSync(

esprima = require("esprima"),
esgen = require("escodegen").generate,
es = require("escodegen"),
esgen = es.generate,
path = require("path"),
fs = require("fs");
fs = require("fs"),
beautify = require("js-beautify").js_beautify,
krasota = require("krasota"),
KParser = krasota.KrasotaJSParser;

@@ -29,6 +32,42 @@ var pp = require("zeHelpers").prettyPrint;

ast = syntax.parse(code);
assert.deepEqual(ast, result );
assert.deepEqual(ast, result );
}
common.testParse = testParse;
function empty(a) { return a.length === 0; }
function isAst(a) { return Array.isArray(a); }
function isPrim(a) { return !isAst(a); }
function dropTag(a, tag) {
return a.filter(function (el) {
return !isAst(el) || (el[0] !== tag);
});
}
function astEqual(a, b, skipTag) {
if (skipTag) {
a = dropTag(a, skipTag);
b = dropTag(b, skipTag);
}
if (empty(a)) {
return empty(b);
}
if (a.length !== b.length) return false;
return a.reduce(function (res, aNode, i) {
var bNode = b[i];
// propagate false
if (!res) return false;
if (isAst(aNode)) {
return isAst(bNode) && astEqual(aNode, bNode, skipTag);
} else {
return isPrim(bNode) && (aNode === bNode);
}
}, true);
}
common.astEqual = astEqual;
function testTransform(source, result, options) {

@@ -42,2 +81,17 @@ var code = getSource(source),

function testKTransform(source, result, options) {
var sAst = KParser.match(syntax.kcompile(source, options), 'topLevel'),
rAst = KParser.match(result, 'topLevel');
assert.ok(common.astEqual(sAst, rAst, 'spacesAndComments'));
}
common.testKTransform = testKTransform;
function testKParse(source, result, skipTag) {
var ast = syntax.kparse(source);
// pp(ast, {prompt: "ast"});
// assert.deepEqual(ast, result );
assert.ok(astEqual(ast, result, skipTag));
}
common.testKParse = testKParse;
function toHtml(source, input) {

@@ -74,2 +128,38 @@ return bemxjst.compile(ibem + source, {}).apply.call(input);

function testKCompile(source, json, html, target) {
var code = getSource(source),
compiled = syntax.kcompile(code),
result = html || (json && toHtml(compat.transpile(code), json));
var poptions = {range: true, tokens: true, comment: true};
// Test against a hand-written template when supplied
if (target) {
target = getSource(target);
// generated code
var cast = esprima.parse(compiled, poptions);
var tast = esprima.parse(target, poptions);
cast = es.attachComments(cast, cast.comments, cast.tokens);
tast = es.attachComments(tast, tast.comments, tast.tokens);
// check esprima+escodegen equality
assert.equal(
es.generate(cast, {comment: true}),
es.generate(tast, {comment: true}));
// check js-beautify equality with newlines preserved
assert.equal(
beautify(compiled, {"preserve_newlines": true}),
beautify(target, {"preserve_newlines": true}));
// HTML via target
json && assert.equal(
toHtml(compiled, json),
toHtml(target, json));
};
// HTML provided or via compat
result && assert.equal(
toHtml(compiled, json),
result);
}
common.testKCompile = testKCompile;
function maybeRead(f) {

@@ -85,3 +175,7 @@ var utf8 = { encoding:'utf8' };

function testDir(dirname) {
function testDir(dirname, test) {
// Parameterise with test-arg:
// test === testCompile to check BEMHTML grammars
// test === testKCompile to check Krasota grammars
test = (typeof test === 'function' && test) || testCompile;
var dir = path.join(path.dirname(module.filename), dirname),

@@ -100,3 +194,3 @@ tests = fs.readdirSync(dir).filter(function (f) {

it(msg, function () {
testCompile(source, json, html, target);
test(source, json, html, target);
});

@@ -103,0 +197,0 @@ });

@@ -1,19 +0,36 @@

var syntax = require("bemhtml-syntax");
var fs = require("fs"),
path = require("path"),
pp = require("zeHelpers").prettyPrint,
assert = require("assert"),
beautify = require("js-beautify").js_beautify;
var source = 'block b1, tag: "a"';
var syntax = require('../'),
common = require("./common"),
parse = syntax.parse,
translate = syntax.translate,
generate = syntax.generate,
compile = syntax.compile;
// Parse BEMHTML code
var ast = syntax.parse(source);
var dir = path.dirname(module.filename),
utf8 = { encoding:'utf8' },
getSource = function (file) {
return fs.readFileSync(path.join(dir, file), utf8);
};
// Transform AST for serialisation
var newAst = syntax.translate(ast);
var source = common.getSource(function () {/*
// line: template
!this.elem { // end: pre-sub
block b, content: { return; } // end: sub-template
// line: sub-template
block b, attrs: { return; } // end: sub-template
// line: post-sub
} // end: template
*/});
// Serialise to JavaScript
var jsCode1 = syntax.generate(newAst);
// source = getSource('./comments.bemhtml');
/* Returns:
* block('b1').tag()('a');
*/
var ast = parse(source);
pp(ast, {prompt: "ast"});
// Or do everything in one go
var jsCode2 = syntax.compile(source);
var tast = translate(ast);
pp(tast, {prompt: "t-ast"});

@@ -19,42 +19,92 @@ var syntax = require(".."),

// var source = fs.readFileSync('./tt.bemhtml', {encoding: 'utf8'});
// // should parse mixed js and templates
// var s1 = fs.readFileSync('./kparser/ext-js.bemhtml', {encoding: 'utf8'});
// should parse mixed js and templates
var s1 = fs.readFileSync('./kparser/ext-js.bemhtml', {encoding: 'utf8'});
// // should parse multiple templates
// var s2 = fs.readFileSync('./basic/info6.bemhtml', {encoding: 'utf8'});
// should parse multiple templates
var s2 = fs.readFileSync('./basic/info6.bemhtml', {encoding: 'utf8'});
// // should parse deeply nested template
// var s5 = fs.readFileSync('./basic/info7.bemhtml', {encoding: 'utf8'});
// should parse deeply nested template
var s5 = fs.readFileSync('./basic/info7.bemhtml', {encoding: 'utf8'});
// // should parse mod and elemMod
// var s6 = fs.readFileSync('./kparser/elem-mod.bemhtml', {encoding: 'utf8'});
// should parse mod and elemMod
var s6 = fs.readFileSync('./kparser/elem-mod.bemhtml', {encoding: 'utf8'});
// // should parse applyCtx expr
// var s7 = fs.readFileSync('./kparser/general-body-apply-expr.bemhtml', {encoding: 'utf8'});
// should parse applyCtx expr
var s7 = fs.readFileSync('./kparser/general-body-apply-expr.bemhtml', {encoding: 'utf8'});
// // should parse local
// var s8 = fs.readFileSync('./kparser/local.bemhtml', {encoding: 'utf8'});
// should parse local
var s8 = fs.readFileSync('./kparser/local.bemhtml', {encoding: 'utf8'});
// // should parse whole-line comments
// var s9 = fs.readFileSync('./basic/info1.bemhtml', {encoding: 'utf8'});
// should parse whole-line comments
var s9 = fs.readFileSync('./basic/info1.bemhtml', {encoding: 'utf8'});
// // should parse end-of-line comments
// var s10 = fs.readFileSync('./basic/info2.bemhtml', {encoding: 'utf8'});
// should parse end-of-line comments
var s10 = fs.readFileSync('./basic/info2.bemhtml', {encoding: 'utf8'});
// // should parse multi-line comments
// var s11 = fs.readFileSync('./basic/info5.bemhtml', {encoding: 'utf8'});
// should parse multi-line comments
var s11 = fs.readFileSync('./basic/info5.bemhtml', {encoding: 'utf8'});
// // should parse before and after comments
// var s12 = fs.readFileSync('./kparser/before-after-comments.bemhtml', {encoding: 'utf8'});
// should parse before and after comments
var s12 = fs.readFileSync('./kparser/before-after-comments.bemhtml', {encoding: 'utf8'});
// var s12 = fs.readFileSync('./kparser/before-after-comments.bemhtml', {encoding: 'utf8'});
// // [s1, s2, s5, s6, s7, s8, s9, s10, s11, s12].forEach(function (s) {
// [s9, s10, s11].forEach(function (s) {
// console.log('--------------------------------------------------------------------');
// console.log(s);
// var ast = syntax.kparse(s);
// var tast = syntax.ktranslate(ast);
// console.log(pp(tast, {stringify: true, prompt: "t-ast"}));
// });
// [s1, s2, s5, s6, s7, s8, s9, s10, s11, s12].forEach(function (s) {
[s9, s10, s11].forEach(function (s) {
console.log('--------------------------------------------------------------------');
console.log(s);
var ast = syntax.kparse(s);
var tast = syntax.ktranslate(ast);
console.log(pp(tast, {stringify: true, prompt: "t-ast"}));
});
var maybeRead = common.maybeRead;
function testDir(dirname) {
var dir = path.join(path.dirname(module.filename), dirname),
tests = fs.readdirSync(dir).filter(function (f) {
return /\.bemhtml$/i.test(f);
}).map(function (f) {
return path.basename(f, '.bemhtml');
}).forEach(function (name) {
var base = dir + '/' + name,
source = maybeRead(base + '.bemhtml'),
target = maybeRead(base + '.bemhtml.js');
console.log('--------------------------------------------------------------------');
console.log(name);
console.log(source);
var ast = syntax.kparse(source);
var astStr = pp(ast, {stringify: true, prompt: "ast"});
var tast = syntax.ktranslate(ast);
var result = syntax.kgenerate(tast);
pp(astStr
+ '\n\n'
+ beautify(pp(result, {stringify: true, prompt: "result"})), {prompt: "result"});
});
}
// console.log('---------------------------------------------------------- basic\n');
// testDir("basic");
// console.log('---------------------------------------------------------- kparser\n');
// testDir("kparser");
var options = {
replace_this_: true,
elemMatch: true,
wrapPattern: true,
assertNoThisElem: false,
assertNoBuf: false,
returnFromDef: true,
applySetsMode: true,
applyCheckFields: true,
assertHasBlock: true
};
// var s = fs.readFileSync('./basic/info6.bemhtml', {encoding: 'utf8'});
var s = fs.readFileSync('./ktransform/return-apply.bemhtml', {encoding: 'utf8'});
var ast = syntax.kparse(s, {});
pp(ast, {prompt: "ast"});
var tast = syntax.ktranslate(ast, {});
pp(tast, {prompt: "t-ast"});
var result = syntax.kgenerate(tast, {});
pp(beautify(result), {prompt: "result"});
var syntax = require(".."),
common = require("./common"),
ometajs = require("ometajs"),
beautify = require("js-beautify").js_beautify,
Parser = ometajs.grammars.BSJSParser,

@@ -13,2 +14,3 @@ Translator = ometajs.grammars.BSJSTranslator,

KParser = krasota.KrasotaJSParser,
KId = krasota.KrasotaJSIdentity,
KSerialiser = krasota.KrasotaJSSerializer;

@@ -26,52 +28,78 @@

// var source = fs.readFileSync('veged/web4.bemhtml', {encoding: 'utf8'});
// var source = fs.readFileSync('veged/granny.bemhtml', {encoding: 'utf8'});
// var source = fs.readFileSync('veged/images.bemhtml', {encoding: 'utf8'});
var source = fs.readFileSync('featured/elemMatch.bemhtml', {encoding: 'utf8'});
// var source = fs.readFileSync('featured/replace-this-underscore.bemhtml', {encoding: 'utf8'});
var source = common.getSource(function () {/*
var a= [callFunc(), callFunc(1, 2)];
// var source = common.getSource(function () {/*
// block b1 {
// elem e1, default: {
// applyCtx({
// block: 'wrapper',
// content: this.ctx
// });
// }
// this.elem === 'e2' && this._buf === this._.bla {
// content: { applyCtx({block: 'wrapper', content: 'ctx' }) }
// }
// elem e3 {
// default: { return applyCtx({block: 'wrapper', content: 'ctx' }) }
// }
// elem e4 {
// default: { apply({test: 42, elemMods: 'bla'});}
// }
// }
// */});
block 'logo', elem 'bla', tag: 'img'
block('logo').elem('bla').tag()('img');
*/});
// var source = common.getSource(function () {/*
// function som(test) { return 0 };
// block my-block {
// this.elem === 'separator': {
// return function () {BEMHTML.apply(separator, a = 1);}
// }
// this.elem === 'bla': {
// return BEMHTML.apply(bla, a = 1);
// }
// }
// */});
// var source = fs.readFileSync('veged/web4.bemhtml', {encoding: 'utf8'});
// var source = fs.readFileSync('veged/granny.bemhtml', {encoding: 'utf8'});
// var source = fs.readFileSync('veged/images.bemhtml', {encoding: 'utf8'});
// var source = common.getSource(function () {/*
// this.elem === 'separator', block my-block: {return 'bla'}
// */});
// pp(syntax.kparse(source), {prompt: "source"});
// var source = common.getSource(function () {/*
// block bla, elem 'image', default: {
// var ctx = this.ctx,
// image = applyNext({
// block: 'image',
// url: ctx.url,
// alt: ctx.alt
// });
// applyNext();
// apply();
// ctx.marginLeft && (image.style = 'margin-left:' + ctx.marginLeft + 'px;');
// applyCtx({ctx: this.ctx});
// }
// */});
var ast = [ 'template',
[ [ 'spacesAndComments', [] ],
[ 'predicates',
[ 'pred',
[ 'block',
[ [ 'spacesAndComments', [] ],
[ 'name', 'block' ],
[ 'spaces', [ ' ' ] ],
[ 'spacesAndComments', [] ],
[ 'value', [ 'string', '\'', [ 'l', 'o', 'g', 'o' ] ] ],
[ 'spacesAndComments', [] ] ] ] ],
[ 'pred',
[ 'elem',
[ [ 'spacesAndComments', [ [ 'spaces', ' ' ] ] ],
[ 'name', 'elem' ],
[ 'spaces', [ ' ' ] ],
[ 'spacesAndComments', [] ],
[ 'value', [ 'string', '\'', [ 'b', 'l', 'a' ] ] ],
[ 'spacesAndComments', [] ] ] ] ],
[ 'pred',
[ 'stdMode',
[ 'spacesAndComments', [ [ 'spaces', ' ' ] ] ],
[ 'name', 'tag' ],
[ 'spacesAndComments', [] ] ] ] ],
[ 'spacesAndComments', [] ],
[ 'body',
[ 'spacesAndComments', [ [ 'spaces', ' ' ] ] ],
[ 'literalBody', [ 'string', '\'', [ 'i', 'm', 'g' ] ] ],
[ 'spacesAndComments', [] ] ]]];
var options = {
replace_this_: true,
elemMatch: true,
wrapPattern: true,
assertNoThisElem: false,
assertNoBuf: false,
returnFromDef: true,
applySetsMode: true,
applyCheckFields: true,
assertHasBlock: true
};
var Transformer = require('/Users/kozin/Documents/bemhtml-syntax/lib/ometa/bemhtml-krasota.ometajs').Transformer;
var tast = Transformer.match(ast, 'topLevel');
console.log(pp(tast, {stringify: true, prompt: "t-ast"}));
pp(source, {prompt: "source"});
var out = KSerialiser.match(tast, 'topLevel');
pp(out, {prompt: "out"});
// var out = syntax.kcompile(source, options);
// pp(beautify(out), {prompt: "out"});
var kast = KParser.match(source, 'topLevel');
pp(kast, {prompt: "kast"});

@@ -8,32 +8,119 @@ var syntax = require(".."),

path = require("path"),
pp = require("zeHelpers").prettyPrint;
pp = require("zeHelpers").prettyPrint,
assert = require("assert"),
beautify = require("js-beautify").js_beautify;
var krasota = require("krasota"),
KParser = krasota.KrasotaJSParser,
KSerializer = krasota.KrasotaJSSerializer;
// var source = fs.readFileSync('veged/granny-dac.bemhtml', {encoding: 'utf8'});
var source = fs.readFileSync('scratch11.bemhtml', {encoding: 'utf8'});
// source = fs.readFileSync('scratch12.bemhtml', {encoding: 'utf8'});
// var source = fs.readFileSync('scratch11.bemhtml', {encoding: 'utf8'});
// source = fs.readFileSync('scratch13.bemhtml', {encoding: 'utf8'});
var dir = path.dirname(module.filename),
utf8 = { encoding:'utf8' },
getSource = function (file) {
return fs.readFileSync(path.join(dir, file), utf8);
},
testParse = function (file, result) {
var source = getSource(file);
common.testKParse(source, result);
},
testTransform = function (file, options) {
var source = getSource(file),
result = getSource(file + '.js');
testKTransform(source, result, options);
};
// var om = Parser.matchAll(source, 'topLevel');
// pp(om, {prompt: "ometa"});
// var kr = KParser.match(source, 'topLevel');
// pp(kr, {prompt: "krasota"});
// var opts = {replace_this_: true};
var opts = {
replace_this_: true,
elemMatch: true,
wrapPattern: true,
assertNoThisElem: false,
assertNoBuf: false,
returnFromDef: true,
applySetsMode: true,
applyCheckFields: true,
assertHasBlock: true
};
function testKTransform(source, result, options) {
var out = syntax.kcompile(source, options);
var sAst = KParser.match(out, 'topLevel'),
rAst = KParser.match(result, 'topLevel');
pp(out, {prompt: "compiles"});
pp(result, {prompt: "should compile"});
// pp(sAst, {prompt: "sAst"});
// pp(rAst, {prompt: "rAst"});
assert.ok(common.astEqual(sAst, rAst, 'spacesAndComments'));
}
// var source = common.getSource(function () {/*
// var t = 1;
// block slider: {
// applyNext(a = 'first');
// if (bla) {
// test();
// applyNext({});
// } else {
// return 'bla';
// }
// apply();
// }
// applyCtx();
// applyCtx({a: 42});
// */});
// var r = Parser.matchAll(source, 'topLevel');
// pp(r, {prompt: "r"});
// var ast = syntax.kparse(source, opts);
// pp(ast, {prompt: "ast"});
// pp(syntax.ktranslate(ast, opts), {prompt: "tast"});
// pp(beautify(syntax.kcompile(source, opts)), {prompt: "code"});
// testTransform('./ktransform/def-replace-wrap.bemhtml', {
// replace_this_: true,
// elemMatch: true,
// wrapPattern: true,
// returnFromDef: true
// });
var opts = {replace_this_: true};
var ast = syntax.parse(source, opts);
// pp(ast, {prompt: "ast"});
pp(syntax.translate(ast, opts), {prompt: "tast"});
// pp(syntax.compile(source, opts), {prompt: "code"});
// testTransform('./ktransform/def-applyCtx.bemhtml', {
// replace_this_: true,
// elemMatch: true,
// wrapPattern: true,
// returnFromDef: true
// });
// testTransform('./ktransform/def-no-wrap.bemhtml', {
// replace_this_: true,
// elemMatch: true,
// wrapPattern: false,
// returnFromDef: true
// });
// testTransform('./ktransform/return-apply.bemhtml', {});
// testTransform('./ktransform/multi.bemhtml', {wrapPattern: true});
// var res = syntax.kcompile(source, {replace_this_: true});
// console.log(beautify(res));
// testTransform('./ktransform/end-of-line-comments.bemhtml', {});
testTransform('./ktransform/before-after-comments.bemhtml', {});
// var source = fs.readFileSync('featured/drop-notelem.bemhtml', {encoding: 'utf8'});
// var source = fs.readFileSync('veged/granny.bemhtml', {encoding: 'utf8'});
// var file = './ktransform/def-replace-wrap.bemhtml';
// var file = './ktransform/def-applyCtx.bemhtml';
// var file = './ktransform/def-replace-wrap.bemhtml';
// var file = './ktransform/def-no-wrap.bemhtml';
// var code = getSource(file);
// var resultAst = KParser.match(getSource(file + '.js'), 'topLevel');
// var options = {wrapPattern: true};
// var ast = syntax.kparse(code, options);
// var extAst = syntax.ktranslate(ast, options);
// pp(extAst, {prompt: "extAst"});
// pp(resultAst, {prompt: "resultAst"});
// pp(code, {prompt: "code"});
// pp(syntax.kgenerate(extAst, options), {prompt: "syntax.kgenerate(extAst, )"});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc