Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored blaze to move ES6 version #397

Open
wants to merge 7 commits into
base: release-3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/blaze-html-templates/package.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* global Package */

Package.describe({
name: 'blaze-html-templates',
summary: "Compile HTML templates into reactive UI with Meteor Blaze",
summary: 'Compile HTML templates into reactive UI with Meteor Blaze',
version: '2.0.0',
git: 'https://github.com/meteor/blaze.git'
git: 'https://github.com/meteor/blaze.git',
});

Package.onUse(function(api) {
Expand All @@ -11,6 +13,6 @@ Package.onUse(function(api) {
'blaze@2.5.0',

// Compile .html files into Blaze reactive views
'templating@1.4.1'
'templating@1.4.1',
]);
});
10 changes: 6 additions & 4 deletions packages/blaze-tools/package.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* global Package */

Package.describe({
name: 'blaze-tools',
summary: "Compile-time tools for Blaze",
version: '1.1.3',
git: 'https://github.com/meteor/blaze.git'
summary: 'Compile-time tools for Blaze',
version: '1.2.0',
git: 'https://github.com/meteor/blaze.git',
});

Package.onUse(function (api) {
Expand All @@ -21,6 +23,6 @@ Package.onTest(function (api) {
api.use('html-tools@1.1.3');

api.addFiles([
'token_tests.js'
'token_tests.js',
]);
});
8 changes: 5 additions & 3 deletions packages/blaze-tools/preamble.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/* eslint-disable no-undef */


import {
EmitCode,
toJSLiteral,
toObjectLiteralKey,
ToJSVisitor,
toJS
toJS,
} from './tojs';

import {
parseNumber,
parseIdentifierName,
parseExtendedIdentifierName,
parseStringLiteral
parseStringLiteral,
} from './tokens';

BlazeTools = {
Expand All @@ -23,7 +25,7 @@ BlazeTools = {
parseNumber,
parseIdentifierName,
parseExtendedIdentifierName,
parseStringLiteral
parseStringLiteral,
};

export { BlazeTools };
142 changes: 70 additions & 72 deletions packages/blaze-tools/tojs.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,108 @@
/* eslint-disable import/no-unresolved */

import { HTML } from 'meteor/htmljs';


export function EmitCode (value) {
if (! (this instanceof EmitCode))
// called without `new`
return new EmitCode(value);
export class EmitCode {
constructor(value) {
if (typeof value !== 'string') throw new Error('EmitCode must be constructed with a string');

if (typeof value !== 'string')
throw new Error('EmitCode must be constructed with a string');
this.value = value;
}

this.value = value;
toJS(/* visitor */) {
return this.value;
}
}

EmitCode.prototype.toJS = function (visitor) {
return this.value;
};

// Turns any JSONable value into a JavaScript literal.
export function toJSLiteral (obj) {
export function toJSLiteral(obj) {
// See <http://timelessrepo.com/json-isnt-a-javascript-subset> for `\u2028\u2029`.
// Also escape Unicode surrogates.
return (JSON.stringify(obj)
.replace(/[\u2028\u2029\ud800-\udfff]/g, function (c) {
return '\\u' + ('000' + c.charCodeAt(0).toString(16)).slice(-4);
}));
.replace(/[\u2028\u2029\ud800-\udfff]/g, function (c) {
return `\\u${(`000${c.charCodeAt(0).toString(16)}`).slice(-4)}`;
}));
}



var jsReservedWordSet = (function (set) {
"abstract else instanceof super boolean enum int switch break export interface synchronized byte extends let this case false long throw catch final native throws char finally new transient class float null true const for package try continue function private typeof debugger goto protected var default if public void delete implements return volatile do import short while double in static with".split(' ').forEach(function (w) {
set[w] = 1;
const jsReservedWordSet = (function (set) {
const _set = set;
'abstract else instanceof super boolean enum int switch break export interface synchronized byte extends let this case false long throw catch final native throws char finally new transient class float null true const for package try continue function private typeof debugger goto protected var default if public void delete implements return volatile do import short while double in static with'.split(' ').forEach(function (w) {
_set[w] = 1;
});
return set;
})({});
return _set;
}({}));

export function toObjectLiteralKey (k) {
if (/^[a-zA-Z$_][a-zA-Z$0-9_]*$/.test(k) && jsReservedWordSet[k] !== 1)
return k;
export function toObjectLiteralKey(k) {
if (/^[a-zA-Z$_][a-zA-Z$0-9_]*$/.test(k) && jsReservedWordSet[k] !== 1) return k;
return toJSLiteral(k);
}

var hasToJS = function (x) {
const hasToJS = function (x) {
return x.toJS && (typeof (x.toJS) === 'function');
};

export const ToJSVisitor = HTML.Visitor.extend();
ToJSVisitor.def({
visitNull: function (nullOrUndefined) {
visitNull() {
return 'null';
},
visitPrimitive: function (stringBooleanOrNumber) {
visitPrimitive(stringBooleanOrNumber) {
return toJSLiteral(stringBooleanOrNumber);
},
visitArray: function (array) {
var parts = [];
for (var i = 0; i < array.length; i++)
parts.push(this.visit(array[i]));
return '[' + parts.join(', ') + ']';
visitArray(array) {
const parts = [];
for (let i = 0; i < array.length; i++) parts.push(this.visit(array[i]));
return `[${parts.join(', ')}]`;
},
visitTag: function (tag) {
visitTag(tag) {
return this.generateCall(tag.tagName, tag.attrs, tag.children);
},
visitComment: function (comment) {
visitComment(comment) {
return this.generateCall('HTML.Comment', null, [comment.value]);
},
visitCharRef: function (charRef) {
visitCharRef(charRef) {
return this.generateCall('HTML.CharRef',
{html: charRef.html, str: charRef.str});
{ html: charRef.html, str: charRef.str });
},
visitRaw: function (raw) {
visitRaw(raw) {
return this.generateCall('HTML.Raw', null, [raw.value]);
},
visitObject: function (x) {
visitObject(x) {
if (hasToJS(x)) {
return x.toJS(this);
}

throw new Error("Unexpected object in HTMLjs in toJS: " + x);
throw new Error(`Unexpected object in HTMLjs in toJS: ${x}`);
},
generateCall: function (name, attrs, children) {
var tagSymbol;
generateCall(name, attrs, children) {
let i;
let needsHTMLAttrs;
let tagSymbol;
if (name.indexOf('.') >= 0) {
tagSymbol = name;
} else if (HTML.isTagEnsured(name)) {
tagSymbol = 'HTML.' + HTML.getSymbolName(name);
tagSymbol = `HTML.${HTML.getSymbolName(name)}`;
} else {
tagSymbol = 'HTML.getTag(' + toJSLiteral(name) + ')';
tagSymbol = `HTML.getTag(${toJSLiteral(name)})`;
}

var attrsArray = null;
let attrsArray = null;
if (attrs) {
attrsArray = [];
var needsHTMLAttrs = false;
needsHTMLAttrs = false;
if (HTML.isArray(attrs)) {
var attrsArray = [];
for (var i = 0; i < attrs.length; i++) {
var a = attrs[i];
attrsArray = [];
for (i = 0; i < attrs.length; i++) {
const a = attrs[i];
if (hasToJS(a)) {
attrsArray.push(a.toJS(this));
needsHTMLAttrs = true;
} else {
var attrsObjStr = this.generateAttrsDictionary(attrs[i]);
if (attrsObjStr !== null)
attrsArray.push(attrsObjStr);
const attrsObjStr = this.generateAttrsDictionary(attrs[i]);
if (attrsObjStr !== null) attrsArray.push(attrsObjStr);
}
}
} else if (hasToJS(attrs)) {
Expand All @@ -113,44 +112,43 @@ ToJSVisitor.def({
attrsArray.push(this.generateAttrsDictionary(attrs));
}
}
var attrsStr = null;
let attrsStr = null;
if (attrsArray && attrsArray.length) {
if (attrsArray.length === 1 && ! needsHTMLAttrs) {
attrsStr = attrsArray[0];
if (attrsArray.length === 1 && !needsHTMLAttrs) {
const [first] = attrsArray;
attrsStr = first;
} else {
attrsStr = 'HTML.Attrs(' + attrsArray.join(', ') + ')';
attrsStr = `HTML.Attrs(${attrsArray.join(', ')})`;
}
}

var argStrs = [];
if (attrsStr !== null)
argStrs.push(attrsStr);
const argStrs = [];
if (attrsStr !== null) argStrs.push(attrsStr);

if (children) {
for (var i = 0; i < children.length; i++)
argStrs.push(this.visit(children[i]));
for (i = 0; i < children.length; i++) argStrs.push(this.visit(children[i]));
}

return tagSymbol + '(' + argStrs.join(', ') + ')';
return `${tagSymbol}(${argStrs.join(', ')})`;
},
generateAttrsDictionary: function (attrsDict) {
generateAttrsDictionary(attrsDict) {
if (attrsDict.toJS && (typeof (attrsDict.toJS) === 'function')) {
// not an attrs dictionary, but something else! Like a template tag.
return attrsDict.toJS(this);
}

var kvStrs = [];
for (var k in attrsDict) {
if (! HTML.isNully(attrsDict[k]))
kvStrs.push(toObjectLiteralKey(k) + ': ' +
this.visit(attrsDict[k]));
}
if (kvStrs.length)
return '{' + kvStrs.join(', ') + '}';
const kvStrs = [];
Object.getOwnPropertyNames(attrsDict).forEach((k) => {
if (!HTML.isNully(attrsDict[k])) {
kvStrs.push(`${toObjectLiteralKey(k)}: ${
this.visit(attrsDict[k])}`);
}
});
if (kvStrs.length) return `{${kvStrs.join(', ')}}`;
return null;
}
},
});

export function toJS (content) {
return (new ToJSVisitor).visit(content);
export function toJS(content) {
return (new ToJSVisitor()).visit(content);
}
Loading