-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(auto-) generate a typescript d.ts
file for better IDE Support
#106
Comments
My hand generated file // ast-types.d.ts
declare module AstTypes {
export interface Type {
check(x) : boolean
assert(x) : boolean
}
export interface SourceLocationType {
type: string
start: PositionType
end: PositionType
source?: string
}
export interface PositionType {
type: string
line: number
column: number
}
export interface PrintableType {
loc: SourceLocationType
}
export interface CommentType extends PrintableType {
value: string
leading: boolean
trailing: boolean
}
export interface NodeType extends PrintableType {
type: string
comments?: CommentType[]
}
export interface PatternType extends NodeType {}
export interface ExpressionType extends NodeType, PatternType {}
export interface AssignmentExpressionType extends ExpressionType {
operator: string
left: PatternType
right: ExpressionType
}
export interface TypeType extends NodeType {}
export interface TypeAnnotationType extends NodeType {
typeAnnotation: TypeType
}
export interface IdentifierType extends NodeType, ExpressionType, PatternType {
name: string
typeAnnotation?: TypeAnnotationType
}
export interface StatementType extends Node {}
export interface ReturnStatementType extends StatementType {
argument?: ExpressionType
}
export interface NamedTypes {
Expression: Type
}
export interface Builders {
identifier(name: string): IdentifierType
assignmentExpression(operator: string, left: PatternType, right: ExpressionType): AssignmentExpressionType
returnStatement(argument?: ExpressionType): ReturnStatementType
}
export interface BuiltInTypes {
string: Type
function: Type
array: Type
object: Type
RegExp: Type
Date: Type
number: Type
boolean: Type
null: Type
undefined: Type
}
export interface Base {
namedTypes: NamedTypes
builders: Builders
builtInTypes: BuiltInTypes
}
} |
This is a really neat idea. I'm happy to make any adjustments necessary to ease the auto-generation of the interface file. What if Once https://github.com/estree/estree is more complete, I'd like to consume it as an input format, so it would be great if |
Yep, I was thinking along those lines. It might also be necessary/desirable to add an optional parameter to the Also, it seems
Interesting. Thanks for the link. Is anyone from the Typescript team involved with that? The syntax looks pretty similar. |
I am going to collect some relevant resources in this comment. Just for my reference as I work on this:
|
Maybe not the final solution, but this could also help with #44. interface NodePath<T> {
node: T
// ...
}
interface Visitor {
visitReturnStatement?(path:NodePath<ReturnStatementType>):any
// ...
} |
See Declaration merging possibly applicable if we process each file in Separate, |
@benjamn |
Yeah, I just generated spec-compliant definitions and updating from time to time using a script. |
Here is generated declarations for Edit: I understand these are incomplete and unfinished, but it allows IDE completions right now for core functionality. I believe that to do proper generation a refactoring is needed. In particular, there should be a way to get correct type names for any field definition. Currently things like |
@spontaliku-softaria hey, do you plan to PR this work ? I think having .d.ts will add great value, thanks! |
@cancerberoSgx no, I'm not using the tool where I needed declarations anymore. And complete declarations might requre big changes in ast-types, which I'm not qualified to do. |
This issue can be closed since #300 was brought in. |
Since basically every interesting method of this library is dynamically generated, it's impossible for IDE's like WebStorm to provide much help with autocompletion and/or static type analysis. I don't want to know how many hours I have lost context switching between my code and
def/*
, trying to figure out what the correct signature was for various build methods.I created a minimal one by hand (currently only supports
Identifier
,ReturnStatement
, andAssignmentExpression
).Note that it's utility would not be limited to Typescript users. IDE's can use them to provide hints for plain old javascript code as well. All the screenshots below are actually code hints generated for a javascript file.
Code Completion
Type Checking
Method Signature Hints
Webstorm just knows the type of the
assign
variable.Everything we need to auto-generate the definition file is already in
def/*
, but we would probably need to extendType
to expose the information it contains in a format readily consumed by the generator (I guess I could parseType
stoString
output, but that seems unnecessarily difficult).The generated file for the default language definitions shipped with
ast-types
should probably be deployed intonpm
as a static asset, but it would also be possible to generate it via apostinstall
script. Ideally we would provide programmatic access as well, so those extending the language definitions (i.e. #57) could create their own definition files that reflect their modifications.I am happy to submit a PR if @benjamn is on board. I am not sure how close #57 is to being a reality, but it would be nice to see what direction you were taking with that before starting on this in earnest.
The text was updated successfully, but these errors were encountered: