Skip to content

Commit b16ce5d

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

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

src/Writers/ModuleWriter.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ 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)
82+
$this->compactor->removeSuffix(
83+
(
84+
count($backParts) === 0
85+
? '.'
86+
: join('/', $backParts)
87+
)
88+
. '/'
89+
. join(
90+
'/',
91+
explode('\\', $importedRest)
92+
)
9193
);
9294

9395
$import .= '} from "' . $sourceModulePath . "\";\n";
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)