UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

25 lines (24 loc) 1.38 kB
import SourceType from 'coffee-lex/dist/SourceType'; import { InOp } from '../nodes'; import getLocation from '../util/getLocation'; import mapAny from './mapAny'; export default function mapIn(context, node) { // We don't use the `negated` flag on `node` because it gets set to // `true` when a parent `If` is an `unless`. var _a = getLocation(context, node), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw; var left = mapAny(context, node.object); var right = mapAny(context, node.array); var isNot = false; var lastTokenIndexOfLeft = context.sourceTokens.indexOfTokenEndingAtSourceIndex(left.end); var firstTokenIndexOfRight = context.sourceTokens.indexOfTokenStartingAtSourceIndex(right.start); var relationTokenIndex = context.sourceTokens.indexOfTokenMatchingPredicate(function (token) { return token.type === SourceType.RELATION; }, lastTokenIndexOfLeft, firstTokenIndexOfRight); if (relationTokenIndex) { var relationToken = context.sourceTokens.tokenAtIndex(relationTokenIndex); if (relationToken) { isNot = context.source.slice(relationToken.start, relationToken.end) !== 'in'; return new InOp(line, column, start, end, raw, left, right, isNot); } } throw new Error("unable to find RELATION token between operands"); }