Page MenuHomePhabricator

Add a method to get the indentation for an XHPASTNode
ClosedPublic

Authored by joshuaspence on Apr 5 2015, 11:19 AM.
Tags
None
Referenced Files
F14048651: D12295.id29523.diff
Thu, Nov 14, 8:47 AM
F14047790: D12295.id29563.diff
Thu, Nov 14, 5:17 AM
F14047728: D12295.id29501.diff
Thu, Nov 14, 5:00 AM
F14047577: D12295.id29501.diff
Thu, Nov 14, 4:31 AM
F14047574: D12295.id29563.diff
Thu, Nov 14, 4:30 AM
F14047531: D12295.diff
Thu, Nov 14, 4:14 AM
F14047524: D12295.id29498.diff
Thu, Nov 14, 4:10 AM
F14047354: D12295.id.diff
Thu, Nov 14, 3:37 AM
Subscribers

Details

Summary

Add a getIndentation() method to XHPASTNode. This method returns the whitespace that was used to indent the node. This is useful for linter rules which enforce various alignment-related nitpicks, such as D11504: Add a linter rule for PHP indentation and D12296: Improve array comma rule.

Test Plan

See D12296.

Diff Detail

Repository
rPHU libphutil
Branch
master
Lint
Lint Warnings
SeverityLocationCodeMessage
Warningsrc/parser/aast/api/AASTNode.php:243TXT3Line Too Long
Unit
Tests Passed
Build Status
Buildable 5141
Build 5159: [Placeholder Plan] Wait for 30 Seconds

Event Timeline

joshuaspence retitled this revision from to Add a `getIndentation` method.
joshuaspence updated this object.
joshuaspence edited the test plan for this revision. (Show Details)
joshuaspence added a reviewer: epriestley.

This seems like it would get the wrong result for:

$a;
        <-- Some trailing whitespace here on this line.
  $b;

Specifically, I would expect the str_replace() at the end to start with an input like <space><space>\n<space><space>, then remove the \n and incorrectly return $b as indented 4 spaces.

Am I misreading?

Right, I guess I am not correctly handling the case in which the whitespace contains more than one newline character.

I'm thinking that I should write some unit tests here

Fix line length. This looks sorta ugly now.

OK. This seems to work better now. I tested this using D12296 on the following file:

<?php

function foo() {
  $x = array(
    1,
    2,
    3);
       
  $y = array(
    1,
    2,
    3);
  
  $z = 
      array(
    1,
    2,
    3);
}

The output (after applying autofixes) was as follows:

<?php

function foo() {
  $x = array(
    1,
    2,
    3,
  );

  $y = array(
    1,
    2,
    3,
  );

  $z =
      array(
    1,
    2,
    3,
      );
}
joshuaspence retitled this revision from Add a `getIndentation` method to Add a method to get the indentation for an XHPASTNode.Apr 5 2015, 1:04 PM
joshuaspence updated this object.
joshuaspence edited the test plan for this revision. (Show Details)

I sort of wonder if this should return something else that makes it easier to patch, but let me look at D12296...

epriestley edited edge metadata.

Ah, irrelevant for D12296.

My thinking here is that if we eventually write a lint rule which adjusts indentation, it couldn't original/replacement a single token because this method may split tokens apart. But I'm not really sure what would be better to return without being a total mess, and such a rule might reasonably use a more top-down approach to analyzing the entire file.

This revision is now accepted and ready to land.Apr 6 2015, 1:26 PM

Ah, irrelevant for D12296.

My thinking here is that if we eventually write a lint rule which adjusts indentation, it couldn't original/replacement a single token because this method may split tokens apart. But I'm not really sure what would be better to return without being a total mess, and such a rule might reasonably use a more top-down approach to analyzing the entire file.

I'm not sure that I follow... this method is only dealing with a single token (although it does destroy the original value of the token, is that your concern?).

This revision was automatically updated to reflect the committed changes.