2

I am trying to get the following code to become jslint-compliant, but am stuck on the following two errors:

Expected to see a statement and instead saw a block.

and

Unexpected 'this'.

What changes should I make to my code to make JSLint happy?

var pvAccess =  {};

pvAccess.Func = function () {
    "use strict";

    function AccessPV(name, rValue, wValue) {

        var url = '/goform/ReadWrite',    
            data = 'redirect=/response.asp&variable=' + escape(name),
            xmlHttp = null,
            wValue = null;

        if (rValue !== null && rValue !== "") {    
            data += '&value=' + escape(rValue);
            data += "&write=1";
        } else {
            data += '&value=none';
            data += "&read = 1";
        }

        try {
            // Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            try {
                // MS Internet Explorer (ab v6)
                xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                try {
                    // MS Internet Explorer (ab v5)
                    xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e3) {
                    xmlHttp  = null;
                }
            }
        }

        if (xmlHttp) {    
            xmlHttp.open('POST', url, 1);
            xmlHttp.onreadystatechange = function () {

                if (xmlHttp.readyState === 4) {
                    if (wValue !== null) {
                        wValue[3] = xmlHttp.responseText;
                        wValue[3] = wValue[3].replace("<!-- B&R ASP Webserver -->", "");
                        // value attribute of node    
                        wValue.value = wValue[3];
                        return wValue;
                    }
                }
            };

            xmlHttp.send(data);
        }
    }

// public
    {    
        this.WritePV = function (name, value) { AccessPV(name, value); }
        this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); }
    }
}

pvAccess = new pvAccess.Func();
DavidRR
  • 18,291
  • 25
  • 109
  • 191
Paolo Pinkel
  • 23
  • 1
  • 3
  • 3
    Why don't you show us which lines it's giving us the errors on. – Jørgen R Dec 11 '14 at 12:28
  • { this.WritePV = function (name, value) { AccessPV(name, value); } this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); } } – Paolo Pinkel Dec 11 '14 at 12:30
  • Regarding "Unexpected `this`": JSLint now offers an option to suppress this complaint. Please see [my answer](http://stackoverflow.com/a/30603515/1497596) to this similar question. – DavidRR Jun 02 '15 at 18:24

2 Answers2

0

The 2 things I can see which might cause this are:

  1. You have unnecessary {}s around your public methods. That is probably causing the expected statement error.
  2. Your public methods are assignment statements not just functions, so you should end the line with a semicolon, after the function definition. Without this, lint is reading it as 1 line of code (this.WritePV = function { ... }this.ReadPV), hence the "unexpected this".

So you need to change your public methods to look like this:

// public
    this.WritePV = function (name, value) { AccessPV(name, value); };

    this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); };
Rhumborl
  • 16,349
  • 4
  • 39
  • 45
  • thanks a lot, that's working :) Only thing he complains now, is: **Unexpected 'pvAccess'.** for column var pvAccess = new pvAccess.Func(); – Paolo Pinkel Dec 11 '14 at 13:08
  • same thing - you need a semicolon at the end of the massive `pvAccess.Func` function definition – Rhumborl Dec 11 '14 at 13:29
0
{

    this.WritePV = function (name, value) { AccessPV(name, value); }

    this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); }
}

^^^

That's causing the error. Remove those braces and it fixes itself. It also wants you to use semicolons.

    this.WritePV = function (name, value) { AccessPV(name, value); };

    this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); };
Scimonster
  • 32,893
  • 9
  • 77
  • 89