Skip to content

Commit ca9a032

Browse files
authored
Merge pull request #2999 from hey-api/refactor/dsl-type-nodes-8
refactor: arktype plugin use dsl
2 parents 381803f + 8ffc4f0 commit ca9a032

File tree

21 files changed

+247
-266
lines changed

21 files changed

+247
-266
lines changed

dev/openapi-ts.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ export default defineConfig(() => {
357357
},
358358
{
359359
name: 'arktype',
360-
// types: {
361-
// infer: true,
362-
// },
360+
types: {
361+
infer: true,
362+
},
363363
},
364364
{
365365
// case: 'SCREAMING_SNAKE_CASE',
Lines changed: 73 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,64 @@
1-
import { tsc } from '~/tsc';
2-
31
export const identifiers = {
42
keywords: {
5-
false: tsc.identifier({ text: 'false' }),
6-
true: tsc.identifier({ text: 'true' }),
3+
false: 'false',
4+
true: 'true',
75
},
86
/**
97
* {@link https://arktype.io/docs/primitives#number Number}
108
*/
119
number: {
12-
Infinity: tsc.identifier({ text: 'Infinity' }),
13-
NaN: tsc.identifier({ text: 'NaN' }),
14-
NegativeInfinity: tsc.identifier({ text: 'NegativeInfinity' }),
15-
epoch: tsc.identifier({ text: 'epoch' }),
16-
integer: tsc.identifier({ text: 'integer' }),
17-
safe: tsc.identifier({ text: 'safe' }),
10+
Infinity: 'Infinity',
11+
NaN: 'NaN',
12+
NegativeInfinity: 'NegativeInfinity',
13+
epoch: 'epoch',
14+
integer: 'integer',
15+
safe: 'safe',
1816
},
1917
/**
2018
* {@link https://arktype.io/docs/primitives Primitives}
2119
*/
2220
primitives: {
23-
bigint: tsc.identifier({ text: 'bigint' }),
24-
boolean: tsc.identifier({ text: 'boolean' }),
25-
keywords: tsc.identifier({ text: 'keywords' }),
21+
bigint: 'bigint',
22+
boolean: 'boolean',
23+
keywords: 'keywords',
2624
null: 'null',
27-
number: tsc.identifier({ text: 'number' }),
25+
number: 'number',
2826
string: 'string',
29-
symbol: tsc.identifier({ text: 'symbol' }),
30-
undefined: tsc.identifier({ text: 'undefined' }),
31-
unit: tsc.identifier({ text: 'unit' }),
27+
symbol: 'symbol',
28+
undefined: 'undefined',
29+
unit: 'unit',
3230
},
3331
/**
3432
* {@link https://arktype.io/docs/primitives#string String}
3533
*/
3634
string: {
37-
NFC: tsc.identifier({ text: 'NFC' }),
38-
NFD: tsc.identifier({ text: 'NFD' }),
39-
NFKC: tsc.identifier({ text: 'NFKC' }),
40-
NFKD: tsc.identifier({ text: 'NFKD' }),
41-
alpha: tsc.identifier({ text: 'alpha' }),
42-
alphanumeric: tsc.identifier({ text: 'alphanumeric' }),
43-
base64: tsc.identifier({ text: 'base64' }),
44-
capitalize: tsc.identifier({ text: 'capitalize' }),
45-
creditCard: tsc.identifier({ text: 'creditCard' }),
35+
NFC: 'NFC',
36+
NFD: 'NFD',
37+
NFKC: 'NFKC',
38+
NFKD: 'NFKD',
39+
alpha: 'alpha',
40+
alphanumeric: 'alphanumeric',
41+
base64: 'base64',
42+
capitalize: 'capitalize',
43+
creditCard: 'creditCard',
4644
date: 'date',
47-
digits: tsc.identifier({ text: 'digits' }),
45+
digits: 'digits',
4846
email: 'email',
49-
epoch: tsc.identifier({ text: 'epoch' }),
50-
hex: tsc.identifier({ text: 'hex' }),
51-
integer: tsc.identifier({ text: 'integer' }),
47+
epoch: 'epoch',
48+
hex: 'hex',
49+
integer: 'integer',
5250
ip: 'ip',
5351
iso: 'iso',
54-
json: tsc.identifier({ text: 'json' }),
55-
lower: tsc.identifier({ text: 'lower' }),
56-
normalize: tsc.identifier({ text: 'normalize' }),
57-
numeric: tsc.identifier({ text: 'numeric' }),
58-
parse: tsc.identifier({ text: 'parse' }),
59-
preformatted: tsc.identifier({ text: 'preformatted' }),
60-
regex: tsc.identifier({ text: 'regex' }),
61-
semver: tsc.identifier({ text: 'semver' }),
62-
trim: tsc.identifier({ text: 'trim' }),
63-
upper: tsc.identifier({ text: 'upper' }),
52+
json: 'json',
53+
lower: 'lower',
54+
normalize: 'normalize',
55+
numeric: 'numeric',
56+
parse: 'parse',
57+
preformatted: 'preformatted',
58+
regex: 'regex',
59+
semver: 'semver',
60+
trim: 'trim',
61+
upper: 'upper',
6462
url: 'url',
6563
uuid: 'uuid',
6664
v1: 'v1',
@@ -76,40 +74,40 @@ export const identifiers = {
7674
* {@link https://arktype.io/docs/type-api Type API}
7775
*/
7876
type: {
79-
$: tsc.identifier({ text: '$' }),
80-
allows: tsc.identifier({ text: 'allows' }),
81-
and: tsc.identifier({ text: 'and' }),
82-
array: tsc.identifier({ text: 'array' }),
83-
as: tsc.identifier({ text: 'as' }),
84-
assert: tsc.identifier({ text: 'assert' }),
85-
brand: tsc.identifier({ text: 'brand' }),
86-
configure: tsc.identifier({ text: 'configure' }),
87-
default: tsc.identifier({ text: 'default' }),
88-
describe: tsc.identifier({ text: 'describe' }),
89-
description: tsc.identifier({ text: 'description' }),
90-
equals: tsc.identifier({ text: 'equals' }),
91-
exclude: tsc.identifier({ text: 'exclude' }),
92-
expression: tsc.identifier({ text: 'expression' }),
93-
extends: tsc.identifier({ text: 'extends' }),
94-
extract: tsc.identifier({ text: 'extract' }),
95-
filter: tsc.identifier({ text: 'filter' }),
96-
from: tsc.identifier({ text: 'from' }),
97-
ifEquals: tsc.identifier({ text: 'ifEquals' }),
98-
ifExtends: tsc.identifier({ text: 'ifExtends' }),
99-
infer: tsc.identifier({ text: 'infer' }),
100-
inferIn: tsc.identifier({ text: 'inferIn' }),
101-
intersect: tsc.identifier({ text: 'intersect' }),
102-
json: tsc.identifier({ text: 'json' }),
103-
meta: tsc.identifier({ text: 'meta' }),
104-
narrow: tsc.identifier({ text: 'narrow' }),
105-
onDeepUndeclaredKey: tsc.identifier({ text: 'onDeepUndeclaredKey' }),
106-
onUndeclaredKey: tsc.identifier({ text: 'onUndeclaredKey' }),
107-
optional: tsc.identifier({ text: 'optional' }),
108-
or: tsc.identifier({ text: 'or' }),
109-
overlaps: tsc.identifier({ text: 'overlaps' }),
110-
pipe: tsc.identifier({ text: 'pipe' }),
111-
select: tsc.identifier({ text: 'select' }),
112-
to: tsc.identifier({ text: 'to' }),
113-
toJsonSchema: tsc.identifier({ text: 'toJsonSchema' }),
77+
$: '$',
78+
allows: 'allows',
79+
and: 'and',
80+
array: 'array',
81+
as: 'as',
82+
assert: 'assert',
83+
brand: 'brand',
84+
configure: 'configure',
85+
default: 'default',
86+
describe: 'describe',
87+
description: 'description',
88+
equals: 'equals',
89+
exclude: 'exclude',
90+
expression: 'expression',
91+
extends: 'extends',
92+
extract: 'extract',
93+
filter: 'filter',
94+
from: 'from',
95+
ifEquals: 'ifEquals',
96+
ifExtends: 'ifExtends',
97+
infer: 'infer',
98+
inferIn: 'inferIn',
99+
intersect: 'intersect',
100+
json: 'json',
101+
meta: 'meta',
102+
narrow: 'narrow',
103+
onDeepUndeclaredKey: 'onDeepUndeclaredKey',
104+
onUndeclaredKey: 'onUndeclaredKey',
105+
optional: 'optional',
106+
or: 'or',
107+
overlaps: 'overlaps',
108+
pipe: 'pipe',
109+
select: 'select',
110+
to: 'to',
111+
toJsonSchema: 'toJsonSchema',
114112
},
115113
};
Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Symbol } from '@hey-api/codegen-core';
2-
import ts from 'typescript';
32

43
import type { IR } from '~/ir/types';
54
import { createSchemaComment } from '~/plugins/shared/utils/schema';
6-
import { tsc } from '~/tsc';
5+
import { $ } from '~/ts-dsl';
76

87
import { identifiers } from '../constants';
98
import type { ArktypePlugin } from '../types';
@@ -27,38 +26,31 @@ export const exportAst = ({
2726
resource: 'arktype.type',
2827
});
2928

30-
const statement = tsc.constVariable({
31-
comment: plugin.config.comments
32-
? createSchemaComment({ schema })
33-
: undefined,
34-
exportConst: symbol.exported,
35-
expression: tsc.callExpression({
36-
functionName: type.placeholder,
37-
parameters: [
38-
ast.def ? tsc.stringLiteral({ text: ast.def }) : ast.expression,
39-
],
40-
}),
41-
name: symbol.placeholder,
42-
// typeName: ast.typeName
43-
// ? (tsc.propertyAccessExpression({
44-
// expression: z.placeholder,
45-
// name: ast.typeName,
46-
// }) as unknown as ts.TypeNode)
47-
// : undefined,
48-
});
29+
const statement = $.const(symbol.placeholder)
30+
.export(symbol.exported)
31+
.$if(plugin.config.comments && createSchemaComment({ schema }), (c, v) =>
32+
c.doc(v as ReadonlyArray<string>),
33+
)
34+
// .type(
35+
// ast.typeName
36+
// ? (tsc.propertyAccessExpression({
37+
// expression: z.placeholder,
38+
// name: ast.typeName,
39+
// }) as unknown as ts.TypeNode)
40+
// : undefined,
41+
// )
42+
.assign(
43+
$(type.placeholder).call(ast.def ? $.literal(ast.def) : ast.expression),
44+
);
4945
plugin.setSymbolValue(symbol, statement);
5046

5147
if (typeInferSymbol) {
52-
const inferType = tsc.typeAliasDeclaration({
53-
exportType: typeInferSymbol.exported,
54-
name: typeInferSymbol.placeholder,
55-
type: ts.factory.createTypeQueryNode(
56-
ts.factory.createQualifiedName(
57-
ts.factory.createIdentifier(symbol.placeholder),
58-
identifiers.type.infer,
59-
),
60-
),
61-
});
48+
const inferType = $.type
49+
.alias(typeInferSymbol.placeholder)
50+
.export(typeInferSymbol.exported)
51+
.type(
52+
$.type(symbol.placeholder).attr(identifiers.type.infer).typeofType(),
53+
);
6254
plugin.setSymbolValue(typeInferSymbol, inferType);
6355
}
6456
};

packages/openapi-ts/src/plugins/arktype/shared/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import type ts from 'typescript';
33

44
import type { IR } from '~/ir/types';
55
import type { ToRefs } from '~/plugins';
6+
import type { $ } from '~/ts-dsl';
67

78
import type { ArktypePlugin } from '../types';
89

910
export type Ast = {
1011
def: string;
11-
expression: ts.Expression;
12+
expression: ReturnType<typeof $.call | typeof $.expr | typeof $.object>;
1213
hasLazyExpression?: boolean;
1314
typeName?: string | ts.Identifier;
1415
};

packages/openapi-ts/src/plugins/arktype/v2/plugin.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { IR } from '~/ir/types';
55
import { buildName } from '~/openApi/shared/utils/name';
66
import type { SchemaWithType } from '~/plugins/shared/types/schema';
77
import { toRefs } from '~/plugins/shared/utils/refs';
8-
import { tsc } from '~/tsc';
8+
import { $ } from '~/ts-dsl';
99
import { pathToJsonPointer, refToName } from '~/utils/ref';
1010

1111
import { exportAst } from '../shared/export';
@@ -43,27 +43,14 @@ export const irSchemaToAst = ({
4343
};
4444
const refSymbol = plugin.referenceSymbol(query);
4545
if (plugin.isSymbolRegistered(query)) {
46-
const ref = tsc.identifier({ text: refSymbol.placeholder });
46+
const ref = $(refSymbol.placeholder);
4747
ast.expression = ref;
4848
} else {
49-
const lazyExpression = tsc.callExpression({
50-
functionName: tsc.propertyAccessExpression({
51-
// expression: z.placeholder,
52-
expression: 'TODO',
53-
name: 'TODO',
54-
// name: identifiers.lazy,
55-
}),
56-
parameters: [
57-
tsc.arrowFunction({
58-
returnType: tsc.keywordTypeNode({ keyword: 'any' }),
59-
statements: [
60-
tsc.returnStatement({
61-
expression: tsc.identifier({ text: refSymbol.placeholder }),
62-
}),
63-
],
64-
}),
65-
],
66-
});
49+
// expression: z.placeholder,
50+
// name: identifiers.lazy,
51+
const lazyExpression = $('TODO')
52+
.attr('TODO')
53+
.call($.func().returns('any').do($.return(refSymbol.placeholder)));
6754
ast.expression = lazyExpression;
6855
ast.hasLazyExpression = true;
6956
state.hasLazyExpression.value = true;

packages/openapi-ts/src/plugins/arktype/v2/toAst/index.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import ts from 'typescript';
2-
31
import type { SchemaWithType } from '~/plugins/shared/types/schema';
2+
import { $ } from '~/ts-dsl';
43

54
import type { Ast, IrSchemaToAstOptions } from '../../shared/types';
65
import { nullToAst } from './null';
@@ -91,30 +90,11 @@ export const irSchemaWithTypeToAst = ({
9190
resource: 'arktype.type',
9291
});
9392

94-
const expression = ts.factory.createCallExpression(
95-
ts.factory.createIdentifier(type.placeholder),
96-
undefined,
97-
[
98-
ts.factory.createObjectLiteralExpression(
99-
[
100-
ts.factory.createPropertyAssignment(
101-
'name',
102-
ts.factory.createStringLiteral('string'),
103-
),
104-
ts.factory.createPropertyAssignment(
105-
'platform',
106-
ts.factory.createStringLiteral("'android' | 'ios'"),
107-
),
108-
ts.factory.createPropertyAssignment(
109-
ts.factory.createComputedPropertyName(
110-
ts.factory.createStringLiteral('versions?'),
111-
),
112-
ts.factory.createStringLiteral('(number | string)[]'),
113-
),
114-
],
115-
true,
116-
),
117-
],
93+
const expression = $(type.placeholder).call(
94+
$.object()
95+
.prop('name', $.literal('string'))
96+
.prop('platform', $.literal("'android' | 'ios'"))
97+
.computed('versions?', $.literal('(number | string)[]')),
11898
);
11999

120100
return {

0 commit comments

Comments
 (0)