Static analysis tool for JavaScript that detects errors and potential problems in code
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core JavaScript code analysis functionality that performs static analysis, error detection, and provides detailed analysis data.
Main linting function that analyzes JavaScript code and returns analysis results.
/**
* Analyzes JavaScript source code for errors and potential problems
* @param source - JavaScript source code as string or array of strings
* @param options - Configuration options object (optional)
* @param globals - Global variables declaration object (optional)
* @returns boolean - true if no errors found, false if errors detected
*/
function JSHINT(source, options, globals);Usage Examples:
const { JSHINT } = require('jshint');
// Basic usage
const result = JSHINT('var x = 1;');
console.log(result); // true (no errors)
// With options
const code = 'function test() { return undefined == null; }';
const options = { eqeqeq: true }; // require === and !==
const result2 = JSHINT(code, options);
console.log(result2); // false (== not allowed)
// With globals
const codeWithGlobals = 'console.log("hello");';
const globals = { console: false }; // console is read-only
const result3 = JSHINT(codeWithGlobals, {}, globals);
console.log(result3); // trueAccess to detailed error information after linting.
/**
* Array of error objects from the last linting run
* Available after calling JSHINT() function
*/
JSHINT.errors: Array<{
file: string,
error: {
line: number,
character: number,
reason: string,
code: string
}
}>;
/**
* Current scope identifier string
*/
JSHINT.scope: string;
/**
* Internal processing data array
*/
JSHINT.internals: any[];
/**
* Blacklisted identifiers object
*/
JSHINT.blacklist: object;Usage Examples:
const { JSHINT } = require('jshint');
const badCode = `
function test() {
var x = 1
var y = 2 // missing semicolon
return x + y;
}
`;
const result = JSHINT(badCode, { asi: false }); // require semicolons
if (!result) {
console.log('Errors found:');
JSHINT.errors.forEach(errorInfo => {
const { error } = errorInfo;
console.log(`Line ${error.line}, Column ${error.character}: ${error.reason}`);
console.log(`Error code: ${error.code}`);
});
}Comprehensive analysis data about the linted code.
/**
* Returns detailed analysis data from the last linting run
* @returns object containing comprehensive analysis results
*/
JSHINT.data(): {
functions: Array<{
name: string,
line: number,
character: number,
last: number,
lastcharacter: number,
params: string[],
closure: any[],
exceptions: any[],
undefined: any[],
unused: any[],
global: any[],
label: any[]
}>,
options: object,
errors: any[],
implieds: Array<{
name: string,
line: number[]
}>,
globals: Array<{
name: string,
line: number[]
}>,
unused: Array<{
name: string,
line: number,
character: number
}>,
member: object,
json: boolean
};Usage Examples:
const { JSHINT } = require('jshint');
const code = `
function calculate(x, y) {
var result = x + y;
console.log(result);
return result;
}
var unused = 42; // unused variable
globalVar = 'implicit global'; // implied global
`;
JSHINT(code, { undef: true, unused: true });
const data = JSHINT.data();
// Function information
console.log('Functions found:');
data.functions.forEach(func => {
console.log(`- ${func.name} at line ${func.line}, params: ${func.params.join(', ')}`);
});
// Unused variables
if (data.unused.length > 0) {
console.log('Unused variables:');
data.unused.forEach(unused => {
console.log(`- ${unused.name} at line ${unused.line}`);
});
}
// Implied globals
if (data.implieds.length > 0) {
console.log('Implied globals:');
data.implieds.forEach(implied => {
console.log(`- ${implied.name} used at lines: ${implied.line.join(', ')}`);
});
}
// Defined globals
if (data.globals.length > 0) {
console.log('Defined globals:');
data.globals.forEach(global => {
console.log(`- ${global.name} at lines: ${global.line.join(', ')}`);
});
}Extension mechanism for adding external modules to the linting process.
/**
* Adds external modules to the linting process
* @param func - Module function to add to JSHint
*/
JSHINT.addModule(func);
/**
* Self-reference to the JSHINT function
*/
JSHINT.jshint: typeof JSHINT;Usage Examples:
const { JSHINT } = require('jshint');
// Add a custom module
JSHINT.addModule(function(api) {
// Custom linting logic using JSHint's internal API
api.on('Identifier', function(data) {
if (data.name === 'badName') {
api.warn('Avoid using badName', data);
}
});
});
// Use JSHINT with custom module
const code = 'var badName = 42;';
const result = JSHINT(code);
// Custom warning will be included in resultsJSHint uses structured error codes for programmatic handling:
const { JSHINT } = require('jshint');
function lintCode(sourceCode, userOptions = {}) {
// Default options
const defaultOptions = {
esversion: 6,
undef: true,
unused: true,
strict: true
};
const options = { ...defaultOptions, ...userOptions };
// Common globals
const globals = {
console: false, // read-only
require: false, // read-only (Node.js)
module: true, // writable (Node.js)
exports: true // writable (Node.js)
};
// Run linting
const isClean = JSHINT(sourceCode, options, globals);
// Collect results
const results = {
success: isClean,
errors: JSHINT.errors || [],
data: JSHINT.data()
};
return results;
}
// Usage
const code = `
function add(a, b) {
return a + b;
}
module.exports = add;
`;
const results = lintCode(code);
if (results.success) {
console.log('Code passed linting!');
} else {
console.log('Linting errors:', results.errors);
}