简体   繁体   English

从coffeescript编译为javascript后缩进?

[英]Keeping indent after compiling from coffeescript to javascript?

I have file app.coffee 我有文件app.coffee

angular
  .module('app', [
    'ngAnimate',
    'angular-loading-bar',
    'ui.router',
    'oc.lazyLoad',
    'ui.bootstrap',
    'ngResource'
  ])

When I compile it to javascript it looks like this 当我将其编译为javascript时,它看起来像这样

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('app', ['ngAnimate', 'angular-loading-bar', 'ui.router', 'oc.lazyLoad', 'ui.bootstrap', 'ngResource']);

}).call(this);

Is there any way I can keep the indenting? 有什么办法可以保持缩进吗?

There is no way to modify the output of the CoffeeScript compiler beyond a few simple options (eg --bare ). 除了一些简单的选项(例如--bare )之外,无法修改CoffeeScript编译器的输出。

Do you care what the generated assembly code of your C programs looks like? 您是否关心C程序生成的汇编代码是什么样的? Or the bytecode of your Python programs? 还是您的Python程序的字节码? Normally you don't, and you only look at it very rarely to debug specific problems. 通常情况下您不会这么做,并且很少会调试特定的问题。 The JavaScript code that CofeeScript generates is the same. CofeeScript生成的JavaScript代码是相同的。 Accept that the compiled JS code is not intended for human consumption. 接受编译后的JS代码不供人类使用。 Sure, it's formatted in such a way that you can read it to debug problems (which is nice), but that's not the primary concern. 当然,它的格式化方式使您可以阅读它来调试问题(很好),但这不是主要问题。


That being said, you can run it through external formatting tools, but how would they distinguish between these two arrays: 话虽如此,您可以通过外部格式化工具运行它,但是它们如何区分这两个数组:

angular
  .module('app', [
    'ngAnimate',
    'angular-loading-bar',
    'ui.router',
    'oc.lazyLoad',
    'ui.bootstrap',
    'ngResource'
  ])

one_line_array = ['ngAnimate', 'angular-loading-bar']

There is no way other than taking the JS code and the CoffeeScript code. 除了获取JS代码 CoffeeScript代码外,别无选择。

If you really want this, you'll need to either modify the CoffeeScript source, or write your own tool which analyses the CofeeScript source and modifies the JavaScript output accordingly. 如果确实要这样做,则需要修改CoffeeScript源代码,或者编写自己的工具来分析CofeeScript源代码并相应地修改JavaScript输出。 If you print the tokens with coffee --tokens , you get: 如果使用coffee --tokens打印令牌,则会得到:

[IDENTIFIER angular] [= =] [{ {] [IDENTIFIER module] [: :] [-> ->] [INDENT 2] [OUTDENT 2] [} }] [TERMINATOR \\n] [IDENTIFIER angular] [. [IDENTIFIER angular] [= =] [{{] [IDENTIFIER模块] [::] [->->] [INDENT 2] [OUTDENT 2] [}}]] [TERMINATOR \\ n] [IDENTIFIER angular] [ .] [IDENTIFIER module] [CALL_START (] [STRING 'app'] [, ,] [[ [] [INDENT 4] [STRING 'ngAnimate'] [, ,] [TERMINATOR \\n] [STRING 'angular-loading-bar'] [, ,] [TERMINATOR \\n] [STRING 'ui.router'] [, ,] [TERMINATOR \\n] [STRING 'oc.lazyLoad'] [, ,] [TERMINATOR \\n] [STRING 'ui.bootstrap'] [, ,] [TERMINATOR \\n] [STRING 'ngResource'] [OUTDENT 4] [] ]] [CALL_END )] [TERMINATOR \\n] 。] [IDENTIFIER模块] [CALL_START(] [STRING'app'] [,,] [[[] [INDENT 4] [STRING'ngAnimate'] [,,] [TERMINATOR \\ n] [STRING'角度加载- bar'] [,,] [TERMINATOR \\ n] [STRING'ui.router'] [,,] [TERMINATOR \\ n] [STRING'oc.lazyLoad'] [,,] [TERMINATOR \\ n] [STRING'ui .bootstrap'] [,,] [终止符\\ n] [STRING'ngResource'] [输出4] []]] [CALL_END)] [终止符\\ n]

Which does store the newlines as [TERMINATOR \\n] . 哪个确实将换行符存储为[TERMINATOR \\n] Writing (and debugging!) a tool which does this is not necessarily easy though, and beyond the scope of a Stack Overflow answer ;-) 编写(和调试!)执行此操作的工具虽然不一定很容易,但超出了Stack Overflow答案的范围;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM