Skip to content

Commit 5dfc1b4

Browse files
committed
Remove imported module name suffix with compactor
1 parent 257c8a1 commit 5dfc1b4

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/Writers/ModuleWriter.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function format(TypesCollection $collection): string {
7070
$import .= join(
7171
', ',
7272
array_map(
73-
fn(NamespacedType $type) => $type->shortName,
73+
fn(NamespacedType $type) => $this->compactor->removeSuffix($type->shortName),
7474
$types
7575
)
7676
);
@@ -79,16 +79,16 @@ public function format(TypesCollection $collection): string {
7979
$currentRest = ltrim(substr($currentModuleTsNamespace, strlen($commonPrefix)), '\\');
8080
$backParts = array_fill(0, substr_count($currentRest, '\\'), '..');
8181
$sourceModulePath =
82-
(
83-
count($backParts) === 0
84-
? '.'
85-
: join('/', $backParts)
86-
)
87-
. '/'
88-
. join(
89-
'/',
90-
explode('\\', $importedRest)
91-
);
82+
(
83+
count($backParts) === 0
84+
? '.'
85+
: join('/', $backParts)
86+
)
87+
. '/'
88+
. join(
89+
'/',
90+
explode('\\', $importedRest)
91+
);
9292

9393
$import .= '} from "' . $sourceModulePath . "\";\n";
9494
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
4+
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
5+
use function PHPUnit\Framework\assertEquals;
6+
7+
it('removes suffix', function () {
8+
$compactor = new ConfigCompactor(
9+
(new TypeScriptTransformerConfig())
10+
->compactorSuffixes(['Data', 'Dto'])
11+
);
12+
assertEquals(
13+
'Hello\User',
14+
$compactor->removeSuffix('Hello\UserData')
15+
);
16+
});
17+
it('removes prefix', function () {
18+
$compactor = new ConfigCompactor(
19+
(new TypeScriptTransformerConfig())
20+
->compactorPrefixes(['App\Data', 'App\Tests\Data'])
21+
);
22+
assertEquals(
23+
'Product',
24+
$compactor->removePrefix('App\Data\Product')
25+
);
26+
assertEquals(
27+
'User',
28+
$compactor->removePrefix('App\Tests\Data\User')
29+
);
30+
});

tests/__snapshots__/IntegrationTest__it_can_transform_to_es_modules__1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {RegularEnum} from "./Enum";
2-
import {YetAnotherDto} from "./LevelUp";
2+
import {YetAnotherDto} from "./Integration/LevelUp";
33
export type Dto = {
44
string: string;
55
nullbable: string | null;

0 commit comments

Comments
 (0)