node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

typling-core

typling-core

Create and verify typlings on Esprima-style nodes

var esprima = require('esprima')
var typling = require('typling-core')
 
var node = esprima.parse(
  `// Number, Number -> Number
   function foo (x, y) { return x + y }
   foo(123, 'hello world')`,
   { attachComment: true }
 )
 
var types = typling.create(node)
var report = typling.verify(node, types)
// [ TypeError { ... } ] 

Takes Esprima-style nodes (see Estree) and can do 3 things of your choosing:

  • create: Parses typlings (e.g. // String -> String) into array of types that optionally point back to the nodes.
  • verify: Verify typlings (probably from create) against a tree of nodes, returning an array of errors if any.
  • check: A type checking function for nodes. Shortcut for create then verify on the same node.

Notice: Typling requires comments to be attached to the nodes with Esprima's attachComment option (or similar in others)

Installation

$ npm install --save typling-core

Usage

typling.check(node, [options])

Type check the node, and return a context object with all the results.

  • node (estree Node): A Node created from any ESTree-compatible parser
  • options (Object): Optional object for creating context
  • options.definitions (Object): An object mapping type names (e.g. String, Number) to a type definition. Has built-in definitions
  • options.typlings (Array): Preloading typlings. Typlings from the node are added in.
// Create context and generate reports: 
var result = typling.check(node)
 
// Result is context object: 
console.log(result.report)
console.log(result.source)

This is typling.create and typling.verify turned into one step if you want simple type checker.

typling.create(node, [options])

Create a context object. Contains definitions, typlings, source, report. Same options as typling.check

// Create context (generates typlings): 
var context = typling.create(node)
 
// Has necessary props, with no report 
console.log(context.typlings)
console.log(context.definitions)
// ... 

Note: report will be empty until you use typling.verify or use typling.check instead.

typling.verify(context)

Verify a context from typling.create. Creates objects on report, otherwise empty.

// Create context, and cache typligns 
var context = typling.create(node)
var typlings = context.typlings
 
// Verify context 
typling.verify(context)
 
// Log any reports: 
console.log(context.reports)

License

MIT © Jamen Marz


version travis downloads license follow