]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Munge XHP class names when generating phutil dependency maps
authorJames Ide <ide+github@jameside.com>
Sat, 23 Jul 2011 23:48:21 +0000 (16:48 -0700)
committerJames Ide <ide+github@jameside.com>
Sun, 24 Jul 2011 00:38:26 +0000 (17:38 -0700)
Summary:
When phutil_analyzer builds the dependency graph I convert all class names w.r.t
XHP's internal naming scheme. It actually wouldn't be a terrible idea to do this
munging when phutil loads the symbols. I guess it doesn't really matter at the
moment since only Arcanist generates phutil maps and only libphutil reads them,
but it'd be nice to do this munging in as few places as possible in the future.

The XHP grammar does not allow elements to be interfaces
(https://github.com/facebook/xhp/blob/dafff2cc1844d1138c3388509c9560b87167a3bb/xhp/parser.y#L1688)
so I've only applied this to classes.

Test Plan:
Created a sample project:

  ./__phutil_library_init__.php
    hopping/__init__.php
           /hophophop.php

where that last file contains

  <?php
  class :bunny:hop-hop-hop extends :x:element {
    protected function render() {
      return <p>bunny goes hop hop hop</p>;
    }
  }

Ran phutil_mapper.php and generated __phutil_library_map__.php:

  <?php

  /**
   * This file is automatically generated. Use 'phutil_mapper.php' to rebuild i\
t.
   * @generated
   */

  phutil_register_library_map(array(
    'class' =>
    array(
      'xhp_bunny__hop_hop_hop' => 'hopping',
    ),
    'function' =>
    array(
    ),
    'requires_class' =>
    array(
      'xhp_bunny__hop_hop_hop' => 'xhp_x__element',
    ),
    'requires_interface' =>
    array(
    ),
  ));

Reviewed By: epriestley
Reviewers: epriestley
Commenters: aran
CC: aran, ide, epriestley
Differential Revision: 715

src/parser/phutilmodule/PhutilModuleRequirements.php

index f6bbcbb7b8a99acafcc68b0379491d7890a20059..779b01791bb2f737dcfcdcf7cd0c27f7d4fd9a1b 100644 (file)
@@ -65,6 +65,7 @@ class PhutilModuleRequirements {
   }
 
   public function addClassDeclaration(XHPASTNode $where, $name) {
+    $name = self::mungeXHPClassName($name);
     return $this->addDeclaration('class', $where, $name);
   }
 
@@ -98,8 +99,10 @@ class PhutilModuleRequirements {
   }
 
   public function addClassDependency($child, XHPASTNode $where, $name) {
+    $name = self::mungeXHPClassName($name);
     if ($child !== null) {
       if (empty($this->builtins['class'][$name])) {
+        $child = self::mungeXHPClassName($child);
         $this->chain['class'][$child] = $name;
       }
     }
@@ -173,4 +176,13 @@ class PhutilModuleRequirements {
       'messages' => $this->messages,
     );
   }
+
+  private static function mungeXHPClassName($name) {
+    if (strlen($name) && $name[0] == ':') {
+      // XHP's semantic actions munge element names without a preceding colon.
+      $name = substr($name, 1);
+      return 'xhp_'.str_replace(array(':', '-'), array('__', '_'), $name);
+    }
+    return $name;
+  }
 }