https://github.com/jamen/typling-core
Check typlings on Esprima-style AST
https://github.com/jamen/typling-core
Last synced: 8 months ago
JSON representation
Check typlings on Esprima-style AST
- Host: GitHub
- URL: https://github.com/jamen/typling-core
- Owner: jamen
- License: mit
- Created: 2016-12-01T19:08:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-06T14:18:11.000Z (over 9 years ago)
- Last Synced: 2025-04-01T20:53:34.217Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 63.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# typling-core
> Check typlings on Esprima-style AST
This is for using typling as a module. See [`typling`](https://github.com/jamen/typling) for using as a tool.
```js
var esprima = require('esprima')
var typling = require('typling-core')
var source = esprima.parse(
`//@ Number, Number -> Number
function foo (x, y) {
return x + y
}
foo(123, 'hello world')`,
{ attachComment: true, loc: true }
)
var result = typling.check(source)
console.log(result.report)
console.log(result.source)
// ...
```
There are 3 functions you can use with `typling`:
- `check(source, options?)`: Create context and generate reports. (`create` and `verify` combined)
- `create(source, options?)`: Create context with typlings, definitions, source, and no reports.
- `verify(context)`: Generate reports on the context.
**Notice:** Nodes must have comments attached, and optionally locations for line/column in reports. Use `{ attachComment: true, loc: true }` with `esprima`, or similar in others.
## Installation
```sh
$ npm install --save typling-core
```
## Usage
### `typling.check(source, options?)`
Type check the node, and return a context object with all the results.
- `source` ([estree `Node`](https://github.com/estree/estree/blob/master/es5.md#node-objects)): A Node (preferably [`Program`](https://github.com/estree/estree/blob/master/es5.md#programs)) created from any ESTree-compatible parser.
- `options` (`Object`): Options object for creating context. Optional.
- `options.definitions` (`Object`): An object mapping type names (e.g. `String`, `Number`) to a type definition. Has [built-in definitions](lib/defs/)
- `options.typlings` (`Array`): Preloading typlings. Typlings from the node are added in.
```js
// 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(source, options?)`
Create a context object. Contains `definitions`, `typlings`, `source`, `report`. Same options as `typling.check`
```js
// 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.
```js
// 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)
```
### `typling.signature(signature, [associate])`
Parse a strip into a signature. `null` is turned into `*`. The optional `associate` parameter is a node or string.
```js
typling.signature('Number, Number -> String')
// => [['Number', 'Number'], 'Number']
typling.signature('Number, *, String -> *')
// => [['Number', null, 'String'], null]
typling.signature('String -> String', { foo: 123 })
// => [['String'], 'String', { foo: 123 }]
```
## License
MIT © [Jamen Marz](https://git.io/jamen)
---
[][package] [](https://travis-ci.org/jamen/typling-core) [][package] [][package] [](https://github.com/jamen)
[package]: https://npmjs.org/package/typling-core