diff --git a/lib/coffee-script/rewriter.js b/lib/coffee-script/rewriter.js index b4fdc074..d3da4dd1 100644 --- a/lib/coffee-script/rewriter.js +++ b/lib/coffee-script/rewriter.js @@ -117,8 +117,7 @@ }; action = function(token, i) { var tok; - tok = ['}', '}', token[2]]; - tok.generated = true; + tok = this.generate('}', '}', token[2]); return this.tokens.splice(i, 0, tok); }; return this.scanTokens(function(token, i, tokens) { @@ -144,8 +143,7 @@ startsLine = !prevTag || (__indexOf.call(LINEBREAKS, prevTag) >= 0); value = new String('{'); value.generated = true; - tok = ['{', value, token[2]]; - tok.generated = true; + tok = this.generate('{', value, token[2]); tokens.splice(idx, 0, tok); this.detectEnd(i + 2, condition, action); return 2; @@ -171,7 +169,7 @@ return !token.generated && this.tag(i - 1) !== ',' && (__indexOf.call(IMPLICIT_END, tag) >= 0 || (tag === 'INDENT' && !seenControl)) && (tag !== 'INDENT' || (((_ref = this.tag(i - 2)) !== 'CLASS' && _ref !== 'EXTENDS') && (_ref2 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref2) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{'))); }; action = function(token, i) { - return this.tokens.splice(i, 0, ['CALL_END', ')', token[2]]); + return this.tokens.splice(i, 0, this.generate('CALL_END', ')', token[2])); }; return this.scanTokens(function(token, i, tokens) { var callObject, current, next, prev, tag, _ref, _ref2, _ref3; @@ -187,7 +185,7 @@ if (!(callObject || (prev != null ? prev.spaced : void 0) && (prev.call || (_ref3 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref3) >= 0)) && (__indexOf.call(IMPLICIT_CALL, tag) >= 0 || !(token.spaced || token.newLine) && __indexOf.call(IMPLICIT_UNSPACED_CALL, tag) >= 0))) { return 1; } - tokens.splice(i, 0, ['CALL_START', '(', token[2]]); + tokens.splice(i, 0, this.generate('CALL_START', '(', token[2])); this.detectEnd(i + 1, condition, action); if (prev[0] === '?') prev[0] = 'FUNC_EXIST'; return 2; @@ -221,9 +219,8 @@ } if (__indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) { starter = tag; - _ref2 = this.indentation(token), indent = _ref2[0], outdent = _ref2[1]; + _ref2 = this.indentation(token, true), indent = _ref2[0], outdent = _ref2[1]; if (starter === 'THEN') indent.fromThen = true; - indent.generated = outdent.generated = true; tokens.splice(i + 1, 0, indent); this.detectEnd(i + 2, condition, action); if (tag === 'THEN') tokens.splice(i, 1); @@ -253,8 +250,20 @@ }); }; - Rewriter.prototype.indentation = function(token) { - return [['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]]; + Rewriter.prototype.indentation = function(token, implicit) { + var indent, outdent; + if (implicit == null) implicit = false; + indent = ['INDENT', 2, token[2]]; + outdent = ['OUTDENT', 2, token[2]]; + if (implicit) indent.generated = outdent.generated = true; + return [indent, outdent]; + }; + + Rewriter.prototype.generate = function(tag, value, line) { + var tok; + tok = [tag, value, line]; + tok.generated = true; + return tok; }; Rewriter.prototype.tag = function(i) { diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 5cd8ac63..133a5aeb 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -118,8 +118,7 @@ class exports.Rewriter one[0] not in ['IDENTIFIER', 'NUMBER', 'STRING', '@', 'TERMINATOR', 'OUTDENT']) action = (token, i) -> - tok = ['}', '}', token[2]] - tok.generated = yes + tok = @generate '}', '}', token[2] @tokens.splice i, 0, tok @scanTokens (token, i, tokens) -> @@ -139,8 +138,7 @@ class exports.Rewriter startsLine = not prevTag or (prevTag in LINEBREAKS) value = new String('{') value.generated = yes - tok = ['{', value, token[2]] - tok.generated = yes + tok = @generate '{', value, token[2] tokens.splice idx, 0, tok @detectEnd i + 2, condition, action 2 @@ -165,7 +163,7 @@ class exports.Rewriter not ((post = @tokens[i + 1]) and post.generated and post[0] is '{'))) action = (token, i) -> - @tokens.splice i, 0, ['CALL_END', ')', token[2]] + @tokens.splice i, 0, @generate 'CALL_END', ')', token[2] @scanTokens (token, i, tokens) -> tag = token[0] @@ -182,7 +180,7 @@ class exports.Rewriter return 1 unless callObject or prev?.spaced and (prev.call or prev[0] in IMPLICIT_FUNC) and (tag in IMPLICIT_CALL or not (token.spaced or token.newLine) and tag in IMPLICIT_UNSPACED_CALL) - tokens.splice i, 0, ['CALL_START', '(', token[2]] + tokens.splice i, 0, @generate 'CALL_START', '(', token[2] @detectEnd i + 1, condition, action prev[0] = 'FUNC_EXIST' if prev[0] is '?' 2 @@ -216,9 +214,8 @@ class exports.Rewriter if tag in SINGLE_LINERS and @tag(i + 1) isnt 'INDENT' and not (tag is 'ELSE' and @tag(i + 1) is 'IF') starter = tag - [indent, outdent] = @indentation token + [indent, outdent] = @indentation token, yes indent.fromThen = true if starter is 'THEN' - indent.generated = outdent.generated = true tokens.splice i + 1, 0, indent @detectEnd i + 2, condition, action tokens.splice i, 1 if tag is 'THEN' @@ -245,8 +242,17 @@ class exports.Rewriter 1 # Generate the indentation tokens, based on another token on the same line. - indentation: (token) -> - [['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]] + indentation: (token, implicit = no) -> + indent = ['INDENT', 2, token[2]] + outdent = ['OUTDENT', 2, token[2]] + indent.generated = outdent.generated = yes if implicit + [indent, outdent] + + # Create a generated token: one that exists due to a use of implicit syntax. + generate: (tag, value, line) -> + tok = [tag, value, line] + tok.generated = yes + tok # Look up a tag by token index. tag: (i) -> @tokens[i]?[0]