Skip to content

Commit

Permalink
feat: [no-unnecessary-type-assertion] allow as const arrow functions (
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzzen authored and JamesHenry committed Aug 19, 2019
1 parent b006667 commit 14c6f80
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
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,
},
],
},
],
});

0 comments on commit 14c6f80

Please sign in to comment.