eslint-plugin-nestjs-leading-slash
v1.0.0
Published
ESLint plugin to enforce leading slash in NestJS route decorators
Downloads
12
Maintainers
Readme
eslint-plugin-nestjs-leading-slash
ESLint plugin to enforce the use of leading slash in NestJS route decorators.
Description
This ESLint plugin validates that all routes defined in NestJS HTTP decorators (@Get(), @Post(), @Put(), @Patch(), @Delete(), @Options(), @Head(), @All()) start with a leading slash (/). This helps maintain consistency in your NestJS application routes and prevents common errors.
Installation
npm install --save-dev eslint-plugin-nestjs-leading-slashUsage
Legacy Configuration (.eslintrc.js or .eslintrc.json)
{
"plugins": ["nestjs-leading-slash"],
"rules": {
"nestjs-leading-slash/enforce-leading-slash": "error"
}
}Flat Config (eslint.config.js)
import nestjsLeadingSlash from "eslint-plugin-nestjs-leading-slash";
export default [
{
plugins: {
"nestjs-leading-slash": nestjsLeadingSlash,
},
rules: {
"nestjs-leading-slash/enforce-leading-slash": "error",
},
},
];Rules
enforce-leading-slash
Enforces that all routes in NestJS HTTP decorators start with a leading slash.
Valid code examples
class UsersController {
@Get("/users")
getUsers() {}
@Post("/api/users")
createUser() {}
@Put("/users/:id")
updateUser() {}
@Delete("/users/:id")
deleteUser() {}
@Get()
getDefault() {}
@Get("/")
getRoot() {}
@Get("")
getEmpty() {}
}Invalid code examples
class UsersController {
@Get("users") // ❌ Missing leading slash
getUsers() {}
@Post("api/users") // ❌ Missing leading slash
createUser() {}
@Put("users/:id") // ❌ Missing leading slash
updateUser() {}
}Auto-fix
This rule is auto-fixable. ESLint can automatically fix errors by adding the leading slash:
// Before
@Get('users')
// After (auto-fix)
@Get('/users')Supported decorators
@Get()@Post()@Put()@Patch()@Delete()@Options()@Head()@All()
Special cases
- Routes without arguments: If the decorator has no arguments (e.g.,
@Get()), it is considered valid since NestJS uses'/'by default. - Empty routes: Empty routes (
'') or with only/are considered valid. - Template literals: Simple template literals without variables are supported (e.g.,
`users`).
License
MIT
Contributing
Contributions are welcome. Please open an issue or a pull request in the repository.
