mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Fixing Issue #552, Indentation bug with chained calls with nested object literals...
This commit is contained in:
parent
071b527b66
commit
f3caa9292f
3 changed files with 61 additions and 21 deletions
32
lib/lexer.js
32
lib/lexer.js
|
@ -242,6 +242,7 @@
|
|||
if (noNewlines) {
|
||||
return this.suppressNewlines();
|
||||
}
|
||||
this.outdebt = 0;
|
||||
diff = size - this.indent;
|
||||
this.token('INDENT', diff);
|
||||
this.indents.push(diff);
|
||||
|
@ -251,19 +252,28 @@
|
|||
this.indent = size;
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.outdentToken = function(moveOut, noNewlines) {
|
||||
var lastIndent;
|
||||
if (moveOut > -this.outdebt) {
|
||||
while (moveOut > 0 && this.indents.length) {
|
||||
lastIndent = this.indents.pop();
|
||||
this.token('OUTDENT', lastIndent);
|
||||
moveOut -= lastIndent;
|
||||
Lexer.prototype.outdentToken = function(moveOut, noNewlines, close) {
|
||||
var dent, len;
|
||||
while (moveOut > 0) {
|
||||
len = this.indents.length - 1;
|
||||
if (this.indents[len] === undefined) {
|
||||
moveOut = 0;
|
||||
} else if (this.indents[len] === this.outdebt) {
|
||||
moveOut -= this.outdebt;
|
||||
this.outdebt = 0;
|
||||
} else if (this.indents[len] < this.outdebt) {
|
||||
this.outdebt -= this.indents[len];
|
||||
moveOut -= this.indents[len];
|
||||
} else {
|
||||
dent = this.indents.pop();
|
||||
dent -= this.outdebt;
|
||||
moveOut -= dent;
|
||||
this.outdebt = 0;
|
||||
this.token('OUTDENT', dent);
|
||||
}
|
||||
} else {
|
||||
this.outdebt += moveOut;
|
||||
}
|
||||
if (!(noNewlines)) {
|
||||
this.outdebt = moveOut;
|
||||
if (dent) {
|
||||
this.outdebt -= moveOut;
|
||||
}
|
||||
if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
|
||||
this.token('TERMINATOR', "\n");
|
||||
|
|
|
@ -205,6 +205,7 @@ exports.Lexer = class Lexer
|
|||
return @newlineToken indent
|
||||
else if size > @indent
|
||||
return @suppressNewlines() if noNewlines
|
||||
@outdebt = 0
|
||||
diff = size - @indent
|
||||
@token 'INDENT', diff
|
||||
@indents.push diff
|
||||
|
@ -215,15 +216,24 @@ exports.Lexer = class Lexer
|
|||
|
||||
# Record an outdent token or multiple tokens, if we happen to be moving back
|
||||
# inwards past several recorded indents.
|
||||
outdentToken: (moveOut, noNewlines) ->
|
||||
if moveOut > -@outdebt
|
||||
while moveOut > 0 and @indents.length
|
||||
lastIndent = @indents.pop()
|
||||
@token 'OUTDENT', lastIndent
|
||||
moveOut -= lastIndent
|
||||
else
|
||||
@outdebt += moveOut
|
||||
@outdebt = moveOut unless noNewlines
|
||||
outdentToken: (moveOut, noNewlines, close) ->
|
||||
while moveOut > 0
|
||||
len = @indents.length - 1
|
||||
if @indents[len] is undefined
|
||||
moveOut = 0
|
||||
else if @indents[len] is @outdebt
|
||||
moveOut -= @outdebt
|
||||
@outdebt = 0
|
||||
else if @indents[len] < @outdebt
|
||||
@outdebt -= @indents[len]
|
||||
moveOut -= @indents[len]
|
||||
else
|
||||
dent = @indents.pop()
|
||||
dent -= @outdebt
|
||||
moveOut -= dent
|
||||
@outdebt = 0
|
||||
@token 'OUTDENT', dent
|
||||
@outdebt -= moveOut if dent
|
||||
@token 'TERMINATOR', "\n" unless @tag() is 'TERMINATOR' or noNewlines
|
||||
true
|
||||
|
||||
|
|
|
@ -32,4 +32,24 @@ func = ->
|
|||
}
|
||||
obj.key - 5
|
||||
|
||||
ok func() is 5
|
||||
ok func() is 5
|
||||
|
||||
|
||||
# Ensure that chained calls with indented implicit object literals below are
|
||||
# alright.
|
||||
result = null
|
||||
obj =
|
||||
method: (val) -> this
|
||||
second: (hash) -> result = hash.three
|
||||
|
||||
|
||||
obj
|
||||
.method(
|
||||
101
|
||||
).second(
|
||||
one:
|
||||
two: 2
|
||||
three: 3
|
||||
)
|
||||
|
||||
ok result is 3
|
||||
|
|
Loading…
Add table
Reference in a new issue