mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
[CS2] return
and export default
can now accept implicit objects (#4532)
* Start with the test I want to pass: return an implicit (braces-less) object * Update Rewriter class to follow pattern of nodes.coffee; move debugging snippet to where it’ll work in CS2 * Allow export default implicit object * `return` assumes a continuation onto the next line *if* the next line is indented * Fix comment; improve test
This commit is contained in:
parent
3e70d31e5d
commit
2f9ab1d328
7 changed files with 45 additions and 14 deletions
|
@ -465,7 +465,7 @@
|
|||
return indent.length;
|
||||
}
|
||||
if (size > this.indent) {
|
||||
if (noNewlines) {
|
||||
if (noNewlines || this.tag() === 'RETURN') {
|
||||
this.indebt = size - this.indent;
|
||||
this.suppressNewlines();
|
||||
return indent.length;
|
||||
|
@ -890,7 +890,7 @@
|
|||
|
||||
unfinished() {
|
||||
var ref;
|
||||
return LINE_CONTINUER.test(this.chunk) || ((ref = this.tag()) === '\\' || ref === '.' || ref === '?.' || ref === '?::' || ref === 'UNARY' || ref === 'MATH' || ref === 'UNARY_MATH' || ref === '+' || ref === '-' || ref === '**' || ref === 'SHIFT' || ref === 'RELATION' || ref === 'COMPARE' || ref === '&' || ref === '^' || ref === '|' || ref === '&&' || ref === '||' || ref === 'BIN?' || ref === 'THROW' || ref === 'EXTENDS');
|
||||
return LINE_CONTINUER.test(this.chunk) || ((ref = this.tag()) === '\\' || ref === '.' || ref === '?.' || ref === '?::' || ref === 'UNARY' || ref === 'MATH' || ref === 'UNARY_MATH' || ref === '+' || ref === '-' || ref === '**' || ref === 'SHIFT' || ref === 'RELATION' || ref === 'COMPARE' || ref === '&' || ref === '^' || ref === '|' || ref === '&&' || ref === '||' || ref === 'BIN?' || ref === 'THROW' || ref === 'EXTENDS' || ref === 'DEFAULT');
|
||||
}
|
||||
|
||||
formatString(str, options) {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// Generated by CoffeeScript 2.0.0-beta1
|
||||
(function() {
|
||||
var BALANCED_PAIRS, CALL_CLOSERS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, SINGLE_CLOSERS, SINGLE_LINERS, generate, k, left, len, rite,
|
||||
var BALANCED_PAIRS, CALL_CLOSERS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, generate, k, left, len, rite,
|
||||
indexOf = [].indexOf;
|
||||
|
||||
generate = function(tag, value, origin) {
|
||||
|
@ -13,7 +13,7 @@
|
|||
return tok;
|
||||
};
|
||||
|
||||
exports.Rewriter = (function() {
|
||||
exports.Rewriter = Rewriter = (function() {
|
||||
class Rewriter {
|
||||
rewrite(tokens1) {
|
||||
this.tokens = tokens1;
|
||||
|
|
|
@ -229,7 +229,6 @@ grammar =
|
|||
o 'AWAIT RETURN', -> new AwaitReturn
|
||||
]
|
||||
|
||||
|
||||
# A block comment.
|
||||
Comment: [
|
||||
o 'HERECOMMENT', -> new Comment $1
|
||||
|
|
|
@ -403,7 +403,7 @@ exports.Lexer = class Lexer
|
|||
return indent.length
|
||||
|
||||
if size > @indent
|
||||
if noNewlines
|
||||
if noNewlines or @tag() is 'RETURN'
|
||||
@indebt = size - @indent
|
||||
@suppressNewlines()
|
||||
return indent.length
|
||||
|
@ -455,7 +455,7 @@ exports.Lexer = class Lexer
|
|||
this
|
||||
|
||||
# Matches and consumes non-meaningful whitespace. Tag the previous token
|
||||
# as being "spaced", because there are some cases where it makes a difference.
|
||||
# as being “spaced”, because there are some cases where it makes a difference.
|
||||
whitespaceToken: ->
|
||||
return 0 unless (match = WHITESPACE.exec @chunk) or
|
||||
(nline = @chunk.charAt(0) is '\n')
|
||||
|
@ -790,7 +790,7 @@ exports.Lexer = class Lexer
|
|||
LINE_CONTINUER.test(@chunk) or
|
||||
@tag() in ['\\', '.', '?.', '?::', 'UNARY', 'MATH', 'UNARY_MATH', '+', '-',
|
||||
'**', 'SHIFT', 'RELATION', 'COMPARE', '&', '^', '|', '&&', '||',
|
||||
'BIN?', 'THROW', 'EXTENDS']
|
||||
'BIN?', 'THROW', 'EXTENDS', 'DEFAULT']
|
||||
|
||||
formatString: (str, options) ->
|
||||
@replaceUnicodeCodePointEscapes str.replace(STRING_OMIT, '$1'), options
|
||||
|
|
|
@ -14,11 +14,7 @@ generate = (tag, value, origin) ->
|
|||
|
||||
# The **Rewriter** class is used by the [Lexer](lexer.html), directly against
|
||||
# its internal array of tokens.
|
||||
class exports.Rewriter
|
||||
|
||||
# Helpful snippet for debugging:
|
||||
#
|
||||
# console.log (t[0] + '/' + t[1] for t in @tokens).join ' '
|
||||
exports.Rewriter = class Rewriter
|
||||
|
||||
# Rewrite the token stream in multiple passes, one logical filter at
|
||||
# a time. This could certainly be changed into a single pass through the
|
||||
|
@ -26,6 +22,8 @@ class exports.Rewriter
|
|||
# like this. The order of these passes matters -- indentation must be
|
||||
# corrected before implicit parentheses can be wrapped around blocks of code.
|
||||
rewrite: (@tokens) ->
|
||||
# Helpful snippet for debugging:
|
||||
# console.log (t[0] + '/' + t[1] for t in @tokens).join ' '
|
||||
@removeLeadingNewlines()
|
||||
@closeOpenCalls()
|
||||
@closeOpenIndexes()
|
||||
|
@ -186,7 +184,7 @@ class exports.Rewriter
|
|||
# Don't end an implicit call on next indent if any of these are in an argument
|
||||
if inImplicitCall() and tag in ['IF', 'TRY', 'FINALLY', 'CATCH',
|
||||
'CLASS', 'SWITCH']
|
||||
stack.push ['CONTROL', i, ours: true]
|
||||
stack.push ['CONTROL', i, ours: yes]
|
||||
return forward(1)
|
||||
|
||||
if tag is 'INDENT' and inImplicit()
|
||||
|
|
|
@ -353,6 +353,28 @@ test "export default object", ->
|
|||
};"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "export default implicit object", ->
|
||||
input = "export default foo: 'bar', baz: 'qux'"
|
||||
output = """
|
||||
export default {
|
||||
foo: 'bar',
|
||||
baz: 'qux'
|
||||
};"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "export default multiline implicit object", ->
|
||||
input = """
|
||||
export default
|
||||
foo: 'bar',
|
||||
baz: 'qux'
|
||||
"""
|
||||
output = """
|
||||
export default {
|
||||
foo: 'bar',
|
||||
baz: 'qux'
|
||||
};"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "export default assignment expression", ->
|
||||
input = "export default foo = 'bar'"
|
||||
output = """
|
||||
|
|
|
@ -575,3 +575,15 @@ test "#4324: Shorthand after interpolated key", ->
|
|||
obj = {"#{1}": 1, a}
|
||||
eq 1, obj[1]
|
||||
eq 2, obj.a
|
||||
|
||||
test "#1263: Braceless object return", ->
|
||||
fn = ->
|
||||
return
|
||||
a: 1
|
||||
b: 2
|
||||
c: -> 3
|
||||
|
||||
obj = fn()
|
||||
eq 1, obj.a
|
||||
eq 2, obj.b
|
||||
eq 3, obj.c()
|
||||
|
|
Loading…
Reference in a new issue