react / wstein / node_modules / jest-cli / node_modules / jasmine-only / node_modules / coffee-script / lib / coffee-script / rewriter.js
81146 views// Generated by CoffeeScript 1.6.31(function() {2var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, SINGLE_CLOSERS, SINGLE_LINERS, generate, left, rite, _i, _len, _ref,3__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },4__slice = [].slice;56generate = function(tag, value) {7var tok;8tok = [tag, value];9tok.generated = true;10return tok;11};1213exports.Rewriter = (function() {14function Rewriter() {}1516Rewriter.prototype.rewrite = function(tokens) {17this.tokens = tokens;18this.removeLeadingNewlines();19this.removeMidExpressionNewlines();20this.closeOpenCalls();21this.closeOpenIndexes();22this.addImplicitIndentation();23this.tagPostfixConditionals();24this.addImplicitBracesAndParens();25this.addLocationDataToGeneratedTokens();26return this.tokens;27};2829Rewriter.prototype.scanTokens = function(block) {30var i, token, tokens;31tokens = this.tokens;32i = 0;33while (token = tokens[i]) {34i += block.call(this, token, i, tokens);35}36return true;37};3839Rewriter.prototype.detectEnd = function(i, condition, action) {40var levels, token, tokens, _ref, _ref1;41tokens = this.tokens;42levels = 0;43while (token = tokens[i]) {44if (levels === 0 && condition.call(this, token, i)) {45return action.call(this, token, i);46}47if (!token || levels < 0) {48return action.call(this, token, i - 1);49}50if (_ref = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) {51levels += 1;52} else if (_ref1 = token[0], __indexOf.call(EXPRESSION_END, _ref1) >= 0) {53levels -= 1;54}55i += 1;56}57return i - 1;58};5960Rewriter.prototype.removeLeadingNewlines = function() {61var i, tag, _i, _len, _ref;62_ref = this.tokens;63for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {64tag = _ref[i][0];65if (tag !== 'TERMINATOR') {66break;67}68}69if (i) {70return this.tokens.splice(0, i);71}72};7374Rewriter.prototype.removeMidExpressionNewlines = function() {75return this.scanTokens(function(token, i, tokens) {76var _ref;77if (!(token[0] === 'TERMINATOR' && (_ref = this.tag(i + 1), __indexOf.call(EXPRESSION_CLOSE, _ref) >= 0))) {78return 1;79}80tokens.splice(i, 1);81return 0;82});83};8485Rewriter.prototype.closeOpenCalls = function() {86var action, condition;87condition = function(token, i) {88var _ref;89return ((_ref = token[0]) === ')' || _ref === 'CALL_END') || token[0] === 'OUTDENT' && this.tag(i - 1) === ')';90};91action = function(token, i) {92return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END';93};94return this.scanTokens(function(token, i) {95if (token[0] === 'CALL_START') {96this.detectEnd(i + 1, condition, action);97}98return 1;99});100};101102Rewriter.prototype.closeOpenIndexes = function() {103var action, condition;104condition = function(token, i) {105var _ref;106return (_ref = token[0]) === ']' || _ref === 'INDEX_END';107};108action = function(token, i) {109return token[0] = 'INDEX_END';110};111return this.scanTokens(function(token, i) {112if (token[0] === 'INDEX_START') {113this.detectEnd(i + 1, condition, action);114}115return 1;116});117};118119Rewriter.prototype.matchTags = function() {120var fuzz, i, j, pattern, _i, _ref, _ref1;121i = arguments[0], pattern = 2 <= arguments.length ? __slice.call(arguments, 1) : [];122fuzz = 0;123for (j = _i = 0, _ref = pattern.length; 0 <= _ref ? _i < _ref : _i > _ref; j = 0 <= _ref ? ++_i : --_i) {124while (this.tag(i + j + fuzz) === 'HERECOMMENT') {125fuzz += 2;126}127if (pattern[j] == null) {128continue;129}130if (typeof pattern[j] === 'string') {131pattern[j] = [pattern[j]];132}133if (_ref1 = this.tag(i + j + fuzz), __indexOf.call(pattern[j], _ref1) < 0) {134return false;135}136}137return true;138};139140Rewriter.prototype.looksObjectish = function(j) {141return this.matchTags(j, '@', null, ':') || this.matchTags(j, null, ':');142};143144Rewriter.prototype.findTagsBackwards = function(i, tags) {145var backStack, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;146backStack = [];147while (i >= 0 && (backStack.length || (_ref2 = this.tag(i), __indexOf.call(tags, _ref2) < 0) && ((_ref3 = this.tag(i), __indexOf.call(EXPRESSION_START, _ref3) < 0) || this.tokens[i].generated) && (_ref4 = this.tag(i), __indexOf.call(LINEBREAKS, _ref4) < 0))) {148if (_ref = this.tag(i), __indexOf.call(EXPRESSION_END, _ref) >= 0) {149backStack.push(this.tag(i));150}151if ((_ref1 = this.tag(i), __indexOf.call(EXPRESSION_START, _ref1) >= 0) && backStack.length) {152backStack.pop();153}154i -= 1;155}156return _ref5 = this.tag(i), __indexOf.call(tags, _ref5) >= 0;157};158159Rewriter.prototype.addImplicitBracesAndParens = function() {160var stack;161stack = [];162return this.scanTokens(function(token, i, tokens) {163var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, nextTag, offset, prevTag, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;164tag = token[0];165prevTag = (i > 0 ? tokens[i - 1] : [])[0];166nextTag = (i < tokens.length - 1 ? tokens[i + 1] : [])[0];167stackTop = function() {168return stack[stack.length - 1];169};170startIdx = i;171forward = function(n) {172return i - startIdx + n;173};174inImplicit = function() {175var _ref, _ref1;176return (_ref = stackTop()) != null ? (_ref1 = _ref[2]) != null ? _ref1.ours : void 0 : void 0;177};178inImplicitCall = function() {179var _ref;180return inImplicit() && ((_ref = stackTop()) != null ? _ref[0] : void 0) === '(';181};182inImplicitObject = function() {183var _ref;184return inImplicit() && ((_ref = stackTop()) != null ? _ref[0] : void 0) === '{';185};186inImplicitControl = function() {187var _ref;188return inImplicit && ((_ref = stackTop()) != null ? _ref[0] : void 0) === 'CONTROL';189};190startImplicitCall = function(j) {191var idx;192idx = j != null ? j : i;193stack.push([194'(', idx, {195ours: true196}197]);198tokens.splice(idx, 0, generate('CALL_START', '('));199if (j == null) {200return i += 1;201}202};203endImplicitCall = function() {204stack.pop();205tokens.splice(i, 0, generate('CALL_END', ')'));206return i += 1;207};208startImplicitObject = function(j, startsLine) {209var idx;210if (startsLine == null) {211startsLine = true;212}213idx = j != null ? j : i;214stack.push([215'{', idx, {216sameLine: true,217startsLine: startsLine,218ours: true219}220]);221tokens.splice(idx, 0, generate('{', generate(new String('{'))));222if (j == null) {223return i += 1;224}225};226endImplicitObject = function(j) {227j = j != null ? j : i;228stack.pop();229tokens.splice(j, 0, generate('}', '}'));230return i += 1;231};232if (inImplicitCall() && (tag === 'IF' || tag === 'TRY' || tag === 'FINALLY' || tag === 'CATCH' || tag === 'CLASS' || tag === 'SWITCH')) {233stack.push([234'CONTROL', i, {235ours: true236}237]);238return forward(1);239}240if (tag === 'INDENT' && inImplicit()) {241if (prevTag !== '=>' && prevTag !== '->' && prevTag !== '[' && prevTag !== '(' && prevTag !== ',' && prevTag !== '{' && prevTag !== 'TRY' && prevTag !== 'ELSE' && prevTag !== '=') {242while (inImplicitCall()) {243endImplicitCall();244}245}246if (inImplicitControl()) {247stack.pop();248}249stack.push([tag, i]);250return forward(1);251}252if (__indexOf.call(EXPRESSION_START, tag) >= 0) {253stack.push([tag, i]);254return forward(1);255}256if (__indexOf.call(EXPRESSION_END, tag) >= 0) {257while (inImplicit()) {258if (inImplicitCall()) {259endImplicitCall();260} else if (inImplicitObject()) {261endImplicitObject();262} else {263stack.pop();264}265}266stack.pop();267}268if ((__indexOf.call(IMPLICIT_FUNC, tag) >= 0 && token.spaced && !token.stringEnd || tag === '?' && i > 0 && !tokens[i - 1].spaced) && (__indexOf.call(IMPLICIT_CALL, nextTag) >= 0 || __indexOf.call(IMPLICIT_UNSPACED_CALL, nextTag) >= 0 && !((_ref = tokens[i + 1]) != null ? _ref.spaced : void 0) && !((_ref1 = tokens[i + 1]) != null ? _ref1.newLine : void 0))) {269if (tag === '?') {270tag = token[0] = 'FUNC_EXIST';271}272startImplicitCall(i + 1);273return forward(2);274}275if (__indexOf.call(IMPLICIT_FUNC, tag) >= 0 && this.matchTags(i + 1, 'INDENT', null, ':') && !this.findTagsBackwards(i, ['CLASS', 'EXTENDS', 'IF', 'CATCH', 'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL'])) {276startImplicitCall(i + 1);277stack.push(['INDENT', i + 2]);278return forward(3);279}280if (tag === ':') {281if (this.tag(i - 2) === '@') {282s = i - 2;283} else {284s = i - 1;285}286while (this.tag(s - 2) === 'HERECOMMENT') {287s -= 2;288}289startsLine = s === 0 || (_ref2 = this.tag(s - 1), __indexOf.call(LINEBREAKS, _ref2) >= 0) || tokens[s - 1].newLine;290if (stackTop()) {291_ref3 = stackTop(), stackTag = _ref3[0], stackIdx = _ref3[1];292if ((stackTag === '{' || stackTag === 'INDENT' && this.tag(stackIdx - 1) === '{') && (startsLine || this.tag(s - 1) === ',' || this.tag(s - 1) === '{')) {293return forward(1);294}295}296startImplicitObject(s, !!startsLine);297return forward(2);298}299if (prevTag === 'OUTDENT' && inImplicitCall() && (tag === '.' || tag === '?.' || tag === '::' || tag === '?::')) {300endImplicitCall();301return forward(1);302}303if (inImplicitObject() && __indexOf.call(LINEBREAKS, tag) >= 0) {304stackTop()[2].sameLine = false;305}306if (__indexOf.call(IMPLICIT_END, tag) >= 0) {307while (inImplicit()) {308_ref4 = stackTop(), stackTag = _ref4[0], stackIdx = _ref4[1], (_ref5 = _ref4[2], sameLine = _ref5.sameLine, startsLine = _ref5.startsLine);309if (inImplicitCall() && prevTag !== ',') {310endImplicitCall();311} else if (inImplicitObject() && sameLine && !startsLine) {312endImplicitObject();313} else if (inImplicitObject() && tag === 'TERMINATOR' && prevTag !== ',' && !(startsLine && this.looksObjectish(i + 1))) {314endImplicitObject();315} else {316break;317}318}319}320if (tag === ',' && !this.looksObjectish(i + 1) && inImplicitObject() && (nextTag !== 'TERMINATOR' || !this.looksObjectish(i + 2))) {321offset = nextTag === 'OUTDENT' ? 1 : 0;322while (inImplicitObject()) {323endImplicitObject(i + offset);324}325}326return forward(1);327});328};329330Rewriter.prototype.addLocationDataToGeneratedTokens = function() {331return this.scanTokens(function(token, i, tokens) {332var column, line, nextLocation, prevLocation, _ref, _ref1;333if (token[2]) {334return 1;335}336if (!(token.generated || token.explicit)) {337return 1;338}339if (token[0] === '{' && (nextLocation = (_ref = tokens[i + 1]) != null ? _ref[2] : void 0)) {340line = nextLocation.first_line, column = nextLocation.first_column;341} else if (prevLocation = (_ref1 = tokens[i - 1]) != null ? _ref1[2] : void 0) {342line = prevLocation.last_line, column = prevLocation.last_column;343} else {344line = column = 0;345}346token[2] = {347first_line: line,348first_column: column,349last_line: line,350last_column: column351};352return 1;353});354};355356Rewriter.prototype.addImplicitIndentation = function() {357var action, condition, indent, outdent, starter;358starter = indent = outdent = null;359condition = function(token, i) {360var _ref, _ref1;361return token[1] !== ';' && (_ref = token[0], __indexOf.call(SINGLE_CLOSERS, _ref) >= 0) && !(token[0] === 'ELSE' && starter !== 'THEN') && !(((_ref1 = token[0]) === 'CATCH' || _ref1 === 'FINALLY') && (starter === '->' || starter === '=>'));362};363action = function(token, i) {364return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);365};366return this.scanTokens(function(token, i, tokens) {367var j, tag, _i, _ref, _ref1;368tag = token[0];369if (tag === 'TERMINATOR' && this.tag(i + 1) === 'THEN') {370tokens.splice(i, 1);371return 0;372}373if (tag === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {374tokens.splice.apply(tokens, [i, 0].concat(__slice.call(this.indentation())));375return 2;376}377if (tag === 'CATCH') {378for (j = _i = 1; _i <= 2; j = ++_i) {379if (!((_ref = this.tag(i + j)) === 'OUTDENT' || _ref === 'TERMINATOR' || _ref === 'FINALLY')) {380continue;381}382tokens.splice.apply(tokens, [i + j, 0].concat(__slice.call(this.indentation())));383return 2 + j;384}385}386if (__indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) {387starter = tag;388_ref1 = this.indentation(true), indent = _ref1[0], outdent = _ref1[1];389if (starter === 'THEN') {390indent.fromThen = true;391}392tokens.splice(i + 1, 0, indent);393this.detectEnd(i + 2, condition, action);394if (tag === 'THEN') {395tokens.splice(i, 1);396}397return 1;398}399return 1;400});401};402403Rewriter.prototype.tagPostfixConditionals = function() {404var action, condition, original;405original = null;406condition = function(token, i) {407var prevTag, tag;408tag = token[0];409prevTag = this.tokens[i - 1][0];410return tag === 'TERMINATOR' || (tag === 'INDENT' && __indexOf.call(SINGLE_LINERS, prevTag) < 0);411};412action = function(token, i) {413if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {414return original[0] = 'POST_' + original[0];415}416};417return this.scanTokens(function(token, i) {418if (token[0] !== 'IF') {419return 1;420}421original = token;422this.detectEnd(i + 1, condition, action);423return 1;424});425};426427Rewriter.prototype.indentation = function(implicit) {428var indent, outdent;429if (implicit == null) {430implicit = false;431}432indent = ['INDENT', 2];433outdent = ['OUTDENT', 2];434if (implicit) {435indent.generated = outdent.generated = true;436}437if (!implicit) {438indent.explicit = outdent.explicit = true;439}440return [indent, outdent];441};442443Rewriter.prototype.generate = generate;444445Rewriter.prototype.tag = function(i) {446var _ref;447return (_ref = this.tokens[i]) != null ? _ref[0] : void 0;448};449450return Rewriter;451452})();453454BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END']];455456exports.INVERSES = INVERSES = {};457458EXPRESSION_START = [];459460EXPRESSION_END = [];461462for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) {463_ref = BALANCED_PAIRS[_i], left = _ref[0], rite = _ref[1];464EXPRESSION_START.push(INVERSES[rite] = left);465EXPRESSION_END.push(INVERSES[left] = rite);466}467468EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);469470IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];471472IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'NULL', 'UNDEFINED', 'UNARY', 'SUPER', 'THROW', '@', '->', '=>', '[', '(', '{', '--', '++'];473474IMPLICIT_UNSPACED_CALL = ['+', '-'];475476IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];477478SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];479480SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];481482LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];483484}).call(this);485486487