0.0.1 • Published 2 years ago

@julr/module-methods-extractor v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@julr/module-methods-extractor

🪛 Utility module to extract public methods for a given default Typescript export. Used by the Adonis.js VSCode extension

Installation

pnpm add @julr/module-methods-extractor

Usage

import { Extractor } from '@julr/module-methods-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 },
  ]
})

Features supported

  • ESM export default is supported.
  • Handle inline class declarations like export default UserController {}.
  • Returns lineno for all methods.
  • Use @babel/parser instead of the Typescript Compiler API to parse the source code. This results in a much lighter dependency ( 274KB instead of 3.32MB)

Limitations

  • Only supports ESM. Commonjs module.exports is not supported. ( Maybe later ? )
  • 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 Adonis.js eco-system and also it is not a great pattern to use either.

Acknowledgements

License

MIT License © 2022 Julien Ripouteau