mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Fixes #1903
This commit is contained in:
parent
ad39150565
commit
af0ee70ea3
4 changed files with 31 additions and 5 deletions
|
@ -102,16 +102,17 @@
|
|||
};
|
||||
|
||||
Rewriter.prototype.addImplicitBraces = function() {
|
||||
var action, condition, stack, start, startIndent;
|
||||
var action, condition, stack, start, startIndent, startsLine;
|
||||
stack = [];
|
||||
start = null;
|
||||
startsLine = null;
|
||||
startIndent = 0;
|
||||
condition = function(token, i) {
|
||||
var one, tag, three, two, _ref, _ref2;
|
||||
_ref = this.tokens.slice(i + 1, (i + 3) + 1 || 9e9), one = _ref[0], two = _ref[1], three = _ref[2];
|
||||
if ('HERECOMMENT' === (one != null ? one[0] : void 0)) return false;
|
||||
tag = token[0];
|
||||
return ((tag === 'TERMINATOR' || tag === 'OUTDENT') && !((two != null ? two[0] : void 0) === ':' || (one != null ? one[0] : void 0) === '@' && (three != null ? three[0] : void 0) === ':')) || (tag === ',' && one && ((_ref2 = one[0]) !== 'IDENTIFIER' && _ref2 !== 'NUMBER' && _ref2 !== 'STRING' && _ref2 !== '@' && _ref2 !== 'TERMINATOR' && _ref2 !== 'OUTDENT'));
|
||||
return ((tag === 'TERMINATOR' || tag === 'OUTDENT') && ((!startsLine && this.tag(i - 1) !== ',') || !((two != null ? two[0] : void 0) === ':' || (one != null ? one[0] : void 0) === '@' && (three != null ? three[0] : void 0) === ':'))) || (tag === ',' && one && ((_ref2 = one[0]) !== 'IDENTIFIER' && _ref2 !== 'NUMBER' && _ref2 !== 'STRING' && _ref2 !== '@' && _ref2 !== 'TERMINATOR' && _ref2 !== 'OUTDENT'));
|
||||
};
|
||||
action = function(token, i) {
|
||||
var tok;
|
||||
|
@ -120,7 +121,7 @@
|
|||
return this.tokens.splice(i, 0, tok);
|
||||
};
|
||||
return this.scanTokens(function(token, i, tokens) {
|
||||
var ago, idx, tag, tok, value, _ref, _ref2;
|
||||
var ago, idx, prevTag, tag, tok, value, _ref, _ref2;
|
||||
if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) {
|
||||
stack.push([(tag === 'INDENT' && this.tag(i - 1) === '{' ? '{' : tag), i]);
|
||||
return 1;
|
||||
|
@ -137,6 +138,8 @@
|
|||
while (this.tag(idx - 2) === 'HERECOMMENT') {
|
||||
idx -= 2;
|
||||
}
|
||||
prevTag = this.tag(idx - 1);
|
||||
startsLine = !prevTag || (__indexOf.call(LINEBREAKS, prevTag) >= 0);
|
||||
value = new String('{');
|
||||
value.generated = true;
|
||||
tok = ['{', value, token[2]];
|
||||
|
|
|
@ -93,13 +93,15 @@ class exports.Rewriter
|
|||
addImplicitBraces: ->
|
||||
stack = []
|
||||
start = null
|
||||
startsLine = null
|
||||
startIndent = 0
|
||||
condition = (token, i) ->
|
||||
[one, two, three] = @tokens[i + 1 .. i + 3]
|
||||
return false if 'HERECOMMENT' is one?[0]
|
||||
[tag] = token
|
||||
(tag in ['TERMINATOR', 'OUTDENT'] and
|
||||
not (two?[0] is ':' or one?[0] is '@' and three?[0] is ':')) or
|
||||
((!startsLine and @tag(i - 1) isnt ',') or
|
||||
not (two?[0] is ':' or one?[0] is '@' and three?[0] is ':'))) or
|
||||
(tag is ',' and one and
|
||||
one[0] not in ['IDENTIFIER', 'NUMBER', 'STRING', '@', 'TERMINATOR', 'OUTDENT'])
|
||||
action = (token, i) ->
|
||||
|
@ -118,6 +120,8 @@ class exports.Rewriter
|
|||
stack.push ['{']
|
||||
idx = if ago is '@' then i - 2 else i - 1
|
||||
idx -= 2 while @tag(idx - 2) is 'HERECOMMENT'
|
||||
prevTag = @tag(idx - 1)
|
||||
startsLine = not prevTag or (prevTag in LINEBREAKS)
|
||||
value = new String('{')
|
||||
value.generated = yes
|
||||
tok = ['{', value, token[2]]
|
||||
|
|
|
@ -272,6 +272,25 @@ test "nothing classes", ->
|
|||
|
||||
c = class
|
||||
ok c instanceof Function
|
||||
|
||||
|
||||
test "classes with static-level implicit objects", ->
|
||||
|
||||
class A
|
||||
@static = one: 1
|
||||
two: 2
|
||||
|
||||
class B
|
||||
@static = one: 1,
|
||||
two: 2
|
||||
|
||||
eq A.static.one, 1
|
||||
eq A.static.two, undefined
|
||||
eq (new A).two, 2
|
||||
|
||||
eq B.static.one, 1
|
||||
eq B.static.two, 2
|
||||
eq (new B).two, undefined
|
||||
|
||||
|
||||
test "classes with value'd constructors", ->
|
||||
|
|
|
@ -157,7 +157,7 @@ test "invoking functions with implicit object literals", ->
|
|||
b:1
|
||||
eq undefined, result
|
||||
|
||||
result = getA b:1
|
||||
result = getA b:1,
|
||||
a:43
|
||||
eq 43, result
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue