Skip to content

Commit

Permalink
Merge pull request #137 from RightCapitalHQ/feature/modify-generic-ty…
Browse files Browse the repository at this point in the history
…pe-parsing

fix: modify generic type transpiling
  • Loading branch information
rainx authored Apr 1, 2024
2 parents 1c9aca4 + 59c143c commit 814d010
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: modify generic type parsing",
"packageName": "@rightcapital/phpdoc-parser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,22 @@ export class PhpDocTypeNodeToTypescriptTypeNodeTranspiler {
this.transpile(sourceTypeNode.genericTypes[0]),
);
}

if (sourceTypeNode.genericTypes.length === 2) {
// Record<KeyType, ValueType>

if (
sourceTypeNode.genericTypes[0].isIdentifierTypeNode() &&
['int', 'integer', 'float', 'double'].includes(
sourceTypeNode.genericTypes[0].name,
)
) {
// turn into regular Array like Type[]
return factory.createArrayTypeNode(
this.transpile(sourceTypeNode.genericTypes[1]),
);
}

return factory.createTypeReferenceNode(
factory.createIdentifier('Record'),
[
Expand Down
2 changes: 2 additions & 0 deletions tests/transpiler/transpiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('TranspilerTest', () => {
const commentText = `/**
* @property-read array|null $person
* @property int $id
* @property-read \\Illuminate\\Database\\Eloquent\\Collection<int, int> $ids
*/`;

// Parse the PHPDoc comment text to get node structures.
Expand All @@ -71,6 +72,7 @@ describe('TranspilerTest', () => {
const transpiledTypeDefinitionTestCases = [
'person: any | null;',
'id: number;',
'ids: number[];',
];

transpiledCommentNodes.forEach((transpiledCommentNode, index) => {
Expand Down

0 comments on commit 814d010

Please sign in to comment.