Google Closure Compiler如何处理引号(字符串文字)? [英] How does Google Closure Compiler handle quotes (string literals)?

查看:134
本文介绍了Google Closure Compiler如何处理引号(字符串文字)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题'Google Closure Compiler如何处理引号(字符串文字)?'也可以重新措辞如下:

The question 'How does Google Closure Compiler handle quotes (string literals)?' could also be re-phrased like:


  • 为什么Closure用双引号替换/交换单引号吗?

  • Closure如何决定使用什么引号格式/样式?

  • (怎么样)我可以改变这个(默认)行为吗?

注1:这个问题是关于为什么Closure(或其他一些缩小器)选择更喜欢双引号(如此处所述) 此处)。

Note 1: this question is not about why Closure (or some other minifiers) have chosen to prefer double quotes (as is asked here and here).

注2:这个问题不是关于单引号和双引号的讨论,但是有些人理解GCC对我们的代码做什么(及其原因)是非常有用的!

Note 2: this question is not about single vs double quote discussions, however some understanding what GCC does to our code (and why) is rather useful!

推荐答案

经常说(或问为什么) G oogle C losure C ompiler(GCC)用双引号替换单引号(即使 compilation_level 设置为 WHITESPACE_ONLY !!!):

It is often stated that (or asked why) Google Closure Compiler (GCC) replaces single quotes with double-quotes (even when the compilation_level is set to WHITESPACE_ONLY !!!):

示例 xmp_1.js:

example xmp_1.js:

alert('Hello world!');           // output: alert("Hello world!");

然而......这只是'真相'的一半,因为:

However... this is only half of 'the truth', because:

示例 xmp_2.js:

example xmp_2.js:

alert('Hello "world"!');         // output: alert('Hello "world"!');
                                 //  *NOT*: alert("Hello \"world\"!");

GCC本质上是'你的原始javascript''更小(更有效)的javascript'翻译:因此它不会盲目地用双引号替换单引号,而是尝试选择'最佳引号字符'(毕竟。其中一个主要目标是'迷你'脚本)。

GCC is essentially a 'your raw javascript' to 'smaller (and more efficient) javascript' translator: so it does not 'blindly' replace single quotes with double quotes, but tries to choose an 'optimal quote-character' (after all.. one of the primary goals is to 'mini-fy' the script).

来自源代码( CompilerOptions.java )和此问题报告可以了解:

From the source-code (CompilerOptions.java) and this issue-report one can learn that:


如果字符串包含更多内容单引号比双引号然后
编译器将使用双引号包装字符串,反之亦然。

如果字符串不包含引号或等于单引号和
双引号
,然后编译器将默认使用双引号

If the string contains more single quotes than double quotes then the compiler will wrap the string using double quotes and vice versa.
If the string contains no quotes or an equal number of single and double quotes, then the compiler will default to using double quotes.

喜欢这个示例 xmp_3.js:

Like this example xmp_3.js:

alert('Hello "w\'orld"!');       // output: alert('Hello "w\'orld"!');
alert('Hello "w\'o\'rld"!');     //         alert("Hello \"w'o'rld\"!");

注意上面的xmp_3如何产生同时使用的混合输出'作为外部报价:最佳选择后跟默认值(当无关紧要时)。

Note how the above xmp_3 results in a 'mixed' output that uses both ' and " as outer quotation: the optimal choice followed by the default (when it didn't matter).

事实证明有一些严重的合法现实案例,默认为单引号会更好。正如 issue 836 (来自2012年10月8日):

As it turned out there are some serious legitimate real-world cases where defaulting to single-quotes would have been better. As explained in the issue 836 (from Oct 8, 2012) referenced above:


FT网络应用程序(应用程序) .ft.com)和Playbook 的Economist应用程序向客户端提供
JavaScript更新以及其他资源
将它们作为JSON编码对象的一部分传输.JSON使用double
本地引用,因此编译的JavaScript
中的所有双引号都需要转发。
当传输大量更新时,这会使FT网络应用程序JS的大小膨胀
约20kB。

