Skip to content

Instantly share code, notes, and snippets.

@datapimp
Created May 26, 2012 08:20
Show Gist options
  • Select an option

  • Save datapimp/2792913 to your computer and use it in GitHub Desktop.

Select an option

Save datapimp/2792913 to your computer and use it in GitHub Desktop.
# So, I have some class defintiions
# written in coffeescript. I am writing
# an app in coffeescript, which reads these files
# on disk. I have access to the coffeescript compiler
# from inside of my app.
class SomeClass
doSomething: ()->
1 + 1 = 2
#
# I want to be able to read the contents of
# this file on disk, and then request the body of any
# method definition by name.
#
# in the coffeescript REPL, SomeClass.doSomething.toString()
# returns the compiled javascript, which is expected. but what
# I want is to be able to get the method definition as it was
# written in coffeescript.
#
# I am able to just parse the file, find the line where a method
# is defined, and then work off of indentation, but this doesn't
# feel correct to me.
@michaelficarra
Copy link

Parse the CoffeeScript, get an AST, walk the tree, find your methods, write a tiny little code generator that takes a coffee AST and outputs coffeescript code, map it over the methods you found earlier.

@datapimp
Copy link
Author

datapimp commented May 26, 2012 via email

@michaelficarra
Copy link

Nothing should be lost. I don't see why it would. Only concrete syntax (and comments), but you can design your rules to format the output however it is preferred.

@datapimp
Copy link
Author

datapimp commented May 27, 2012 via email

@michaelficarra
Copy link

I would take advantage of the CoffeeScript compiler's built-in parser. You're setting yourself up for failure if you try to do naive string matching. Do it the right way the first time and you'll end up saving yourself time in the long run. It's not even very difficult.

@datapimp
Copy link
Author

datapimp commented May 27, 2012 via email

@michaelficarra
Copy link

You're also going to introduce (inconsequential) whitespace differences. What you really need is an AST that preserves the concrete syntax that caused the parser to create it. For that, you're either going to have to wait for my kickstarter to be done or fork CS and do it yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment