decaffeinate-parser
Version:
A better AST for CoffeeScript, inspired by CoffeeScriptRedux.
33 lines (32 loc) • 1.33 kB
JavaScript
import { SourceType } from 'coffee-lex';
/**
* Given the ranges of two operands, determine from the token list whether there
* is a real '+' operator between them. A plus operation without an actual '+'
* operator is an implicit string interpolation operation.
*/
export default function isPlusTokenBetweenRanges(leftRange, rightRange, context) {
var tokens = context.sourceTokens;
var leftEnd = tokens.indexOfTokenContainingSourceIndex(leftRange[1] - 1);
var rightStart = tokens.indexOfTokenContainingSourceIndex(rightRange[0]);
// Normal '+' operators should find tokens here, so if we don't, this must be
// an implicit '+' operator.
if (!leftEnd || !rightStart) {
return false;
}
var afterLeftEnd = leftEnd.next();
if (!afterLeftEnd) {
return false;
}
var tokensBetweenOperands = tokens.slice(afterLeftEnd, rightStart);
// If we find an actual operator, this must have been a real '+'. Otherwise,
// this must be an implicit '+'.
var foundPlusToken = false;
tokensBetweenOperands.forEach(function (_a) {
var type = _a.type, start = _a.start, end = _a.end;
if (type === SourceType.OPERATOR &&
context.source.slice(start, end) === '+') {
foundPlusToken = true;
}
});
return foundPlusToken;
}