BenoitZugmeyer / eslint-plugin-html

An ESLint plugin to extract and lint scripts from HTML files.
ISC License
430 stars 51 forks source link

Recalculate code location after dedent #69

Closed hsxfjames closed 7 years ago

hsxfjames commented 7 years ago

This is a plunker to reproduce the problem: http://plnkr.co/edit/SKvcqNj8S3KiG4x7GRsL .

I installed eslint-plugin-html and eslint-friendly-formatter to lint my code. But I found if I let eslint-plugin-html to dedent by default, I'll get a wrong message. e.g. for this code:

<script>
    function a(){
        function b () {
        }
    }
</script>

The code violates space-before-blocks and space-before-function-paren rules, but error messages from formatter shows:

  ✘  http://eslint.org/docs/rules/space-before-blocks          Missing space before opening brace            
  index.html:2:17
  function a(){
                   ^

  ✘  http://eslint.org/docs/rules/space-before-function-paren  Unexpected space before function parentheses  
  index.html:3:19
      function b () {
                     ^

And I write a simple formatter.js, and just return some fields from reports structure, get

{"column":17,"sourceLength":13,"source":"function a(){"}
{"column":19,"sourceLength":19,"source":"    function b () {"}

You can see the source code is dedented, but the code location isn't recalculated, so we get a wrong column field.

I've tried modifying line 107 in TransformableString.js to

    const originalIndex = this.originalIndex(index) - 4

then we'll get correct column field. Though it works, I haven't completely figure out how plugin works. Hope you can find a better way to subtract the dedent length.

hsxfjames commented 7 years ago

I think return values of extract() should include dedent length, which can be passed to code.originalLocation(), then subtract it some where. Subtracting it at return value of indexToLocation() looks good.

BenoitZugmeyer commented 7 years ago

I will look into it, but dedent informations should already be taken into account. The issue may be linked to the usage of a "custom" formatter. Can you confirm that without specifying the --format option, columns are reported correctly?

BenoitZugmeyer commented 7 years ago

I published a fix, let me know if it works for you.

hsxfjames commented 7 years ago

Hi, @BenoitZugmeyer Without specifying --format option, ESLint will use default stylish formatter and show the same column. The fix works fine, since the original location is matched with original line. Thx.