CoffeeScript is a programming language that compiles into JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell to enhance JavaScript's brevity and readability. CoffeeScript provides significant whitespace, arrow functions, destructuring assignment, classes, and other features that make JavaScript more concise and expressive.
npm install -g coffee-script or npm install coffee-scriptconst CoffeeScript = require('coffee-script');For individual functions:
const { compile, run, nodes, tokens } = require('coffee-script');
const evalFunction = require('coffee-script')["eval"]; // Note: eval requires bracket notationBrowser usage:
<script src="path/to/coffee-script.js"></script>const CoffeeScript = require('coffee-script');
// Compile CoffeeScript to JavaScript
const coffeeCode = `
square = (x) -> x * x
list = [1, 2, 3, 4, 5]
squares = (square num for num in list)
`;
const jsCode = CoffeeScript.compile(coffeeCode);
console.log(jsCode);
// Run CoffeeScript directly
CoffeeScript.run('console.log "Hello from CoffeeScript!"');
// Evaluate CoffeeScript in a sandbox
const result = CoffeeScript.eval('6 * 7');
console.log(result); // 42CoffeeScript is structured around several key components:
Primary compilation functions for transforming CoffeeScript source code into JavaScript.
function compile(code, options);
function tokens(code, options);
function nodes(source, options);Direct execution of CoffeeScript code in various contexts including Node.js main module and sandboxed environments.
function run(code, options);
function eval(code, options);Complete set of AST node classes for representing and manipulating CoffeeScript language constructs programmatically.
// Base node classes
class Base { }
class Block extends Base { }
class Literal extends Base { }
// Expression nodes
class Value extends Base { }
class Call extends Base { }
class Op extends Base { }
class Assign extends Base { }
// Control flow nodes
class If extends Base { }
class While extends Base { }
class For extends Base { }
class Try extends Base { }Command-line tools for compiling, running, and building CoffeeScript projects.
// Command module functions
function run(); // Main coffee command
function cake.run(); // Cake build toolInteractive CoffeeScript Read-Eval-Print Loop and runtime registration for requiring .coffee files.
function start(opts); // Start REPL
function register(); // Enable .coffee file requiresComprehensive utility functions for string manipulation, array processing, error handling, and source location management.
// String utilities
function starts(string, literal, start);
function ends(string, literal, back);
function repeat(str, n);
function count(string, substr);
// Array/Object utilities
function compact(array);
function merge(options, overrides);
function extend(object, properties);
function flatten(array);interface CompileOptions {
bare?: boolean;
header?: boolean;
sourceMap?: boolean;
inlineMap?: boolean;
filename?: string;
literate?: boolean;
referencedVars?: string[];
}
interface RunOptions {
filename?: string;
}
interface EvalOptions {
sandbox?: object;
filename?: string;
modulename?: string;
}
interface TokenizeOptions {
rewrite?: boolean;
literate?: boolean;
}
const VERSION: string;
const FILE_EXTENSIONS: string[];
const helpers: object;