Skip to content

Commit

Permalink
feat(eslint-plugin): squash to first commit
Browse files Browse the repository at this point in the history
Add 'missingReturnType' check for arguments, add docs for 'allowDirectConstAssertionInArrowFunctions' option.
  • Loading branch information
Guy Perkal committed Dec 26, 2019
1 parent 237b197 commit 5df0e66
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/README.md
Expand Up @@ -107,7 +107,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
| [`@typescript-eslint/consistent-type-definitions`](./docs/rules/consistent-type-definitions.md) | Consistent with type definition either `interface` or `type` | | :wrench: | |
| [`@typescript-eslint/explicit-function-return-type`](./docs/rules/explicit-function-return-type.md) | Require explicit return types on functions and class methods | :heavy_check_mark: | | |
| [`@typescript-eslint/explicit-member-accessibility`](./docs/rules/explicit-member-accessibility.md) | Require explicit accessibility modifiers on class properties and methods | | | |
| [`@typescript-eslint/explicit-module-boundary-types`](./docs/rules/explicit-module-boundary-types.md) | Require explicit return types on exported functions' and classes' public class methods | | | |
| [`@typescript-eslint/explicit-module-boundary-types`](./docs/rules/explicit-module-boundary-types.md) | Require explicit return and argument types on exported functions' and classes' public class methods | | | |
| [`@typescript-eslint/func-call-spacing`](./docs/rules/func-call-spacing.md) | Require or disallow spacing between function identifiers and their invocations | | :wrench: | |
| [`@typescript-eslint/generic-type-naming`](./docs/rules/generic-type-naming.md) | Enforces naming of generic type variables | | | |
| [`@typescript-eslint/indent`](./docs/rules/indent.md) | Enforce consistent indentation | | :wrench: | |
Expand Down
@@ -1,4 +1,4 @@
# Require explicit return type on exported functions' and classes' public class methods (explicit-module-boundary-types)
# Require explicit return and argument types on exported functions' and classes' public class methods (explicit-module-boundary-types)

Explicit types for function return values makes it clear to any calling code what is the module boundary's output.

Expand Down Expand Up @@ -66,6 +66,8 @@ type Options = {
allowTypedFunctionExpressions?: boolean;
// if true, functions immediately returning another function expression will not be checked
allowHigherOrderFunctions?: boolean;
// if true, body-less arrow functions are allowed to return an object as const
allowDirectConstAssertionInArrowFunctions?: boolean;
// an array of function/method names that will not be checked
allowedNames?: string[];
};
Expand Down Expand Up @@ -165,7 +167,26 @@ export function fn() {
}
```

### `allowedName`
### `allowDirectConstAssertionInArrowFunctions`

Examples of additional **correct** code for this rule with `{ allowDirectConstAssertionInArrowFunctions: true }`:

```ts
export const func = (value: number) => ({ type: 'X', value } as const);
```

Examples of additional **incorrect** code for this rule with `{ allowDirectConstAssertionInArrowFunctions: true }`:

```ts
export const func = (value: number) => ({ type: 'X', value });
export const foo = () => {
return {
bar: true,
} as const;
};
```

### `allowedNames`

You may pass function/method names you would like this rule to ignore, like so:

Expand Down
28 changes: 26 additions & 2 deletions packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts
Expand Up @@ -13,20 +13,21 @@ type Options = [
allowedNames?: string[];
},
];
type MessageIds = 'missingReturnType';
type MessageIds = 'missingReturnType' | 'missingArgType';

export default util.createRule<Options, MessageIds>({
name: 'explicit-module-boundary-types',
meta: {
type: 'problem',
docs: {
description:
"Require explicit return types on exported functions' and classes' public class methods",
"Require explicit return and argument types on exported functions' and classes' public class methods",
category: 'Stylistic Issues',
recommended: false,
},
messages: {
missingReturnType: 'Missing return type on function.',
missingArgType: "Argument '{{name}}' should be typed.",
},
schema: [
{
Expand Down Expand Up @@ -324,6 +325,22 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): void {
const paramIdentifiers = node.params.filter(
param => param.type === AST_NODE_TYPES.Identifier,
) as TSESTree.Identifier[];
const untypedArgs = paramIdentifiers.filter(isArgumentUntyped);
if (untypedArgs.length) {
untypedArgs.forEach(untypedArg =>
context.report({
node,
messageId: 'missingArgType',
data: {
name: untypedArg.name,
},
}),
);
}

if (isAllowedName(node.parent)) {
return;
}
Expand Down Expand Up @@ -431,6 +448,13 @@ export default util.createRule<Options, MessageIds>({
return false;
}

function isArgumentUntyped(node: TSESTree.Identifier): boolean {
return (
!node.typeAnnotation ||
node.typeAnnotation.typeAnnotation.type === AST_NODE_TYPES.TSAnyKeyword
);
}

return {
ArrowFunctionExpression: checkFunctionExpressionReturnType,
FunctionDeclaration: checkFunctionReturnType,
Expand Down
Expand Up @@ -62,7 +62,7 @@ export class Test {
return 1;
}
set prop() {}
private method() {
private method(one) {
return;
}
arrow = (): string => 'arrow';
Expand Down Expand Up @@ -145,7 +145,7 @@ export class App {
filename: 'test.ts',
code: `
export const myObj = {
set myProp(val) {
set myProp(val: number) {
this.myProp = val;
},
};
Expand Down Expand Up @@ -252,7 +252,7 @@ export const func4 = (value: number) => x as const;
{
filename: 'test.ts',
code: `
export const func1 = (value) => value;
export const func1 = (value: string) => value;
export const func2 = (value: number) => ({ type: "X", value });
`,
options: [
Expand Down Expand Up @@ -365,7 +365,7 @@ export class Test {
method() {
return;
}
arrow = () => 'arrow';
arrow = (arg) => 'arrow';
private method() {
return;
}
Expand All @@ -386,12 +386,19 @@ export class Test {
column: 3,
endColumn: 11,
},
{
messageId: 'missingArgType',
line: 11,
endLine: 11,
column: 11,
endColumn: 27,
},
{
messageId: 'missingReturnType',
line: 11,
endLine: 11,
column: 11,
endColumn: 16,
endColumn: 19,
},
],
},
Expand Down Expand Up @@ -765,5 +772,31 @@ export const func2 = (value: number) => value;
},
],
},
{
filename: 'test.ts',
code: 'export function fn(test): string { return "123" };',
errors: [
{
messageId: 'missingArgType',
line: 1,
endLine: 1,
column: 8,
endColumn: 50,
},
],
},
{
filename: 'test.ts',
code: 'export const fn = (one: number, two): string => "123";',
errors: [
{
messageId: 'missingArgType',
line: 1,
endLine: 1,
column: 19,
endColumn: 54,
},
],
},
],
});

0 comments on commit 5df0e66

Please sign in to comment.