Skip to content

Commit

Permalink
feat: parse enum, type argu, and other typescript feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Solo-steven committed Oct 26, 2024
1 parent 3e82239 commit 2611046
Show file tree
Hide file tree
Showing 257 changed files with 13,736 additions and 169 deletions.
79 changes: 70 additions & 9 deletions web-infras/common/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,29 @@ export interface TSInstantiationExpression extends ExpressionModuleItem {
expression: Expression;
}

export interface TSTypeAssertionExpression extends ExpressionModuleItem {
kind: SyntaxKinds.TSTypeAssertionExpression;
expression: Expression;
typeAnnotation: TSTypeNode;
}

export interface TSAsExpression extends ExpressionModuleItem {
kind: SyntaxKinds.TSAsExpression;
expression: Expression;
typeAnnotation: TSTypeNode;
}

export interface TSSatisfiesExpression extends ExpressionModuleItem {
kind: SyntaxKinds.TSSatisfiesExpression;
expression: Expression;
typeAnnotation: TSTypeNode;
}

export interface TSNonNullExpression extends ExpressionModuleItem {
kind: SyntaxKinds.TSNonNullExpression;
expression: Expression;
}

export type Expression =
// jsx element
| JSXElement
Expand Down Expand Up @@ -398,7 +421,11 @@ export type Expression =
| AssigmentExpression
| SequenceExpression
// TS expression
| TSInstantiationExpression;
| TSInstantiationExpression
| TSTypeAssertionExpression
| TSAsExpression
| TSSatisfiesExpression
| TSNonNullExpression;
export interface ExpressionStatement extends ModuleItem {
kind: SyntaxKinds.ExpressionStatement;
expr: Expression;
Expand Down Expand Up @@ -447,7 +474,11 @@ export type Pattern =
| ObjectPattern
| ArrayPattern
| Identifier
| MemberExpression;
| MemberExpression
| TSAsExpression
| TSTypeAssertionExpression
| TSSatisfiesExpression
| TSNonNullExpression;

/** ==========================
* Statement
Expand Down Expand Up @@ -540,6 +571,8 @@ export interface ForOfStatement extends ModuleItem {
await: boolean;
body: Statement;
}
// TODO. better Type
// type ForOfInStatementLeft = VariableDeclaration | Pattern | TSAsExpression | TSTypeAssertionExpression;
export interface ForInStatement extends ModuleItem {
kind: SyntaxKinds.ForInStatement;
left: Expression | VariableDeclaration;
Expand Down Expand Up @@ -648,12 +681,9 @@ export interface Decorator extends ModuleItem {
kind: SyntaxKinds.Decorator;
expression: Expression;
}
export type Declaration =
| FunctionDeclaration
| VariableDeclaration
| ClassDeclaration
| TSTypeAliasDeclaration
| TSInterfaceDeclaration;
export type Declaration = FunctionDeclaration | VariableDeclaration | ClassDeclaration | TSDeclaration;

type TSDeclaration = TSTypeAliasDeclaration | TSInterfaceDeclaration | TSEnumDeclaration | TSDeclareFunction;

/** ==========================================
* Import Declaration
Expand Down Expand Up @@ -696,7 +726,13 @@ export interface ExportSpecifier extends ModuleItem {
}
export interface ExportDefaultDeclaration extends ModuleItem {
kind: SyntaxKinds.ExportDefaultDeclaration;
declaration: FunctionDeclaration | FunctionExpression | ClassDeclaration | ClassExpression | Expression;
declaration:
| FunctionDeclaration
| FunctionExpression
| ClassDeclaration
| ClassExpression
| Expression
| TSDeclaration;
}
export interface ExportAllDeclaration extends ModuleItem {
kind: SyntaxKinds.ExportAllDeclaration;
Expand Down Expand Up @@ -803,9 +839,29 @@ export interface TSTypeParameterInstantiation extends ModuleItem {
export interface TSFunctionType extends TSFunctionSignatureBase {
kind: SyntaxKinds.TSFunctionType;
}
export interface TSDeclareFunction extends Omit<Function, "body">, ModuleItem {
kind: SyntaxKinds.TSDeclareFunction;
}
export interface TSConstrcutorType extends TSFunctionSignatureBase {
kind: SyntaxKinds.TSConstructorType;
}
export interface TSEnumDeclaration extends ModuleItem {
id: Identifier;
kind: SyntaxKinds.TSEnumDeclaration;
body: TSEnumBody;
}

export interface TSEnumBody extends ModuleItem {
kind: SyntaxKinds.TSEnumBody;
members: Array<TSEnumMember>;
}
export interface TSEnumMember extends ModuleItem {
kind: SyntaxKinds.TSEnumMember;
computed: boolean;
id: Identifier;
init: Expression | undefined;
}

export interface TSTypeAliasDeclaration extends ModuleItem {
kind: SyntaxKinds.TSTypeAliasDeclaration;
name: Identifier;
Expand All @@ -817,6 +873,11 @@ export interface TSInterfaceDeclaration extends ModuleItem {
name: Identifier;
body: TSInterfaceBody;
typeParameters: TSTypeParameterDeclaration | undefined;
extends: Array<TSInterfaceHeritage>;
}
export interface TSInterfaceHeritage extends ModuleItem {
typeName: TSEntityName;
typeArguments: TSTypeParameterInstantiation | undefined;
}
export interface TSInterfaceBody extends ModuleItem {
kind: SyntaxKinds.TSInterfaceBody;
Expand Down
143 changes: 143 additions & 0 deletions web-infras/common/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,7 @@ export function createTSInterfaceBody(
export function createTSInterface(
name: AST.TSInterfaceDeclaration["name"],
typeParameters: AST.TSInterfaceDeclaration["typeParameters"],
extendsTypes: AST.TSInterfaceDeclaration["extends"],
body: AST.TSInterfaceDeclaration["body"],
start: SourcePosition,
end: SourcePosition,
Expand All @@ -1777,12 +1778,28 @@ export function createTSInterface(
kind: SyntaxKinds.TSInterfaceDeclaration,
name,
typeParameters,
extends: extendsTypes,
body,
start,
end,
};
}

export function createTSInterfaceHeritage(
typeName: AST.TSEntityName,
typeArguments: AST.TSTypeReference["typeArguments"],
start: SourcePosition,
end: SourcePosition,
): AST.TSInterfaceHeritage {
return {
kind: SyntaxKinds.TSInterfaceHeritage,
typeName,
typeArguments,
start,
end,
};
}

export function createTSTypeParameterInstantiation(
params: Array<AST.TSTypeNode>,
start: SourcePosition,
Expand Down Expand Up @@ -2009,3 +2026,129 @@ export function createTSInstantiationExpression(
end,
};
}

export function createTSTypeAssertionExpression(
expression: AST.TSTypeAssertionExpression["expression"],
typeAnnotation: AST.TSTypeAssertionExpression["typeAnnotation"],
start: SourcePosition,
end: SourcePosition,
): AST.TSTypeAssertionExpression {
return {
kind: SyntaxKinds.TSTypeAssertionExpression,
expression,
typeAnnotation,
start,
end,
};
}

export function createTSAsExpression(
expression: AST.TSAsExpression["expression"],
typeAnnotation: AST.TSAsExpression["typeAnnotation"],
start: SourcePosition,
end: SourcePosition,
): AST.TSAsExpression {
return {
kind: SyntaxKinds.TSAsExpression,
expression,
typeAnnotation,
start,
end,
};
}

export function createTSSatisfiesExpression(
expression: AST.TSSatisfiesExpression["expression"],
typeAnnotation: AST.TSSatisfiesExpression["typeAnnotation"],
start: SourcePosition,
end: SourcePosition,
): AST.TSSatisfiesExpression {
return {
kind: SyntaxKinds.TSSatisfiesExpression,
expression,
typeAnnotation,
start,
end,
};
}

export function createTSNonNullExpression(
expression: AST.TSNonNullExpression["expression"],
start: SourcePosition,
end: SourcePosition,
): AST.TSNonNullExpression {
return {
kind: SyntaxKinds.TSNonNullExpression,
expression,
start,
end,
};
}

export function createTSEnumDeclaration(
id: AST.TSEnumDeclaration["id"],
body: AST.TSEnumDeclaration["body"],
start: SourcePosition,
end: SourcePosition,
): AST.TSEnumDeclaration {
return {
kind: SyntaxKinds.TSEnumDeclaration,
id,
body,
start,
end,
};
}

export function createTSEnumBody(
members: AST.TSEnumBody["members"],
start: SourcePosition,
end: SourcePosition,
): AST.TSEnumBody {
return {
kind: SyntaxKinds.TSEnumBody,
members,
start,
end,
};
}

export function createTSEnumMember(
id: AST.TSEnumMember["id"],
computed: AST.TSEnumMember["computed"],
init: AST.TSEnumMember["init"],
start: SourcePosition,
end: SourcePosition,
): AST.TSEnumMember {
return {
kind: SyntaxKinds.TSEnumMember,
computed,
id,
init,
start,
end,
};
}

export function createTSDeclarFunction(
name: AST.TSDeclareFunction["name"],
returnType: AST.TSDeclareFunction["returnType"],
params: AST.TSDeclareFunction["params"],
typeParameters: AST.TSDeclareFunction["typeParameters"],
generator: AST.TSDeclareFunction["generator"],
async: AST.TSDeclareFunction["async"],
start: SourcePosition,
end: SourcePosition,
): AST.TSDeclareFunction {
return {
kind: SyntaxKinds.TSDeclareFunction,
name,
params,
returnType,
typeParameters,
generator,
async,
start,
end,
};
}
19 changes: 19 additions & 0 deletions web-infras/common/src/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,24 @@ export enum SyntaxKinds {
// ========= Type Interface and Type Alias =====
TSTypeAliasDeclaration,
TSInterfaceDeclaration,
TSInterfaceHeritage,
TSInterfaceBody,
TSTypeLiteral,
TSCallSignatureDeclaration,
TSConstructSignatureDeclaration,
TSIndexSignature,
TSPropertySignature,
TSMethodSignature,
// ======== Type Enum
TSEnumDeclaration,
TSEnumBody,
TSEnumMember,
// ========= Type Expression
TSInstantiationExpression,
TSTypeAssertionExpression,
TSAsExpression,
TSSatisfiesExpression,
TSNonNullExpression,
// ========= Type Parameter
TSTypeParameterInstantiation,
TSTypeParameterDeclaration,
Expand All @@ -284,6 +293,7 @@ export enum SyntaxKinds {
TSTypeQuery,
TSTupleType,
TSLiteralType,
TSDeclareFunction,
// ======= Atom Basic Type ======
TSStringKeyword,
TSNumberKeyword,
Expand Down Expand Up @@ -774,6 +784,15 @@ export const SytaxKindsMapLexicalLiteral: Record<SyntaxKinds, string> = {
[SyntaxKinds.TSLiteralType]: "TSLiteralType",
[SyntaxKinds.TSVoidKeyword]: "TSVoidKeyword",
[SyntaxKinds.TSInstantiationExpression]: "TSInstantiationExpression",
[SyntaxKinds.TSInterfaceHeritage]: "TSInterfaceHeritage",
[SyntaxKinds.TSTypeAssertionExpression]: "TSTypeAssertionExpression",
[SyntaxKinds.TSAsExpression]: "TSAsExpression",
[SyntaxKinds.TSSatisfiesExpression]: "TSSatisfiesExpression",
[SyntaxKinds.TSNonNullExpression]: "TSNonNullExpression",
[SyntaxKinds.TSEnumDeclaration]: "TSEnumDeclaration",
[SyntaxKinds.TSEnumBody]: "TSEnumBody",
[SyntaxKinds.TSEnumMember]: "TSEnumMember",
[SyntaxKinds.TSDeclareFunction]: "TSDeclareFunction",
};
/** ===================================
* Union SytaxKinds
Expand Down
Loading

0 comments on commit 2611046

Please sign in to comment.