UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

34 lines (33 loc) 1.69 kB
import { SourceType } from 'coffee-lex'; import { Conditional } from '../nodes'; import getLocation from '../util/getLocation'; import mapAny from './mapAny'; import mapPossiblyEmptyBlock from './mapPossiblyEmptyBlock'; export default function mapIf(context, node) { var _a = getLocation(context, node), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw; var condition = mapAny(context, node.condition); var consequent = mapPossiblyEmptyBlock(context, node.body); var alternate = mapPossiblyEmptyBlock(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 === SourceType.IF && context.source.slice(token.start, token.end) === 'unless'; }, left, right) !== null; } return new Conditional(line, column, start, end, raw, condition, consequent, alternate, isUnless); }