UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

21 lines (20 loc) 1.15 kB
import { SourceType } from 'coffee-lex'; import { Block, Loop, While } from '../nodes'; import getLocation from '../util/getLocation'; import mapAny from './mapAny'; import mapPossiblyEmptyBlock from './mapPossiblyEmptyBlock'; export default function mapWhile(context, node) { var _a = getLocation(context, node), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw; var startTokenIndex = context.sourceTokens.indexOfTokenStartingAtSourceIndex(start); var startToken = startTokenIndex && context.sourceTokens.tokenAtIndex(startTokenIndex); if (startToken && startToken.type === SourceType.LOOP) { return new Loop(line, column, start, end, raw, mapPossiblyEmptyBlock(context, node.body)); } var condition = mapAny(context, node.condition); var guard = node.guard ? mapAny(context, node.guard) : null; var body = mapPossiblyEmptyBlock(context, node.body); if (body instanceof Block && body.start < condition.start) { body = body.withInline(true); } return new While(line, column, start, end, raw, condition, guard, body, node.condition.inverted === true); }