Skip to content
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

feat(eslint-plugin): [explicit-function-return-type] allow as const arrow functions #876

Merged
merged 3 commits into from Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -9,6 +9,7 @@ type Options = [
allowExpressions?: boolean;
allowTypedFunctionExpressions?: boolean;
allowHigherOrderFunctions?: boolean;
allowDirectConstAssertionInArrowFunctions?: boolean;
},
];
type MessageIds = 'missingReturnType';
Expand Down Expand Up @@ -39,6 +40,9 @@ export default util.createRule<Options, MessageIds>({
allowHigherOrderFunctions: {
type: 'boolean',
},
allowDirectConstAssertionInArrowFunctions: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -49,6 +53,7 @@ export default util.createRule<Options, MessageIds>({
allowExpressions: false,
allowTypedFunctionExpressions: true,
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
},
],
create(context, [options]) {
Expand Down Expand Up @@ -203,6 +208,30 @@ export default util.createRule<Options, MessageIds>({
);
}

/**
* Checks if a function belongs to:
* `() => ({ action: 'xxx' }) as const`
*/
function returnsConstAssertionDirectly(
node: TSESTree.ArrowFunctionExpression,
): boolean {
const { body } = node;
if (body.type === AST_NODE_TYPES.TSAsExpression) {
const { typeAnnotation } = body;
if (typeAnnotation.type === AST_NODE_TYPES.TSTypeReference) {
const { typeName } = typeAnnotation;
if (
typeName.type === AST_NODE_TYPES.Identifier &&
typeName.name === 'const'
) {
return true;
}
}
}

return false;
}

/**
* Checks if a function declaration/expression has a return type.
*/
Expand Down Expand Up @@ -263,6 +292,15 @@ export default util.createRule<Options, MessageIds>({
}
}

// https://github.com/typescript-eslint/typescript-eslint/issues/653
if (
node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
options.allowDirectConstAssertionInArrowFunctions &&
returnsConstAssertionDirectly(node)
) {
return;
}

checkFunctionReturnType(node);
}

Expand Down
Expand Up @@ -308,6 +308,20 @@ foo({
},
],
},
{
filename: 'test.ts',
code: `
const func = (value: number) => (({ type: "X", value }) as const);
const func = (value: number) => ({ type: "X", value } as const);
const func = (value: number) => (x as const);
const func = (value: number) => x as const;
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: true,
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -749,5 +763,47 @@ foo({
},
],
},
{
filename: 'test.ts',
code: `
const func = (value: number) => ({ type: "X", value } as any);
const func = (value: number) => ({ type: "X", value } as Action);
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 2,
column: 14,
},
{
messageId: 'missingReturnType',
line: 3,
column: 14,
},
],
},
{
filename: 'test.ts',
code: `
const func = (value: number) => ({ type: "X", value } as const);
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: false,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 2,
column: 14,
},
],
},
],
});