Merge branch 'master' of github.com:jashkenas/coffeescript into 2

# Conflicts:
#	lib/coffee-script/rewriter.js
This commit is contained in:
Geoffrey Booth 2017-05-13 21:23:05 -07:00
commit 7134856df2
3 changed files with 42 additions and 13 deletions

View File

@ -168,7 +168,7 @@
stack = []; stack = [];
start = null; start = null;
return this.scanTokens(function(token, i, tokens) { 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; [tag] = token;
[prevTag] = prevToken = i > 0 ? tokens[i - 1] : []; [prevTag] = prevToken = i > 0 ? tokens[i - 1] : [];
[nextTag] = i < tokens.length - 1 ? tokens[i + 1] : []; [nextTag] = i < tokens.length - 1 ? tokens[i + 1] : [];
@ -179,17 +179,24 @@
forward = function(n) { forward = function(n) {
return i - startIdx + 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() { inImplicit = function() {
var ref, ref1; return isImplicit(stackTop());
return (ref = stackTop()) != null ? (ref1 = ref[2]) != null ? ref1.ours : void 0 : void 0;
}; };
inImplicitCall = function() { inImplicitCall = function() {
var ref; return isImplicitCall(stackTop());
return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '(';
}; };
inImplicitObject = function() { inImplicitObject = function() {
var ref; return isImplicitObject(stackTop());
return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '{';
}; };
inImplicitControl = function() { inImplicitControl = function() {
var ref; var ref;
@ -310,8 +317,13 @@
startImplicitObject(s, !!startsLine); startImplicitObject(s, !!startsLine);
return forward(2); return forward(2);
} }
if (inImplicitObject() && indexOf.call(LINEBREAKS, tag) >= 0) { if (indexOf.call(LINEBREAKS, tag) >= 0) {
stackTop()[2].sameLine = false; for (k = stack.length - 1; k >= 0; k += -1) {
stackItem = stack[k];
if (isImplicitObject(stackItem)) {
stackItem[2].sameLine = false;
}
}
} }
newLine = prevTag === 'OUTDENT' || prevToken.newLine; newLine = prevTag === 'OUTDENT' || prevToken.newLine;
if (indexOf.call(IMPLICIT_END, tag) >= 0 || indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) { if (indexOf.call(IMPLICIT_END, tag) >= 0 || indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) {

View File

@ -149,9 +149,12 @@ exports.Rewriter = class Rewriter
forward = (n) -> i - startIdx + n forward = (n) -> i - startIdx + n
# Helper functions # Helper functions
inImplicit = -> stackTop()?[2]?.ours isImplicit = (stackItem) -> stackItem?[2]?.ours
inImplicitCall = -> inImplicit() and stackTop()?[0] is '(' isImplicitObject = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '{'
inImplicitObject = -> inImplicit() and stackTop()?[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 # Unclosed control statement inside implicit parens (like
# class declaration or if-conditionals) # class declaration or if-conditionals)
inImplicitControl = -> inImplicit() and stackTop()?[0] is 'CONTROL' inImplicitControl = -> inImplicit() and stackTop()?[0] is 'CONTROL'
@ -298,7 +301,10 @@ exports.Rewriter = class Rewriter
# .g b # .g b
# .h a # .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 newLine = prevTag is 'OUTDENT' or prevToken.newLine
if tag in IMPLICIT_END or tag in CALL_CLOSERS and newLine if tag in IMPLICIT_END or tag in CALL_CLOSERS and newLine

View File

@ -128,6 +128,9 @@ test "indented heredoc", ->
# * single line arguments # * single line arguments
# * inline function literal # * inline function literal
# * inline object literal # * inline object literal
#
# * chaining inside
# * implicit object literal
test "chaining after outdent", -> test "chaining after outdent", ->
id = (x) -> x id = (x) -> x
@ -221,6 +224,14 @@ test "chaining should work within spilling ternary", ->
.a .a
eq 3, result.h 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 # Nested blocks caused by paren unwrapping
test "#1492: Nested blocks don't cause double semicolons", -> test "#1492: Nested blocks don't cause double semicolons", ->
js = CoffeeScript.compile '(0;0)' js = CoffeeScript.compile '(0;0)'