# ecmas-annotations

> Annotation parser library for ES2014/ES2015, ES5/ES6

Latest version **1.0.2** (published 2016-05-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install ecmas-annotations
pnpm add ecmas-annotations
yarn add ecmas-annotations
bun add ecmas-annotations
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-05-20 |
| First published | 2016-01-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 4.0.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Jimmy Aumard |
| Maintainers | jaumard |
| Keywords | annotation, annotations, ecmas, es6, es2015 |

## Links

- npm: https://www.npmjs.com/package/ecmas-annotations
- Repository: https://github.com/jaumard/ecmas-annotations
- Homepage: https://github.com/jaumard/ecmas-annotations#readme
- Issues: https://github.com/jaumard/ecmas-annotations/issues
- npm.io page: https://npm.io/package/ecmas-annotations

## Dependencies (1)

- [StringScanner](https://npm.io/package/StringScanner.md) 0.0.3

## Recent versions

- 1.0.2 (latest) — 2016-05-20
- 1.0.1 — 2016-05-12
- 0.3.1 — 2016-01-28
- 0.3.0 — 2016-01-24
- 0.2.1 — 2016-01-23
- 0.2.0 — 2016-01-22

## README

# ecmas-annotations 
[![Gitter][gitter-image]][gitter-url]
[![NPM version][npm-image]][npm-url]
[![NPM downloads][npm-download]][npm-url]
[![Build status][ci-image]][ci-url]
[![Dependency Status][daviddm-image]][daviddm-url]
[![Code Climate][codeclimate-image]][codeclimate-url]

## Overview

This is a Node.js library which allows you to add annotations describing metdata data
about constructors, methods, and properties within javascript files.

Example:


    /**
     * @MyConstructorAnnotation("some value")
     */
    function HelloWorld(){}

    HelloWorld.prototype = {
        
        /**
         * @MyPropertyAnnotation(foo=[1,2,3,4], bar={"hello":"there"})
         */
        myProperty: 'foo',

        /**
         * @MyMethodAnnotation("foobar", something="hello")
         */
        myMethod: function(){

        }
    }


## Installation
Need Node >= 4.0.0

    > npm install ecmas-annotations

## Usage

Full example in [example folder](https://github.com/jaumard/ecmas-annotations/tree/master/example) (`node main.js` to test is). 

### Create an Annotation

    // my-constructor-annotation.js
    // ----------------------------
    'use strict'
    const Annotation = require('esmac-annotations').Annotation;

    module.exports = class MyConstructorAnnotation extends Annotation {

        /**
         * The possible targets
         *
         * (Annotation.DEFINITION, Annotation.CONSTRUCTOR, Annotation.PROPERTY, Annotation.METHOD)
         *
         * @type {Array}
         */
        static get targets() { return [Annotation.CONSTRUCTOR] }

        /**
         * Constructor to add attributes
         * @type {Array}
         */
        constructor(data, filePath){
          super(data, filePath)
          /**
           * The main value
           *
           * @type {String}
           */
          value = 'default value'
  
          /**
           * An additional attribute
           *
           * @type {String}
           */
          sample = 'default value for sample'
        
        }
        
        /**
         * Optional initialization method that
         * can be used to transform data
         *
         * @param  {Object} data
         * @return {void}
         */
        init: function(data){
            // do something with data
        }
        
    });


### Add the Annotation to a File

    // my-sample.js
    // ------------

    /**
     * @MyConstructorAnnotation("some value", sample="here is an attribute value")
     */
    function MySample(){}

### Parse the Annotation

    // my-parser.js
    // ------------

    const path = require('path')
    const annotations = require('ecmas-annotations')

    // create the registry
    const registry = new annotations.Registry()

    // add annotations to the registry
    registry.registerAnnotation(path.join(__dirname, 'my-constructor-annotation'))

    // create the annotation reader
    const reader = new annotations.Reader(registry)

    // parse the annotations from a file, default parse ES6 file, Reader.ES5 to force ES5
    reader.parse(path.join(__dirname, 'my-sample.js'), Reader.ES6)

    // get the annotations
    const definitionAnnotations = reader.definitionAnnotations
    const constructorAnnotations = reader.constructorAnnotations
    const methodAnnotations = reader.methodAnnotations
    const propertyAnnotations = reader.propertyAnnotations

    // loop through and handle the annotations
    constructorAnnotations.forEach(function(annotation){

        // @MyConstructorAnnotation
        if (annotation.annotation === 'MyConstructorAnnotation'){

            // do something with the annotation data
            console.log(annotation.target); // -> "MySample"
            console.log(annotation.value); // -> "some value"
            console.log(annotation.sample); // -> "here is an attribute value"
        }

    });

## Supported Attribute Types

    // string
    @MyAnnotation(foo="a string")

    // boolean
    @MyAnnotation(foo=true)

    // array
    @MyAnnotation(foo=[1,2,3,4])

    // object
    @MyAnnotation(foo={hi:"there"})

    // nested annotations
    @MyAnnotation(foo=@MyNestedAnnotation("this is nested", foo=true))

    // array of nested annotations
    @MyAnnotation(foo=[@MyNestedAnnotation("nested 1"), @MyNestedAnnotation("nested 2")])

## License
[MIT](https://github.com/jaumard/ecmas-annotations/blob/master/LICENSE)

[npm-image]: https://img.shields.io/npm/v/ecmas-annotations.svg?style=flat-square
[npm-url]: https://npmjs.org/package/ecmas-annotations
[npm-download]: https://img.shields.io/npm/dt/ecmas-annotations.svg
[ci-image]: https://travis-ci.org/jaumard/ecmas-annotations.svg?branch=master
[ci-url]: https://travis-ci.org/jaumard/ecmas-annotations
[daviddm-image]: http://img.shields.io/david/jaumard/ecmas-annotations.svg?style=flat-square
[daviddm-url]: https://david-dm.org/jaumard/ecmas-annotations
[codeclimate-image]: https://img.shields.io/codeclimate/github/jaumard/ecmas-annotations.svg?style=flat-square
[codeclimate-url]: https://codeclimate.com/github/jaumard/ecmas-annotations
[gitter-image]: http://img.shields.io/badge/+%20GITTER-JOIN%20CHAT%20%E2%86%92-1DCE73.svg?style=flat-square
[gitter-url]: https://gitter.im/jaumard/ecmas-annotations

---
_Source: https://npm.io/package/ecmas-annotations · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
