From f84eb9ed47e81c3ccc4ae72dc787f01bf162daca Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 14 May 2010 21:50:17 -0400 Subject: [PATCH] fixing heredoc indentation from herecomment tweaks --- lib/lexer.js | 9 +++++---- lib/nodes.js | 4 ++-- src/lexer.coffee | 5 +++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index dd207ea3..37438785 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -422,10 +422,11 @@ // Sanitize a heredoc or herecomment by escaping internal double quotes and // erasing all external indentation on the left-hand side. Lexer.prototype.sanitize_heredoc = function sanitize_heredoc(doc, options) { - var indent, match; + var attempt, indent, match; while (match = HEREDOC_INDENT.exec(doc)) { - if (!indent || match[1].length < indent.length) { - indent = match[1]; + attempt = match[2] || match[3]; + if (!indent || attempt.length < indent.length) { + indent = attempt; } } doc = doc.replace(new RegExp("^" + indent, 'gm'), ''); @@ -675,7 +676,7 @@ STRING_NEWLINES = /\n[ \t]*/g; COMMENT_CLEANER = /(^[ \t]*#|\n[ \t]*$)/mg; NO_NEWLINE = /^([+\*&|\/\-%=<>:!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)$/; - HEREDOC_INDENT = /\n+([ \t]*)/g; + HEREDOC_INDENT = /(\n+([ \t]*)|^([ \t]+))/g; // Tokens which a regular expression will never immediately follow, but which // a division operator might. // See: http://www.mozilla.org/js/language/js20-2002-04/rationale/syntax.html#regular-expressions diff --git a/lib/nodes.js b/lib/nodes.js index 3fae39d0..6d16a8cc 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1855,10 +1855,10 @@ // Correctly set up a prototype chain for inheritance, including a reference // to the superclass for `super()` calls. See: // [goog.inherits](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html#line1206). - __extends: " function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.__superClass__ = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n}", + __extends: "function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.__superClass__ = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n }", // Bind a function to a calling context, optionally including curried arguments. // See [Underscore's implementation](http://jashkenas.github.com/coffee-script/documentation/docs/underscore.html#section-47). - __bind: " function(func, obj, args) {\n return function() {\n return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);\n };\n}", + __bind: "function(func, obj, args) {\n return function() {\n return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);\n };\n }", // Shortcuts to speed up the lookup time for native functions. __hasProp: 'Object.prototype.hasOwnProperty', __slice: 'Array.prototype.slice' diff --git a/src/lexer.coffee b/src/lexer.coffee index 286e269d..9f015f0a 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -295,7 +295,8 @@ exports.Lexer: class Lexer # erasing all external indentation on the left-hand side. sanitize_heredoc: (doc, options) -> while match: HEREDOC_INDENT.exec doc - indent: match[1] if not indent or match[1].length < indent.length + attempt: match[2] or match[3] + indent: attempt if not indent or attempt.length < indent.length doc: doc.replace(new RegExp("^" +indent, 'gm'), '') return doc if options.herecomment doc.replace(MULTILINER, "\\n") @@ -503,7 +504,7 @@ MULTILINER : /\n/g STRING_NEWLINES : /\n[ \t]*/g COMMENT_CLEANER : /(^[ \t]*#|\n[ \t]*$)/mg NO_NEWLINE : /^([+\*&|\/\-%=<>:!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)$/ -HEREDOC_INDENT : /\n+([ \t]*)/g +HEREDOC_INDENT : /(\n+([ \t]*)|^([ \t]+))/g # Tokens which a regular expression will never immediately follow, but which # a division operator might.