mfussenegger / nvim-lint

An asynchronous linter plugin for Neovim complementary to the built-in Language Server Protocol support.
GNU General Public License v3.0
1.88k stars 198 forks source link

phpcs doesn't seem to follow rulesets with exclude-pattern #436

Open kourylape opened 10 months ago

kourylape commented 10 months ago

I'm new to nvim-lint and php is not my primary language, but I noticed an inconsistency between running phpcs locally versus inside neovim.

.phpcs.xml

<?xml version="1.0"?>
<ruleset name="MyApp">
  <file>blog/wp-content/themes/mytheme-child/</file>
  <file>app/</file>

  <rule ref="WordPress">
    <exclude-pattern>app/</exclude-pattern>
  </rule>

  <rule ref="PSR12">
    <exclude-pattern>blog/</exclude-pattern>
  </rule>

  <arg name="colors"/>
  <arg value="sp"/>
  <arg name="parallel" value="8"/>
  <arg name="report" value="full"/>

  <ini name="memory_limit" value="128M"/>
</ruleset>

plugins.lua

  {
    'mfussenegger/nvim-lint',
    config = function()
      require('plugin.linter')
    end
  },

plugins/linter.lua

local lint = require("lint")

-- PHP
local phpcs = lint.linters.phpcs
phpcs.cmd = "vendor/bin/phpcs"

-- Setup Linters
lint.linters_by_ft = {
    php = { "phpcs" },
}

Scenario 1: Inside neovim

kourylape commented 10 months ago

I believe the issue is with stdin and lua/lint/linters/phpcs.lua. I can create a PR, but not sure if I'm heading down the right path.

I was able to match the functionality between running phpcs locally and using the linter in nvim.

I made the following changes to phpcs.lua.

  parser = function(output, bufnr)
    -- other unmodified code

    local filename = vim.fn.fnamemodify(vim.fn.bufname(bufnr), ":p")
    local files = decoded["files"]
    local messages = {}

    if files["STDIN"] == nil then
      if files[filename] == nil then
        return {}
      end
      messages = files[filename]["messages"]
    else
      messages = files["STDIN"]["messages"]
    end

   -- other unmodified code
  end,

And customized the linter in my config to be:

  local lint = require("lint")

-- PHP
local phpcs = lint.linters.phpcs
phpcs.cmd = "vendor/bin/phpcs"
phpcs.stdin = false
phpcs.args = {
    "--report=json",
    "-q",
}

This does work, but is this something worth submitting?