coding-standard icon indicating copy to clipboard operation
coding-standard copied to clipboard

AlphabeticallySortedUses disagrees with VSCode on alphabetical order of '\' and '_' characters

Open joachim-n opened this issue 1 year ago • 4 comments

The coding standard wants the lines to be like this:

use Drupal\action_link\Ajax\ActionLinkMessageCommand;
use Drupal\action_link\Entity\ActionLinkInterface;
use Drupal\action_link\Plugin\ActionLinkStyle\Ajax;
use Drupal\action_link_formatter_links\DisplayBuildAlter;

But if I select them in VSCode and do 'Sort lines ascending' I get this:

use Drupal\action_link_formatter_links\DisplayBuildAlter;
use Drupal\action_link\Ajax\ActionLinkMessageCommand;
use Drupal\action_link\Entity\ActionLinkInterface;
use Drupal\action_link\Plugin\ActionLinkStyle\Ajax;

joachim-n avatar Feb 26 '24 14:02 joachim-n

The sorting is compatible with PHPStorm. I was not able to find specification of the order between \ and _ in PSR 12. I'm ok to merge PR that will make this configurable.

kukulich avatar Mar 09 '24 09:03 kukulich

I'd argue that the coding standard and PHPStorm are correct here and that VSCode is wrong. VSCode is sorting the strings as-is whereas the coding standards and PHPStorm are more aware that we're dealing with "nested sections". If we alphabetically sort words then "action_link" would come before "action_link_formatter_links".

We can see that PHP treats these as nested "paths" because the following is uncommon but valid PHP:


use Drupal\action_link\{
    Ajax\ActionLinkMessageCommand,
    Entity\ActionLinkInterface,
    Plugin\ActionLinkStyle\Ajax,
};
use Drupal\action_link_formatter_links\{
    DisplayBuildAlter,
};

We should probably file a PHP issue with VSCode instead.

Kingdutch avatar Apr 17 '24 09:04 Kingdutch

Looks like there is an existing VSCode issue - https://github.com/microsoft/vscode/issues/48123

mstrelan avatar Sep 11 '24 03:09 mstrelan

JFYI, because of different preferences in the past, in another CS, we have added configurability here: https://github.com/mayflower/mo4-coding-standard?tab=readme-ov-file#mo4formattingalphabeticalusestatements - although persoanlly especially using a locale-dependent function is a bit too much, how high is the probability that a PR, introducing the same options, gets merged here?

mmoll avatar Sep 15 '24 16:09 mmoll