UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

26 lines (25 loc) 1.32 kB
"use strict"; exports.__esModule = true; var SourceType_1 = require("coffee-lex/dist/SourceType"); var nodes_1 = require("../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. */ 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_1["default"].LPAREN && token.type !== SourceType_1["default"].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 nodes_1.OperatorInfo(context.source.slice(operatorToken.start, operatorToken.end), operatorToken); } exports["default"] = getOperatorInfoInRange;