From 863de88671d82b3c3cecd4026ce0db8d4ea29b80 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 8 Feb 2010 23:45:46 -0500 Subject: [PATCH] CoffeeScript-in-CoffeeScript can compile object[indexes] --- lib/coffee_script/nodes.js | 12 +++++++++++- src/nodes.coffee | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index 947450e1..495f0dcb 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -1,5 +1,5 @@ (function(){ - var AccessorNode, CallNode, CommentNode, Expressions, ExtendsNode, LiteralNode, Node, ReturnNode, TAB, TRAILING_WHITESPACE, ValueNode, any, compact, del, dup, flatten, inherit, merge, statement; + var AccessorNode, CallNode, CommentNode, Expressions, ExtendsNode, IndexNode, LiteralNode, Node, ReturnNode, TAB, TRAILING_WHITESPACE, ValueNode, any, compact, del, dup, flatten, inherit, merge, statement; var __hasProp = Object.prototype.hasOwnProperty; process.mixin(require('./scope')); // The abstract base class for all CoffeeScript nodes. @@ -734,4 +734,14 @@ return '.' + (this.prototype ? 'prototype.' : '') + this.name.compile(o); } })); + // An indexed accessor into a part of an array or object. + IndexNode = (exports.IndexNode = inherit(Node, { + constructor: function constructor(index) { + this.children = [(this.index = index)]; + return this; + }, + compile_node: function compile_node(o) { + return '[' + this.index.compile(o) + ']'; + } + })); })(); \ No newline at end of file diff --git a/src/nodes.coffee b/src/nodes.coffee index 8ddaf674..4b5034b0 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -460,6 +460,19 @@ AccessorNode: exports.AccessorNode: inherit Node, { } +# An indexed accessor into a part of an array or object. +IndexNode: exports.IndexNode: inherit Node, { + + constructor: (index) -> + @children: [@index: index] + this + + compile_node: (o) -> + '[' + @index.compile(o) + ']' + +} + +