@goast/typescript 0.7.6 → 0.8.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +52 -52
  3. package/assets/client/angular/angular-service.utils.ts +82 -82
  4. package/assets/client/angular/request-builder.ts +377 -377
  5. package/assets/client/fetch/fetch-client.utils.ts +52 -52
  6. package/assets/client/k6/request-builder.js +448 -448
  7. package/assets/client/k6/request-builder.ts +366 -366
  8. package/assets/stubs/easy-network-stub/easy-network-stub.utils.ts +257 -257
  9. package/esm/src/generators/services/angular-services/angular-service-generator.js +9 -9
  10. package/esm/src/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
  11. package/esm/src/generators/services/fetch-clients/fetch-client-generator.js +4 -4
  12. package/esm/src/generators/services/k6-clients/k6-client-generator.js +11 -11
  13. package/package.json +2 -2
  14. package/script/src/generators/services/angular-services/angular-service-generator.js +9 -9
  15. package/script/src/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
  16. package/script/src/generators/services/fetch-clients/fetch-client-generator.js +4 -4
  17. package/script/src/generators/services/k6-clients/k6-client-generator.js +11 -11
  18. package/src/mod.ts +0 -7
  19. package/src/src/assets.ts +0 -9
  20. package/src/src/ast/_index.ts +0 -54
  21. package/src/src/ast/common.ts +0 -1
  22. package/src/src/ast/index.ts +0 -1
  23. package/src/src/ast/node.ts +0 -10
  24. package/src/src/ast/nodes/argument.ts +0 -40
  25. package/src/src/ast/nodes/array-type.ts +0 -49
  26. package/src/src/ast/nodes/arrow-function.ts +0 -91
  27. package/src/src/ast/nodes/call.ts +0 -55
  28. package/src/src/ast/nodes/class.ts +0 -133
  29. package/src/src/ast/nodes/constructor-parameter.ts +0 -70
  30. package/src/src/ast/nodes/constructor.ts +0 -77
  31. package/src/src/ast/nodes/decorator.ts +0 -76
  32. package/src/src/ast/nodes/doc-tag.ts +0 -291
  33. package/src/src/ast/nodes/doc.ts +0 -102
  34. package/src/src/ast/nodes/enum-value.ts +0 -72
  35. package/src/src/ast/nodes/enum.ts +0 -77
  36. package/src/src/ast/nodes/export.ts +0 -54
  37. package/src/src/ast/nodes/function-type.ts +0 -60
  38. package/src/src/ast/nodes/function.ts +0 -107
  39. package/src/src/ast/nodes/generic-parameter.ts +0 -71
  40. package/src/src/ast/nodes/indexer.ts +0 -60
  41. package/src/src/ast/nodes/interface.ts +0 -117
  42. package/src/src/ast/nodes/intersection-type.ts +0 -67
  43. package/src/src/ast/nodes/lookup-type.ts +0 -53
  44. package/src/src/ast/nodes/method.ts +0 -138
  45. package/src/src/ast/nodes/object-type.ts +0 -52
  46. package/src/src/ast/nodes/object.ts +0 -48
  47. package/src/src/ast/nodes/parameter.ts +0 -85
  48. package/src/src/ast/nodes/property-accessor.ts +0 -173
  49. package/src/src/ast/nodes/property.ts +0 -192
  50. package/src/src/ast/nodes/reference.ts +0 -194
  51. package/src/src/ast/nodes/string.ts +0 -49
  52. package/src/src/ast/nodes/tuple.ts +0 -53
  53. package/src/src/ast/nodes/type-alias.ts +0 -75
  54. package/src/src/ast/nodes/typeof.ts +0 -42
  55. package/src/src/ast/nodes/types.ts +0 -40
  56. package/src/src/ast/nodes/union-type.ts +0 -62
  57. package/src/src/ast/nodes/variable.ts +0 -73
  58. package/src/src/ast/references/angular.ts +0 -24
  59. package/src/src/ast/references/easy-network-stub.ts +0 -21
  60. package/src/src/ast/references/index.ts +0 -5
  61. package/src/src/ast/references/k6.ts +0 -8
  62. package/src/src/ast/references/rxjs.ts +0 -7
  63. package/src/src/ast/references/typescript.ts +0 -35
  64. package/src/src/ast/utils/get-type-script-builder-options.ts +0 -19
  65. package/src/src/ast/utils/to-ts-node.ts +0 -27
  66. package/src/src/ast/utils/write-ts-generic-parameters.ts +0 -25
  67. package/src/src/ast/utils/write-ts-members.ts +0 -28
  68. package/src/src/ast/utils/write-ts-nodes.ts +0 -35
  69. package/src/src/ast/utils/write-ts-parameters.ts +0 -25
  70. package/src/src/common-results.ts +0 -26
  71. package/src/src/config.ts +0 -96
  72. package/src/src/file-builder.ts +0 -137
  73. package/src/src/generators/file-generator.ts +0 -15
  74. package/src/src/generators/index.ts +0 -6
  75. package/src/src/generators/models/index.ts +0 -3
  76. package/src/src/generators/models/model-generator.ts +0 -493
  77. package/src/src/generators/models/models-generator.ts +0 -108
  78. package/src/src/generators/models/models.ts +0 -121
  79. package/src/src/generators/services/angular-services/angular-service-generator.ts +0 -431
  80. package/src/src/generators/services/angular-services/angular-services-generator.ts +0 -295
  81. package/src/src/generators/services/angular-services/index.ts +0 -3
  82. package/src/src/generators/services/angular-services/models.ts +0 -277
  83. package/src/src/generators/services/angular-services/refs.ts +0 -54
  84. package/src/src/generators/services/easy-network-stub/easy-network-stub-generator.ts +0 -250
  85. package/src/src/generators/services/easy-network-stub/easy-network-stubs-generator.ts +0 -211
  86. package/src/src/generators/services/easy-network-stub/index.ts +0 -3
  87. package/src/src/generators/services/easy-network-stub/models.ts +0 -124
  88. package/src/src/generators/services/easy-network-stub/refs.ts +0 -47
  89. package/src/src/generators/services/fetch-clients/fetch-client-generator.ts +0 -305
  90. package/src/src/generators/services/fetch-clients/fetch-clients-generator.ts +0 -163
  91. package/src/src/generators/services/fetch-clients/index.ts +0 -3
  92. package/src/src/generators/services/fetch-clients/models.ts +0 -130
  93. package/src/src/generators/services/fetch-clients/refs.ts +0 -21
  94. package/src/src/generators/services/k6-clients/index.ts +0 -3
  95. package/src/src/generators/services/k6-clients/k6-client-generator.ts +0 -505
  96. package/src/src/generators/services/k6-clients/k6-clients-generator.ts +0 -171
  97. package/src/src/generators/services/k6-clients/models.ts +0 -247
  98. package/src/src/generators/services/k6-clients/refs.ts +0 -16
  99. package/src/src/import-collection.ts +0 -236
  100. package/src/src/utils.ts +0 -79
