A few examples don't pass JSHint #46

Open
pdehaan opened this Issue Apr 1, 2013 · 1 comment

Comments

Projects
None yet
1 participant

pdehaan commented Apr 1, 2013

I parsed the Markdown files, converted them to markdown, extracted the <pre><code>...</code></pre> tags, and ran the inner code through JSHint (using Node.js, but I excluded all the warnings about missing semicolons)...

Not sure how many are valid, versus expected warnings for null type checking.

https://raw.github.com/raganwald/homoiconic/master/2012/12/combinators_1.md

var inventory = {
  apples: 0,
  oranges 144,
  eggs: 36
};

get('oranges')(inventory)
  //=> 144

Errors:

  1. line:3 char:11 -- Expected ':' and instead saw '144'.
  2. line:3 char:14 -- Expected an identifier and instead saw ','.
  3. line:4 char:3 -- Expected '}' to match '{' from line 1 and instead saw 'eggs'.
  4. line:4 char:7 -- Expected '(end)' and instead saw ':'.

function isSomething (value) {
  return value != null
}

function checksForSomething (value) {
  if (isSomething(value)) {
    // function's true logic
  }
}

Errors:

  1. line:2 char:16 -- Use '!==' to compare with 'null'.

function maybe (fn) {
  return function () {
    var i;

    if (arguments.length === 0) {
      return
    }
    else {
      for (i = 0; i < arguments.length; ++i) {
        if (arguments[i] == null) return arguments[i]
      }
      return fn.apply(this, arguments)
    }
  }
}

Errors:

  1. line:10 char:26 -- Use '===' to compare with 'null'.

function checksForSomething = maybe(function (value) {
  // function's true logic
});

Errors:

  1. line:1 char:29 -- Expected '(' and instead saw '='.
  2. line:1 char:36 -- Expected ')' to match '=' from line 1 and instead saw '('.
  3. line:1 char:37 -- Expected '{' and instead saw 'function'.
  4. line:1 char:46 -- Missing name in function declaration.
  5. line:3 char:2 -- Expected an identifier and instead saw ')'.
  6. line:3 char:2 -- Expected an assignment or function call and instead saw an expression.

https://raw.github.com/raganwald/homoiconic/master/2012/12/combinators_2md

function (boundmethod) { 
  return boundmethod() 
}

Errors:

  1. line:1 char:10 -- Missing name in function declaration.

function Cake () {}

extend(Cake.prototype, {
  setFlavour: function (flavour) { 
    return this.flavour = flavour 
  },
  setLayers: function (layers) { 
    return this.layers = layers 
  },
  bake: function () {
    // do some baking
  }
});

var cake = new Cake();
cake.setFlavour('chocolate');
cake.setLayers(3);
cake.bake();

Errors:

  1. line:5 char:26 -- Did you mean to return a conditional instead of an assignment?
  2. line:8 char:25 -- Did you mean to return a conditional instead of an assignment?

pdehaan commented Apr 1, 2013

... and my sketchy Node.js code is:

var jshint = require("jshint").JSHINT,
    marked = require("marked"),
    request = require("request");

(function () {
    "use strict";

    var MD_URI = "https://raw.github.com/raganwald/homoiconic/master/2012/12/combinators_1.md";

    request(MD_URI, function (err, res, data) {
        var output;
        var scripts;
        if (!err && (res.statusCode === 200)) {
            output = marked(data);
            scripts = output.match(/<pre><code>([\s\S]+?)<\/code><\/pre>/igm);
            scripts.forEach(validator);
        }
    });

    function cleanCode(code) {
        return code.replace(/(<pre><code>|<\/code><\/pre>)/g, "").replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&#39;/g, "\'");
    }

    function validator(code) {
        var output = [];
        code = cleanCode(code.trim());
        if (!jshint(code, {"asi":true, "lastsemic":true})) {
            var code2 = code.split("\n").join("\n    ");

            output.push("--\n    " + code2 + "\n\nErrors:");
            jshint.errors.forEach(function (err, idx) {
                output.push((idx+1) + ". line:" + err.line + " char:" + err.character + " -- " + err.reason);
            });
            output.push("\n\n");
            console.log(output.join("\n"));
        } else {
            return;
        }
    }
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment