Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 35626d0

Browse files
author
Martynas Žilinskas
authored
Merge pull request #93 from SimplrJS/dev
v4.0.0-beta.7
2 parents ef19437 + 546f26b commit 35626d0

16 files changed

Lines changed: 368 additions & 22 deletions

examples/simple/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export function Foo(arg: string | number) {
2-
return arg;
3-
}
1+
export const ObjectLiteralConst = { Property: "value" };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-extractor",
3-
"version": "4.0.0-beta.6",
3+
"version": "4.0.0-beta.7",
44
"description": "TypeScript AST extractor to useful JSON structure.",
55
"keywords": [
66
"typescript",

src/api-helpers.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export namespace ApiHelpers {
6666
apiItem = new ApiEnumMember(declaration, symbol, options);
6767
} else if (ts.isInterfaceDeclaration(declaration)) {
6868
apiItem = new ApiInterface(declaration, symbol, options);
69-
} else if (ts.isPropertySignature(declaration)) {
69+
} else if (ts.isPropertySignature(declaration) || ts.isPropertyAssignment(declaration)) {
7070
apiItem = new ApiProperty(declaration, symbol, options);
7171
} else if (ts.isMethodSignature(declaration)) {
7272
apiItem = new ApiMethod(declaration, symbol, options);
@@ -94,9 +94,9 @@ export namespace ApiHelpers {
9494
apiItem = new ApiConstruct(declaration, symbol, options);
9595
} else if (ts.isTypeParameterDeclaration(declaration)) {
9696
apiItem = new ApiTypeParameter(declaration, symbol, options);
97-
} else if (ts.isTypeLiteralNode(declaration)) {
97+
} else if (ts.isTypeLiteralNode(declaration) || ts.isObjectLiteralExpression(declaration)) {
9898
apiItem = new ApiTypeLiteral(declaration, symbol, options);
99-
} else if (ts.isFunctionTypeNode(declaration)) {
99+
} else if (ts.isFunctionTypeNode(declaration) || ts.isArrowFunction(declaration) || ts.isFunctionExpression(declaration)) {
100100
apiItem = new ApiFunctionType(declaration, symbol, options);
101101
} else if (ts.isMappedTypeNode(declaration)) {
102102
apiItem = new ApiMapped(declaration, symbol, options);
@@ -181,12 +181,22 @@ export namespace ApiHelpers {
181181
}
182182
const symbolItems: string[] = [];
183183

184-
symbol.declarations.forEach(declaration => {
184+
// Filter out the same namespace.
185+
const filteredDeclarations: ts.Declaration[] = [];
186+
for (const declaration of symbol.declarations) {
187+
if (filteredDeclarations.find(x => ts.isModuleDeclaration(x)) != null) {
188+
continue;
189+
}
190+
191+
filteredDeclarations.push(declaration);
192+
}
193+
194+
for (const declaration of filteredDeclarations) {
185195
const itemId = GetItemId(declaration, symbol, options);
186196
if (itemId != null) {
187197
symbolItems.push(itemId);
188198
}
189-
});
199+
}
190200

191201
return {
192202
Alias: symbol.name,
@@ -263,7 +273,7 @@ export namespace ApiHelpers {
263273
export function LogWithNodePosition(logLevel: LogLevel, declaration: ts.Node, message: string): void {
264274
const sourceFile = declaration.getSourceFile();
265275
const position = sourceFile.getLineAndCharacterOfPosition(declaration.getStart());
266-
const linePrefix = `${sourceFile.fileName}[${position.line + 1}:${position.character + 1}]`;
276+
const linePrefix = `${sourceFile.fileName}(${position.line + 1},${position.character + 1})`;
267277
Logger.Log(logLevel, `${linePrefix}: ${message}`);
268278
}
269279

src/contracts/api-item-kinds.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export enum ApiItemKinds {
66
Interface = "interface",
77
Method = "method",
88
Namespace = "namespace",
9+
ImportNamespace = "import-namespace",
910
Parameter = "parameter",
1011
Property = "property",
1112
Variable = "variable",
@@ -24,6 +25,9 @@ export enum ApiItemKinds {
2425
ImportSpecifier = "import-specifier",
2526
TypeParameter = "type-parameter",
2627
TypeLiteral = "type-literal",
28+
ObjectLiteral = "object-literal",
2729
FunctionType = "function-type",
30+
ArrowFunction = "arrow-function",
31+
FunctionExpression = "function-expression",
2832
Mapped = "mapped"
2933
}

src/contracts/definitions/api-function-type-dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { ApiCallableDto } from "../api-callable-dto";
22
import { ApiItemKinds } from "../api-item-kinds";
33

44
export interface ApiFunctionTypeDto extends ApiCallableDto {
5-
ApiKind: ApiItemKinds.FunctionType;
5+
ApiKind: ApiItemKinds.FunctionType | ApiItemKinds.ArrowFunction | ApiItemKinds.FunctionExpression;
66
}

src/contracts/definitions/api-namespace-dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { ApiItemReference } from "../api-item-reference";
33
import { ApiItemKinds } from "../api-item-kinds";
44

55
export interface ApiNamespaceDto extends ApiBaseItemDto {
6-
ApiKind: ApiItemKinds.Namespace;
6+
ApiKind: ApiItemKinds.Namespace | ApiItemKinds.ImportNamespace;
77
Members: ApiItemReference[];
88
}

src/contracts/definitions/api-type-literal-dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { ApiItemReference } from "../api-item-reference";
33
import { ApiItemKinds } from "../api-item-kinds";
44

55
export interface ApiTypeLiteralDto extends ApiBaseItemDto {
6-
ApiKind: ApiItemKinds.TypeLiteral;
6+
ApiKind: ApiItemKinds.TypeLiteral | ApiItemKinds.ObjectLiteral;
77
Members: ApiItemReference[];
88
}

src/definitions/api-function-type.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@ import { ApiMetadataDto } from "../contracts/api-metadata-dto";
77
import { ApiCallableBase } from "../abstractions/api-callable-base";
88
import { ApiItemLocationDto } from "../contracts/api-item-location-dto";
99

10-
export class ApiFunctionType extends ApiCallableBase<ts.FunctionTypeNode, ApiFunctionTypeDto> {
10+
export type FunctionTypes = ts.FunctionTypeNode | ts.ArrowFunction | ts.FunctionExpression;
11+
12+
// TODO: Rename to appropriate class name.
13+
export class ApiFunctionType extends ApiCallableBase<FunctionTypes, ApiFunctionTypeDto> {
14+
protected ResolveApiKind(): ApiItemKinds.FunctionType | ApiItemKinds.ArrowFunction | ApiItemKinds.FunctionExpression {
15+
if (ts.isFunctionTypeNode(this.Declaration)) {
16+
return ApiItemKinds.FunctionType;
17+
} else if (ts.isFunctionExpression(this.Declaration)) {
18+
return ApiItemKinds.FunctionExpression;
19+
} else {
20+
return ApiItemKinds.ArrowFunction;
21+
}
22+
}
23+
1124
public OnExtract(): ApiFunctionTypeDto {
1225
const parentId: string | undefined = ApiHelpers.GetParentIdFromDeclaration(this.Declaration, this.Options);
1326
const metadata: ApiMetadataDto = this.GetItemMetadata();
1427
const location: ApiItemLocationDto = ApiHelpers.GetApiItemLocationDtoFromNode(this.Declaration, this.Options);
28+
const apiKind = this.ResolveApiKind();
1529

1630
return {
17-
ApiKind: ApiItemKinds.FunctionType,
31+
ApiKind: apiKind,
1832
Name: this.Symbol.name,
1933
ParentId: parentId,
2034
Metadata: metadata,

src/definitions/api-namespace.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import { ApiItemLocationDto } from "../contracts/api-item-location-dto";
1111
export class ApiNamespace extends ApiItem<ts.ModuleDeclaration | ts.NamespaceImport, ApiNamespaceDto> {
1212
private members: ApiItemReference[] = [];
1313

14+
protected ResolveApiKind(): ApiItemKinds.Namespace | ApiItemKinds.ImportNamespace {
15+
if (ts.isModuleDeclaration(this.Declaration)) {
16+
return ApiItemKinds.Namespace;
17+
} else {
18+
return ApiItemKinds.ImportNamespace;
19+
}
20+
}
21+
1422
protected OnGatherData(): void {
1523
// Members
1624
this.members = ApiHelpers.GetItemsIdsFromSymbolsMap(this.Symbol.exports, this.Options);
@@ -20,9 +28,10 @@ export class ApiNamespace extends ApiItem<ts.ModuleDeclaration | ts.NamespaceImp
2028
const parentId: string | undefined = ApiHelpers.GetParentIdFromDeclaration(this.Declaration, this.Options);
2129
const metadata: ApiMetadataDto = this.GetItemMetadata();
2230
const location: ApiItemLocationDto = ApiHelpers.GetApiItemLocationDtoFromNode(this.Declaration, this.Options);
31+
const apiKind = this.ResolveApiKind();
2332

2433
return {
25-
ApiKind: ApiItemKinds.Namespace,
34+
ApiKind: apiKind,
2635
Name: this.Declaration.name.getText(),
2736
ParentId: parentId,
2837
Metadata: metadata,

src/definitions/api-property.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ import { ApiMetadataDto } from "../contracts/api-metadata-dto";
99
import { ApiItemLocationDto } from "../contracts/api-item-location-dto";
1010
import { ApiTypeHelpers } from "../api-type-helpers";
1111

12-
export class ApiProperty extends ApiItem<ts.PropertySignature, ApiPropertyDto> {
12+
export class ApiProperty extends ApiItem<ts.PropertySignature | ts.PropertyAssignment, ApiPropertyDto> {
1313
private type: ApiType;
1414
private isOptional: boolean;
1515
private isReadonly: boolean;
1616

1717
protected OnGatherData(): void {
1818
// Type
19+
const typeNode: ts.TypeNode | undefined = ts.isPropertySignature(this.Declaration) ? this.Declaration.type : undefined;
1920
const type = this.TypeChecker.getTypeOfSymbolAtLocation(this.Symbol, this.Declaration);
20-
this.type = ApiTypeHelpers.ResolveApiType(this.Options, type, this.Declaration.type);
21+
this.type = ApiTypeHelpers.ResolveApiType(this.Options, type, typeNode);
2122

2223
// IsOptional
2324
this.isOptional = Boolean(this.Declaration.questionToken);

0 commit comments

Comments
 (0)