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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting proper method name from propertylookup chain (with a solution) #325

Open
swashata opened this issue Jan 30, 2024 · 0 comments
Open

Comments

@swashata
Copy link

swashata commented Jan 30, 2024

Hi! 馃憢

Firstly, thanks for your work on this project! 馃檪

Today I used patch-package to patch wp-pot@1.10.2 for the project I'm working on.

I realized that the package goes only one level deep when trying to figure out a prototype call.

So if I have something like

gettextFunctions: [
    {name: '$this->_fs->get_text_inline'},
    {name: '$this->_fs->get_text_x_inline', context: 2},
]

It fails to extract. However the following works:

gettextFunctions: [
    {name: '$fs->get_text_inline'},
    {name: '$fs->get_text_x_inline', context: 2},
]

Here is the diff that solved my problem:

diff --git a/node_modules/wp-pot/src/parsers/php-parser.js b/node_modules/wp-pot/src/parsers/php-parser.js
index c8f411d..319a5da 100644
--- a/node_modules/wp-pot/src/parsers/php-parser.js
+++ b/node_modules/wp-pot/src/parsers/php-parser.js
@@ -220,6 +220,32 @@ class PHPParser {
     return comment;
   }
 
+  /**
+   * Get method name from property lookup, going as deep as necessary.
+   * Example: $this->plugin->text_domain->translate
+   *
+   * @param {object} what
+   * @return {string}
+   * @private
+   */
+  _getMethodNameFromPropertyLookup(what) {
+    let methodName = [];
+
+    while(what) {
+      if (what.kind === 'propertylookup') {
+        methodName.push(what.offset.name);
+        what = what.what;
+      } else if (what.kind === 'variable') {
+        methodName.push(what.name);
+        what = null;
+      } else {
+        what = null;
+      }
+    }
+
+    return `\$${methodName.reverse().join('->')}`;
+  }
+
   /**
    * Check if ast is a valid function call
    *
@@ -231,8 +257,8 @@ class PHPParser {
     if (ast.kind === 'call') {
       let methodName = ast.what.name;
 
-      if (ast.what.kind === 'propertylookup' && ast.what.what.kind === 'variable') {
-        methodName = ['$', ast.what.what.name, '->', ast.what.offset.name].join('');
+      if (ast.what.kind === 'propertylookup') {
+        methodName = this._getMethodNameFromPropertyLookup(ast.what);
       } else if (ast.what.kind === 'name' && ast.what.resolution === 'fqn') {
         methodName = ast.what.name.replace(/^\\/, '');
       }

This will keep on looking down the prototype until it has found the variable and then create the method name.

If you're interested in this kind of solution, please let me know. I will create a PR, with tests. Thank you.

This issue body was partially generated by patch-package.

@swashata swashata changed the title Getting proper method name from prototypelookup chain (with a solution) Getting proper method name from propertylookup chain (with a solution) Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant