CoffeeLint is a style checker that helps keep CoffeeScript code clean and consistent. It analyzes CoffeeScript source code and reports style violations based on configurable rules, supporting both programmatic API usage and command-line interface.
npm install coffeelintconst coffeelint = require('coffeelint');For browser usage (via browserify):
// CoffeeLint is available as window.coffeelint
const coffeelint = window.coffeelint;const coffeelint = require('coffeelint');
// Lint CoffeeScript source code
const source = `
class Person
constructor: (@name) ->
greet: -> console.log "Hello, #{@name}!"
`;
// Basic linting with default configuration
const errors = coffeelint.lint(source);
// Linting with custom configuration
const config = {
"max_line_length": {
"level": "error",
"value": 80
},
"no_tabs": {
"level": "error"
}
};
const configErrors = coffeelint.lint(source, config);
// Check for errors
console.log(`Found ${errors.length} style issues`);
errors.forEach(error => {
console.log(`Line ${error.lineNumber}: ${error.message}`);
});CoffeeLint is built around several key components:
lint() function that orchestrates three types of lintersMain programmatic interface for linting CoffeeScript code with configurable rules and error reporting.
/**
* 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);
/**
* Current version of CoffeeLint
*/
const VERSION;
/**
* Get all available rules with their default configurations
* @returns Object mapping rule names to rule configurations
*/
function getRules();System for registering custom rules and managing the built-in rule set.
/**
* Register a new linting rule
* @param RuleConstructor - Constructor function for the rule
* @param ruleName - Optional override name for the rule
*/
function registerRule(RuleConstructor, ruleName);
/**
* Object containing all rule definitions
*/
const RULES;Tools for loading, merging, and managing linting configurations from various sources.
/**
* Remove default values from config to produce minimal configuration
* @param userConfig - User configuration object
* @returns Minimal configuration object with only non-default values
*/
function trimConfig(userConfig);
/**
* Convert literate CoffeeScript to regular CoffeeScript format
* @param source - Literate CoffeeScript source
* @returns Regular CoffeeScript source string
*/
function invertLiterate(source);Comprehensive error reporting system for collecting, formatting, and outputting linting results.
/**
* Create a new ErrorReport instance for collecting linting results
* @returns ErrorReport instance
*/
function getErrorReport();
/**
* Set cache object for linting results
* @param obj - Cache implementation object
*/
function setCache(obj);Full-featured CLI for linting files and directories with various output formats and configuration options.
# Basic usage
coffeelint file.coffee
# Lint multiple files and directories
coffeelint src/ test/ lib/main.coffee
# Use custom configuration file
coffeelint -f coffeelint.json src/
# Output in different formats
coffeelint --reporter csv src/
coffeelint --reporter jslint src/
coffeelint --reporter checkstyle src/
# Lint from stdin
cat file.coffee | coffeelint --stdin
# Generate configuration files
coffeelint --makeconfig > coffeelint.json
coffeelint --trimconfig/**
* Error object returned by lint function
*/
interface LintError {
/** Name of the violated rule */
rule: string;
/** Line number where violation occurred */
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]: {
/** Rule severity level */
level: 'error' | 'warn' | 'ignore';
/** Optional custom error message */
message?: string;
/** Rule-specific configuration options */
[option: string]: any;
};
/** Global coffeelint configuration */
coffeelint?: {
/** Source code transformers to apply before linting */
transforms?: string[];
/** Custom CoffeeScript compiler module */
coffeescript?: string;
};
}
/**
* 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;
}