function processFile(file, config) { if (/\.(js|json|jshintrc)$/.test(file)) { var fileContents = fs.readFileSync(file, 'utf8'); var success = jshint(fileContents, config); if(!success){ process.stdout.write("x"); var errors = jshint.errors.map(function(error) { if(!error){return {"reason" : null, "line": 0, "character" : 0};} return {"reason" : error.reason, "line": error.line, "character" : error.character}; }); return {"file" : file, "success" : false, "errors" : errors }; } process.stdout.write("."); return {"file" : file, "success" : true}; } return null; }
FS.readFile(file, function(err, data) { if (err) { return cb(err); } var result = jshint(data.toString(), self.config.testConfig); if (!result) { self.logs.errors.push({ file: file, errors: jshint.errors }); } cb(null, result); });
function end() { var files = jshintcli.gather({ args: [file] }); var isIgnored = files.length === 0; if (!isIgnored) { var config = jshintcli.getConfig(file); delete config.dirname; if (!JSHINT(data, config, config.globals)) { this.emit('error', JSHINT.errors.map(formatError).join('\n')); } } this.queue(data); this.queue(null); }
JSHinter.prototype.processString = function (content, relativePath) { var passed = JSHINT(content, this.jshintrc); var errors = this.processErrors(relativePath, JSHINT.errors), generalError; if (this.failOnAnyError && errors.length > 0){ generalError = new Error('JSHint failed'); generalError.jshintErrors = errors; throw generalError; } if (!passed && this.log) { this.logError(errors); } if (!this.disableTestGenerator) { return this.testGenerator(relativePath, passed, errors); } };
Codesurgeon.prototype.hint = function(success, fail, options) { if(typeof fail !== 'function') { option = fail; } var valid = jshint.JSHINT(this.output, options); if(valid === false && !this.options.quiet) { console.log('Hint fail!'); eyes.inspect(jshint.JSHINT.errors); fail && fail.call(this); } else { success && success.call(this); } return this; };
rcLoader.for(file.path, function (err, cfg) { if (err) return cb(err); var globals; if (cfg.globals) { globals = cfg.globals; delete cfg.globals; } // get or create file.jshint, we will write all output here var out = file.jshint || (file.jshint = {}); var str = (out.extracted) || file.contents.toString('utf8'); out.success = jshint(str, cfg, globals); if (!out.success) reportErrors(file, out, cfg); return cb(null, file); });
function hint( src, path ) { console.log('about to jshint : ' + path.underline); if (jshint( src, config.jshint)) { console.log('ok'.green.bold); } else { console.log('-------fail jshint-------'.red); console.log('jshint.errors.length : '.red + jshint.errors.length + ' length'.red); jshint.errors.forEach(function(e) { if (!e) { return; } var str = e.evidence ? e.evidence.inverse : ""; str = str.replace( /\t/g, " " ).trim(); console.log( path + " [L" + e.line + ":C" + e.character + "] " + e.reason + "\n " + str ); }); fail('hint failed!'.red); } }
stdin.on('end', function() { var error , ok = jshint( body.join('\n'), options ); if( ok ){ return; } var data = jshint.data(); for( var i = 0, len = data.errors.length; i < len; i += 1 ){ error = data.errors[i]; if( error && error.reason ){ puts( [error.line, error.character, error.reason].join(':') ); } } });
// # Lint some source code. // From http://jshint.com function hint( src, path ) { write( "Validating with JSHint..."); if ( jshint( src, config.jshint ) ) { ok(); } else { error(); jshint.errors.forEach(function( e ) { if ( !e ) { return; } var str = e.evidence ? e.evidence.inverse : ""; str = str.replace( /\t/g, " " ).trim(); error( path + " [L" + e.line + ":C" + e.character + "] " + e.reason + "\n " + str ); }); fail( "JSHint found errors." ); } }
function lintFile(filename, options, globals) { var source = fs.readFileSync(filename, "utf-8"), pass = jshint(source, options, globals); if (pass) { console.log("\u2714", filename); } else { console.log("\u2718", filename); for (var i = 0; i < jshint.errors.length; i++) { var error = jshint.errors[i]; if (!error) continue; if (error.evidence) console.log(error.line + ": " + error.evidence.trim()); console.log(" " + error.reason); } } return pass; }
it('should run ' + name + ' examples without errors', function() { jshint(snippet, { // in several snippets we give an example as to how to access // a property (like metadata) without doing anything with it // e.g. `list[0].metadata` expr: true, // this allows us to redefine variables, generally it's bad, buuut // for copy/paste purposes this is desirable within the docs shadow: true }); if (jshint.errors.length) { throw new JSHintError(jshint.errors[0]); } runCodeInSandbox(snippet, sandbox); });
// individual checks --------------------------------------------------------------- /** * JSHint * * Checks known invalid JSON file * content in order to produce a * verbose error message. */ function jshint (contents) { if (!JSHINT(contents)) { var out = JSHINT.data(); for(var i = 0; out.errors.length > i; ++i){ var error = out.errors[i]; if(error){ issues.push(new Issue({ code: 27, file: file, line: error.line ? error.line : null, character: error.character ? error.character : null, reason: error.reason ? error.reason : null, evidence: error.evidence ? error.evidence : null })); } } } }
module.exports.onFileContent = function(callback, config, fileObject) { var result = jshint(fileObject.content, options, {}); fileObject.content = "\n\n" + fileObject.path + " jshint results\n=========================================\n\n"; if (!result) { jshint.errors.forEach(function(result) { if(result) { fileObject.content += 'line ' + result.line + ', col ' + result.character + ', ' + result.reason + "\n"; if(!config.errors) { config.errors = 1; } else { config.errors ++; } } }); } callback(); };
return function (next) { var result = jshint(this.data, options); if (result) { next(); } else { var errorString = "Error in " + this.path + ": \n"; for (var i = 0; i < jshint.errors.length; i++) { var e = jshint.errors[i]; if (e === null) { errorString += "JSHint returned 'null' error for some reason.\n"; continue; } errorString += e.id + " [Line " + e.line + ", Char " + e.character + "] " + e.reason + "\n" + " >>> " + e.evidence + "\n"; } next( "JSHint: " + errorString ); } };
hint: function(src){ TUILD.log('JS: Checking the code with JSHint.') try{ JSHINT(src, { evil: true, forin: true, undef: true, browser: true }); /** * If errors, but not fail */ if(JSHINT.errors){ var i = 0; JSHINT.errors.forEach(function(erro){ if(!JS.jsHintOK[erro.reason]){ i++; TUILD.log(erro.id + " line " + erro.line + ": " + erro.reason + '\n' + erro.evidence + '\n'); } }); /** * The return */ if(i) TUILD.log('JS: ' + i + ' Error(s) found.'); else TUILD.log('JS: JSHint found no errors. Congratulations!'); } /** * Fail */ } catch(e){ TUILD.log("JS: JSHint FAIL: " + e.message); process.exit(1); } return src; },
function RunTest(path, options) { var content; try { content = fs.read(path); } catch (err) { console.error("cannot load test file '%s'", path); return false; } var result = {}; result["passed"] = JSHINT(content, Object.assign({}, jshintrc, options)); if (JSHINT.errors) { result["errors"] = JSHINT.errors; } return result; }
exports.validateSource = function(sourceCode, options, globals, description) { description = description ? description + " " : ""; var pass = jshint(sourceCode, options, globals); if (pass) { console.log(description + "ok"); } else { console.log(description + "failed"); for (var i = 0; i < jshint.errors.length; i++) { var error = jshint.errors[i]; if (!error) continue; if (error.evidence) { console.log(error.line + ": " + error.evidence.trim()); } console.log(" " + error.reason); } } return pass; };
function hintFile( file ) { var src = fs.readFileSync( _SRC_ + file, "utf8"), ok = { // warning.reason "Expected an identifier and instead saw 'undefined' (a reserved word).": true, "Use '===' to compare with 'null'.": true, "Use '!==' to compare with 'null'.": true, "Expected an assignment or function call and instead saw an expression.": true, "Expected a 'break' statement before 'case'.": true, "'e' is already defined.": true, // warning.raw "Expected an identifier and instead saw \'{a}\' (a reserved word).": true }, found = 0, errors, warning; jshint( src, { evil: true, forin: false, maxerr: 100 }); errors = jshint.errors; for ( var i = 0; i < errors.length; i++ ) { warning = errors[i]; if ( warning ) { //print( w ); if ( !ok[ warning.reason ] && !ok[ warning.raw ] ) { found++; print( "\n" + file + " at L" + warning.line + " C" + warning.character + ": " + warning.reason ); print( "\n" + warning.evidence + "\n"); } } } if ( found > 0 ) { print( "\n\n" + found + " Error(s) found.\n" ); } else { print( " " + file + " => Success!\n" ); } }
storedjsArray.forEach( function(storedjs) { //console.log(storedjs.value.code); var options = { "eqeqeq" : false, "eqnull" : false, "laxbreak" : true, "sub" : true }; var globals = {}; if(jshint(storedjs.value.code.toString(), options, globals)) { console.log('Function ' + storedjs._id + ' has no errors. Congrats!'); } else { var out = jshint.data(), errors = out.errors; console.log(errors.length + ' errors in function ' + storedjs._id); console.log(''); for(var j=0;j<errors.length;j++) { if (errors[j].code.substring(0, 1)=='W') { // if (errors[j].code=='W025' || // errors[j].code=='W041') { suppressedErrors++; } else { console.log(errors[j].line + ':' + errors[j].character + ' (' + errors[j].code + ') -> ' + errors[j].reason + ' -> ' + errors[j].evidence); } totalErrors++; } // List globals console.log(''); console.log('Globals: '); for(j=0;j<out.globals.length;j++) { console.log(' ' + out.globals[j]); } } console.log('-----------------------------------------'); } );
LintDecorator.prototype.process = function(sourceTree) { var files, name, results, pushErr; results = []; files = sourceTree.getFiles(); pushErr = function (err) { if (err) results.push(err); }; for (name in files) { if (!files.hasOwnProperty(name)) continue; results = []; if (!jshint(files[name].content, this.config, this.globals)) { jshint.errors.forEach(pushErr); } sourceTree.updateReport(name, 'lint', results); } return this.component.process(sourceTree); };
function lintFiles(files) { var errorsFound = 0, i, j, len, len2, src, errors, e; for (i = 0, len = files.length; i < len; i++) { jshint.JSHINT(fs.readFileSync(files[i], 'utf8'), hintrc, i ? {L: true} : null); errors = jshint.JSHINT.errors; for (j = 0, len2 = errors.length; j < len2; j++) { e = errors[j]; console.log(files[i] + '\tline ' + e.line + '\tcol ' + e.character + '\t ' + e.reason); } errorsFound += len2; } return errorsFound; }
// Run strict jshint on the file function jsHint(obj, callback) { console.log('jshint', obj.filename); if(obj.err) { return callback(obj); } jshint('/*jshint strict: true, trailing: true, curly: true, ' + 'es5: true, eqeqeq: true*/(function(){"use strict";' + '/*global require:true,module:true,exports:true,' + 'console:true,setTimeout:true */' + obj.filedata + '})();'); obj.jshint = ''; // Reporting jshint.errors.forEach(function(err) { if(err) { obj.jshint += mustache.to_html( '<div>{{file}} line {{line}} pos {{pos}}: {{err}}</div>', {file: obj.filename, line: err.line, pos: err.character, err: err.reason}); } }); return callback(obj); }
function doLint(data, options, globals, lineOffset, charOffset) { // Lint the code and write readable error output to the console. try { jshint(data, options, globals); } catch (e) {} jshint.errors .sort(function(first, second) { first = first || {}; second = second || {}; if (!first.line) { return 1; } else if (!second.line){ return -1; } else if (first.line == second.line) { return +first.character < +second.character ? -1 : 1; } else { return +first.line < +second.line ? -1 : 1; } }) .forEach(function(e) { // If the argument is null, then we could not continue (too many errors). if (!e) { return; } // Do some formatting if the error data is available. if (e.raw) { var message = e.reason; if(e.a !== undefined && e.b !== undefined && e.c !== undefined && e.d !== undefined) message = e.raw.replace("{a}", e.a) .replace("{b}", e.b) .replace("{c}", e.c) .replace("{d}", e.d); console.log([e.line + lineOffset, e.character + charOffset, message].join(" :: ")); } }); }
Lint.prototype.file = function (filepath, jshintSettings) { if (fs.lstatSync(filepath).isFile()) { debug("processing file: %s", filepath); var file = fs.readFileSync(filepath, 'utf-8'); var self = this; if (!JSHint.JSHINT(file, jshintSettings || {})) { _.each(JSHint.JSHINT.errors, function (error) { if (error) { self.results.push({ file: filepath, error: error }); } }); } this.count++; } };
fs.readFile(file, function(err, data) { if(err) { console.log('Error: ' + err); return; } if(jshint(data.toString())) { console.log('File ' + file + ' has no errors.'); console.log('-----------------------------------------'); callback(false); } else { console.log('Errors in file ' + file); var out = jshint.data(), errors = out.errors; for(var j = 0; j < errors.length; j++) { console.log(errors[j].line + ':' + errors[j].character + ' -> ' + errors[j].reason + ' -> ' + errors[j].evidence); } console.log('-----------------------------------------'); callback(true); } });
function lint(source, config, globals) { config = config || {}; var results = []; var data = []; // Remove potential Unicode BOM. source = source.replace(/^\uFEFF/, ''); if (!JSHINT(source, config, globals)) { JSHINT.errors.forEach(function (err) { if (err) { results.push({ error: err }); } }); } var lintData = JSHINT.data(); if (lintData) { data.push(lintData); } return { results : results, data : data }; }
warnings:function(info){ var warns = []; var jshintOpts = JSON.parse(info.files['.jshintrc'].content); for(var file in info.files) { if(file.match(/(.js)$/)) { var content = info.files[file].content; jsh.JSHINT(content, jshintOpts , false); var data = jsh.JSHINT.data(); if(data.errors) { if(qaControl.verbose){ console.log('JSHINT output:'); console.log('jshintOpts',jshintOpts); console.log('There are '+data.length+ " JSHINT errors"); console.log(data.errors); //console.log(data); } warns.push({warning:'jshint_warnings_in_file_1', params:[file], scoring:{jshint:1}}); } } } return warns; }
function process(filePath) { var stat = fs.statSync(filePath); if (stat.isFile() && path.extname(filePath) == '.js') { if (!jshint(fs.readFileSync(filePath).toString(), options)) { // Print the errors console.log(color('Errors in file ' + filePath, 'red')); var out = jshint.data(), errors = out.errors; Object.keys(errors).forEach(function(error){ error = errors[error]; console.log('line: ' + error.line + ':' + error.character+ ' -> ' + error.reason ); console.log(color(error.evidence,'yellow')); }); } } else if (stat.isDirectory()) { fs.readdirSync(filePath).forEach(function(fileName) { process(path.join(filePath, fileName)); }); } }
function lint(full) { jshint(full.toString(), { browser: true, undef: true, unused: true, immed: true, eqeqeq: true, eqnull: true, noarg: true, predef: ['define', 'module', 'exports'] }); if (jshint.errors.length) { jshint.errors.forEach(function (err) { console.log(err.line+':'+err.character+' '+err.reason); }); } else { console.log('linted') } return true; }
return helper.readFile(filePath, 'utf-8').then(function(buf){ buf = buf.replace(/^\uFEFF/, ''); // remove Byte Order Mark var success = jshint.JSHINT(buf, taskConfig.jshint.options, taskConfig.jshint.globals); if(success){ log.info('jshint', 'Passed for ' + filePath + '.'); }else{ log.error('jshint', 'Found ' + jshint.JSHINT.errors.length.toString() + ' issues while linting ' + filePath + '.'); if(context.options.verbose){ jshint.JSHINT.errors.forEach(function(error){ log.error('jshint', error); }); } if(taskConfig.stopOnErrors){ log.error('jshint', 'Errors detected. Stopped.'); process.exit(1); } } }).fail(function(error){