base.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import State from "../tokenizer/state";
  2. import {charCodes} from "../util/charcodes";
  3. export let isJSXEnabled;
  4. export let isTypeScriptEnabled;
  5. export let isFlowEnabled;
  6. export let state;
  7. export let input;
  8. export let nextContextId;
  9. export function getNextContextId() {
  10. return nextContextId++;
  11. }
  12. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  13. export function augmentError(error) {
  14. if ("pos" in error) {
  15. const loc = locationForIndex(error.pos);
  16. error.message += ` (${loc.line}:${loc.column})`;
  17. error.loc = loc;
  18. }
  19. return error;
  20. }
  21. export class Loc {
  22. constructor(line, column) {
  23. this.line = line;
  24. this.column = column;
  25. }
  26. }
  27. export function locationForIndex(pos) {
  28. let line = 1;
  29. let column = 1;
  30. for (let i = 0; i < pos; i++) {
  31. if (input.charCodeAt(i) === charCodes.lineFeed) {
  32. line++;
  33. column = 1;
  34. } else {
  35. column++;
  36. }
  37. }
  38. return new Loc(line, column);
  39. }
  40. export function initParser(
  41. inputCode,
  42. isJSXEnabledArg,
  43. isTypeScriptEnabledArg,
  44. isFlowEnabledArg,
  45. ) {
  46. input = inputCode;
  47. state = new State();
  48. nextContextId = 1;
  49. isJSXEnabled = isJSXEnabledArg;
  50. isTypeScriptEnabled = isTypeScriptEnabledArg;
  51. isFlowEnabled = isFlowEnabledArg;
  52. }