Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Latest commit

 

History

History
111 lines (83 loc) · 3.85 KB

README.md

File metadata and controls

111 lines (83 loc) · 3.85 KB

Module methods exporter

Returns a list of method names and line number for a JavaScript or TypeScript module.

circleci-image npm-image license-image audit-report-image

This module is used by the AdonisJs VsCode extension to show an autocomplete list of controller methods, event listeners and so on.

The module is tailored for AdonisJs only, which helps in optimizing the way we scan the source code AST.

Table of contents

Usage

Install the package from npm registry as follows:

import { Extractor } from '@poppinss/Extractor'

const extractor = new Extractor()

const response = extractor.extract(`
export default class UserController {
  public async index () {
  }

  public async store () {
  }
}
`)

assert.deepEqual(response, {
  kind: 'class',
  methods: [
    {
      name: 'index',
      lineno: 2
    },
    {
      name: 'store',
      lineno: 5
    },    
  ]
})

Design & Limitations

The module is written to work with AdonisJs, where modules are not imported explicitly but their filenames are passed as a reference. For example:

Route.get('users', 'UsersController.index')

In the above example, The UsersController is an actual module that has a default export on the UserController class. AdonisJs behind the scenes will use its IoC container to lazy load this class and invoke the defined method.

Features supported

  • CommonJs module.exports and exports are supported.
  • ESM export default is supported.
  • Handle assignment references like module.exports = exports = UserController.
  • Handle inline class declarations like export default UserController {}.
  • Returns lineno for all methods.

Limitations

  • Named exports are not allowed, since they are also forbidden by the IoC container automatic bindings.

  • The export reference must be located as a top level property. For example:

    const someObject = {
      prop: class UserController {}
    }
    
    export default someObject.prop

    The above expression is not something we advocate in the AdonisJs eco-system and also it is not a great pattern to use either.

  • Only 3 levels deep assignments are supported.

    // works
    module.exports = exports = UserController
    
    // works
    module.exports = exports = someFunc = UserController
    
    // does not work
    module.exports = exports = someFunc = someOtherFunc = UserController