Skip to content

Commit aa7e836

Browse files
committed
Don't fix *.main.js files.
1 parent ef7119c commit aa7e836

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

src/Helper/TypeScript/TypeScriptFixHelper.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,21 @@ public function fixJavaScriptFile(string $path): void
7474

7575
if (!$this->hasBeenProcessed())
7676
{
77-
$this->io->logInfo('TypeScript fixing: <fso>%s</fso>', $path);
77+
if ($this->requiresFixing($path))
78+
{
79+
$this->io->logInfo('TypeScript fixing: <fso>%s</fso>', $path);
80+
81+
$this->fixDefine();
82+
$this->fixExports1('Object.defineProperty(exports, "__esModule", { value: true });');
83+
$this->fixExports1(sprintf('exports.%1$s = %1$s;', Path::getFilename($this->namespace)));
84+
$this->fixExports2();
85+
$this->fixReferences();
86+
}
87+
else
88+
{
89+
$this->io->logVeryVerbose("Main file doesn't require fixing: <fso>%s</fso>", $path);
90+
}
7891

79-
$this->fixDefine();
80-
$this->fixExports1('Object.defineProperty(exports, "__esModule", { value: true });');
81-
$this->fixExports1(sprintf('exports.%1$s = %1$s;', Path::getFilename($this->namespace)));
82-
$this->fixExports2();
83-
$this->fixReferences();
8492
$this->writeJsSource($path);
8593
}
8694
else
@@ -290,6 +298,19 @@ private function readJsSource(string $path)
290298
}
291299
}
292300

301+
//--------------------------------------------------------------------------------------------------------------------
302+
/**
303+
* Returns true if and only if the JS source requires fixing
304+
*
305+
* @param string $path The path to JS file.
306+
*
307+
* @return bool
308+
*/
309+
private function requiresFixing($path): bool
310+
{
311+
return (!str_ends_with($path, '.main.js'));
312+
}
313+
293314
//--------------------------------------------------------------------------------------------------------------------
294315
/**
295316
* Writes the modified JS source.

test/Helper/TypeScript/TypeScriptFixHelperTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,37 @@
1313
*/
1414
class TypeScriptFixHelperTest extends TestCase
1515
{
16+
//--------------------------------------------------------------------------------------------------------------------
17+
/**
18+
* Test *.main.js files are not modified.
19+
*/
20+
public function testDontTouchMain(): void
21+
{
22+
putenv(sprintf('PLAISIO_CONFIG_DIR=%s', Path::join(__DIR__, __FUNCTION__)));
23+
24+
$jsPath = Path::makeRelative(Path::join(__DIR__,
25+
__FUNCTION__,
26+
'js/Plaisio/PageDecorator',
27+
'CorePageDecorator.main.js'),
28+
getcwd());
29+
$orgPath = Path::changeExtension($jsPath, 'org.js');
30+
$expectedPath = Path::changeExtension($jsPath, 'expected.js');
31+
copy($orgPath, $jsPath);
32+
33+
$application = new PlaisioApplication();
34+
$application->setAutoExit(false);
35+
36+
$tester = new ApplicationTester($application);
37+
$tester->run(['command' => 'plaisio:type-script-fixer',
38+
'path' => $jsPath]);
39+
40+
if ($tester->getStatusCode()!==0) echo $tester->getDisplay();
41+
42+
self::assertSame(0, $tester->getStatusCode());
43+
self::assertFileEquals($expectedPath, $jsPath);
44+
45+
unlink($jsPath);
46+
}
1647
//--------------------------------------------------------------------------------------------------------------------
1748
/**
1849
* Test fixing defining deps with global packages and local packages.
@@ -33,6 +64,8 @@ public function testFixDefineDeps(): void
3364
$tester->run(['command' => 'plaisio:type-script-fixer',
3465
'path' => $jsPath]);
3566

67+
if ($tester->getStatusCode()!==0) echo $tester->getDisplay();
68+
3669
self::assertSame(0, $tester->getStatusCode());
3770
self::assertFileEquals($expectedPath, $jsPath);
3871

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//----------------------------------------------------------------------------------------------------------------------
2+
// @ts-ignore
3+
requirejs.config({
4+
baseUrl: '/js',
5+
paths: {
6+
'jquery': 'jquery/jquery',
7+
'jquery.cookie': 'js-cookie/js.cookie',
8+
'js-cookie': 'js-cookie/js.cookie'
9+
}
10+
});
11+
//----------------------------------------------------------------------------------------------------------------------
12+
// @ts-ignore
13+
require(["Plaisio/PageDecorator/CorePageDecorator"]);
14+
// Modified by Plaisio\Console\Helper\TypeScript\TypeScriptFixHelper
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//----------------------------------------------------------------------------------------------------------------------
2+
// @ts-ignore
3+
requirejs.config({
4+
baseUrl: '/js',
5+
paths: {
6+
'jquery': 'jquery/jquery',
7+
'jquery.cookie': 'js-cookie/js.cookie',
8+
'js-cookie': 'js-cookie/js.cookie'
9+
}
10+
});
11+
//----------------------------------------------------------------------------------------------------------------------
12+
// @ts-ignore
13+
require(["Plaisio/PageDecorator/CorePageDecorator"]);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<assets>
3+
<root>test/Helper/TypeScript/testFixDefineDeps</root>
4+
</assets>

0 commit comments

Comments
 (0)