TyrealHu / acorn-typescript

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

TypeError: Class constructors cannot be invoked without 'new' #29

Closed cecicifu closed 1 year ago

cecicifu commented 1 year ago

Hi, im getting the error: TypeError: Class constructors cannot be invoked without 'new'

Stack trace...

MyExtension error: TypeError: Class constructors cannot be invoked without 'new'
    at e [as constructor] (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:15137)
    at e [as constructor] (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:16812)
    at new e (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:22553)
    at Function.e.parse (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:98376)
    at ClassAnalyzer.astParse (c:\Users\myuser\Desktop\Workspace\my-extension\src\ClassAnalyzer.ts:20:4)
    at c:\Users\myuser\Desktop\Workspace\my-extension\src\ClassAnalyzer.ts:47:22
    at Array.forEach (<anonymous>)
    at ClassAnalyzer.getComponents (c:\Users\myuser\Desktop\Workspace\my-extension\src\ClassAnalyzer.ts:44:7)
    at MyExtension.provideCompletionItems (c:\Users\myuser\Desktop\Workspace\my-extension\src\MyExtension.ts:69:44)
    at oe.provideCompletionItems (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:100:52903)
    at c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:100:73510
    at le.s (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:100:65165)
    at le.$provideCompletionItems (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:100:73496)
    at a.N (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:11620)
    at a.M (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:11338)
    at a.H (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:10393)
    at a.G (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:9412)
    at c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:8200
    at v.invoke (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:63:145)
    at h.deliver (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:63:2121)
    at i.fire (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:63:1729)
    at g.fire (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:72:14852)
    at c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:126:32403
    at v.invoke (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:63:145)
    at h.deliver (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:63:2121)
    at i.fire (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:63:1729)
    at g.fire (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:72:14852)
    at MessagePortMain.<anonymous> (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:126:30529)
    at MessagePortMain.emit (C:\Users\myuser\Desktop\Workspace\my-extension\lib\events.js:513:28)
    at MessagePortMain.emit (C:\Users\myuser\Desktop\Workspace\my-extension\lib\domain.js:489:12)
    at Object.MessagePortMain._internalPort.emit (C:\Users\myuser\Desktop\Workspace\my-extension\lib\electron\js2c\utility_init.js:2:367)
    at Object.topLevelDomainCallback (C:\Users\myuser\Desktop\Workspace\my-extension\lib\domain.js:161:15)
    at Object.callbackTrampoline (node:internal/async_hooks:128:24) {vslsStack: Array(33), stack: 'TypeError: Class constructors cannot be invok…Trampoline (node:internal/async_hooks:128:24)', message: 'Class constructors cannot be invoked without 'new''}

Part of my code...

import { Node, Parser } from "acorn";
import { tsPlugin } from "acorn-typescript";
import { lstatSync, readFileSync, readdirSync } from "fs";
import path = require("path");

export class ClassAnalyzer{
  private parser: any;

