From 963f7a033882a4e91ccc93c845c58ff477137ad2 Mon Sep 17 00:00:00 2001 From: Martin Ficzel Date: Wed, 4 Oct 2023 17:40:52 +0200 Subject: [PATCH] TASK: Migrate fusion `context.currentRenderingMode` statements Statements accessing `context.currentRenderingMode.edit|preview|name|title|fusionPath|options` on `node|site|documentNode` are migrated. Other uses of `context.currentRenderingMode` are marked with a comment. Resolves: #23 --- ...usionContextCurrentRenderingModeRector.php | 56 +++++++++++++++++++ .../Fixture/some_file.fusion.inc | 45 +++++++++++++++ ...nContextCurrentRenderingModeRectorTest.php | 31 ++++++++++ .../config/configured_rule.php | 17 ++++++ 4 files changed, 149 insertions(+) create mode 100644 src/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector.php create mode 100644 tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/Fixture/some_file.fusion.inc create mode 100644 tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/FusionContextCurrentRenderingModeRectorTest.php create mode 100644 tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/config/configured_rule.php diff --git a/src/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector.php b/src/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector.php new file mode 100644 index 0000000..7374a02 --- /dev/null +++ b/src/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector.php @@ -0,0 +1,56 @@ +process(fn(string $eelExpression) => preg_replace( + '/(node|documentNode|site)\.context\.currentRenderingMode\.name', + 'renderingMode.name', + $eelExpression + )) + ->process(fn(string $eelExpression) => preg_replace( + '/(node|documentNode|site)\.context\.currentRenderingMode\.edit', + 'renderingMode.isEdit', + $eelExpression + )) + ->process(fn(string $eelExpression) => preg_replace( + '/(node|documentNode|site)\.context\.currentRenderingMode\.preview', + 'renderingMode.isPreview', + $eelExpression + )) + ->process(fn(string $eelExpression) => preg_replace( + '/(node|documentNode|site)\.context\.currentRenderingMode\.fusionPath', + 'renderingMode.fusionPath', + $eelExpression + )) + ->process(fn(string $eelExpression) => preg_replace( + '/(node|documentNode|site)\.context\.currentRenderingMode\.options', + 'renderingMode.options', + $eelExpression + )) + ->process(fn(string $eelExpression) => preg_replace( + '/(node|documentNode|site)\.context\.currentRenderingMode\.title', + 'renderingMode.title', + $eelExpression + )) + ->addCommentsIfRegexMatches( + '/\.context\.currentRenderingMode/', + '// TODO 9.0 migration: Line %LINE: You very likely need to rewrite "VARIABLE.context.currentRenderingMode..." to "renderingMode...". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.' + )->getProcessedContent(); + } +} diff --git a/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/Fixture/some_file.fusion.inc b/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/Fixture/some_file.fusion.inc new file mode 100644 index 0000000..02b56f9 --- /dev/null +++ b/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/Fixture/some_file.fusion.inc @@ -0,0 +1,45 @@ +prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) { + + renderer = Neos.Fusion:Component { + + # + # pass down props + # + nodeAttributes = ${node.context.currentRenderingMode.edit || node.currentRenderingMode.preview || node.context.currentRenderingMode.title || node.context.currentRenderingMode.name || node.context.currentRenderingMode.fusionPath || node.context.currentRenderingMode.options['foo']} + siteAttributes = ${site.context.currentRenderingMode.edit || site.currentRenderingMode.preview || site.context.currentRenderingMode.title || site.context.currentRenderingMode.name || site.context.currentRenderingMode.fusionPath || site.context.currentRenderingMode.options['foo']} + documentNodeAttributes = ${documentNode.context.currentRenderingMode.edit || documentNode.currentRenderingMode.preview || documentNode.context.currentRenderingMode.title || documentNode.context.currentRenderingMode.name || documentNode.context.currentRenderingMode.fusionPath || documentNode.context.currentRenderingMode.options['foo']} + other = ${other.context.currentRenderingMode.edit || other.currentRenderingMode.preview || other.context.currentRenderingMode.title || other.context.currentRenderingMode.name || other.context.currentRenderingMode.fusionPath || other.context.currentRenderingMode.options['foo']} + + renderer = afx` + + ` + } +} +----- +// TODO 9.0 migration: Line 26: You very likely need to rewrite "VARIABLE.context.currentRenderingMode..." to "renderingMode...". We did not auto-apply this migration because we cannot be sure whether the variable is a Node. +prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) { + + renderer = Neos.Fusion:Component { + + nodeAttributes = ${renderingMode.isEdit || renderingMode.isPreview || renderingMode.title || renderingMode.name || renderingMode.fusionPath || renderingMode.options['foo']} + siteAttributes = ${renderingMode.isEdit || renderingMode.isPreview || renderingMode.title || renderingMode.name || renderingMode.fusionPath || renderingMode.options['foo']} + documentNodeAttributes = ${renderingMode.isEdit || renderingMode.isPreview || renderingMode.title || renderingMode.name || renderingMode.fusionPath || renderingMode.options['foo']} + other = ${other.context.currentRenderingMode.edit || other.currentRenderingMode.preview || other.context.currentRenderingMode.title || other.context.currentRenderingMode.name || other.context.currentRenderingMode.fusionPath || other.context.currentRenderingMode.options['foo']} + + renderer = afx` + + ` + } +} diff --git a/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/FusionContextCurrentRenderingModeRectorTest.php b/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/FusionContextCurrentRenderingModeRectorTest.php new file mode 100644 index 0000000..8fc413a --- /dev/null +++ b/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/FusionContextCurrentRenderingModeRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($fileInfo); + } + + /** + * @return \Iterator + */ + public function provideData(): \Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture', '*.fusion.inc'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/config/configured_rule.php b/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/config/configured_rule.php new file mode 100644 index 0000000..c24ab5e --- /dev/null +++ b/tests/ContentRepository90/Rules/FusionContextCurrentRenderingModeRector/config/configured_rule.php @@ -0,0 +1,17 @@ +services(); + $services->defaults() + ->public() + ->autowire() + ->autoconfigure(); + $services->set(\Neos\Rector\Core\FusionProcessing\FusionFileProcessor::class); + $rectorConfig->disableParallel(); // does not work for fusion files - see https://github.com/rectorphp/rector-src/pull/2597#issuecomment-1190120688 + + $rectorConfig->rule(FusionContextCurrentRenderingModeRector::class); +};