1

假设我在咖啡脚本文件中有这个功能

test = (arr, fn) ->
    console.log item for item in arr
    fn()

我是这样称呼它的

test [1, 2, 3, 4, 5], ->
    console.log "start"
    # function body
    console.log "finish" 

一切正常,直到数组变得太长,我想把它分成几行。像这样

test ["first element here",
      "second element here",
      "third element here",
      "fourth element here",
      "fifth element here"], ->
    console.log "start"
    # function body
    console.log "finish"

这是有效的,因为咖啡脚本编译器完全按照我的预期编译它,但 IDEA 说行中有Unexpected 缩进console.log "start"我按Ctrl+Alt+L,IDEA 给了我这个

test ["first element here",
      "second element here",
      "third element here",
      "fourth element here",
      "fifth element here"], ->
console.log "start"
# function body
console.log "finish"

这是错误的,因为在这种情况下,空函数作为参数传递。这是一个错误还是我可以自己修复它?

4

3 回答 3

1

JetBrains 的支持人员告诉我这是一个错误,所以我在这里打开了一个问题

于 2013-05-11T13:24:53.577 回答
0

也许您必须将函数体缩进一点才能使其工作:

test ["first element here",
      "second element here",
      "third element here",
      "fourth element here",
      "fifth element here"], ->
       console.log "start"
       # function body
       console.log "finish"
#      ^ This column is indented two from the start of the array
#    ^ Array starts here

至少这在 IntelliJ 中对我有用。

于 2013-05-11T11:52:14.217 回答
0

正如 user1737909提到的,可能是 CoffeeScript IDEA 插件上的一个错误。

但是,如果您现在想解决这个问题,我建议您只为数组使用一个变量并继续:

arr = [
  "first element here"
  "second element here"
  "third element here"
  "fourth element here"
  "fifth element here"
]

test arr, ->
  console.log "start"
  # function body
  console.log "finish"
于 2013-05-11T13:01:34.493 回答