  constructor() {
    this.parser = Parser.extend(
      require("acorn-jsx")(),
      tsPlugin({
        dts: true,
      })
    );
  }
...

Weird errors in index.d.ts from the library... image

Thanks.

cecicifu commented 1 year ago

could u p fo vi d

What ? I don't understand what do you mean

TyrealHu commented 1 year ago

could u provide the code u gonna to parse?

cecicifu commented 1 year ago

Sure!

index.d.ts

import type { ReactNode, Ref } from "react";
import type { CommonProps } from "./types";

export type SlideApi = {
  goToNextSlide: () => void;
  goToPreviousSlide: () => void;
};

export type SlideProps = CommonProps & {
  children: ReactNode;
  defaultSlide?: number;
  onSlideChange?: (slide: number) => void;
  ref?: Ref<SlideApi>;
};

declare function SlideProps(props: SlideProps): JSX.Element;

export default SlideProps;

Another example of code that I want to parse..

CustomButton.jsx

import React, { forwardRef } from "react";
import PropTypes from "prop-types";

const CustomButton = forwardRef(
  (
    {
      iconStart,
      iconEnd,
      text
    },
    ref
  ) => {
    return (
      <Button ref={ref}>
        {iconStart}
        {text}
        {iconEnd}
      </Button>
    );
  }
);

CustomButton.displayName = "CustomButton";

CustomButton.propTypes = {
  /**
   * Main text.
   */
  text: PropTypes.string,

  /**
   * An optional icon at the start.
   */
  iconStart: PropTypes.element,

  /**
   * An optional icon at the end.
   */
  iconEnd: PropTypes.element,
};

export default CustomButton;
TyrealHu commented 1 year ago

i will check it

TyrealHu commented 1 year ago

constructor() { this.parser = Parser.extend( require("acorn-jsx")(), tsPlugin({ dts: true, }) ); }

You shouldn't use acorn-typescript with acorn-jsx. Maybe its a bug using acorn-typescript with acorn-jsx.

TyrealHu commented 1 year ago

Just using acorn-typescript could help u to parse jsx or tsx

TyrealHu commented 1 year ago

index.d.ts

import type { ReactNode, Ref } from "react";
import type { CommonProps } from "./types";

export type SlideApi = {
  goToNextSlide: () => void;
  goToPreviousSlide: () => void;
};

export type SlideProps = CommonProps & {
  children: ReactNode;
  defaultSlide?: number;
  onSlideChange?: (slide: number) => void;
  ref?: Ref<SlideApi>;
};

declare function SlideProps(props: SlideProps): JSX.Element;

export default SlideProps;

duplicate definition in dts has been fixed in acorn-typescript@1.3.5

cecicifu commented 1 year ago

Thanks for the reply 😊, I tried using your solution and now I'm getting another error:

Stack trace

MyExtension error: TypeError: Cannot read properties of undefined (reading 'index')
    at e.p.hasPrecedingLineBreak (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:30676)
    at e.p.tsParseArrayTypeOrHigher (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47156)
    at c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47720
    at e.p.tsInAllowConditionalTypesContext (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:37496)
    at e.p.tsParseTypeOperatorOrHigher (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47667)
    at e.p.tsParseUnionOrIntersectionType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:40745)
    at e.p.tsParseIntersectionTypeOrHigher (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47807)
    at e.p.tsParseUnionOrIntersectionType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:40745)
    at e.p.tsParseUnionTypeOrHigher (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47968)
    at e.p.tsParseNonConditionalType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:48391)
    at e.p.tsParseType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:48481)
    at c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:38367
    at e.p.tsInType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:49207)
    at e.p.tsParseTypeAnnotation (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:38307)
    at c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:39690
    at e.p.tsInType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:49207)
    at e.p.tsParseTypeOrTypePredicateAnnotation (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:39024)
    at e.p.tsFillSignature (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:40112)
    at c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:40593
    at e.p.tsInAllowConditionalTypesContext (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:37496)
    at e.p.tsParseFunctionOrConstructorType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:40540)
    at e.p.tsParseNonConditionalType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:48157)
    at e.p.tsParseType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:48481)
    at c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:38367
    at e.p.tsInType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:49207)
    at e.p.tsParseTypeAnnotation (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:38307)
    at e.p.tsTryParseTypeAnnotation (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:26573)
    at e.p.tsParsePropertyOrMethodSignature (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:55410)
    at e.p.tsParseTypeMember (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:56367)
    at e.p.tsParseList (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:56495)
    at e.p.tsParseObjectTypeMembers (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:56581)
    at e.p.tsParseTypeLiteral (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:43658)
    at e.p.tsParseNonArrayType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:46168)
    at e.p.tsParseArrayTypeOrHigher (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47128)
    at c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47720
    at e.p.tsInAllowConditionalTypesContext (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:37496)
    at e.p.tsParseTypeOperatorOrHigher (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47667)
    at e.p.tsParseUnionOrIntersectionType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:40745)
    at e.p.tsParseIntersectionTypeOrHigher (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47807)
    at e.p.tsParseUnionOrIntersectionType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:40745)
    at e.p.tsParseUnionTypeOrHigher (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:47968)
    at e.p.tsParseNonConditionalType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:48391)
    at e.p.tsParseType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:48481)
    at c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:59801
    at e.p.tsInType (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:49207)
    at e.p.tsParseTypeAliasDeclaration (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:59544)
    at e.p.tsParseDeclaration (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:60449)
    at e.p.tsTryParseExportDeclaration (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:60535)
    at e.p.parseExportDeclaration (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:75355)
    at e.p.parseExport (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:68098)
    at e.pp$8.parseStatement (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn\dist\acorn.js:937:76)
    at e.p.parseStatement (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:73246)
    at e.pp$8.parseTopLevel (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn\dist\acorn.js:818:23)
    at e.parse (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn\dist\acorn.js:590:17)
    at Function.e.parse (c:\Users\myuser\Desktop\Workspace\my-extension\node_modules\acorn-typescript\lib\index.js:1:98442)
    at ClassAnalyzer.astParse (c:\Users\myuser\Desktop\Workspace\my-extension\src\ClassAnalyzer.ts:19:24)
    at c:\Users\myuser\Desktop\Workspace\my-extension\src\ClassAnalyzer.ts:45:24
    at Array.forEach (<anonymous>)
    at ClassAnalyzer.getComponents (c:\Users\myuser\Desktop\Workspace\my-extension\src\ClassAnalyzer.ts:43:11)
    at Extension.provideCompletionItems (c:\Users\myuser\Desktop\Workspace\my-extension\src\Extension.ts:66:44)
    at oe.provideCompletionItems (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:100:52903)
    at c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:100:73510
    at le.s (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:100:65165)
    at le.$provideCompletionItems (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:100:73496)
    at a.N (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:11620)
    at a.M (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:11338)
    at a.H (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:10393)
    at a.G (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:9412)
    at c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:110:8200
    at v.invoke (c:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:63:145)
    a… ……

