UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

23 lines (22 loc) 1.22 kB
import SourceType from 'coffee-lex/dist/SourceType'; import { OperatorInfo } from '../nodes'; /** * Gets information about an operator token found between start and end source * offsets, exclusive. When calling, make sure `start` is before the text of * the operator in the range, and `end` is after it. */ export default function getOperatorInfoInRange(context, start, end) { var startIndex = context.sourceTokens.indexOfTokenNearSourceIndex(start); var endIndex = context.sourceTokens.indexOfTokenNearSourceIndex(end); if (!startIndex || !endIndex) { throw new Error("cannot find token indexes of range bounds: [" + start + ", " + end + "]"); } var operatorIndex = context.sourceTokens.indexOfTokenMatchingPredicate(function (token) { return token.type !== SourceType.LPAREN && token.type !== SourceType.RPAREN; }, startIndex.next(), endIndex); var operatorToken = operatorIndex && context.sourceTokens.tokenAtIndex(operatorIndex); if (!operatorToken) { throw new Error("cannot find operator token in range: [" + start + ", " + end + "]"); } return new OperatorInfo(context.source.slice(operatorToken.start, operatorToken.end), operatorToken); }