如何在iojs中获取模板字符串的原始版本

发布于 2025-02-07 07:54:13 字数 239 浏览 1 评论 0原文

是否可以在IOJS中获得模板字符串的原始版本?

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

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

edif> edit1 : 我的需要是检测模板字符串是否取决于其是否仅是一个可能包含CR和LF的“常数”字符串

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撞了怀 2025-02-14 07:54:13

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

不,不是。不可能获得文字的原始表示形式,就像在这些情况下没有办法获得“原始”字面意思:

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

请注意一词“模板字符串”是误导的(因为这可能表明您可以以某种方式获得字符串的原始值(这也不是上面所示的情况))。正确的术语是字面意思”。

我的需求是检测模板字符串是否取决于其上下文,即IF是一个可能包含Cr和LF

的“常数”字符串

这似乎是静态分析工具的工作。例如,您可以使用 recast 来解析源代码并穿越所有模板文字。

例如, 代码>是

“在此处inter

如果这样的AST节点为一个空的expression 属性,然后您知道该值是恒定的。


有一种方法可以确定模板在运行时是“静态”还是“动态”,但这涉及更改代码的行为。

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

示例:

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

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

IE如果foo仅通过一个参数,则模板文字不包含表达式。但是,foo还必须用动态零件插入静态零件并返回结果(未在上面的示例中显示)。

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:

enter image description here

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文