node.js - how to get the raw version of a template string in iojs -
is possible raw version of template string in iojs ?
var s = `foo${1+1}bar` console.log(s); // foo2bar
in previous example string: foo${1+1}bar
edit1: need detect whether template string depends on context of if is 'constant' string may contain cr , lf
is possible raw version of template string in iojs ?
no not. it's not possible raw representation of literal, there no way "raw" literal in these cases:
var foo = {[1+1]: 42}; var bar = 1e10; var baz = "\"42\"";
note term "template string" misleading (as may indicate somehow raw value of string (which not case shown above)). correct term "template literal".
my need detect whether template string depends on context of if is 'constant' string may contain cr , lf
seems job static analysis tool. e.g. can use recast parse source code , traverse template literals.
for example, the ast representation of `foo${1+1}bar`
is:
if such ast node empty expression
property, know value constant.
there way determine whether template literal "static" or "dynamic" @ runtime, involves changing behavior of code.
you can use tagged templates. tagged templates functions passed static , dynamic portions of template literal.
example:
function foo(template, ...expressions) { console.log(template, expressions); } foo`foo${1+1}bar` // logs (["foo", "bar"], [2]) returns `undefined`
i.e. if foo
gets passed single argument, template literal not contain expressions. however, foo
have interpolate static parts dynamic parts , return result (not shown in above example).
Comments
Post a Comment