-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Migrate
${node.nodeType.name}
and ${node.nodeType}
Solves partially #57 Also migrates `q(node).property('_nodeType.name')` as we actually used the syntax once as well: neos/neos-development-collection#3641
- Loading branch information
Showing
8 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/ContentRepository90/Rules/FusionNodeNodeTypeRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Rector\ContentRepository90\Rules; | ||
|
||
use Neos\Rector\Core\FusionProcessing\EelExpressionTransformer; | ||
use Neos\Rector\Core\FusionProcessing\FusionRectorInterface; | ||
use Neos\Rector\Utility\CodeSampleLoader; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
class FusionNodeNodeTypeRector implements FusionRectorInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return CodeSampleLoader::fromFile('Fusion: Rewrite "node.nodeType" and "q(node).property(\'_nodeType\')" to "Neos.Node.getNodeType(node)"', __CLASS__); | ||
} | ||
|
||
public function refactorFileContent(string $fileContent): string | ||
{ | ||
return EelExpressionTransformer::parse($fileContent) | ||
->process(fn(string $eelExpression) => preg_replace( | ||
'/(node|documentNode|site)\.nodeType\.name/', | ||
'q($1).nodeTypeName()', | ||
$eelExpression | ||
)) | ||
->process(fn(string $eelExpression) => preg_replace( | ||
'/(node|documentNode|site)\.nodeType/', | ||
'Neos.Node.getNodeType($1)', | ||
$eelExpression | ||
)) | ||
->addCommentsIfRegexMatches( | ||
'/\.nodeType\b(?!\()/', | ||
'// TODO 9.0 migration: Line %LINE: You very likely need to rewrite "VARIABLE.nodeType" to "Neos.Node.getNodeType(VARIABLE)". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.' | ||
) | ||
->process(fn(string $eelExpression) => preg_replace( | ||
'/\.property\\((\'|")_nodeType.name(\'|")\\)/', | ||
'.nodeTypeName()', | ||
$eelExpression | ||
)) | ||
->process(fn(string $eelExpression) => preg_replace( | ||
'/q\(([^)]+)\)\.property\\([\'"]_nodeType(\.[^\'"]*)?[\'"]\\)/', | ||
'Neos.Node.getNodeType($1)$2', | ||
$eelExpression | ||
)) | ||
->getProcessedContent(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ContentRepository90/Rules/FusionNodeNodeTypeRector/Fixture/flow_query_property.fusion.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) { | ||
node = ${q(node).property('_nodeType') || q(documentNode).property("_nodeType") || q(site).property("_nodeType")} | ||
otherVariable = ${q(someOtherVariable).property('_nodeType')} | ||
nested = ${q(someOtherVariable).property('_nodeType.properties')} | ||
deepNested = ${q(someOtherVariable).property('_nodeType.options.myOption')} | ||
inAfx = afx`<Neos.Fusion:Value value={q(node).property('_nodeType')}/>` | ||
} | ||
----- | ||
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) { | ||
node = ${Neos.Node.getNodeType(node) || Neos.Node.getNodeType(documentNode) || Neos.Node.getNodeType(site)} | ||
otherVariable = ${Neos.Node.getNodeType(someOtherVariable)} | ||
nested = ${Neos.Node.getNodeType(someOtherVariable).properties} | ||
deepNested = ${Neos.Node.getNodeType(someOtherVariable).options.myOption} | ||
inAfx = afx`<Neos.Fusion:Value value={Neos.Node.getNodeType(node)}/>` | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/ContentRepository90/Rules/FusionNodeNodeTypeRector/Fixture/node_getter.fusion.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) { | ||
node = ${node.nodeType || documentNode.nodeType || site.nodeType} | ||
otherVariable = ${someOtherVariable.nodeType} | ||
inAfx = afx`<Neos.Fusion:Value value={node.nodeType}/>` | ||
} | ||
----- | ||
// TODO 9.0 migration: Line 4: You very likely need to rewrite "VARIABLE.nodeType" to "Neos.Node.getNodeType(VARIABLE)". We did not auto-apply this migration because we cannot be sure whether the variable is a Node. | ||
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) { | ||
node = ${Neos.Node.getNodeType(node) || Neos.Node.getNodeType(documentNode) || Neos.Node.getNodeType(site)} | ||
otherVariable = ${someOtherVariable.nodeType} | ||
inAfx = afx`<Neos.Fusion:Value value={Neos.Node.getNodeType(node)}/>` | ||
} |
11 changes: 11 additions & 0 deletions
11
...0/Rules/FusionNodeNodeTypeRector/Fixture/node_nodeTypeName_flow_query_property.fusion.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) { | ||
node = ${q(node).property('_nodeType.name') || q(documentNode).property("_nodeType.name") || q(site).property("_nodeType.name")} | ||
otherVariable = ${q(someOtherVariable).property('_nodeType.name')} | ||
inAfx = afx`<Neos.Fusion:Value value={q(node).property('_nodeType.name')}/>` | ||
} | ||
----- | ||
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) { | ||
node = ${q(node).nodeTypeName() || q(documentNode).nodeTypeName() || q(site).nodeTypeName()} | ||
otherVariable = ${q(someOtherVariable).nodeTypeName()} | ||
inAfx = afx`<Neos.Fusion:Value value={q(node).nodeTypeName()}/>` | ||
} |
12 changes: 12 additions & 0 deletions
12
...ntRepository90/Rules/FusionNodeNodeTypeRector/Fixture/node_nodeTypeName_getter.fusion.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) { | ||
node = ${node.nodeType.name || documentNode.nodeType.name || site.nodeType.name} | ||
otherVariable = ${someOtherVariable.nodeType.name} | ||
inAfx = afx`<Neos.Fusion:Value value={node.nodeType.name}/>` | ||
} | ||
----- | ||
// TODO 9.0 migration: Line 4: You very likely need to rewrite "VARIABLE.nodeType" to "Neos.Node.getNodeType(VARIABLE)". We did not auto-apply this migration because we cannot be sure whether the variable is a Node. | ||
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) { | ||
node = ${q(node).nodeTypeName() || q(documentNode).nodeTypeName() || q(site).nodeTypeName()} | ||
otherVariable = ${someOtherVariable.nodeType.name} | ||
inAfx = afx`<Neos.Fusion:Value value={q(node).nodeTypeName()}/>` | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/ContentRepository90/Rules/FusionNodeNodeTypeRector/FusionNodeNodeTypeRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Rector\Tests\ContentRepository90\Rules\FusionNodeNodeTypeRector; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class FusionNodeNodeTypeRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $fileInfo): void | ||
{ | ||
$this->doTestFile($fileInfo); | ||
} | ||
|
||
/** | ||
* @return \Iterator<string> | ||
*/ | ||
public function provideData(): \Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture', '*.fusion.inc'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/ContentRepository90/Rules/FusionNodeNodeTypeRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
|
||
use Neos\Rector\ContentRepository90\Rules\FusionNodeNodeTypeRector; | ||
use Neos\Rector\Core\FusionProcessing\FusionFileProcessor; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig) : void { | ||
$services = $rectorConfig->services(); | ||
$services->defaults() | ||
->public() | ||
->autowire() | ||
->autoconfigure(); | ||
$services->set(FusionFileProcessor::class); | ||
$rectorConfig->disableParallel(); // does not work for fusion files - see https://github.com/rectorphp/rector-src/pull/2597#issuecomment-1190120688 | ||
|
||
$rectorConfig->rule(FusionNodeNodeTypeRector::class); | ||
}; |