Skip to content

Commit 063209f

Browse files
authored
Fix magic methods detection (#2228)
* Fix magic methods detection * Fix logic
1 parent 6d21c9b commit 063209f

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/printer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2885,7 +2885,7 @@ function printNode(path, options, print) {
28852885
case "error":
28862886
default:
28872887
// istanbul ignore next
2888-
return `Have not implemented kind ${node.kind} yet.`;
2888+
throw new Error(`Have not implemented kind '${node.kind}' yet.`);
28892889
}
28902890
}
28912891

src/util.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -666,17 +666,15 @@ const magicMethods = [
666666
"__clone",
667667
"__debugInfo",
668668
];
669-
const MagicMethodsMap = magicMethods.reduce((map, obj) => {
670-
map[obj.toLowerCase()] = obj;
671-
672-
return map;
673-
}, {});
669+
const magicMethodsMap = new Map(
670+
magicMethods.map((name) => [name.toLowerCase(), name])
671+
);
674672

675673
function normalizeMagicMethodName(name) {
676674
const loweredName = name.toLowerCase();
677675

678-
if (MagicMethodsMap[loweredName]) {
679-
return MagicMethodsMap[loweredName];
676+
if (magicMethodsMap.has(loweredName)) {
677+
return magicMethodsMap.get(loweredName);
680678
}
681679

682680
return name;

tests/case/__snapshots__/jsfmt.spec.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ class Connection
569569
public function __DEBUGINFO()
570570
{
571571
}
572+
573+
// Not magic methods, JS Object prototype properties
574+
public function __pRoTo__() {}
575+
public function cOnStRuCtOr() {}
572576
}
573577
574578
=====================================output=====================================
@@ -638,6 +642,14 @@ class Connection
638642
public function __debugInfo()
639643
{
640644
}
645+
646+
// Not magic methods, JS Object prototype properties
647+
public function __pRoTo__()
648+
{
649+
}
650+
public function cOnStRuCtOr()
651+
{
652+
}
641653
}
642654
643655
================================================================================

tests/case/magic-methods.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ public function __CLONE()
6464
public function __DEBUGINFO()
6565
{
6666
}
67+
68+
// Not magic methods, JS Object prototype properties
69+
public function __pRoTo__() {}
70+
public function cOnStRuCtOr() {}
6771
}

0 commit comments

Comments
 (0)