UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

34 lines (33 loc) 1.24 kB
import { SourceType } from 'coffee-lex'; import { Call, Literal, Value } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes'; /** * Determine if the given CoffeeScript AST node is an interpolated heregex node * that's pretending to be a function call to the RegExp function. */ export default function isHeregexTemplateNode(node, context) { if (!(node instanceof Call) || !node.variable || !(node.variable instanceof Value) || !node.variable.base || !(node.variable.base instanceof Literal) || node.variable.base.value !== 'RegExp') { return false; } var sourceTokens = context.sourceTokens, linesAndColumns = context.linesAndColumns; var start = linesAndColumns.indexForLocation({ line: node.locationData.first_line, column: node.locationData.first_column }); if (start === null) { return false; } var startTokenIndex = sourceTokens.indexOfTokenContainingSourceIndex(start); if (startTokenIndex === null) { return false; } var startToken = sourceTokens.tokenAtIndex(startTokenIndex); if (startToken === null) { return false; } return startToken.type === SourceType.HEREGEXP_START; }