The core linting API provides the primary interface for programmatically analyzing CoffeeScript code. This includes the main lint function and supporting utilities for rule management and configuration.
Analyzes CoffeeScript source code and returns detailed error information based on configured rules.
/**
* Lint CoffeeScript source code and return array of errors
* @param source - CoffeeScript source code to lint
* @param userConfig - Optional configuration object with rule settings
* @param literate - Optional flag to treat source as literate CoffeeScript
* @returns Array of error objects with rule, lineNumber, level, message, context
*/
function lint(source, userConfig, literate);Usage Examples:
const coffeelint = require('coffeelint');
// Basic linting with default rules
const source = `
class Person
constructor: (@name) ->
greet: -> console.log "Hello, #{@name}!"
`;
const errors = coffeelint.lint(source);
console.log(`Found ${errors.length} issues`);
// Custom configuration
const config = {
"max_line_length": {
"level": "error",
"value": 80
},
"no_tabs": {
"level": "warn"
}
};
const configErrors = coffeelint.lint(source, config);
// Literate CoffeeScript
const literateSource = `
This is a literate CoffeeScript file.
class Person
constructor: (@name) ->
The class defines a simple person.
`;
const literateErrors = coffeelint.lint(literateSource, {}, true);Current version of the CoffeeLint library.
/**
* Current version of CoffeeLint
*/
const VERSION: string;Usage Example:
const coffeelint = require('coffeelint');
console.log(`Using CoffeeLint version ${coffeelint.VERSION}`);Retrieve all available linting rules with their default configurations.
/**
* Get all available rules with their default configurations
* @returns Object mapping rule names to rule configurations
*/
function getRules();Usage Example:
const coffeelint = require('coffeelint');
const rules = coffeelint.getRules();
console.log('Available rules:');
Object.keys(rules).forEach(ruleName => {
const rule = rules[ruleName];
console.log(`${ruleName}: ${rule.message} (level: ${rule.level})`);
});
// Check if a specific rule exists
if (rules['max_line_length']) {
console.log(`Max line length rule defaults to ${rules['max_line_length'].value} characters`);
}/**
* Error object returned by lint function
*/
interface LintError {
/** Name of the violated rule */
rule: string;
/** Line number where violation occurred (1-based) */
lineNumber: number;
/** Error level: 'error', 'warn', or 'ignore' */
level: 'error' | 'warn' | 'ignore';
/** Human-readable error message */
message: string;
/** Optional additional context about the violation */
context?: string;
/** Optional end line number for multi-line violations */
lineNumberEnd?: number;
}
/**
* Configuration object for linting rules
*/
interface LintConfig {
[ruleName: string]: RuleConfig;
/** Global coffeelint configuration */
coffeelint?: {
/** Source code transformers to apply before linting */
transforms?: string[];
/** Custom CoffeeScript compiler module */
coffeescript?: string;
};
}
/**
* Individual rule configuration
*/
interface RuleConfig {
/** Rule severity level */
level: 'error' | 'warn' | 'ignore';
/** Optional custom error message */
message?: string;
/** Rule-specific configuration options */
[option: string]: any;
}
/**
* Rule definition object
*/
interface RuleDefinition {
/** Unique rule name */
name: string;
/** Default severity level */
level: 'error' | 'warn' | 'ignore';
/** Default error message */
message: string;
/** Human-readable rule description */
description: string;
/** Rule-specific default options */
[option: string]: any;
}The lint function does not throw errors under normal circumstances. Instead, it returns an array of LintError objects describing all violations found. If the source code cannot be parsed due to syntax errors, those will be reported as lint errors.
const coffeelint = require('coffeelint');
// Invalid CoffeeScript syntax
const invalidSource = `
class Person
constructor: (@name) ->
# Missing closing quote
console.log "Hello, #{@name}
`;
const errors = coffeelint.lint(invalidSource);
// Errors array will contain syntax error information
errors.forEach(error => {
console.log(`${error.level.toUpperCase()}: ${error.message} (line ${error.lineNumber})`);
});