-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: add eslint rule for annotation checks
Inversify >=6.1 requires to annotate all constructor parameters of injectable classes. To avoid runtime errors we add a new eslint rule to check for this at lint time.
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
dev-packages/private-eslint-plugin/rules/annotation-check.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// @ts-check | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 EclipseSource and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
/** | ||
* @typedef {import('@typescript-eslint/utils').TSESTree.ClassDeclaration} ClassDeclaration | ||
* @typedef {import('@typescript-eslint/utils').TSESTree.ClassElement} ClassElement | ||
* @typedef {import('@typescript-eslint/utils').TSESTree.Decorator} Decorator | ||
* @typedef {import('@typescript-eslint/utils').TSESTree.MethodDefinition} MethodDefinition | ||
* @typedef {import('@typescript-eslint/utils').TSESTree.Parameter} Parameter | ||
* @typedef {import('estree').Node} Node | ||
* @typedef {import('eslint').Rule.RuleModule} RuleModule | ||
*/ | ||
|
||
/** | ||
* Type guard to check if a ClassElement is a MethodDefinition. | ||
* @param {ClassElement} element | ||
* @returns {element is MethodDefinition} | ||
*/ | ||
function isMethodDefinition(element) { | ||
return element.type === 'MethodDefinition'; | ||
} | ||
|
||
/** @type {RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Ensure @injectable classes have annotated constructor parameters', | ||
}, | ||
messages: { | ||
missingAnnotation: 'Constructor parameters in an @injectable class must be annotated with @inject, @unmanaged or @multiInject', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
/** | ||
* @param {ClassDeclaration} node | ||
*/ | ||
ClassDeclaration(node) { | ||
// Check if the class has a decorator named `injectable` | ||
const hasInjectableDecorator = node.decorators?.some( | ||
(/** @type {Decorator} */ decorator) => | ||
decorator.expression.type === 'CallExpression' && | ||
decorator.expression.callee.type === 'Identifier' && | ||
decorator.expression.callee.name === 'injectable' | ||
); | ||
|
||
if (hasInjectableDecorator) { | ||
// Find the constructor method within the class body | ||
const constructor = node.body.body.find( | ||
member => | ||
isMethodDefinition(member) && | ||
member.kind === 'constructor' | ||
); | ||
|
||
if ( | ||
constructor && | ||
// We need to re-apply 'isMethodDefinition' here because the type guard is not properly preserved | ||
isMethodDefinition(constructor) && | ||
constructor.value && | ||
constructor.value.params.length > 0 | ||
) { | ||
constructor.value.params.forEach( | ||
/** @type {Parameter} */ param => { | ||
// Check if each constructor parameter has a decorator | ||
const hasAnnotation = param.decorators?.some( | ||
(/** @type {Decorator} */ decorator) => | ||
decorator.expression.type === 'CallExpression' && | ||
decorator.expression.callee.type === 'Identifier' && | ||
(decorator.expression.callee.name === 'inject' || | ||
decorator.expression.callee.name === 'unmanaged' || | ||
decorator.expression.callee.name === 'multiInject') | ||
); | ||
|
||
if (!hasAnnotation) { | ||
context.report({ | ||
node: /** @type Node */ (param), | ||
messageId: 'missingAnnotation', | ||
}); | ||
} | ||
} | ||
); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |