0

Please consider the following code.

The following is according to JSLint or JSHint perfectly valid Javascript code:

function fun() {
    "use strict";
    var myvar = "";
    myvar.foo();
}

The following not:

function fun() {
    "use strict";
    var myvar = "";
    foo();
}

Why is JSLint (or JSHint) not detecting that myvar.foo() wasn't declared before, whereas it detects that foo() wasn't declared? Is there an option which I'm missing?

I would like those types of errors to be detected upon validation. Is there another javascript tool which can detect the use of undeclared properties or methods inside an object as explained in the examples above?

Prasanth S
  • 3,725
  • 9
  • 43
  • 75
finrod
  • 521
  • 8
  • 21
  • Well, maybe it *is* declared as `String.prototype.foo`? JSLint doesn't know of this, and it doesn't bring a whole type system with it. You might want to have a look at TypeScript or similar. – Bergi Apr 08 '14 at 11:04

2 Answers2

1

All linters simply ready your code, they don't run it. In your case, it's impossible to tell whether myvar.foo was defined or not. As for foo() if you defined it within the same file, JSHint will detect that. Otherwise, it won't.

Anton Kovalyov
  • 2,808
  • 1
  • 22
  • 11
0

Linters just parses/scans your code for "problems". If you really want to test your code you need to use unit testing.

like Qunit: https://qunitjs.com/

JSLint takes a JavaScript source and scans it. If it finds a problem, it returns a message describing the problem and an approximate location within the source. The problem is not necessarily a syntax error, although it often is. JSLint looks at some style conventions as well as structural problems. It does not prove that your program is correct. It just provides another set of eyes to help spot problems.
http://www.jslint.com/lint.html

Rick Lancee
  • 1,629
  • 1
  • 15
  • 20