Skip to content

ESLint plugin for AdonisJS to enforce framework specific rules

License

Notifications You must be signed in to change notification settings

adonisjs/eslint-plugin-adonisjs

@adonisjs/eslint-plugin

Compatible with ESLint>=9.0 and TypeScript >=5.4



ESLint plugin for AdonisJS applications

The plugin forces your application to use lazy imports for controllers and event listeners. Lazy imports are a must when you are using HMR mode in AdonisJS.


gh-workflow-imagetypescript-imagenpm-imagelicense-image

Installation

The package comes pre-configured with the @adonisjs/eslint-config preset and hence manual installation is not required.

However, you can install and configure it as follows.

npm i -D @adonisjs/eslint-plugin@beta # Install peer dependencies npm i -D eslint@9 typescript typescript-eslint

Usage

After installation, you can register the following as follows. Make sure to also setup the typescript-eslint parser in order for the rules to work.

// eslint.config.jsimportadonisJSPluginfrom'@adonisjs/eslint-plugin'exportdefault[{plugins: {'@adonisjs': adonisJSPlugin,},rules: {'@adonisjs/prefer-lazy-controller-import': 'error','@adonisjs/prefer-lazy-listener-import': 'error',},},]

prefer-lazy-controller-import

Important

The HMR mode of AdonisJS only works with Lazy loaded controllers

The @adonisjs/prefer-lazy-controller-import rule complains when you import a controller using the import expression and assign it to a route. For example:

importrouterfrom'@adonisjs/core/services/router'// ❌ Error: Replace standard import with lazy controller importimportUsersControllerfrom'#controllers/user_controller'router.get('users',[UsersController,'index'])

The rule is auto fixable, therefore you can apply the fix depending upon the shortcuts provided by your code editor.

importrouterfrom'@adonisjs/core/services/router'// ✅ FixedconstUsersController=()=>import('#controllers/user_controller')router.get('users',[UsersController,'index'])

prefer-lazy-listener-import

Important

The HMR mode of AdonisJS only works with Lazy loaded event listeners

The @adonisjs/prefer-lazy-listener-import rule complains when you import an event listener using the import expression and assign it to an event. For example:

importemitterfrom'@adonisjs/core/services/emitter'// ❌ Error: Replace standard import with lazy controller importimportSendVerificationEmailfrom'#listeners/send_verification_email'emitter.on('user:created',[SendVerificationEmail,'handle'])

The rule is auto fixable, therefore you can apply the fix depending upon the shortcuts provided by your code editor.

importemitterfrom'@adonisjs/core/services/emitter'// ✅ FixedconstSendVerificationEmail=()=>import('#listeners/send_verification_email')emitter.on('user:created',[SendVerificationEmail,'handle'])
Built with ❤︎ by Julien Ripouteau and Harminder Virk

About

ESLint plugin for AdonisJS to enforce framework specific rules

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published