valeryan / vscode-phpsab

Php Sniffer and Beautifier for VS Code
https://marketplace.visualstudio.com/items?itemName=ValeryanM.vscode-phpsab
MIT License
58 stars 12 forks source link

Stop adding newline to end of file #138

Open Dekadinious opened 2 months ago

Dekadinious commented 2 months ago

I came from the wongjn extension recently, and I can't seem to get this extension to stop adding a new line at the end of my files.

I have turned "Files: Insert Final Newline" in VSCode off. I also have this in my ruleset:

<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="FL" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">

    <rule ref="WordPress">
        <exclude name="WordPress.PHP.YodaConditions.NotYoda"/>
        <exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
        <exclude name="WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase"/>
        <exclude name="Universal.Operators.DisallowShortTernary.Found"/>
        <exclude name="PSR2.Files.EndFileNewline.NoneFound"/>
        <exclude name="Generic.Files.EndFileNewline"/>
        <exclude name="WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder"/>
    </rule>

</ruleset>

When running phpcbf manually, no newlines are added and no fixes are available on the file. When I save the file and the formatter runs, a newline is added. If I disable phpcbf from this extension, no newline is added. If I run the exact phpcbf command this extension runs, no fixes are available and no newlines are added.

The only time a newline is added is if I save the file or run formatting on it, or run F1 -> PHPCBF: Fix this file.

How can I make it stop?

jonathanbossenger commented 2 months ago

Can I ask what your output looks like when you run phpcs in the terminal against your file?

phpcs plugin-file.php
Dekadinious commented 2 months ago

Yes, it just states: "No fixable errors were found" if I ensure to have the file saved without a newline at the end and no other errors. If I add other errors, it will list and fix them, but not add the newline. So I am a bit stumped as to where it comes from when formatting through the extension and how to log that. 😅

jonathanbossenger commented 2 months ago

Yeah, it is weird, because I don't see anything adding a newline in the extension code either.

I've never noticed this because I don't exclude the PSR2.Files.EndFileNewline.NoneFound rule, but I am able to replicate the issue using your phpcs.xml file, so I will keep digging.

jonathanbossenger commented 2 months ago

@Dekadinious I found the problem, and opened a PR to fix it in #142

Are you able to test the PR to confirm it resolves the issue?

Dekadinious commented 2 months ago

@jonathanbossenger That's great, and I would love that. But how do I test a specific PR? 😅

jonathanbossenger commented 2 months ago

@Dekadinious, essentially you use the same steps to run the development version listed here

  1. Clone this repo git clone https://github.com/valeryan/vscode-phpsab.git
  2. Switch to the specific branch with the changes cd vscode-phpsab git checkout 138-fix-newline-error
  3. Open the extension in VS Code and run the Debug/Development version as detailed in the steps above

This will open a new VS code instance running the code from that branch, which you can use to test.

Dekadinious commented 2 months ago

@jonathanbossenger Thank you for the guidance!

I can confirm that it works, and I also tested https://github.com/valeryan/vscode-phpsab/pull/139 and can confirm that both spaces and special characters no longer trigger the error :)