termi / es6-transpiler

Tomorrow's JavaScript syntax today
Other
216 stars 18 forks source link

Add TaggedTemplateExpression support #29

Closed termi closed 10 years ago

termi commented 10 years ago

proposal

var assert = function(a, m){ if(!a)throw new Error(m||"") }

function test(quasis, ...expressionValues){
    var {raw} = quasis;

    assert(quasis.length);
    assert(raw.length);
    assert(quasis.length === raw.length);
    assert(expressionValues.length);

    if(quasis.length === 0)return '';

    expressionValues = expressionValues.map( (x) => `(${x})` )

    var s = '', i = 0, len = raw.length;
    while (true) {
        s += raw[i];
        if (i + 1 === len) {
            return s;
        }
        s += expressionValues[i++];
    }
}
var value = 'val';
var a = test`\n<${ 40 + 2 }>\t - \n<${ value }>\t`
var b = `\n${ 40 + 2 }\t`
var c = String.raw`\n<${ 40 + 2 }>\t - \n<${ value }>\t`

console.log(a, a === '\\n<(42)>\\t - \\n<(val)>\\t')
console.log(b, b === '\n42\t')
console.log(c, c === '\\n<42>\\t - \\n<val>\\t' )

tests from here

termi commented 10 years ago

done it recently 91229b1138ab8c3c81e6739630232a57171d7084