summaryrefslogtreecommitdiff
path: root/src/compiler/parse/acorn.ts
blob: c70756d79a578415ca67daa41ac8fb491a33e142 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { Node } from 'acorn';
import { parseExpression } from '@babel/parser';
// import acorn from 'acorn';
// // @ts-ignore
// import jsx from 'acorn-jsx';
// const acornJsx = acorn.Parser.extend(jsx());

export const parse = (source: string): Node => {
  throw new Error('No longer used.');
  // acorn.parse(source, {
  //   sourceType: 'module',
  //   ecmaVersion: 2020,
  //   locations: true,
  // });
};

export const parse_expression_at = (source: string, index: number): number => {
  // TODO: Clean up after acorn -> @babel/parser move
  try {
    // First, try to parse the expression. Unlike acorn, @babel/parser isn't relaxed
    // enough to just stop after the first expression, so we almost always expect a
    // parser error here instead. This is expected, so handle it.
    parseExpression(source.slice(index), {
      sourceType: 'module',
      plugins: ['jsx', 'typescript'],
    });
    throw new Error('Parse error.'); // Expected to fail.
  } catch (err) {
    if (err.message.startsWith('Unexpected token') && source[index + err.pos] === '}') {
      return index + err.pos;
    }
    if (err.pos) {
      err.pos = index + err.pos;
    }
    throw err;
  }
};
// acornJsx.parseExpressionAt(source, index, {
//   sourceType: 'module',
//   ecmaVersion: 2020,
//   locations: true,
// });