plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

fix: attribute PSR compatibility

Open apollisa opened this issue 7 months ago • 6 comments

This partially fixes #2360, in the sense that formatting now matches PSR guidelines. It will still split across multiple lines if need be though.

apollisa avatar May 02 '25 09:05 apollisa

I’m confused I ran the exact same command on my laptop and everything passes; did I miss something?

apollisa avatar May 02 '25 19:05 apollisa

Hello, @apollisa The test fails because the AST changed, run AST_COMPARE=1 yarn test you'll see the failure on local.

fisker avatar Jun 07 '25 10:06 fisker

Oh indeed with AST_COMPARE the tests do fail in local :+1:

Should Prettier keep the AST unchanged? Because here if we want to stick with the PSR rules, we must split attributes that span multiple lines:

#[
    IA("interface"),
    \\Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong\\Namespace\\WithStuff\\IB
]

becomes

#[IA("interface")]
#[\\Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong\\Namespace\\WithStuff\\IB]

apollisa avatar Jun 08 '25 09:06 apollisa

I'm not sure if this will be accepted, but if it's expected to produce different AST, you'll need change the old shape to new shape in https://github.com/prettier/plugin-php/blob/main/src/clean.mjs

fisker avatar Jun 08 '25 09:06 fisker

Well I’m sorry I can’t seem to make it to work :thinking: It would seem that the clean function only gets a single attribute group at a time, so I don’t see how I could split it into multiple ones

apollisa avatar Jun 08 '25 10:06 apollisa

Looking at how printAttrs is called, I think you can

if (node.kind === 'arrowfunc' || node.kind === 'parameter' /* || other kinds */) {
  // Update newObj.attrGroups
}

clean should also have a third argument to access parent, I believe.

fisker avatar Jun 08 '25 10:06 fisker