The FT web app (app.ft.com) and the Economist app for Playbook deliver JavaScript updates to the client along with other resources by transmitting them as part of a JSON-encoded object. JSON uses double quotes natively, so all the double quotes in the compiled JavaScript need to be escaped. This inflates the size of the FT web app's JS by about 20kB when transmitting a large update.



<该问题的记者带来了一个礼物:一个补丁,添加了选项 prefer_single_quotes ,将默认报价字符从双引号更改为单引号。

The reporter of the issue came with a gift: a patch that added the option prefer_single_quotes to change the default quote-character from double quote to single quote.

这个问题非常认真,项目成员Santos 考虑将默认双引号更改为单引号('并查看是否有人抱怨') .. TWICE (也是在记者/补丁提供者声明他将其作为一个选项实施后,因此它不会产生任何向后兼容性的后果,因为'某人可能依赖于输出的字符串双引号有些奇怪的原因')。

This issue was taken seriously enough that project member Santos considered changing the default double quote to single quote ('and see if anybody complains').. TWICE (also after the reporter/patch-contributer stated that he implemented it as an option so that it wouldn't have any backward-compatibility consequences since 'someone might be relying on strings being output with double quotes for some bizarre reason').

然而,大约一周后补丁被接受(r2258),又一周后重做(r2257),2012年10月30日桑托斯报告说现在可以选择启用:

- 格式= SINGLE_QUOTES

(所以除了<之外的第三个选项code> PRETTY_PRINT 和 PRINT_INPUT_DELIMITER 格式 -key)。

(注意:在当前的源代码中,目前仍然可以找到许多对'prefer_single_quotes'的引用。)

However, about one week later the patch was accepted (r2258), another week later reworked (r2257) and on Oct 30, 2012 Santos reported back that the option could now be enabled with:
--formatting=SINGLE_QUOTES
(so a third option besides PRETTY_PRINT and PRINT_INPUT_DELIMITER for the formatting-key).
(Note: in the current source-code one can currently still find numerous references to 'prefer_single_quotes' as well.)

用法:

如果您(下载并且)使用(本地java)应用程序:

Usage:
If you (download and) use the (local java) application:

java -jar编译器。 jar --js xmp_1.js - 格式化SINGLE_QUOTES
你会看到: alert('Hello world!'); 现在编译为 alert('Hello world!');

但是,在编写本文时,编译器服务API和UI(最有可能使用API​​)位于 http://closure-compiler.appspot.com 接受此第三个(新的,虽然存在一年)格式化选项: SINGLE_QUOTES 并将抛出错误:

17:未知格式化选项single_quotes。

However, at this time of writing, the Compiler Service API and UI (that most probably uses the API) located at http://closure-compiler.appspot.com, do not accept this third (new, although a year in existence) formatting-option: SINGLE_QUOTES and will throw an error:
17: Unknown formatting option single_quotes.

在通过源挖掘(再次)之后,似乎(我不是Java专家)这是因为 jscomp / webservice / common / Protocol.java 只接受旧的 PRETTY_PRINT PRINT_INPUT_DELIMITER

After digging (again) through the source, it seems (I'm not a Java-expert) that this is because jscomp/webservice/common/Protocol.java only accepts the older PRETTY_PRINT and PRINT_INPUT_DELIMITER

 * All the possible values for the FORMATTING key.
 */
public static enum FormattingKey implements ProtocolEnum {
  PRETTY_PRINT("pretty_print"),
  PRINT_INPUT_DELIMITER("print_input_delimiter"),
  ;

如果此选项在API和/或用户界面中可用,我将更新此答案。

I will update this answer should this option become available in the API and/or UI.

希望这可以帮助并节省一些时间,因为谷歌可以找到的唯一文档和参考 SINGLE_QUOTES 目前在这一个问题836和源中的一些评论。现在它对SO有一些解释(我期望它)。

Hope this helps and saves someone some time, since the only documentation and reference google can find about SINGLE_QUOTES is currently in this one issue 836 and some comments in the source. Now it has some explanation on SO (where I'd expect it).

这篇关于Google Closure Compiler如何处理引号(字符串文字)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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