From cdc5d5ded42127d64203e95892796b96a84454b1 Mon Sep 17 00:00:00 2001 From: Satoshi Watanabe Date: Thu, 16 Jun 2016 16:58:21 +0900 Subject: [PATCH] fix problem of not loading jshintrc file when not supplied options. - cherry-pick of #187 by @sassy - solved #182 - added test-case for #182 --- lib/reporters/jshint/index.js | 4 ++-- test/fixtures/.jshintrc | 2 +- test/fixtures/issue_182.js | 8 ++++++++ test/issues/issue_16_test.js | 5 ++--- test/issues/issue_182_test.js | 36 +++++++++++++++++++++++++++++++++++ test/issues/issue_217_test.js | 3 +-- test/plato_test.js | 2 +- 7 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/issue_182.js create mode 100644 test/issues/issue_182_test.js diff --git a/lib/reporters/jshint/index.js b/lib/reporters/jshint/index.js index 66a2eeac..e401ef7a 100644 --- a/lib/reporters/jshint/index.js +++ b/lib/reporters/jshint/index.js @@ -6,10 +6,10 @@ var jsHintCli = require("jshint/src/cli.js"); // Provides a regexp to test for ES6 / ES Modules. If the pass tests then esversion is set to 6 if not already specified. var esmRegex = /(^\s*|[}\);\n]\s*)(import\s*(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s*from\s*['"]|\{)|export\s+\*\s+from\s+["']|export\s* (\{|default|function|class|var|const|let|async\s+function))/; -exports.process = function (source, options/*, reportInfo */) { +exports.process = function (source, options, reportInfo) { if (options == null || Object.getOwnPropertyNames(options).length === 0) { options = { options : {}, globals : {}}; - var jsHintOptions = jsHintCli.getConfig(source); + var jsHintOptions = jsHintCli.getConfig(reportInfo.file || ""); delete jsHintOptions.dirname; if (jsHintOptions != null && Object.getOwnPropertyNames(jsHintOptions).length > 0) { diff --git a/test/fixtures/.jshintrc b/test/fixtures/.jshintrc index a76ff7b6..1ab84ce7 100644 --- a/test/fixtures/.jshintrc +++ b/test/fixtures/.jshintrc @@ -15,7 +15,7 @@ "boss": true, "eqnull": true, "node": true, - "es5": true + "maxparams": 3 } /* foo */ diff --git a/test/fixtures/issue_182.js b/test/fixtures/issue_182.js new file mode 100644 index 00000000..fac363e1 --- /dev/null +++ b/test/fixtures/issue_182.js @@ -0,0 +1,8 @@ +/** + * exceeded maxparams(3) + */ +function foo(a, b, c, d) { + return a + b + c + d; +} + +foo(); diff --git a/test/issues/issue_16_test.js b/test/issues/issue_16_test.js index 5e57316d..503b4972 100644 --- a/test/issues/issue_16_test.js +++ b/test/issues/issue_16_test.js @@ -26,9 +26,8 @@ exports['issue_16'] = { var file = "test/fixtures/issue_16.js", source = fs.readFileSync(file).toString().trim(), - config = {}, - globals = [], - report = linter.process(source, config, globals); + options = {}, + report = linter.process(source, options, {}); test.equal(report.messages.length, 0, "Report returned with messages"); test.done(); diff --git a/test/issues/issue_182_test.js b/test/issues/issue_182_test.js new file mode 100644 index 00000000..75d9e281 --- /dev/null +++ b/test/issues/issue_182_test.js @@ -0,0 +1,36 @@ +'use strict'; +/** + * #182 the default behavior of JSHint is to look for ".jshintsrc" up the processed file's hierarchy. + * The difference between `.jshintrc` and `test/fixtures/.jshintrc` is maxparams. + * if jshint finds `test/fixtures/.jshintrc` then a maxparams error should occur. + */ +var fs = require('fs-extra'), + linter = require('../../lib/reporters/jshint'); + +exports['issue_182'] = { + setUp: function(done) { + done(); + }, + + 'look for ".jshintsrc" up the processed file hierarchy': function(test) { + + var file = "test/fixtures/issue_182.js", + source = fs.readFileSync(file).toString().trim(), + options = {}, + reportInfo = { + file : 'test/fixtures/issue_182.js', + fileShort : '', + fileSafe : '', + link : '' + }, + report; + + report = linter.process(source, options, reportInfo); + test.equal(report.messages.length, 1, "a maxparams error should occur"); + + report = linter.process(source, options, {}); + test.equal(report.messages.length, 0, "no error"); + + test.done(); + } +}; diff --git a/test/issues/issue_217_test.js b/test/issues/issue_217_test.js index a540d4b1..18123a0e 100644 --- a/test/issues/issue_217_test.js +++ b/test/issues/issue_217_test.js @@ -19,8 +19,7 @@ exports['issue_217'] = { "unused": false }) }, - globals = [], - report = linter.process(source, config, globals); + report = linter.process(source, config, {}); test.equal(report.messages.length, 0, "Report returned with messages"); test.done(); diff --git a/test/plato_test.js b/test/plato_test.js index f3764837..755a3a09 100644 --- a/test/plato_test.js +++ b/test/plato_test.js @@ -49,7 +49,7 @@ exports['plato'] = { var files = './test/fixtures/*.js'; plato.inspect(files, null, {}, function(reports) { - test.equal(reports.length, 8, 'Should properly test against the array produced by the glob'); + test.equal(reports.length, 9, 'Should properly test against the array produced by the glob'); test.done(); }); },