react / wstein / node_modules / jest-cli / node_modules / coffee-script / lib / coffee-script / rewriter.js
80677 views// Generated by CoffeeScript 1.9.31(function() {2var 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, ref, rite,3indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },4slice = [].slice;56generate = function(tag, value, origin) {7var tok;8tok = [tag, value];9tok.generated = true;10if (origin) {11tok.origin = origin;12}13return tok;14};1516exports.Rewriter = (function() {17function Rewriter() {}1819Rewriter.prototype.rewrite = function(tokens1) {20this.tokens = tokens1;21this.removeLeadingNewlines();22this.closeOpenCalls();23this.closeOpenIndexes();24this.normalizeLines();25this.tagPostfixConditionals();26this.addImplicitBracesAndParens();27this.addLocationDataToGeneratedTokens();28return this.tokens;29};3031Rewriter.prototype.scanTokens = function(block) {32var i, token, tokens;33tokens = this.tokens;34i = 0;35while (token = tokens[i]) {36i += block.call(this, token, i, tokens);37}38return true;39};4041Rewriter.prototype.detectEnd = function(i, condition, action) {42var levels, ref, ref1, token, tokens;43tokens = this.tokens;44levels = 0;45while (token = tokens[i]) {46if (levels === 0 && condition.call(this, token, i)) {47return action.call(this, token, i);48}49if (!token || levels < 0) {50return action.call(this, token, i - 1);51}52if (ref = token[0], indexOf.call(EXPRESSION_START, ref) >= 0) {53levels += 1;54} else if (ref1 = token[0], indexOf.call(EXPRESSION_END, ref1) >= 0) {55levels -= 1;56}57i += 1;58}59return i - 1;60};6162Rewriter.prototype.removeLeadingNewlines = function() {63var i, k, len, ref, tag;64ref = this.tokens;65for (i = k = 0, len = ref.length; k < len; i = ++k) {66tag = ref[i][0];67if (tag !== 'TERMINATOR') {68break;69}70}71if (i) {72return this.tokens.splice(0, i);73}74};7576Rewriter.prototype.closeOpenCalls = function() {77var action, condition;78condition = function(token, i) {79var ref;80return ((ref = token[0]) === ')' || ref === 'CALL_END') || token[0] === 'OUTDENT' && this.tag(i - 1) === ')';81};82action = function(token, i) {83return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END';84};85return this.scanTokens(function(token, i) {86if (token[0] === 'CALL_START') {87this.detectEnd(i + 1, condition, action);88}89return 1;90});91};9293Rewriter.prototype.closeOpenIndexes = function() {94var action, condition;95condition = function(token, i) {96var ref;97return (ref = token[0]) === ']' || ref === 'INDEX_END';98};99action = function(token, i) {100return token[0] = 'INDEX_END';101};102return this.scanTokens(function(token, i) {103if (token[0] === 'INDEX_START') {104this.detectEnd(i + 1, condition, action);105}106return 1;107});108};109110Rewriter.prototype.indexOfTag = function() {111var fuzz, i, j, k, pattern, ref, ref1;112i = arguments[0], pattern = 2 <= arguments.length ? slice.call(arguments, 1) : [];113fuzz = 0;114for (j = k = 0, ref = pattern.length; 0 <= ref ? k < ref : k > ref; j = 0 <= ref ? ++k : --k) {115while (this.tag(i + j + fuzz) === 'HERECOMMENT') {116fuzz += 2;117}118if (pattern[j] == null) {119continue;120}121if (typeof pattern[j] === 'string') {122pattern[j] = [pattern[j]];123}124if (ref1 = this.tag(i + j + fuzz), indexOf.call(pattern[j], ref1) < 0) {125return -1;126}127}128return i + j + fuzz - 1;129};130131Rewriter.prototype.looksObjectish = function(j) {132var end, index;133if (this.indexOfTag(j, '@', null, ':') > -1 || this.indexOfTag(j, null, ':') > -1) {134return true;135}136index = this.indexOfTag(j, EXPRESSION_START);137if (index > -1) {138end = null;139this.detectEnd(index + 1, (function(token) {140var ref;141return ref = token[0], indexOf.call(EXPRESSION_END, ref) >= 0;142}), (function(token, i) {143return end = i;144}));145if (this.tag(end + 1) === ':') {146return true;147}148}149return false;150};151152Rewriter.prototype.findTagsBackwards = function(i, tags) {153var backStack, ref, ref1, ref2, ref3, ref4, ref5;154backStack = [];155while (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))) {156if (ref = this.tag(i), indexOf.call(EXPRESSION_END, ref) >= 0) {157backStack.push(this.tag(i));158}159if ((ref1 = this.tag(i), indexOf.call(EXPRESSION_START, ref1) >= 0) && backStack.length) {160backStack.pop();161}162i -= 1;163}164return ref5 = this.tag(i), indexOf.call(tags, ref5) >= 0;165};166167Rewriter.prototype.addImplicitBracesAndParens = function() {168var stack, start;169stack = [];170start = null;171return this.scanTokens(function(token, i, tokens) {172var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, newLine, nextTag, offset, prevTag, prevToken, ref, ref1, ref2, ref3, ref4, ref5, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag;173tag = token[0];174prevTag = (prevToken = i > 0 ? tokens[i - 1] : [])[0];175nextTag = (i < tokens.length - 1 ? tokens[i + 1] : [])[0];176stackTop = function() {177return stack[stack.length - 1];178};179startIdx = i;180forward = function(n) {181return i - startIdx + n;182};183inImplicit = function() {184var ref, ref1;185return (ref = stackTop()) != null ? (ref1 = ref[2]) != null ? ref1.ours : void 0 : void 0;186};187inImplicitCall = function() {188var ref;189return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '(';190};191inImplicitObject = function() {192var ref;193return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '{';194};195inImplicitControl = function() {196var ref;197return inImplicit && ((ref = stackTop()) != null ? ref[0] : void 0) === 'CONTROL';198};199startImplicitCall = function(j) {200var idx;201idx = j != null ? j : i;202stack.push([203'(', idx, {204ours: true205}206]);207tokens.splice(idx, 0, generate('CALL_START', '('));208if (j == null) {209return i += 1;210}211};212endImplicitCall = function() {213stack.pop();214tokens.splice(i, 0, generate('CALL_END', ')', ['', 'end of input', token[2]]));215return i += 1;216};217startImplicitObject = function(j, startsLine) {218var idx, val;219if (startsLine == null) {220startsLine = true;221}222idx = j != null ? j : i;223stack.push([224'{', idx, {225sameLine: true,226startsLine: startsLine,227ours: true228}229]);230val = new String('{');231val.generated = true;232tokens.splice(idx, 0, generate('{', val, token));233if (j == null) {234return i += 1;235}236};237endImplicitObject = function(j) {238j = j != null ? j : i;239stack.pop();240tokens.splice(j, 0, generate('}', '}', token));241return i += 1;242};243if (inImplicitCall() && (tag === 'IF' || tag === 'TRY' || tag === 'FINALLY' || tag === 'CATCH' || tag === 'CLASS' || tag === 'SWITCH')) {244stack.push([245'CONTROL', i, {246ours: true247}248]);249return forward(1);250}251if (tag === 'INDENT' && inImplicit()) {252if (prevTag !== '=>' && prevTag !== '->' && prevTag !== '[' && prevTag !== '(' && prevTag !== ',' && prevTag !== '{' && prevTag !== 'TRY' && prevTag !== 'ELSE' && prevTag !== '=') {253while (inImplicitCall()) {254endImplicitCall();255}256}257if (inImplicitControl()) {258stack.pop();259}260stack.push([tag, i]);261return forward(1);262}263if (indexOf.call(EXPRESSION_START, tag) >= 0) {264stack.push([tag, i]);265return forward(1);266}267if (indexOf.call(EXPRESSION_END, tag) >= 0) {268while (inImplicit()) {269if (inImplicitCall()) {270endImplicitCall();271} else if (inImplicitObject()) {272endImplicitObject();273} else {274stack.pop();275}276}277start = stack.pop();278}279if ((indexOf.call(IMPLICIT_FUNC, tag) >= 0 && token.spaced || 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))) {280if (tag === '?') {281tag = token[0] = 'FUNC_EXIST';282}283startImplicitCall(i + 1);284return forward(2);285}286if (indexOf.call(IMPLICIT_FUNC, tag) >= 0 && this.indexOfTag(i + 1, 'INDENT') > -1 && this.looksObjectish(i + 2) && !this.findTagsBackwards(i, ['CLASS', 'EXTENDS', 'IF', 'CATCH', 'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL'])) {287startImplicitCall(i + 1);288stack.push(['INDENT', i + 2]);289return forward(3);290}291if (tag === ':') {292s = (function() {293var ref2;294switch (false) {295case ref2 = this.tag(i - 1), indexOf.call(EXPRESSION_END, ref2) < 0:296return start[1];297case this.tag(i - 2) !== '@':298return i - 2;299default:300return i - 1;301}302}).call(this);303while (this.tag(s - 2) === 'HERECOMMENT') {304s -= 2;305}306this.insideForDeclaration = nextTag === 'FOR';307startsLine = s === 0 || (ref2 = this.tag(s - 1), indexOf.call(LINEBREAKS, ref2) >= 0) || tokens[s - 1].newLine;308if (stackTop()) {309ref3 = stackTop(), stackTag = ref3[0], stackIdx = ref3[1];310if ((stackTag === '{' || stackTag === 'INDENT' && this.tag(stackIdx - 1) === '{') && (startsLine || this.tag(s - 1) === ',' || this.tag(s - 1) === '{')) {311return forward(1);312}313}314startImplicitObject(s, !!startsLine);315return forward(2);316}317if (inImplicitObject() && indexOf.call(LINEBREAKS, tag) >= 0) {318stackTop()[2].sameLine = false;319}320newLine = prevTag === 'OUTDENT' || prevToken.newLine;321if (indexOf.call(IMPLICIT_END, tag) >= 0 || indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) {322while (inImplicit()) {323ref4 = stackTop(), stackTag = ref4[0], stackIdx = ref4[1], (ref5 = ref4[2], sameLine = ref5.sameLine, startsLine = ref5.startsLine);324if (inImplicitCall() && prevTag !== ',') {325endImplicitCall();326} else if (inImplicitObject() && !this.insideForDeclaration && sameLine && tag !== 'TERMINATOR' && prevTag !== ':') {327endImplicitObject();328} else if (inImplicitObject() && tag === 'TERMINATOR' && prevTag !== ',' && !(startsLine && this.looksObjectish(i + 1))) {329if (nextTag === 'HERECOMMENT') {330return forward(1);331}332endImplicitObject();333} else {334break;335}336}337}338if (tag === ',' && !this.looksObjectish(i + 1) && inImplicitObject() && !this.insideForDeclaration && (nextTag !== 'TERMINATOR' || !this.looksObjectish(i + 2))) {339offset = nextTag === 'OUTDENT' ? 1 : 0;340while (inImplicitObject()) {341endImplicitObject(i + offset);342}343}344return forward(1);345});346};347348Rewriter.prototype.addLocationDataToGeneratedTokens = function() {349return this.scanTokens(function(token, i, tokens) {350var column, line, nextLocation, prevLocation, ref, ref1;351if (token[2]) {352return 1;353}354if (!(token.generated || token.explicit)) {355return 1;356}357if (token[0] === '{' && (nextLocation = (ref = tokens[i + 1]) != null ? ref[2] : void 0)) {358line = nextLocation.first_line, column = nextLocation.first_column;359} else if (prevLocation = (ref1 = tokens[i - 1]) != null ? ref1[2] : void 0) {360line = prevLocation.last_line, column = prevLocation.last_column;361} else {362line = column = 0;363}364token[2] = {365first_line: line,366first_column: column,367last_line: line,368last_column: column369};370return 1;371});372};373374Rewriter.prototype.normalizeLines = function() {375var action, condition, indent, outdent, starter;376starter = indent = outdent = null;377condition = function(token, i) {378var ref, ref1, ref2, ref3;379return token[1] !== ';' && (ref = token[0], indexOf.call(SINGLE_CLOSERS, ref) >= 0) && !(token[0] === 'TERMINATOR' && (ref1 = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref1) >= 0)) && !(token[0] === 'ELSE' && starter !== 'THEN') && !(((ref2 = token[0]) === 'CATCH' || ref2 === 'FINALLY') && (starter === '->' || starter === '=>')) || (ref3 = token[0], indexOf.call(CALL_CLOSERS, ref3) >= 0) && this.tokens[i - 1].newLine;380};381action = function(token, i) {382return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);383};384return this.scanTokens(function(token, i, tokens) {385var j, k, ref, ref1, ref2, tag;386tag = token[0];387if (tag === 'TERMINATOR') {388if (this.tag(i + 1) === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {389tokens.splice.apply(tokens, [i, 1].concat(slice.call(this.indentation())));390return 1;391}392if (ref = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref) >= 0) {393tokens.splice(i, 1);394return 0;395}396}397if (tag === 'CATCH') {398for (j = k = 1; k <= 2; j = ++k) {399if (!((ref1 = this.tag(i + j)) === 'OUTDENT' || ref1 === 'TERMINATOR' || ref1 === 'FINALLY')) {400continue;401}402tokens.splice.apply(tokens, [i + j, 0].concat(slice.call(this.indentation())));403return 2 + j;404}405}406if (indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) {407starter = tag;408ref2 = this.indentation(tokens[i]), indent = ref2[0], outdent = ref2[1];409if (starter === 'THEN') {410indent.fromThen = true;411}412tokens.splice(i + 1, 0, indent);413this.detectEnd(i + 2, condition, action);414if (tag === 'THEN') {415tokens.splice(i, 1);416}417return 1;418}419return 1;420});421};422423Rewriter.prototype.tagPostfixConditionals = function() {424var action, condition, original;425original = null;426condition = function(token, i) {427var prevTag, tag;428tag = token[0];429prevTag = this.tokens[i - 1][0];430return tag === 'TERMINATOR' || (tag === 'INDENT' && indexOf.call(SINGLE_LINERS, prevTag) < 0);431};432action = function(token, i) {433if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {434return original[0] = 'POST_' + original[0];435}436};437return this.scanTokens(function(token, i) {438if (token[0] !== 'IF') {439return 1;440}441original = token;442this.detectEnd(i + 1, condition, action);443return 1;444});445};446447Rewriter.prototype.indentation = function(origin) {448var indent, outdent;449indent = ['INDENT', 2];450outdent = ['OUTDENT', 2];451if (origin) {452indent.generated = outdent.generated = true;453indent.origin = outdent.origin = origin;454} else {455indent.explicit = outdent.explicit = true;456}457return [indent, outdent];458};459460Rewriter.prototype.generate = generate;461462Rewriter.prototype.tag = function(i) {463var ref;464return (ref = this.tokens[i]) != null ? ref[0] : void 0;465};466467return Rewriter;468469})();470471BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END'], ['STRING_START', 'STRING_END'], ['REGEX_START', 'REGEX_END']];472473exports.INVERSES = INVERSES = {};474475EXPRESSION_START = [];476477EXPRESSION_END = [];478479for (k = 0, len = BALANCED_PAIRS.length; k < len; k++) {480ref = BALANCED_PAIRS[k], left = ref[0], rite = ref[1];481EXPRESSION_START.push(INVERSES[rite] = left);482EXPRESSION_END.push(INVERSES[left] = rite);483}484485EXPRESSION_CLOSE = ['CATCH', 'THEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);486487IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];488489IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'STRING_START', 'JS', 'REGEX', 'REGEX_START', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'NULL', 'UNDEFINED', 'UNARY', 'YIELD', 'UNARY_MATH', 'SUPER', 'THROW', '@', '->', '=>', '[', '(', '{', '--', '++'];490491IMPLICIT_UNSPACED_CALL = ['+', '-'];492493IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];494495SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];496497SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];498499LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];500501CALL_CLOSERS = ['.', '?.', '::', '?::'];502503}).call(this);504505506