Domanda

I have the following code fragment that appears to be correct, but jslint doesn't like it.

var VALID_TYPE = {
    "stringType" : "string",
    "arrayType" : "array",
    "objectType" : "object"
},
    DEFAULT_FIRST = 1, DEFAULT_LAST = 1, PRIMITIVE_TYPE = {
    "stringType" : "string",
    "arrayType" : "array",
    "objectType" : "object",
    "undefinedType" : "undefined",
    "booleanType" : "boolean",
    "numberType" : "number"
};
VALID_TYPE.toString = function () {
    var types = [], currentType;
    for (currentType in this) {
        if (typeof this[currentType] === PRIMITIVE_TYPE.stringType) {
            types.push(this[currentType]);
        }
    }
    var outputString = types.join(', ');
    return outputString;
};

The erroneous line is this, at the ".": if (typeof this[currentType] === PRIMITIVE_TYPE.stringType) {

The exact text of the error is: Expected a string and instead saw '.'.

toString() performs as expected. I can't see what I should change to avoid the error, except for placing the right side of the expression into another variable. The error is not yet described at jslinterrors.com.

È stato utile?

Soluzione

As @SLaks stated in the comments, JSLint will warn when it encounters a comparison operator in which one of the operands is a typeof expression and the other operand is not a string literal.

Here's a cut down version of the code that performs this check:

function relation(s, eqeq) {
    var x = infix(s, 100, function (left, that) {
        // ...
        if (are_similar(left, right) ||
                ((left.id === '(string)' || left.id === '(number)') &&
                (right.id === '(string)' || right.id === '(number)'))) {
            that.warn('weird_relation');
        } else if (left.id === 'typeof') {
            if (right.id !== '(string)') {
                right.warn("expected_string_a", artifact(right));
            } else if (right.string === 'undefined' || right.string === 'null') {
                left.warn("unexpected_typeof_a", right.string);
            }
        } else if (right.id === 'typeof') {
            if (left.id !== '(string)') {
                left.warn("expected_string_a", artifact(left));
            } else if (left.string === 'undefined' || left.string === 'null') {
                right.warn("unexpected_typeof_a", left.string);
            }
        }
        // ...
    });
    // ...
}

The only other time that specific warning is given is when JSLint encounters an unquoted JSON property:

{
    a: 1
}

I'll get this up on http://jslinterrors.com as soon as I get a chance.

Altri suggerimenti

toString() performs as expected.

The code is perfectly valid, so yes it would do.

Remember that jsLint isn't looking for errors; it's looking for things it thinks are bad practice.

But those things aren't always definitively wrong in every case; often there is a legitimate use case for it, and if you've got one of those cases, then you'll still get the error, but just have to ignore it.

Lint errors should be considered as a guide rather than something to be strictly adhered to and causing build failures.

You may also want to consider using jsHint rather than jsLint. jsHint is based on jsLint, but tends to be a bit more pragmatic about what it complains about.

Hope that helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top