Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/coffeescript/coffeescript.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions lib/coffeescript/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 60 additions & 1 deletion lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/coffeescript.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
options.bare = yes
break

fragments = parser.parse(tokens).compileToFragments options
nodes = parser.parse tokens
# If all that was requested was a POJO representation of the nodes, e.g.
# the abstract syntax tree (AST), we can stop now and just return that.
if options.ast
return nodes.toJSON()

fragments = nodes.compileToFragments options

currentLine = 0
currentLine += 1 if options.header
Expand Down
7 changes: 6 additions & 1 deletion src/command.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ BANNER = '''

# The list of all the valid option flags that `coffee` knows how to handle.
SWITCHES = [
[ '--ast', 'generate an abstract syntax tree of nodes']
['-b', '--bare', 'compile without a top-level function wrapper']
['-c', '--compile', 'compile to JavaScript and save as .js files']
['-e', '--eval', 'pass a string from the command line as input']
['-h', '--help', 'display this help message']
['-i', '--interactive', 'run an interactive CoffeeScript REPL']
['-j', '--join [FILE]', 'concatenate the source CoffeeScript before compiling']
['-l', '--literate', 'treat stdio as literate style coffeescript']
['-m', '--map', 'generate source map and save as .js.map files']
['-M', '--inline-map', 'generate source map and include it directly in output']
['-n', '--nodes', 'print out the parse tree that the parser produces']
Expand All @@ -47,7 +49,6 @@ SWITCHES = [
['-p', '--print', 'print out the compiled JavaScript']
['-r', '--require [MODULE*]', 'require the given module before eval or REPL']
['-s', '--stdio', 'listen for and compile scripts over stdio']
['-l', '--literate', 'treat stdio as literate style coffeescript']
['-t', '--transpile', 'pipe generated JavaScript through Babel']
[ '--tokens', 'print out the tokens that the lexer/rewriter produce']
['-v', '--version', 'display the version number']
Expand Down Expand Up @@ -207,6 +208,9 @@ compileScript = (file, input, base = null) ->
printTokens CoffeeScript.tokens task.input, task.options
else if opts.nodes
printLine CoffeeScript.nodes(task.input, task.options).toString().trim()
else if opts.ast
compiled = CoffeeScript.compile task.input, task.options
printLine JSON.stringify(compiled, null, 2)
else if opts.run
CoffeeScript.register()
CoffeeScript.eval opts.prelude, task.options if opts.prelude
Expand Down Expand Up @@ -499,6 +503,7 @@ compileOptions = (filename, base) ->
transpile: opts.transpile
sourceMap: opts.map
inlineMap: opts['inline-map']
ast: opts.ast

if filename
if base
Expand Down
41 changes: 40 additions & 1 deletion src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,53 @@ exports.Base = class Base
lastNode: (list) ->
if list.length is 0 then null else list[list.length - 1]

# `toString` representation of the node, for inspecting the parse tree.
# Debugging representation of the node, for inspecting the parse tree.
# This is what `coffee --nodes` prints out.
toString: (idt = '', name = @constructor.name) ->
tree = '\n' + idt + name
tree += '?' if @soak
@eachChild (node) -> tree += node.toString idt + TAB
tree

# Plain JavaScript object representation of the node, that can be serialized
# as JSON. This is used for generating an abstract syntax tree (AST).
# This is what the `ast` option in the Node API returns.
toJSON: ->
# We try to follow the [Babel AST spec](https://github.com/babel/babel/blob/master/packages/babylon/ast/spec.md)
# as closely as possible, for improved interoperability with other tools.
obj =
type: @constructor.name
# Convert `locationData` to Babel’s style.
loc:
start:
line: @locationData.first_line
column: @locationData.first_column
end:
line: @locationData.last_line
column: @locationData.last_column

# Add serializable properties to the output. Properties that aren’t
# automatically serializable (because they’re already a primitive type)
# should be handled on a case-by-case basis in child node classes’ own
# `toJSON` methods.
for property, value of this
continue if property in ['locationData', 'children']
continue if value is undefined # Don’t skip `null` or `false` values.
if typeof value is 'boolean' or typeof value is 'number' or typeof value is 'string'
obj[property] = value

# Work our way down the tree. This is like `eachChild`, except that we
# preserve the child node name, and arrays.
for attr in @children when @[attr]
if Array.isArray(@[attr])
obj[attr] = []
for child in flatten [@[attr]]
obj[attr].push child.unwrap().toJSON()
else
obj[attr] = @[attr].unwrap().toJSON()

obj

# Passes each child to a function, breaking when the function returns `false`.
eachChild: (func) ->
return this unless @children
Expand Down
3 changes: 2 additions & 1 deletion test/argument_parsing.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ Usage: coffee [options] path/to/script.coffee [args]

If called without options, `coffee` will run your script.

--ast generate an abstract syntax tree of nodes
-b, --bare compile without a top-level function wrapper
-c, --compile compile to JavaScript and save as .js files
-e, --eval pass a string from the command line as input
-h, --help display this help message
-i, --interactive run an interactive CoffeeScript REPL
-j, --join concatenate the source CoffeeScript before compiling
-l, --literate treat stdio as literate style coffeescript
-m, --map generate source map and save as .js.map files
-M, --inline-map generate source map and include it directly in output
-n, --nodes print out the parse tree that the parser produces
Expand All @@ -134,7 +136,6 @@ If called without options, `coffee` will run your script.
-p, --print print out the compiled JavaScript
-r, --require require the given module before eval or REPL
-s, --stdio listen for and compile scripts over stdio
-l, --literate treat stdio as literate style coffeescript
-t, --transpile pipe generated JavaScript through Babel
--tokens print out the tokens that the lexer/rewriter produce
-v, --version display the version number
Expand Down