-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): added new rule no-untyped-public-signature #801
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
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
ff6fae2
fix prefer-readonly rule TypeError when class has computed member exp…
eranshabi 3e60496
fix lint
eranshabi 24a8dd1
chore: tighter linting (#535)
bradzacher e649022
feat(typescript-estree)!: throw error on file not in project when `pr…
uniqueiniquity eb5c6c3
test: ensure integration tests can fail, add vue-sfc (#768)
JamesHenry fcd1d27
docs: fix typo in documentation for explicit-function-return-type (#772)
d0c1171
fix(eslint-plugin): [no-explicit-any] Fix ignoreRestArgs for interfac…
Dilatorily 067b938
docs(eslint-plugin): Improve ban-types description (#773)
glenwinters 44aef10
docs(prefer-readonly): add rule name to title (#779)
cherryblossom000 f0cdb5a
fix(eslint-plugin): [typedef] support default value for parameter (#785)
octogonz 1af3102
fix(eslint-plugin): [typedef] support "for..in", "for..of" (#787)
octogonz fe533f3
Merge pull request #2 from typescript-eslint/master
eranshabi 5a83f2f
add rule no-untyped-public-signature
eranshabi 519560f
add ignoredMethods option to no-untyped-public-signature
eranshabi ce19131
Merge branch 'master' into master
eranshabi 3860608
Merge branch 'master' into master
eranshabi 7a3ff11
Merge branch 'master' into master
eranshabi e9b1d15
Merge branch 'master' into master
eranshabi e037488
fix format
eranshabi 1d98ad3
Merge branch 'master' into master
eranshabi 43db5b3
Merge branch 'master' into master
eranshabi 3071a9c
Merge branch 'master' into master
eranshabi dcf12ef
Merge branch 'master' into master
eranshabi 6a782ba
fix lint
eranshabi dcab7e7
format
eranshabi 05761ab
Merge branch 'master' into master
eranshabi aa7a7b2
Merge branch 'master' into master
eranshabi 86dc356
Merge branch 'master' into master
eranshabi 74ba76a
Merge branch 'master' into master
eranshabi 4773b5f
Merge branch 'master' into master
eranshabi a4677b0
fix some code review feedback
eranshabi b49f298
support new cases
eranshabi 1699283
Merge branch 'master' into master
eranshabi 6338cf3
test support for abstract method
eranshabi 886c2c1
Merge branch 'master' into master
eranshabi f1bac82
Merge branch 'master' into master
bradzacher 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
57 changes: 57 additions & 0 deletions
57
packages/eslint-plugin/docs/rules/no-untyped-public-signature.md
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,57 @@ | ||
# Disallow untyped public methods (no-untyped-public-signature) | ||
|
||
public methods are meant to be used by code outside of your class. By typing both the parameters and the return type of public methods they will be more readable and easy to use. | ||
|
||
## Rule Details | ||
|
||
This rule aims to ensure that only typed public methods are declared in the code. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```ts | ||
// untyped parameter | ||
public foo(param1): void { | ||
} | ||
|
||
// untyped parameter | ||
public foo(param1: any): void { | ||
} | ||
|
||
// untyped return type | ||
public foo(param1: string) { | ||
} | ||
|
||
// untyped return type | ||
public foo(param1: string): any { | ||
} | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```ts | ||
// typed public method | ||
public foo(param1: string): void { | ||
} | ||
|
||
// untyped private method | ||
private foo(param1) { | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
This rule, in its default state, does not require any argument. | ||
|
||
### ignoredMethods | ||
|
||
You may pass method names you would like this rule to ignore, like so: | ||
|
||
```cjson | ||
{ | ||
"@typescript-eslint/no-untyped-public-signature": ["error", { "ignoredMethods": ["ignoredMethodName"] }] | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't wish to type public methods. |
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
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
120 changes: 120 additions & 0 deletions
120
packages/eslint-plugin/src/rules/no-untyped-public-signature.ts
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,120 @@ | ||
import * as util from '../util'; | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
|
||
type MessageIds = 'noReturnType' | 'untypedParameter'; | ||
|
||
type Options = [{ ignoredMethods: string[] }]; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'no-unused-public-signature', | ||
meta: { | ||
docs: { | ||
description: | ||
'Requires that all public method arguments and return type will be explicitly typed', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
noReturnType: 'Public method has no return type', | ||
untypedParameter: 'Public method parameters should be typed', | ||
}, | ||
schema: [ | ||
{ | ||
allowAdditionalProperties: false, | ||
properties: { | ||
ignoredMethods: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [{ ignoredMethods: [] }], | ||
create(context, [options]) { | ||
const ignoredMethods = new Set(options.ignoredMethods); | ||
|
||
function isPublicMethod( | ||
node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition, | ||
): boolean { | ||
return node.accessibility === 'public' || !node.accessibility; | ||
} | ||
|
||
function isIgnoredMethod( | ||
node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition, | ||
ignoredMethods: Set<string>, | ||
): boolean { | ||
if ( | ||
node.key.type === AST_NODE_TYPES.Literal && | ||
typeof node.key.value === 'string' | ||
) { | ||
return ignoredMethods.has(node.key.value); | ||
} | ||
if ( | ||
node.key.type === AST_NODE_TYPES.TemplateLiteral && | ||
node.key.expressions.length === 0 | ||
) { | ||
return ignoredMethods.has(node.key.quasis[0].value.raw); | ||
} | ||
if (node.key.type === AST_NODE_TYPES.Identifier && !node.computed) { | ||
return ignoredMethods.has(node.key.name); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isParamTyped(node: TSESTree.Identifier): boolean { | ||
return ( | ||
!!node.typeAnnotation && | ||
node.typeAnnotation.typeAnnotation.type !== AST_NODE_TYPES.TSAnyKeyword | ||
); | ||
} | ||
|
||
function isReturnTyped( | ||
node: TSESTree.TSTypeAnnotation | undefined, | ||
): boolean { | ||
if (!node) { | ||
return false; | ||
} | ||
return ( | ||
node.typeAnnotation && | ||
node.typeAnnotation.type !== AST_NODE_TYPES.TSAnyKeyword | ||
); | ||
} | ||
|
||
return { | ||
'TSAbstractMethodDefinition, MethodDefinition'( | ||
node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition, | ||
): void { | ||
if (isPublicMethod(node) && !isIgnoredMethod(node, ignoredMethods)) { | ||
const paramIdentifiers = node.value.params.filter( | ||
param => param.type === AST_NODE_TYPES.Identifier, | ||
) as TSESTree.Identifier[]; | ||
const identifiersHaveTypes = paramIdentifiers.every(isParamTyped); | ||
if (!identifiersHaveTypes) { | ||
context.report({ | ||
node, | ||
messageId: 'untypedParameter', | ||
data: {}, | ||
}); | ||
} | ||
|
||
if (!isReturnTyped(node.value.returnType)) { | ||
context.report({ | ||
node, | ||
messageId: 'noReturnType', | ||
data: {}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.