Skip to content

feat: rule warns when token duplicates type #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/rules/check-inject-decorator.rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ESLintUtils } from '@typescript-eslint/utils';

const createRule = ESLintUtils.RuleCreator(
(name) => `https://eslint.org/docs/latest/rules/${name}`
);

export type MessageIds =
| 'tokenDuplicatesType'
| 'typeIsInterface'
| 'propertyMissingInject';

const defaultOptions: unknown[] = [];

export default createRule({
name: 'check-inject-decorator',
meta: {
type: 'problem',
docs: {
description: 'Ensure proper use of the @Inject decorator',
recommended: 'recommended',
},
fixable: undefined,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this rule fixable in some cases would be nice too, @tuxmachine - but probably a work for another issue, since it's a bit harder to do it

schema: [], // no options
messages: {
tokenDuplicatesType: '⚠️ Token duplicates type',
typeIsInterface: '⚠️ Type is an interface and cannot be injected',
propertyMissingInject: '⚠️ Did you want to `@Inject({{type}})`?',
},
},
defaultOptions,
create(context) {
return {
// Matches code that defines a variable of type TestingModule
// e.g. `let testingModule: TestingModule;`
'Program:exit': (node) => {
context.report({
node,
messageId: 'tokenDuplicatesType',
loc: node.loc,
});
},
};
},
});
47 changes: 47 additions & 0 deletions tests/rules/check-inject-decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { RuleTester } from '@typescript-eslint/rule-tester';
import checkInjectDecorator from '../../src/rules/check-inject-decorator.rule';

// This test required changes to the tsconfig file to allow importing from the rule-tester package.
// See https://github.com/typescript-eslint/typescript-eslint/issues/7284

const ruleTester = new RuleTester({
parserOptions: {
project: './tsconfig.json',
},
parser: '@typescript-eslint/parser',
defaultFilenames: {
// We need to specify a filename that will be used by the rule parser.
// Since the test process starts at the root of the project, we need to point to the sub folder containing it.
ts: './tests/rules/file.ts',
tsx: '',
},
});

ruleTester.run('check-inject-decorator', checkInjectDecorator, {
valid: [
// TODO: add following tests:
// 1 - 🆗 Type is a class and was injected in the constructor
// 2 - 🆗 Token differs from type
// 3 - 🆗 Token duplicates type, but class properties are not injected automatically
],
invalid: [
{
code: `
class FooBarController {
constructor(
@Inject(BazService) // ⚠️ Token duplicates type
private readonly bazService: BazService,
) {}
}
`,
errors: [
{
messageId: 'tokenDuplicatesType',
},
],
},
// TODO: add following tests:
// 1 - 🚫 Type is an interface and cannot be injected
// 2 - private readonly fooService: FooService; // ⚠️ Did you want to `@Inject(FooService)`?
],
});