From 26e527a06df4a776ca187e08a44f0cbf5da912ce Mon Sep 17 00:00:00 2001 From: fisker Date: Mon, 28 Aug 2023 11:12:50 +0800 Subject: [PATCH 1/2] Fix magic methods detection --- src/printer.js | 2 +- src/util.js | 13 ++++--------- tests/case/__snapshots__/jsfmt.spec.js.snap | 18 +++++++++++++++--- tests/case/magic-methods.php | 4 ++++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/printer.js b/src/printer.js index 1b00955ba..acc3061c1 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2888,7 +2888,7 @@ function printNode(path, options, print) { case "error": default: // istanbul ignore next - return `Have not implemented kind ${node.kind} yet.`; + throw new Error(`Have not implemented kind '${node.kind}' yet.`); } } diff --git a/src/util.js b/src/util.js index e8c6b59b8..137dc530b 100644 --- a/src/util.js +++ b/src/util.js @@ -648,7 +648,7 @@ function getAncestorNode(path, typeOrTypes) { return counter === -1 ? null : path.getParentNode(counter); } -const magicMethods = [ +const magicMethods = new Set([ "__construct", "__destruct", "__call", @@ -664,18 +664,13 @@ const magicMethods = [ "__set_state", "__clone", "__debugInfo", -]; -const MagicMethodsMap = magicMethods.reduce((map, obj) => { - map[obj.toLowerCase()] = obj; - - return map; -}, {}); +]); function normalizeMagicMethodName(name) { const loweredName = name.toLowerCase(); - if (MagicMethodsMap[loweredName]) { - return MagicMethodsMap[loweredName]; + if (magicMethods.has(loweredName)) { + return loweredName; } return name; diff --git a/tests/case/__snapshots__/jsfmt.spec.js.snap b/tests/case/__snapshots__/jsfmt.spec.js.snap index 4a591ad01..4bf407c61 100644 --- a/tests/case/__snapshots__/jsfmt.spec.js.snap +++ b/tests/case/__snapshots__/jsfmt.spec.js.snap @@ -569,6 +569,10 @@ class Connection public function __DEBUGINFO() { } + + // Not magic methods, JS Object prototype properties + public function __pRoTo__() {} + public function cOnStRuCtOr() {} } =====================================output===================================== @@ -591,7 +595,7 @@ class Connection { } - public static function __callStatic($name, $arguments) + public static function __CALLSTATIC($name, $arguments) { } @@ -619,7 +623,7 @@ class Connection { } - public function __toString() + public function __TOSTRING() { } @@ -635,7 +639,15 @@ class Connection { } - public function __debugInfo() + public function __DEBUGINFO() + { + } + + // Not magic methods, JS Object prototype properties + public function __pRoTo__() + { + } + public function cOnStRuCtOr() { } } diff --git a/tests/case/magic-methods.php b/tests/case/magic-methods.php index 16b15a7a1..8761bc035 100644 --- a/tests/case/magic-methods.php +++ b/tests/case/magic-methods.php @@ -64,4 +64,8 @@ public function __CLONE() public function __DEBUGINFO() { } + + // Not magic methods, JS Object prototype properties + public function __pRoTo__() {} + public function cOnStRuCtOr() {} } From e1ef744b260c1c37bec6b580afe7ce1a60ac4972 Mon Sep 17 00:00:00 2001 From: fisker Date: Mon, 28 Aug 2023 11:18:11 +0800 Subject: [PATCH 2/2] Fix logic --- src/util.js | 11 +++++++---- tests/case/__snapshots__/jsfmt.spec.js.snap | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/util.js b/src/util.js index 137dc530b..8d65ff99a 100644 --- a/src/util.js +++ b/src/util.js @@ -648,7 +648,7 @@ function getAncestorNode(path, typeOrTypes) { return counter === -1 ? null : path.getParentNode(counter); } -const magicMethods = new Set([ +const magicMethods = [ "__construct", "__destruct", "__call", @@ -664,13 +664,16 @@ const magicMethods = new Set([ "__set_state", "__clone", "__debugInfo", -]); +]; +const magicMethodsMap = new Map( + magicMethods.map((name) => [name.toLowerCase(), name]) +); function normalizeMagicMethodName(name) { const loweredName = name.toLowerCase(); - if (magicMethods.has(loweredName)) { - return loweredName; + if (magicMethodsMap.has(loweredName)) { + return magicMethodsMap.get(loweredName); } return name; diff --git a/tests/case/__snapshots__/jsfmt.spec.js.snap b/tests/case/__snapshots__/jsfmt.spec.js.snap index 4bf407c61..aed718b35 100644 --- a/tests/case/__snapshots__/jsfmt.spec.js.snap +++ b/tests/case/__snapshots__/jsfmt.spec.js.snap @@ -595,7 +595,7 @@ class Connection { } - public static function __CALLSTATIC($name, $arguments) + public static function __callStatic($name, $arguments) { } @@ -623,7 +623,7 @@ class Connection { } - public function __TOSTRING() + public function __toString() { } @@ -639,7 +639,7 @@ class Connection { } - public function __DEBUGINFO() + public function __debugInfo() { }