Missing semicolon in JS

  • ALM used: GitHub
  • CI system used: not sure
  • Scanner command used when applicable: Statements should end with semicolons
  • Languages of the repository: Mixed (but error will be in JS)
  • Error observed: Missing semicolon.
  • Steps to reproduce:

I have enabled the rule to always add ; at EOL in JS.
This works fine.

However, at this function it throws me a false alarm (or, I cannot see the missing semicolon, as for me, they are all there as it should be):

function read_url( input ) {
    if ( input.files && input.files[0] ) {
        var reader = new FileReader();
        reader.onload = function ( e ) {
            $( img_id ).attr( 'src', e.target.result );
        }
        reader.readAsDataURL( input.files[0] );
    }
}

The error is thrown at line:
reader.readAsDataURL( input.files[0] );

That line has a semicolon!

AFAIK end of functions (thus, after } no semicolon is needed?
So I am not sure what this is about (false alarm or me missing something)

Thanks for a second pair of eyes

Hello Beda,

Thank you for your feedback!

You are missing a semicolon after assigning the anonymous function to reader.onload. In other words, here is what your fixed code snippet should look like:

function read_url( input ) {
    if ( input.files && input.files[0] ) {
        var reader = new FileReader();
        reader.onload = function ( e ) {
            $( img_id ).attr( 'src', e.target.result );
        }; // added a semicolon: this is the end of a statement
        reader.readAsDataURL( input.files[0] );
    }
}

Having said that, and thanks to your feedback, we can notice that the rule is currently flagging an issue on the wrong line. In that regard, I created this ticket to address that soon.

Hope this helps,
Yassin

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.