2

I'm creating a gruntfile.coffee for a project with a series of tasks. The format of some of these tasks is conveniently:

"path/to/dest" : ["path/to/src1", "path/to/src2"]

However, I'd like to save the directory names in variables at the top of the file, so a snippet of the gruntfile might look like this:

BUILD_DIR = "build"

...

files:
  "#{BUILD_DIR}/src/js/production.js": ["Thing.js"]

...

However, I keep getting the following error:

Error: In /Users/me/path/to/project/gruntfile.coffee, Parse error on line X: Unexpected '{'

Is there something wrong with my syntax? I've seen this brought up in their issues a lot and their documentation explicity states that they support string interpolation in object keys.

Abe Fehr
  • 729
  • 9
  • 23

2 Answers2

0

The thing wrong with your syntax seems to be that you're using keys in an array literal, not that you're using interpolated string keys in object literal. Use

… {
  …
  files: {
    "#{BUILD_DIR}/src/js/production.js": ["Thing.js"]
  }
  …
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • One of the instances in my file is an array and the other is an object so it doesn't make a difference. I updated the question to show an object literal. Did you try this? Is there any particular reason you using the curly braces in coffeescript? – Abe Fehr Jun 27 '15 at 14:59
  • @AbeFehr: Yes, I did try this, it does work in the online coffeescript repl. Are you sure that you've got the latest version? If so, there might be a different mistake that is not related to the string interpolation, it would help if you could post the full code that produces this error. – Bergi Jun 27 '15 at 16:02
  • I feel like this is a coffeescript issue. I just went to the repl and typed the following two lines: x = 5 and then { "prop#{x}": 5 }, which failed with the same error as mine. The line: { "prop5": 5 } succeeded though. – Abe Fehr Jun 27 '15 at 19:05
  • @AbeFehr: You'll probably have to use `({"prop#{x}": 5})` to make sure it's an expression - the old [block vs object literal problem](http://stackoverflow.com/q/26347326/1048572) – Bergi Jun 27 '15 at 19:08
0

Your code works in CoffeeScript 1.9.3. From the changelog:

  • Bugfix for interpolation in the first key of an object literal in an implicit call.
lydell
  • 1,147
  • 12
  • 16