-
Notifications
You must be signed in to change notification settings - Fork 0
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
thiagomini
merged 14 commits into
main
from
feat/rule-for-correct-usage-of-inject-decorator
Jan 16, 2024
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e64db3
feat: rule warns when token duplicates type
thiagomini 4e126cc
feat: rule accepts when injected token and type are different
thiagomini d4546ea
feat: rule rejects constructor parameter that is an interface and has…
thiagomini c42a939
test: inject decorator in class property is valid
thiagomini 63a526a
test: interface with injected token is valid
thiagomini 5583a31
chore: add failing test
thiagomini b6ae912
fix: check inject token for each node
thiagomini 23b7124
refactor: extract inject decorator function
thiagomini 26d2f74
chore: remove todo
thiagomini d92c831
Merge branch 'main' into feat/rule-for-correct-usage-of-inject-decorator
thiagomini 0b38c5a
refactor: rename utility functions
thiagomini def82c6
Merge branch 'feat/rule-for-correct-usage-of-inject-decorator' of git…
thiagomini 2eb284d
chore: add new rule to plugin
thiagomini 8d14e2b
docs: add doc for check inject decorator
thiagomini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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, | ||
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, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains hidden or 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,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)`? | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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