Home

Awesome

Catharsis

Build Status

A JavaScript parser for Google Closure Compiler and JSDoc type expressions.

Catharsis is designed to be:

Example

const catharsis = require('catharsis');

// Closure Compiler parsing
const type = '!Object';
let parsedType;
try {
    parsedType = catharsis.parse(type); // {"type":"NameExpression,"name":"Object","nullable":false}
} catch(e) {
    console.error('unable to parse %s: %s', type, e);
}

// JSDoc-style type expressions enabled
const jsdocType = 'string[]';  // Closure Compiler expects Array.<string>
let parsedJsdocType;
try {
    parsedJsdocType = catharsis.parse(jsdocType, {jsdoc: true});
} catch (e) {
    console.error('unable to parse %s: %s', jsdocType, e);
}

// Converting parse results back to type expressions
catharsis.stringify(parsedType);                              // !Object
catharsis.stringify(parsedJsdocType);                         // string[]
catharsis.stringify(parsedJsdocType, {restringify: true});    // Array.<string>

// Converting parse results to descriptions of the type expression
catharsis.describe(parsedType).simple;                        // non-null Object
catharsis.describe(parsedJsdocType).simple;                   // Array of string

See the test/specs directory for more examples of Catharsis' parse results.

Methods

parse(typeExpression, options)

Parse a type expression, and return the parse results. Throws an error if the type expression cannot be parsed.

When called without options, Catharsis attempts to parse type expressions in the same way as Closure Compiler. When the jsdoc option is enabled, Catharsis can also parse several kinds of type expressions that are permitted in JSDoc:

Parameters

Returns

An object containing the parse results. See the test/specs directory for examples of the parse results for different type expressions.

The object also includes two non-enumerable properties:

stringify(parsedType, options)

Stringify parsedType, and return the type expression. If validation is enabled, throws an error if the stringified type expression cannot be parsed.

Parameters

Returns

A string containing the type expression.

describe(parsedType, options)

Convert a parsed type to a description of the type expression. This method is especially useful if your users are not familiar with the syntax for type expressions.

The describe() method returns the description in two formats:

For example, when you call describe('?function(new:MyObject, string)='), the method returns the following data:

{
  simple: 'optional nullable function(constructs MyObject, string)',
  extended: {
    description: 'function(string)',
    modifiers: {
      functionNew: 'Returns MyObject when called with new.',
      functionThis: '',
      optional: 'Optional.',
      nullable: 'May be null.',
      repeatable: ''
    },
    returns: ''
  }
}

Parameters

Returns

An object with the following properties:

Changelog

License

MIT license.