Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix magic methods detection #2228

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,17 +665,15 @@ const magicMethods = [
"__clone",
"__debugInfo",
];
const MagicMethodsMap = magicMethods.reduce((map, obj) => {
map[obj.toLowerCase()] = obj;

return map;
}, {});
const magicMethodsMap = new Map(
magicMethods.map((name) => [name.toLowerCase(), name])
);

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

if (MagicMethodsMap[loweredName]) {
return MagicMethodsMap[loweredName];
if (magicMethodsMap.has(loweredName)) {
return magicMethodsMap.get(loweredName);
}

return name;
Expand Down
12 changes: 12 additions & 0 deletions tests/case/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ class Connection
public function __DEBUGINFO()
{
}

// Not magic methods, JS Object prototype properties
public function __pRoTo__() {}
public function cOnStRuCtOr() {}
}

=====================================output=====================================
Expand Down Expand Up @@ -638,6 +642,14 @@ class Connection
public function __debugInfo()
{
}

// Not magic methods, JS Object prototype properties
public function __pRoTo__()
{
}
public function cOnStRuCtOr()
{
}
}

================================================================================
Expand Down
4 changes: 4 additions & 0 deletions tests/case/magic-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public function __CLONE()
public function __DEBUGINFO()
{
}

// Not magic methods, JS Object prototype properties
public function __pRoTo__() {}
public function cOnStRuCtOr() {}
}