mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
Allowing classes to be better decorated with implicit calls. Issue #548
This commit is contained in:
@@ -175,8 +175,9 @@
|
||||
return size;
|
||||
}, this);
|
||||
return this.scanTokens(__bind(function(prev, token, post, i) {
|
||||
var _c, _d, j, nx, open, size, tag;
|
||||
var _c, _d, before, j, nx, open, size, tag;
|
||||
tag = token[0];
|
||||
before = this.tokens[i - 2] && this.tokens[i - 2][0];
|
||||
if (tag === 'OUTDENT') {
|
||||
stack[stack.length - 2] += stack.pop();
|
||||
}
|
||||
@@ -190,7 +191,7 @@
|
||||
return 2;
|
||||
}
|
||||
if (include(EXPRESSION_START, tag)) {
|
||||
if (tag === 'INDENT' && !token.generated && open && !(prev && include(IMPLICIT_BLOCK, prev[0]))) {
|
||||
if (tag === 'INDENT' && !token.generated && open && !((prev && include(IMPLICIT_BLOCK, prev[0])) || before && before === 'CLASS')) {
|
||||
size = closeCalls(i);
|
||||
stack.push(0);
|
||||
return size;
|
||||
@@ -385,7 +386,7 @@
|
||||
})();
|
||||
EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
|
||||
IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@'];
|
||||
IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'TRY', 'DELETE', 'TYPEOF', 'SWITCH', 'THIS', 'NULL', 'TRUE', 'FALSE', 'YES', 'NO', 'ON', 'OFF', '!', '!!', '@', '->', '=>', '[', '(', '{'];
|
||||
IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'TRY', 'DELETE', 'TYPEOF', 'SWITCH', 'THIS', 'NULL', 'TRUE', 'FALSE', 'YES', 'NO', 'ON', 'OFF', '!', '!!', '@', '->', '=>', '[', '(', '{'];
|
||||
IMPLICIT_BLOCK = ['->', '=>', '{', '[', ','];
|
||||
IMPLICIT_END = ['IF', 'UNLESS', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'TERMINATOR', 'INDENT'].concat(EXPRESSION_END);
|
||||
SINGLE_LINERS = ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN'];
|
||||
|
||||
@@ -162,6 +162,7 @@ exports.Rewriter = class Rewriter
|
||||
size
|
||||
@scanTokens (prev, token, post, i) =>
|
||||
tag = token[0]
|
||||
before = @tokens[i - 2] and @tokens[i - 2][0]
|
||||
stack[stack.length - 2] += stack.pop() if tag is 'OUTDENT'
|
||||
open = stack[stack.length - 1] > 0
|
||||
if prev and prev.spaced and include(IMPLICIT_FUNC, prev[0]) and include(IMPLICIT_CALL, tag) and
|
||||
@@ -171,7 +172,8 @@ exports.Rewriter = class Rewriter
|
||||
stack.push 0 if include(EXPRESSION_START, tag)
|
||||
return 2
|
||||
if include(EXPRESSION_START, tag)
|
||||
if tag is 'INDENT' and !token.generated and open and not (prev and include(IMPLICIT_BLOCK, prev[0]))
|
||||
if tag is 'INDENT' and !token.generated and open and not
|
||||
((prev and include(IMPLICIT_BLOCK, prev[0])) or before and before is 'CLASS')
|
||||
size = closeCalls(i)
|
||||
stack.push 0
|
||||
return size
|
||||
@@ -329,7 +331,7 @@ IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@
|
||||
|
||||
# If preceded by an `IMPLICIT_FUNC`, indicates a function invocation.
|
||||
IMPLICIT_CALL = [
|
||||
'IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START',
|
||||
'IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS',
|
||||
'TRY', 'DELETE', 'TYPEOF', 'SWITCH', 'THIS', 'NULL',
|
||||
'TRUE', 'FALSE', 'YES', 'NO', 'ON', 'OFF',
|
||||
'!', '!!', '@', '->', '=>', '[', '(', '{'
|
||||
|
||||
@@ -206,3 +206,15 @@ class MyElement extends Element
|
||||
ok MyElement.extended is Base.extended
|
||||
ok MyElement.fromHTML is Element.fromHTML
|
||||
ok MyElement.__superClass__ is Element.prototype
|
||||
|
||||
|
||||
# Test classes wrapped in decorators.
|
||||
func = (klass) ->
|
||||
klass::prop = 'value'
|
||||
klass
|
||||
|
||||
func class Test
|
||||
prop2: 'value2'
|
||||
|
||||
ok (new Test).prop is 'value'
|
||||
ok (new Test).prop2 is 'value2'
|
||||
|
||||
Reference in New Issue
Block a user