如何获取iojs中的模板字符串的原始版本 [英] how to get the raw version of a template string in iojs

查看:141
本文介绍了如何获取iojs中的模板字符串的原始版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var s =`foo $ {1 +1} bar` 
console.log(s); // foo2bar

在上一个例子中,我想获取字符串: foo $ {1 + 1} bar



edit1
我的需要是检测是否模板字符串取决于它的上下文if is只是一个可能包含CR和LF的常量字符串

解决方案


可以在iojs中获取模板字符串的原始版本吗?


不,不是。在这些情况下,不可能获得原始的文字表示,就像在这些情况下没有办法得到原始文字:

  var foo = {[1 + 1]:42}; 
var bar = 1e10;
var baz =\42 \;

请注意,模板字符串一词具有误导性(因为它可能表明您可以以某种方式获得字符串的原始值(上述情况也不是这样))。正确的术语是模板文字


我的需要是检测一个模板字符串是否取决于它的上下文是否只是一个常量 '可能包含CR和LF的字符串


似乎是静态分析工具的作业。例如。您可以使用重新创建来解析源代码并遍历所有模板文字。



例如, AST表示 foo $ {1 + 1} bar`





如果这样一个AST节点为空的表达式属性,那么你知道该值是不变的。






有一种方法来确定一个模板文字在运行时是静态或动态的,但这涉及到更改代码的行为。



您可以使用标记的模板。标记的模板是通过模板文字的静态和动态部分的函数。



示例:

  function foo(template,... expressions){
console.log(template,expressions);
}

foo`foo $ {1 + 1} bar` // logs([foo,bar],[2]),但返回`undefined`

如果 foo 仅传递一个参数,则该模板文字不包含表达式。但是, foo 也必须使用动态部件插入静态部分并返回结果(未在上述示例中显示)。


Is it possible to get the raw version of a template string in iojs ?

var s = `foo${1+1}bar`
console.log(s); // foo2bar

In the previous example I would like to get the string: foo${1+1}bar

edit1: My need is to detect whether a template string depends on its context of if is is just a 'constant' string that may contain CR and LF

解决方案

Is it possible to get the raw version of a template string in iojs ?

No it is not. It's not possible to get the raw representation of the literal, just like there is no way to get the "raw" literal in these cases:

var foo = {[1+1]: 42};
var bar = 1e10;
var baz = "\"42\"";

Note that the term "template string" is misleading (as it may indicate that you could somehow get the raw value of the string (which is also not the case as shown above)). The correct term is "template literal".

My need is to detect whether a template string depends on its context of if is is just a 'constant' string that may contain CR and LF

Seems like a job for a static analysis tool. E.g. you can use recast to parse the source code and traverse all template literals.

For example, the AST representation of `foo${1+1}bar` is:

If such an AST node as an empty expression property, then you know that the value is constant.


There is a way to determine whether a template literal is "static" or "dynamic" at runtime, but that involves changing the behavior of the code.

You can use tagged templates. Tagged templates are functions that get passed the static and dynamic portions of a template literal.

Example:

function foo(template, ...expressions) {
    console.log(template, expressions);
}

foo`foo${1+1}bar` // logs (["foo", "bar"], [2]) but returns `undefined`

I.e. if foo gets passed only a single argument, the template literal does not contain expressions. However, foo would also have to interpolate the static parts with the dynamic parts and return the result (not shown in the above example).

这篇关于如何获取iojs中的模板字符串的原始版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