mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Merge branch 'master' of github.com:jashkenas/coffeescript into 2
# Conflicts: # lib/coffee-script/rewriter.js
This commit is contained in:
commit
7134856df2
3 changed files with 42 additions and 13 deletions
|
@ -168,7 +168,7 @@
|
|||
stack = [];
|
||||
start = null;
|
||||
return this.scanTokens(function(token, i, tokens) {
|
||||
var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, newLine, nextTag, offset, prevTag, prevToken, ref, ref1, ref2, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag;
|
||||
var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, isImplicit, isImplicitCall, isImplicitObject, k, newLine, nextTag, offset, prevTag, prevToken, ref, ref1, ref2, s, sameLine, stackIdx, stackItem, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag;
|
||||
[tag] = token;
|
||||
[prevTag] = prevToken = i > 0 ? tokens[i - 1] : [];
|
||||
[nextTag] = i < tokens.length - 1 ? tokens[i + 1] : [];
|
||||
|
@ -179,17 +179,24 @@
|
|||
forward = function(n) {
|
||||
return i - startIdx + n;
|
||||
};
|
||||
isImplicit = function(stackItem) {
|
||||
var ref;
|
||||
return stackItem != null ? (ref = stackItem[2]) != null ? ref.ours : void 0 : void 0;
|
||||
};
|
||||
isImplicitObject = function(stackItem) {
|
||||
return isImplicit(stackItem) && (stackItem != null ? stackItem[0] : void 0) === '{';
|
||||
};
|
||||
isImplicitCall = function(stackItem) {
|
||||
return isImplicit(stackItem) && (stackItem != null ? stackItem[0] : void 0) === '(';
|
||||
};
|
||||
inImplicit = function() {
|
||||
var ref, ref1;
|
||||
return (ref = stackTop()) != null ? (ref1 = ref[2]) != null ? ref1.ours : void 0 : void 0;
|
||||
return isImplicit(stackTop());
|
||||
};
|
||||
inImplicitCall = function() {
|
||||
var ref;
|
||||
return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '(';
|
||||
return isImplicitCall(stackTop());
|
||||
};
|
||||
inImplicitObject = function() {
|
||||
var ref;
|
||||
return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '{';
|
||||
return isImplicitObject(stackTop());
|
||||
};
|
||||
inImplicitControl = function() {
|
||||
var ref;
|
||||
|
@ -310,8 +317,13 @@
|
|||
startImplicitObject(s, !!startsLine);
|
||||
return forward(2);
|
||||
}
|
||||
if (inImplicitObject() && indexOf.call(LINEBREAKS, tag) >= 0) {
|
||||
stackTop()[2].sameLine = false;
|
||||
if (indexOf.call(LINEBREAKS, tag) >= 0) {
|
||||
for (k = stack.length - 1; k >= 0; k += -1) {
|
||||
stackItem = stack[k];
|
||||
if (isImplicitObject(stackItem)) {
|
||||
stackItem[2].sameLine = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
newLine = prevTag === 'OUTDENT' || prevToken.newLine;
|
||||
if (indexOf.call(IMPLICIT_END, tag) >= 0 || indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) {
|
||||
|
|
|
@ -149,9 +149,12 @@ exports.Rewriter = class Rewriter
|
|||
forward = (n) -> i - startIdx + n
|
||||
|
||||
# Helper functions
|
||||
inImplicit = -> stackTop()?[2]?.ours
|
||||
inImplicitCall = -> inImplicit() and stackTop()?[0] is '('
|
||||
inImplicitObject = -> inImplicit() and stackTop()?[0] is '{'
|
||||
isImplicit = (stackItem) -> stackItem?[2]?.ours
|
||||
isImplicitObject = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '{'
|
||||
isImplicitCall = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '('
|
||||
inImplicit = -> isImplicit stackTop()
|
||||
inImplicitCall = -> isImplicitCall stackTop()
|
||||
inImplicitObject = -> isImplicitObject stackTop()
|
||||
# Unclosed control statement inside implicit parens (like
|
||||
# class declaration or if-conditionals)
|
||||
inImplicitControl = -> inImplicit() and stackTop()?[0] is 'CONTROL'
|
||||
|
@ -298,7 +301,10 @@ exports.Rewriter = class Rewriter
|
|||
# .g b
|
||||
# .h a
|
||||
|
||||
stackTop()[2].sameLine = no if inImplicitObject() and tag in LINEBREAKS
|
||||
# Mark all enclosing objects as not sameLine
|
||||
if tag in LINEBREAKS
|
||||
for stackItem in stack by -1 when isImplicitObject stackItem
|
||||
stackItem[2].sameLine = no
|
||||
|
||||
newLine = prevTag is 'OUTDENT' or prevToken.newLine
|
||||
if tag in IMPLICIT_END or tag in CALL_CLOSERS and newLine
|
||||
|
|
|
@ -128,6 +128,9 @@ test "indented heredoc", ->
|
|||
# * single line arguments
|
||||
# * inline function literal
|
||||
# * inline object literal
|
||||
#
|
||||
# * chaining inside
|
||||
# * implicit object literal
|
||||
|
||||
test "chaining after outdent", ->
|
||||
id = (x) -> x
|
||||
|
@ -221,6 +224,14 @@ test "chaining should work within spilling ternary", ->
|
|||
.a
|
||||
eq 3, result.h
|
||||
|
||||
test "method call chaining inside objects", ->
|
||||
f = (x) -> c: 42
|
||||
result =
|
||||
a: f 1
|
||||
b: f a: 1
|
||||
.c
|
||||
eq 42, result.b
|
||||
|
||||
# Nested blocks caused by paren unwrapping
|
||||
test "#1492: Nested blocks don't cause double semicolons", ->
|
||||
js = CoffeeScript.compile '(0;0)'
|
||||
|
|
Loading…
Add table
Reference in a new issue