@@ -1,138 +0,0 @@
1
- import {
2
- type AppendValue,
3
- type AstNodeOptions,
4
- type BasicAppendValue,
5
- notNullish,
6
- type Nullable,
7
- type Prettify,
8
- type SourceBuilder,
9
- } from '@goast/core';
10
-
11
- import type { TsAccessModifier } from '../common.js';
12
- import { TsNode } from '../node.js';
13
- import { writeTsNode, writeTsNodes } from '../utils/write-ts-nodes.js';
14
- import { type TsDecorator, tsDecorator } from './decorator.js';
15
- import { type TsDoc, tsDoc } from './doc.js';
16
- import { type TsGenericParameter, tsGenericParameter } from './generic-parameter.js';
17
- import { TsObject } from './object.js';
18
- import { type TsParameter, tsParameter } from './parameter.js';
19
- import type { TsType } from './types.js';
20
-
21
- type Injects = 'doc' | 'decorators' | 'modifiers' | 'name' | 'generics' | 'params' | 'returnType' | 'body';
22
-
23
- type Options<TBuilder extends SourceBuilder, TInjects extends string = never> = AstNodeOptions<
24
- typeof TsNode<TBuilder, TInjects | Injects>,
25
- {
26
- doc?: Nullable<TsDoc<TBuilder>>;
27
- decorators?: Nullable<Nullable<TsDecorator<TBuilder>>[]>;
28
- accessModifier?: Nullable<TsAccessModifier>;
29
- async?: Nullable<boolean>;
30
- static?: Nullable<boolean>;
31
- abstract?: Nullable<boolean>;
32
- override?: Nullable<boolean>;
33
- name: string;
34
- optional?: Nullable<boolean>;
35
- generics?: Nullable<Nullable<TsGenericParameter<TBuilder> | BasicAppendValue<TBuilder>>[]>;
36
- parameters?: Nullable<Nullable<TsParameter<TBuilder> | BasicAppendValue<TBuilder>>[]>;
37
- returnType?: Nullable<TsType<TBuilder>>;
38
- body?: Nullable<AppendValue<TBuilder>>;
39
- }
40
- >;
41
-
42
- export class TsMethod<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsNode<
43
- TBuilder,
44
- TInjects | Injects
45
- > {
46
- public doc: TsDoc<TBuilder> | null;
47
- public decorators: TsDecorator<TBuilder>[];
48
- public accessModifier: TsAccessModifier | null;
49
- public static: boolean;
50
- public abstract: boolean;
51
- public override: boolean;
52
- public async: boolean;
53
- public name: string;
54
- public optional: boolean;
55
- public generics: (TsGenericParameter<TBuilder> | BasicAppendValue<TBuilder>)[];
56
- public parameters: (TsParameter<TBuilder> | BasicAppendValue<TBuilder>)[];
57
- public returnType: TsType<TBuilder> | null;
58
- public body: AppendValue<TBuilder> | null;
59
-
60
- constructor(options: Options<TBuilder, TInjects>) {
61
- super(options);
62
- this.doc = options.doc ?? null;
63
- this.decorators = options.decorators?.filter(notNullish) ?? [];
64
- this.accessModifier = options.accessModifier ?? null;
65
- this.static = options.static ?? false;
66
- this.abstract = options.abstract ?? false;
67
- this.override = options.override ?? false;
68
- this.async = options.async ?? false;
69
- this.name = options.name;
70
- this.optional = options.optional ?? false;
71
- this.generics = options.generics?.filter(notNullish) ?? [];
72
- this.parameters = options.parameters?.filter(notNullish) ?? [];
73
- this.returnType = options.returnType ?? null;
74
- this.body = options.body ?? null;
75
- }
76
-
77
- protected override onWrite(builder: TBuilder): void {
78
- const isInObjectDeclaration = this.getParentNode(builder) instanceof TsObject;
79
-
80
- builder.append(this.inject.beforeDoc);
81
- tsDoc.write(builder, tsDoc.get(this.doc, { generics: this.generics, parameters: this.parameters }));
82
- builder.append(this.inject.afterDoc);
83
-
84
- builder.append(this.inject.beforeDecorators);
85
- tsDecorator.write(builder, this.decorators, { multiline: true });
86
- builder.append(this.inject.afterDecorators);
87
-
88
- builder.append(this.inject.beforeModifiers);
89
- if (this.accessModifier) builder.append(this.accessModifier, ' ');
90
- if (this.static) builder.append('static ');
91
- if (this.abstract) builder.append('abstract ');
92
- if (this.override) builder.append('override ');
93
- if (this.async) builder.append('async ');
94
- builder.append(this.inject.afterModifiers);
95
-
96
- builder.append(this.inject.beforeName, this.name, this.inject.afterName);
97
- if (this.optional) builder.append('?');
98
-
99
- builder.append(this.inject.beforeGenerics);
100
- tsGenericParameter.write(builder, this.generics);
101
- builder.append(this.inject.afterGenerics);
102
-
103
- builder.append(this.inject.beforeParams);
104
- tsParameter.write(builder, this.parameters);
105
- builder.append(this.inject.afterParams);
106
-
107
- if (this.returnType) {
108
- builder.append(': ');
109
- builder.append(this.inject.beforeReturnType);
110
- writeTsNode(builder, this.returnType);
111
- builder.append(this.inject.afterReturnType);
112
- }
113
-
114
- if (this.body) {
115
- builder.append(' ');
116
- builder.append(this.inject.beforeBody);
117
- builder.parenthesize('{}', this.body, { multiline: !!this.body });
118
- builder.append(this.inject.afterBody);
119
- } else if (!isInObjectDeclaration) {
120
- builder.append(';');
121
- }
122
-
123
- if (isInObjectDeclaration) {
124
- builder.append(',');
125
- }
126
-
127
- builder.appendLine();
128
- }
129
- }
130
-
131
- export const createMethod = <TBuilder extends SourceBuilder>(
132
- name: Options<TBuilder>['name'],
133
- options?: Prettify<Omit<Options<TBuilder>, 'name'>>,
134
- ): TsMethod<TBuilder> => new TsMethod<TBuilder>({ ...options, name });
135
-
136
- export const tsMethod: typeof createMethod & { write: typeof writeTsNodes } = Object.assign(createMethod, {
137
- write: writeTsNodes,
138
- });
@@ -1,52 +0,0 @@
1
- import { type AstNodeOptions, type BasicAppendValue, notNullish, type Nullable, type SourceBuilder } from '@goast/core';
2
-
3
- import { TsNode } from '../node.js';
4
- import { writeTsMembers } from '../utils/write-ts-members.js';
5
- import { writeTsNodes } from '../utils/write-ts-nodes.js';
6
- import type { TsIndexer } from './indexer.js';
7
- import type { TsMethod } from './method.js';
8
- import type { TsProperty } from './property.js';
9
-
10
- type Injects = never;
11
-
12
- type Options<TBuilder extends SourceBuilder, TInjects extends string = never> = AstNodeOptions<
13
- typeof TsNode<TBuilder, TInjects | Injects>,
14
- {
15
- members?: Nullable<Nullable<Member<TBuilder>>[]>;
16
- }
17
- >;
18
-
19
- type Member<TBuilder extends SourceBuilder> =
20
- | TsProperty<TBuilder>
21
- | TsMethod<TBuilder>
22
- | TsIndexer<TBuilder>
23
- | BasicAppendValue<TBuilder>;
24
-
25
- export class TsObjectType<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsNode<
26
- TBuilder,
27
- TInjects | Injects
28
- > {
29
- public members: Member<TBuilder>[];
30
-
31
- constructor(options: Options<TBuilder, TInjects>) {
32
- super(options);
33
- this.members = options.members?.filter(notNullish) ?? [];
34
- }
35
-
36
- protected override onWrite(builder: TBuilder): void {
37
- builder.parenthesize(
38
- '{}',
39
- (b) => {
40
- writeTsMembers(b, this.members);
41
- },
42
- { multiline: this.members.length > 0 },
43
- );
44
- }
45
- }
46
-
47
- const createObjectType = <TBuilder extends SourceBuilder>(options?: Options<TBuilder>): TsObjectType<TBuilder> =>
48
- new TsObjectType<TBuilder>(options ?? {});
49
-
50
- export const tsObjectType: typeof createObjectType & { write: typeof writeTsNodes } = Object.assign(createObjectType, {
51
- write: writeTsNodes,
52
- });
@@ -1,48 +0,0 @@
1
- import { type AstNodeOptions, type BasicAppendValue, notNullish, type Nullable, type SourceBuilder } from '@goast/core';
2
-
3
- import { TsNode } from '../node.js';
4
- import { writeTsMembers } from '../utils/write-ts-members.js';
5
- import { writeTsNodes } from '../utils/write-ts-nodes.js';
6
-
7
- import type { TsMethod } from './method.js';
8
- import type { TsProperty } from './property.js';
9
-
10
- type Injects = never;
11
-
12
- type Options<TBuilder extends SourceBuilder, TInjects extends string = never> = AstNodeOptions<
13
- typeof TsNode<TBuilder, TInjects | Injects>,
14
- {
15
- members?: Nullable<Nullable<Member<TBuilder>>[]>;
16
- }
17
- >;
18
-
19
- type Member<TBuilder extends SourceBuilder> = TsProperty<TBuilder> | TsMethod<TBuilder> | BasicAppendValue<TBuilder>;
20
-
21
- export class TsObject<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsNode<
22
- TBuilder,
23
- TInjects | Injects
24
- > {
25
- public members: Member<TBuilder>[];
26
-
27
- constructor(options: Options<TBuilder, TInjects>) {
28
- super(options);
29
- this.members = options.members?.filter(notNullish) ?? [];
30
- }
31
-
32
- protected override onWrite(builder: TBuilder): void {
33
- builder.parenthesize(
34
- '{}',
35
- (b) => {
36
- writeTsMembers(b, this.members);
37
- },
38
- { multiline: this.members.length > 0 },
39
- );
40
- }
41
- }
42
-
43
- const createObject = <TBuilder extends SourceBuilder>(options?: Options<TBuilder>): TsObject<TBuilder> =>
44
- new TsObject<TBuilder>(options ?? {});
45
-
46
- export const tsObject: typeof createObject & { write: typeof writeTsNodes } = Object.assign(createObject, {
47
- write: writeTsNodes,
48
- });
@@ -1,85 +0,0 @@
1
- import {
2
- type AstNodeOptions,
3
- type BasicAppendValue,
4
- notNullish,
5
- type Nullable,
6
- type Prettify,
7
- type SourceBuilder,
8
- } from '@goast/core';
9
-
10
- import { TsNode } from '../node.js';
11
- import { writeTsNode } from '../utils/write-ts-nodes.js';
12
- import { writeTsParameters } from '../utils/write-ts-parameters.js';
13
- import { type TsDecorator, tsDecorator } from './decorator.js';
14
- import type { TsType, TsValue } from './types.js';
15
-
16
- type Injects = 'decorators' | 'name' | 'type' | 'default';
17
-
18
- type Options<TBuilder extends SourceBuilder, TInjects extends string = never> = AstNodeOptions<
19
- typeof TsNode<TBuilder, TInjects | Injects>,
20
- {
21
- name: string;
22
- description?: Nullable<BasicAppendValue<TBuilder>>;
23
- decorators?: Nullable<Nullable<TsDecorator<TBuilder>>[]>;
24
- type?: Nullable<TsType<TBuilder>>;
25
- optional?: Nullable<boolean>;
26
- default?: Nullable<TsValue<TBuilder>>;
27
- }
28
- >;
29
-
30
- export class TsParameter<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsNode<
31
- TBuilder,
32
- TInjects | Injects
33
- > {
34
- public name: string;
35
- public description: BasicAppendValue<TBuilder> | null;
36
- public decorators: TsDecorator<TBuilder>[];
37
- public type: TsType<TBuilder> | null;
38
- public optional: boolean;
39
- public default: TsValue<TBuilder> | null;
40
-
41
- constructor(options: Options<TBuilder, TInjects>) {
42
- super(options);
43
- this.name = options.name;
44
- this.description = options.description ?? null;
45
- this.decorators = options.decorators?.filter(notNullish) ?? [];
46
- this.type = options.type ?? null;
47
- this.optional = options.optional ?? false;
48
- this.default = options.default ?? null;
49
- }
50
-
51
- protected override onWrite(builder: TBuilder): void {
52
- builder.append(this.inject.beforeDecorators);
53
- tsDecorator.write(builder, this.decorators);
54
- builder.append(this.inject.afterDecorators);
55
-
56
- builder.append(this.inject.beforeName, this.name, this.inject.afterName);
57
- if (this.optional) builder.append('?');
58
-
59
- if (this.type) {
60
- builder.append(': ');
61
- builder.append(this.inject.beforeType);
62
- writeTsNode(builder, this.type);
63
- builder.append(this.inject.afterType);
64
- }
65
-
66
- if (this.default) {
67
- builder.append(' = ');
68
- builder.append(this.inject.beforeDefault);
69
- writeTsNode(builder, this.default);
70
- builder.append(this.inject.afterDefault);
71
- }
72
- }
73
- }
74
-
75
- const createParameter = <TBuilder extends SourceBuilder>(
76
- name: Options<TBuilder>['name'],
77
- options?: Prettify<Omit<Options<TBuilder>, 'name'>>,
78
- ): TsParameter<TBuilder> => new TsParameter<TBuilder>({ ...options, name });
79
-
80
- export const tsParameter: typeof createParameter & { write: typeof writeTsParameters } = Object.assign(
81
- createParameter,
82
- {
83
- write: writeTsParameters,
84
- },
85
- );
@@ -1,173 +0,0 @@
1
- import { type AppendValue, type AstNodeOptions, notNullish, type Nullable, type SourceBuilder } from '@goast/core';
2
-
3
- import type { TsAccessModifier } from '../common.js';
4
- import { TsNode } from '../node.js';
5
- import { writeTsNode } from '../utils/write-ts-nodes.js';
6
- import { type TsDecorator, tsDecorator } from './decorator.js';
7
- import { type TsDoc, tsDoc } from './doc.js';
8
- import type { TsProperty } from './property.js';
9
- import type { TsType } from './types.js';
10
-
11
- type Injects = never;
12
-
13
- type Options<TBuilder extends SourceBuilder, TInjects extends string = never> = AstNodeOptions<
14
- typeof TsNode<TBuilder, TInjects | Injects>,
15
- {
16
- propertyName?: Nullable<string>;
17
- doc?: Nullable<TsDoc<TBuilder>>;
18
- type?: Nullable<TsType<TBuilder>>;
19
- decorators?: Nullable<Nullable<TsDecorator<TBuilder>>[]>;
20
- body?: Nullable<AppendValue<TBuilder>>;
21
- accessModifier?: Nullable<TsAccessModifier>;
22
- static?: Nullable<boolean>;
23
- abstract?: Nullable<boolean>;
24
- override?: Nullable<boolean>;
25
- }
26
- >;
27
-
28
- abstract class TsPropertyAccessor<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsNode<
29
- TBuilder,
30
- TInjects | Injects
31
- > {
32
- private _propertyName: string | null;
33
- private _type: TsType<TBuilder> | null;
34
- private _accessModifier: TsAccessModifier | null;
35
- private _static: boolean;
36
- private _abstract: boolean;
37
- private _override: boolean;
38
-
39
- public doc: TsDoc<TBuilder> | null;
40
- public decorators: TsDecorator<TBuilder>[];
41
- public body: AppendValue<TBuilder> | null;
42
-
43
- public get propertyName(): string {
44
- return this.property?.name ?? this._propertyName ?? 'someProperty';
45
- }
46
- public set propertyName(value: string | null) {
47
- this._propertyName = value;
48
- }
49
-
50
- public get type(): TsType<TBuilder> | null {
51
- return this.property?.type ?? this._type;
52
- }
53
- public set type(value: TsType<TBuilder> | null) {
54
- this._type = value;
55
- }
56
-
57
- public get accessModifier(): TsAccessModifier | null {
58
- return this.property?.accessModifier ?? this._accessModifier;
59
- }
60
- public set accessModifier(value: TsAccessModifier | null) {
61
- this._accessModifier = value;
62
- }
63
-
64
- public get static(): boolean {
65
- return this.property?.static ?? this._static;
66
- }
67
- public set static(value: boolean) {
68
- this._static = value;
69
- }
70
-
71
- public get abstract(): boolean {
72
- return this.property?.abstract ?? this._abstract;
73
- }
74
- public set abstract(value: boolean) {
75
- this._abstract = value;
76
- }
77
-
78
- public get override(): boolean {
79
- return this.property?.override ?? this._override;
80
- }
81
- public set override(value: boolean) {
82
- this._override = value;
83
- }
84
-
85
- protected property?: TsProperty<TBuilder>;
86
-
87
- constructor(options: Options<TBuilder, TInjects>) {
88
- super(options);
89
- this._propertyName = options.propertyName ?? null;
90
- this.doc = options.doc ?? null;
91
- this._type = options.type ?? null;
92
- this.decorators = options.decorators?.filter(notNullish) ?? [];
93
- this.body = options.body ?? null;
94
- this._accessModifier = options.accessModifier ?? null;
95
- this._static = options.static ?? false;
96
- this._abstract = options.abstract ?? false;
97
- this._override = options.override ?? false;
98
- }
99
- }
100
-
101
- export class TsPropertySetter<TBuilder extends SourceBuilder, TInjects extends string = never>
102
- extends TsPropertyAccessor<
103
- TBuilder,
104
- TInjects
105
- > {
106
- protected override onWrite(builder: TBuilder): void {
107
- tsDoc.write(builder, this.doc);
108
- tsDecorator.write(builder, this.decorators);
109
-
110
- if (this.accessModifier) builder.append(this.accessModifier, ' ');
111
- if (this.static) builder.append('static ');
112
- if (this.abstract) builder.append('abstract ');
113
- if (this.override) builder.append('override ');
114
-
115
- builder.append('set ', this.propertyName);
116
- builder.parenthesize('()', (b) => {
117
- b.append('value');
118
- if (this.type !== null) {
119
- b.append(': ');
120
- writeTsNode(b, this.type);
121
- }
122
- });
123
-
124
- if (this.body !== null) {
125
- builder.append(' ');
126
- builder.parenthesize('{}', this.body, { multiline: true });
127
- } else {
128
- builder.append(';');
129
- }
130
-
131
- builder.appendLine();
132
- }
133
- }
134
-
135
- export class TsPropertyGetter<TBuilder extends SourceBuilder, TInjects extends string = never>
136
- extends TsPropertyAccessor<
137
- TBuilder,
138
- TInjects
139
- > {
140
- protected override onWrite(builder: TBuilder): void {
141
- tsDoc.write(builder, this.doc);
142
- tsDecorator.write(builder, this.decorators);
143
-
144
- if (this.accessModifier) builder.append(this.accessModifier, ' ');
145
- if (this.static) builder.append('static ');
146
- if (this.abstract) builder.append('abstract ');
147
- if (this.override) builder.append('override ');
148
-
149
- builder.append('get ', this.propertyName, '()');
150
-
151
- if (this.type) {
152
- builder.append(': ');
153
- writeTsNode(builder, this.type);
154
- }
155
-
156
- if (this.body !== null) {
157
- builder.append(' ');
158
- builder.parenthesize('{}', this.body, { multiline: true });
159
- } else {
160
- builder.append(';');
161
- }
162
-
163
- builder.appendLine();
164
- }
165
- }
166
-
167
- export const createPropertySetter = <TBuilder extends SourceBuilder>(
168
- options?: Options<TBuilder>,
169
- ): TsPropertySetter<TBuilder> => new TsPropertySetter<TBuilder>(options ?? {});
170
-
171
- export const createPropertyGetter = <TBuilder extends SourceBuilder>(
172
- options?: Options<TBuilder>,
173
- ): TsPropertyGetter<TBuilder> => new TsPropertyGetter<TBuilder>(options ?? {});
@@ -1,192 +0,0 @@
1
- import { type AstNodeOptions, notNullish, type Nullable, type Prettify, type SourceBuilder } from '@goast/core';
2
-
3
- import type { TsAccessModifier } from '../common.js';
4
- import { TsNode } from '../node.js';
5
- import { writeTsNode, writeTsNodes } from '../utils/write-ts-nodes.js';
6
- import { type TsDecorator, tsDecorator } from './decorator.js';
7
- import { type TsDoc, tsDoc } from './doc.js';
8
- import { TsObject } from './object.js';
9
- import {
10
- createPropertyGetter,
11
- createPropertySetter,
12
- type TsPropertyGetter,
13
- type TsPropertySetter,
14
- } from './property-accessor.js';
15
- import { tsString } from './string.js';
16
- import type { TsType, TsValue } from './types.js';
17
-
18
- type BaseInjects = never;
19
-
20
- type BaseOptions<TBuilder extends SourceBuilder, TInjects extends string = never> = AstNodeOptions<
21
- typeof TsNode<TBuilder, TInjects | BaseInjects>,
22
- {
23
- name: string;
24
- doc?: Nullable<TsDoc<TBuilder>>;
25
- decorators?: Nullable<Nullable<TsDecorator<TBuilder>>[]>;
26
- type?: Nullable<TsType<TBuilder>>;
27
- accessModifier?: Nullable<TsAccessModifier>;
28
- static?: Nullable<boolean>;
29
- abstract?: Nullable<boolean>;
30
- override?: Nullable<boolean>;
31
- }
32
- >;
33
-
34
- type SimpleOptions<TBuilder extends SourceBuilder, TInjects extends string = never> =
35
- & BaseOptions<TBuilder, TInjects>
36
- & {
37
- value?: Nullable<TsValue<TBuilder>>;
38
- readonly?: Nullable<boolean>;
39
- optional?: Nullable<boolean>;
40
- };
41
-
42
- type AccessorOptions<TBuilder extends SourceBuilder, TInjects extends string = never> =
43
- & BaseOptions<TBuilder, TInjects>
44
- & {
45
- get?: Nullable<TsPropertyGetter<TBuilder>>;
46
- set?: Nullable<TsPropertySetter<TBuilder>>;
47
- };
48
-
49
- export abstract class TsProperty<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsNode<
50
- TBuilder,
51
- TInjects | BaseInjects
52
- > {
53
- public name: string;
54
- public doc: TsDoc<TBuilder> | null;
55
- public decorators: TsDecorator<TBuilder>[];
56
- public type: TsType<TBuilder> | null;
57
- public accessModifier: TsAccessModifier | null;
58
- public static: boolean | null;
59
- public abstract: boolean | null;
60
- public override: boolean | null;
61
-
62
- constructor(options: BaseOptions<TBuilder, TInjects>) {
63
- super(options);
64
- this.name = options.name;
65
- this.doc = options.doc ?? null;
66
- this.decorators = options.decorators?.filter(notNullish) ?? [];
67
- this.type = options.type ?? null;
68
- this.accessModifier = options.accessModifier ?? null;
69
- this.static = options.static ?? null;
70
- this.abstract = options.abstract ?? null;
71
- this.override = options.override ?? null;
72
- }
73
- }
74
-
75
- export class TsSimpleProperty<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsProperty<
76
- TBuilder,
77
- TInjects
78
- > {
79
- public value: TsValue<TBuilder> | null;
80
- public readonly: boolean;
81
- public optional: boolean;
82
-
83
- constructor(options: SimpleOptions<TBuilder, TInjects>) {
84
- super(options);
85
- this.value = options.value ?? null;
86
- this.readonly = options.readonly ?? false;
87
- this.optional = options.optional ?? false;
88
- }
89
-
90
- protected override onWrite(builder: TBuilder): void {
91
- const isInObjectDeclaration = this.getParentNode(builder) instanceof TsObject;
92
-
93
- tsDoc.write(builder, this.doc);
94
- tsDecorator.write(builder, this.decorators);
95
-
96
- if (this.accessModifier) builder.append(this.accessModifier, ' ');
97
- if (this.static) builder.append('static ');
98
- if (this.abstract) builder.append('abstract ');
99
- if (this.override) builder.append('override ');
100
- if (this.readonly) builder.append('readonly ');
101
-
102
- writeTsNode(builder, /^([a-zA-Z_$][a-zA-Z_$0-9]*|[0-9]+)$/.test(this.name) ? this.name : tsString(this.name));
103
- if (this.optional) builder.append('?');
104
-
105
- if (this.type && !isInObjectDeclaration) {
106
- builder.append(': ');
107
- writeTsNode(builder, this.type);
108
- }
109
-
110
- if (this.value) {
111
- builder.append(isInObjectDeclaration ? ': ' : ' = ');
112
- writeTsNode(builder, this.value);
113
- }
114
-
115
- builder.append(isInObjectDeclaration ? ',' : ';');
116
-
117
- builder.appendLine();
118
- }
119
- }
120
-
121
- export class TsAccessorProperty<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsProperty<
122
- TBuilder,
123
- TInjects
124
- > {
125
- private _get: TsPropertyGetter<TBuilder> | null = null;
126
- private _set: TsPropertySetter<TBuilder> | null = null;
127
-
128
- public get get(): TsPropertyGetter<TBuilder> | null {
129
- return this._get;
130
- }
131
- public set get(value: TsPropertyGetter<TBuilder> | null) {
132
- if (value) {
133
- value['property'] = this;
134
- }
135
- if (this._get) {
136
- this._get['property'] = undefined;
137
- }
138
- this._get = value;
139
- }
140
-
141
- public get set(): TsPropertySetter<TBuilder> | null {
142
- return this._set;
143
- }
144
- public set set(value: TsPropertySetter<TBuilder> | null) {
145
- if (value) {
146
- value['property'] = this;
147
- }
148
- if (this._set) {
149
- this._set['property'] = undefined;
150
- }
151
- this._set = value;
152
- }
153
-
154
- constructor(options: AccessorOptions<TBuilder, TInjects>) {
155
- super(options);
156
- this.get = options.get ?? null;
157
- this.set = options.set ?? null;
158
- }
159
-
160
- protected override onWrite(builder: TBuilder): void {
161
- builder.append(
162
- (b) => tsDoc.write(b, this.doc),
163
- (b) => tsDecorator.write(b, this.decorators),
164
- );
165
- if (this.get) {
166
- this.get.write(builder);
167
- }
168
- if (this.set) {
169
- this.set.write(builder);
170
- }
171
- }
172
- }
173
-
174
- const createProperty = <TBuilder extends SourceBuilder>(
175
- name: BaseOptions<TBuilder>['name'],
176
- options?: Prettify<Omit<SimpleOptions<TBuilder>, 'name'>> | Prettify<Omit<AccessorOptions<TBuilder>, 'name'>>,
177
- ): TsProperty<TBuilder> => {
178
- if (options && ('get' in options || 'set' in options)) {
179
- return new TsAccessorProperty({ ...options, name });
180
- }
181
- return new TsSimpleProperty({ ...options, name });
182
- };
183
-
184
- export const tsProperty: typeof createProperty & {
185
- write: typeof writeTsNodes;
186
- getter: typeof createPropertyGetter;
187
- setter: typeof createPropertySetter;
188
- } = Object.assign(createProperty, {
189
- write: writeTsNodes,
190
- getter: createPropertyGetter,
191
- setter: createPropertySetter,
192
- });