Closed cecicifu closed 1 year ago
could u p fo vi d
What ? I don't understand what do you mean
could u provide the code u gonna to parse?
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 will check it
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
.
Just using acorn-typescript
could help u to parse jsx
or tsx
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
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
You should provide the code you want to parse, So i can check if its a issue in plugin
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
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, }, })
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
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;
HAHA, I known that, while u parse the code u should enable locations in acorn options
I will perfect the readme of this plugin
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
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.
ok nice. I look forward to that feature. Thank you Tyreal 😊. You can close the issue.
Hi, im getting the error: TypeError: Class constructors cannot be invoked without 'new'
Stack trace...
Part of my code...
Weird errors in index.d.ts from the library...
Thanks.