UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

37 lines (36 loc) 1.82 kB
"use strict"; exports.__esModule = true; var coffee_lex_1 = require("coffee-lex"); var nodes_1 = require("../nodes"); var getLocation_1 = require("../util/getLocation"); var mapAny_1 = require("./mapAny"); var mapPossiblyEmptyBlock_1 = require("./mapPossiblyEmptyBlock"); function mapIf(context, node) { var _a = getLocation_1["default"](context, node), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw; var condition = mapAny_1["default"](context, node.condition); var consequent = mapPossiblyEmptyBlock_1["default"](context, node.body); var alternate = mapPossiblyEmptyBlock_1["default"](context, node.elseBody); var isUnless = false; var left = null; var right = null; if (consequent && consequent.start < condition.start) { consequent = consequent.withInline(true); // POST-if, so look for tokens between the consequent and the condition left = context.sourceTokens.indexOfTokenEndingAtSourceIndex(consequent.end); right = context.sourceTokens.indexOfTokenStartingAtSourceIndex(condition.start); } else { // regular `if`, so look from the start of the node until the condition left = context.sourceTokens.indexOfTokenStartingAtSourceIndex(start); right = context.sourceTokens.indexOfTokenStartingAtSourceIndex(condition.start); } if (left && right) { isUnless = context.sourceTokens.indexOfTokenMatchingPredicate(function (token) { return token.type === coffee_lex_1.SourceType.IF && context.source.slice(token.start, token.end) === 'unless'; }, left, right) !== null; } return new nodes_1.Conditional(line, column, start, end, raw, condition, consequent, alternate, isUnless); } exports["default"] = mapIf;