TyrealHu / acorn-typescript

Alternative, TypeScript parser
https://www.npmjs.com/package/acorn-typescript?activeTab=readme
MIT License
145 stars 18 forks source link

Error calling parseExpressionAt #4

Closed ota-meshi closed 1 year ago

ota-meshi commented 1 year ago

I have tried the code below but it seems to result in an error.

import * as acorn from "acorn";
import { tsPlugin } from "acorn-typescript";

const acornTs = acorn.Parser.extend(tsPlugin());

// OK
let node = acorn.Parser.parseExpressionAt("<tag prop={foo}/>", 11, {
  sourceType: "module",
  ecmaVersion: "latest",
  locations: true,
});
console.log(node);

// Error
node = acornTs.parseExpressionAt("<tag prop={foo as number}/>", 11, {
  sourceType: "module",
  ecmaVersion: "latest",
  locations: true,
});

console.log(node);

console:

TypeError: i.parseExpressionAt is not a function
    at e.parseExpressionAt (/xxxxxxxxx/node_modules/.pnpm/acorn-typescript@1.2.4_acorn@8.8.2/node_modules/acorn-typescript/lib/index.js:1:100013)

I think we probably need to call parser.parseExpression() instead of parser.parseExpressionAt(). https://github.com/TyrealHu/acorn-typescript/blob/48e555ddf43b212b069b156888c8560fedb85c23/src/index.ts#L5172