1.0.0 • Published 8 years ago

declarations-js v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

declarations-js-node

declarations-js-node is a Node.js module that can scan javascript sources and return the list of declarations. It's useful for creating packages that jump to declarations for IDEs/TextEditors.

Installation

npm install --save declarations-js-node

Features

Scanner

  • Supports latest JS syntax
  • Supports filePath in import * from x
  • Supports filePath in named and default requires and imports

API

type Declaration = {
  name: string,
  position: { line: number, column: number },
  source: {
    name: ?string,
    filePath: ?string,
    position: { line: number, column: number },
  }
}

export function scanDeclarations(
  filePath: string,
  fileContents: string,
  nodeInRange: ((node: Object) => boolean)
): Array<Declaration>

Examples

/* @flow */

import FS from 'fs'
import promisify from 'sb-promisify'
import { scanDeclarations } from 'declarations-js'

const readFile = promisify(FS.readFile)

readFile('./test.js', 'utf8').then(function(fileContents) {
  const declarations = scanDeclarations('./test.js', fileContents, function(node) {
    return node.line > 2 && node.line < 10 && node.column > 0
  })
  console.log('declarations', declarations)
}).catch(function(error) {
  console.error('Unable to read file', error)
})

License

This project is licensed under the terms of MIT License. See the LICENSE file for more info.