Skip to content

Commit 5b00fb2

Browse files
authored
Merge pull request #139 from RightCapitalHQ/feature/add-more-test-cases-for-transpiler
fix: add more test cases for transpiler
2 parents e3ecb11 + 5ab9c4f commit 5b00fb2

File tree

2 files changed

+152
-29
lines changed

2 files changed

+152
-29
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "none",
3+
"comment": "fix: add more test cases for transpiler",
4+
"packageName": "@rightcapital/phpdoc-parser",
5+
"email": "yilunsun11@yeah.net",
6+
"dependentChangeType": "none"
7+
}

tests/transpiler/transpiler.test.ts

Lines changed: 145 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import {
1313
} from '../../src';
1414

1515
class ExtendedTranspiler extends PhpDocTypeNodeToTypescriptTypeNodeTranspiler {
16-
public customProperty: string = '';
17-
1816
constructor(public resolver: NameNodePathResolver<ExtendedTranspiler>) {
1917
super(
2018
(nodeParts: string[]) =>
@@ -47,39 +45,28 @@ const getPropertyTagValueNodesFromComment = (commentText: string) => {
4745
return propertyTagValueNodes;
4846
};
4947

50-
describe('TranspilerTest', () => {
51-
// Define a resolver function for path resolving in the transpiler.
52-
const nameNodePathResolver: NameNodePathResolver<ExtendedTranspiler> =
53-
// eslint-disable-next-line func-names
54-
function (this: ExtendedTranspiler, nodeParts: string[]) {
55-
return {
56-
name: nodeParts.at(-1),
57-
path: '',
58-
isTypeOnly: false,
59-
};
48+
// Define a resolver function for path resolving in the transpiler.
49+
const nameNodePathResolver: NameNodePathResolver<ExtendedTranspiler> =
50+
// eslint-disable-next-line func-names
51+
function (this: ExtendedTranspiler, nodeParts: string[]) {
52+
return {
53+
name: nodeParts.at(-1),
54+
path: '',
55+
isTypeOnly: false,
6056
};
57+
};
6158

62-
const commentText = `/**
63-
* @property-read array|null $person
64-
* @property int $id
65-
* @property-read \\Illuminate\\Database\\Eloquent\\Collection<int, int> $ids
66-
*/`;
67-
68-
// Parse the PHPDoc comment text to get node structures.
59+
const testCommentTextTranspile = (
60+
commentText: string,
61+
transpiledTypeDefinitionTestCases: string[],
62+
) => {
6963
const transpiledCommentNodes =
7064
getPropertyTagValueNodesFromComment(commentText);
7165

72-
const transpiledTypeDefinitionTestCases = [
73-
'person: any | null;',
74-
'id: number;',
75-
'ids: number[];',
76-
];
77-
7866
transpiledCommentNodes.forEach((transpiledCommentNode, index) => {
7967
const transpiler = new ExtendedTranspiler(nameNodePathResolver);
80-
transpiler.customProperty = 'this is a custom property'; // Set your configurations
81-
transpiler.beforeTranspile(); // Initialize transpilation state(reset the state of importDeclarations)
82-
const transpiledTypeNode = transpiler.transpile(transpiledCommentNode.type); // Transpile the node
68+
transpiler.beforeTranspile();
69+
const transpiledTypeNode = transpiler.transpile(transpiledCommentNode.type);
8370

8471
const typescriptNode = factory.createPropertySignature(
8572
undefined,
@@ -88,7 +75,6 @@ describe('TranspilerTest', () => {
8875
transpiledTypeNode,
8976
);
9077

91-
// Render the TypeScript node to a string
9278
const propertyDefinitionText = renderTsNodeToString(typescriptNode);
9379

9480
it(propertyDefinitionText, () => {
@@ -97,4 +83,134 @@ describe('TranspilerTest', () => {
9783
);
9884
});
9985
});
86+
};
87+
88+
describe('Transpiler', () => {
89+
describe('UnionTypeNode', () => {
90+
const commentText = `/**
91+
* @property string | int | bool $three_types
92+
* @property int|null $id
93+
*/`;
94+
95+
const transpiledTypeDefinitionTestCases = [
96+
'three_types: string | number | boolean;',
97+
'id: number | null;',
98+
];
99+
100+
testCommentTextTranspile(commentText, transpiledTypeDefinitionTestCases);
101+
});
102+
103+
describe('ArrayTypeNode', () => {
104+
const commentText = `/**
105+
* @property string[] $names
106+
*/`;
107+
108+
const transpiledTypeDefinitionTestCases = ['names: string[];'];
109+
110+
testCommentTextTranspile(commentText, transpiledTypeDefinitionTestCases);
111+
});
112+
113+
describe('ObjectShapeNode and ArrayShapeNode', () => {
114+
const commentText = `/**
115+
* @property object{'foo': int} $object_shape
116+
* @property array{'foo': int, "bar"?: string, 0: boolean} $array_shape
117+
*/`;
118+
119+
const transpiledTypeDefinitionTestCases = [
120+
`object_shape: {
121+
foo: number;
122+
};`,
123+
`array_shape: {
124+
foo: number;
125+
bar?: string;
126+
0: boolean;
127+
};`,
128+
];
129+
130+
testCommentTextTranspile(commentText, transpiledTypeDefinitionTestCases);
131+
});
132+
133+
describe('GenericTypeNode', () => {
134+
const commentText = `/**
135+
* @property array<Type> $type_1
136+
* @property non-empty-array<Type> $type_2
137+
* @property list<Type> $type_3
138+
* @property non-empty-list<Type> $type_4
139+
* @property \\Illuminate\\Database\\Eloquent\\Collection<Type> $type_5
140+
* @property array<int, Type> $type_6
141+
* @property non-empty-array<integer, Type> $type_7
142+
* @property list<float, Type> $type_8
143+
* @property non-empty-list<double, Type> $type_9
144+
* @property \\Illuminate\\Database\\Eloquent\\Collection<int, Type> $type_10
145+
* @property array<string, Type> $type_11
146+
* @property non-empty-array<string, Type> $type_12
147+
* @property list<string, Type> $type_13
148+
* @property non-empty-list<string, Type> $type_14
149+
* @property \\Illuminate\\Database\\Eloquent\\Collection<string, Type> $type_15
150+
*/`;
151+
152+
const transpiledTypeDefinitionTestCases = [
153+
`type_1: Type[];`,
154+
`type_2: Type[];`,
155+
`type_3: Type[];`,
156+
`type_4: Type[];`,
157+
`type_5: Type[];`,
158+
`type_6: Type[];`,
159+
`type_7: Type[];`,
160+
`type_8: Type[];`,
161+
`type_9: Type[];`,
162+
`type_10: Type[];`,
163+
`type_11: Record<string, Type>;`,
164+
`type_12: Record<string, Type>;`,
165+
`type_13: Record<string, Type>;`,
166+
`type_14: Record<string, Type>;`,
167+
`type_15: Record<string, Type>;`,
168+
];
169+
170+
testCommentTextTranspile(commentText, transpiledTypeDefinitionTestCases);
171+
});
172+
173+
describe('IdentifierTypeNode', () => {
174+
const commentText = `/**
175+
* @property bool $boolean_type_1
176+
* @property boolean $boolean_type_2
177+
* @property true $boolean_type_3
178+
* @property false $boolean_type_4
179+
* @property int $number_type_1
180+
* @property integer $number_type_2
181+
* @property float $number_type_3
182+
* @property double $number_type_4
183+
* @property string $string_type
184+
* @property array-key $array_key_type
185+
* @property scalar $scalar_type
186+
* @property mixed $mixed_type
187+
* @property void $void_type
188+
* @property null $null_type
189+
* @property Expr $expr_type
190+
* @property Node\\Arg $arg_type_1
191+
* @property \\Ast\\Node\\Arg $arg_type_2
192+
*/`;
193+
194+
const transpiledTypeDefinitionTestCases = [
195+
'boolean_type_1: boolean;',
196+
'boolean_type_2: boolean;',
197+
'boolean_type_3: boolean;',
198+
'boolean_type_4: boolean;',
199+
'number_type_1: number;',
200+
'number_type_2: number;',
201+
'number_type_3: number;',
202+
'number_type_4: number;',
203+
'string_type: string;',
204+
'array_key_type: string | number;',
205+
'scalar_type: string | number | boolean;',
206+
'mixed_type: any;',
207+
'void_type: void;',
208+
'null_type: null;',
209+
'expr_type: Expr;',
210+
'arg_type_1: Arg;',
211+
'arg_type_2: Arg;',
212+
];
213+
214+
testCommentTextTranspile(commentText, transpiledTypeDefinitionTestCases);
215+
});
100216
});

0 commit comments

Comments
 (0)