My code

import { Node, Parser } from "acorn";
import { tsPlugin } from "acorn-typescript"; // I have also tried: import tsPlugin from "acorn-typescript";
import { lstatSync, readFileSync, readdirSync } from "fs";
import path = require("path");

export class ClassAnalyzer{
  private parser: any;

  constructor() {
    this.parser = Parser.extend(
      tsPlugin({
        dts: true,
        jsx: {
          allowNamespaces: true,
        },
      })
    );
  }
...

And.. the library keeps having errors

image

TyrealHu commented 1 year ago

You should provide the code you want to parse, So i can check if its a issue in plugin

cecicifu commented 1 year ago

Sure!

index.d.ts

import type { ReactNode, Ref } from "react";
import type { CommonProps } from "./types";

export type SlideApi = {
  goToNextSlide: () => void;
  goToPreviousSlide: () => void;
};

export type SlideProps = CommonProps & {
  children: ReactNode;
  defaultSlide?: number;
  onSlideChange?: (slide: number) => void;
  ref?: Ref<SlideApi>;
};

declare function SlideProps(props: SlideProps): JSX.Element;

export default SlideProps;

Another example of code that I want to parse..

CustomButton.jsx

import React, { forwardRef } from "react";
import PropTypes from "prop-types";

const CustomButton = forwardRef(
  (
    {
      iconStart,
      iconEnd,
      text
    },
    ref
  ) => {
    return (
      <Button ref={ref}>
        {iconStart}
        {text}
        {iconEnd}
      </Button>
    );
  }
);

CustomButton.displayName = "CustomButton";

CustomButton.propTypes = {
  /**
   * Main text.
   */
  text: PropTypes.string,

  /**
   * An optional icon at the start.
   */
  iconStart: PropTypes.element,

  /**
   * An optional icon at the end.
   */
  iconEnd: PropTypes.element,
};

export default CustomButton;

I'm providing the same code above

TyrealHu commented 1 year ago
  tsPlugin({
        dts: true,
        jsx: {
          allowNamespaces: true,
        },
      })

It seems that u want to parse a tsx file but u tag it to dts. If u want to parse dts.

  tsPlugin({
        dts: true
      })

If u want to parse tsx/ts.

  tsPlugin({
        jsx: {
          allowNamespaces: true,
        },
      })
cecicifu commented 1 year ago

I want to parse both (.d.ts & .jsx). Anyway I tried to parse only the dts with only the option dts: true and I'm getting same error

TyrealHu commented 1 year ago

I want to parse both (.d.ts & .jsx). Anyway I tried to parse only the dts with only the option dts: true and I'm getting same error

What version do u use? I have allready added the test for following code, and ut is passed

import type { ReactNode, Ref } from "react";
import type { CommonProps } from "./types";
export type SlideApi = {
  goToNextSlide: () => void;
  goToPreviousSlide: () => void;
};
export type SlideProps = CommonProps & {
  children: ReactNode;
  defaultSlide?: number;
  onSlideChange?: (slide: number) => void;
  ref?: Ref<SlideApi>;
};
declare function SlideProps(props: SlideProps): JSX.Element;
export default SlideProps;
TyrealHu commented 1 year ago

here https://github.com/TyrealHu/acorn-typescript/blob/master/__test__/expression/type.test.ts

TyrealHu commented 1 year ago

HAHA, I known that, while u parse the code u should enable locations in acorn options

TyrealHu commented 1 year ago

I will perfect the readme of this plugin

cecicifu commented 1 year ago

hahahaha nice, works perfect! But.. one question, I want to parse both files (dts and jsx/tsx), doesn't the plugin allow it?

Thank you so much

TyrealHu commented 1 year ago

doesn't the plugin allow it

acorn doesn't support a runtime env options. So i didn't know the code u want to parse is a tsx/ts file or a dts file. The two files are in conflict. Maybe i will give a PR to acorn to solve this situation.

cecicifu commented 1 year ago

ok nice. I look forward to that feature. Thank you Tyreal 😊. You can close the issue.