Google Closure Compiler and JSDoc Type Expression Parser
Catharsis is a JavaScript library designed for parsing and manipulating type expressions used by Google Closure Compiler and JSDoc. It provides robust capabilities to convert these expressions into a structured Abstract Syntax Tree (AST), and then back into a string or a human-readable description. The current stable version is 0.11.0, last published about a year ago, indicating an irregular or maintenance-focused release cadence. Key differentiators include its high accuracy, backed by a Peggy grammar and a comprehensive test suite, and its flexibility in handling both standard Closure Compiler syntax and JSDoc-specific extensions like `string[]` for `Array<string>`. This library is primarily used by tools and systems that need to interpret or generate type annotations, offering programmatic access to the structure and meaning of complex type expressions.
Common errors
-
TypeError: catharsis.parse is not a function
cause The `catharsis` module was imported incorrectly, leading to its methods not being accessible on the imported object. This often happens when attempting named imports (`import { parse } from 'catharsis';`) for a package that only provides a default export object.fixEnsure you are importing the default export object and accessing its methods: `import catharsis from 'catharsis'; catharsis.parse(...);` or for CommonJS: `const catharsis = require('catharsis'); catharsis.parse(...);` -
Error: Unable to parse [...]
cause The provided type expression does not conform to either Google Closure Compiler or JSDoc type expression syntax, or the `jsdoc: true` option was not enabled for JSDoc-specific syntax.fixReview the type expression for syntax errors. If parsing JSDoc-specific syntax (e.g., `string[]`, `function`), ensure `catharsis.parse(expression, { jsdoc: true })` is used.
Warnings
- gotcha Parsing JSDoc-style type expressions requires explicitly enabling the `jsdoc` option in the `parse()` method. Without it, JSDoc-specific syntax like `string[]` will fail to parse correctly.
- breaking Since version 0.11.0, the `stringify()` method now always re-stringifies the parsed type object rather than potentially returning the original input string. Additionally, the `options.cssClass` property for `stringify()` and `describe()` methods has been removed (it was deprecated in 0.8.0).
- gotcha When using `stringify()` with `options.htmlSafe: true`, only angle brackets (`<` and `>`) are escaped. Characters within name expressions (e.g., `MyClass<string>`) are not escaped, which might lead to unexpected rendering if names contain special HTML characters.
- gotcha The `catharsis` package currently appears to be in maintenance mode. While functional and stable, active feature development or frequent updates should not be expected. The latest version was published over a year ago.
Install
-
npm install catharsis -
yarn add catharsis -
pnpm add catharsis
Imports
- catharsis
import { catharsis } from 'catharsis';import catharsis from 'catharsis';
- parse
import { parse } from 'catharsis';import catharsis from 'catharsis'; catharsis.parse(typeExpression, options);
- stringify
import { stringify } from 'catharsis';import catharsis from 'catharsis'; catharsis.stringify(parsedType, options);
- describe
import { describe } from 'catharsis';import catharsis from 'catharsis'; catharsis.describe(parsedType);
Quickstart
import catharsis from 'catharsis';
// --- Closure Compiler Style Parsing ---
const closureType = '!Object';
let parsedClosureType;
try {
parsedClosureType = catharsis.parse(closureType);
console.log('Parsed Closure Type:', JSON.stringify(parsedClosureType));
console.log('Stringified:', catharsis.stringify(parsedClosureType));
console.log('Description:', catharsis.describe(parsedClosureType).simple);
} catch (e) {
console.error(`Unable to parse '${closureType}':`, e.message);
}
// --- JSDoc Style Parsing (requires 'jsdoc: true') ---
const jsdocType = 'string[]'; // Equivalent to Closure Compiler's Array<string>
let parsedJsdocType;
try {
parsedJsdocType = catharsis.parse(jsdocType, { jsdoc: true });
console.log('Parsed JSDoc Type:', JSON.stringify(parsedJsdocType));
console.log('Stringified:', catharsis.stringify(parsedJsdocType));
console.log('Description:', catharsis.describe(parsedJsdocType).simple);
} catch (e) {
console.error(`Unable to parse '${jsdocType}':`, e.message);
}
// --- HTML-safe Stringification ---
const typeWithGenerics = 'Array<number>';
const parsedGenerics = catharsis.parse(typeWithGenerics);
console.log('HTML-safe stringify:', catharsis.stringify(parsedGenerics, { htmlSafe: true }));