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): add support for object props in CallExpressions #728

Merged
merged 5 commits into from Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -141,6 +141,13 @@ let objectPropCast = <ObjectType>{

declare functionWithArg(arg: () => number);
functionWithArg(() => 1);

declare functionWithObjectArg(arg: { meth: () => number });
functionWithObjectArg({
meth() {
return 1;
},
});
```

### allowHigherOrderFunctions
Expand Down
21 changes: 11 additions & 10 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -9,7 +9,7 @@ type Options = [
allowExpressions?: boolean;
allowTypedFunctionExpressions?: boolean;
allowHigherOrderFunctions?: boolean;
}
},
];
type MessageIds = 'missingReturnType';

Expand Down Expand Up @@ -118,28 +118,29 @@ export default util.createRule<Options, MessageIds>({
* `const x = <Foo>{ prop: () => {} }`
*/
function isPropertyOfObjectWithType(
parent: TSESTree.Node | undefined,
property: TSESTree.Node | undefined,
): boolean {
if (!parent || parent.type !== AST_NODE_TYPES.Property) {
if (!property || property.type !== AST_NODE_TYPES.Property) {
return false;
}
parent = parent.parent; // this shouldn't happen, checking just in case
const objectExpr = property.parent; // this shouldn't happen, checking just in case
/* istanbul ignore if */ if (
!parent ||
parent.type !== AST_NODE_TYPES.ObjectExpression
!objectExpr ||
objectExpr.type !== AST_NODE_TYPES.ObjectExpression
) {
return false;
}

parent = parent.parent; // this shouldn't happen, checking just in case
const parent = objectExpr.parent; // this shouldn't happen, checking just in case
/* istanbul ignore if */ if (!parent) {
return false;
}

return (
isTypeCast(parent) ||
isClassPropertyWithTypeAnnotation(parent) ||
isVariableDeclaratorWithTypeAnnotation(parent)
isVariableDeclaratorWithTypeAnnotation(parent) ||
isFunctionArgument(parent)
);
}

Expand Down Expand Up @@ -193,12 +194,12 @@ export default util.createRule<Options, MessageIds>({
*/
function isFunctionArgument(
parent: TSESTree.Node,
child: TSESTree.Node,
callee?: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
): boolean {
return (
parent.type === AST_NODE_TYPES.CallExpression &&
// make sure this isn't an IIFE
parent.callee !== child
parent.callee !== callee
);
}

Expand Down
Expand Up @@ -281,6 +281,32 @@ new Accumulator().accumulate(() => 1);
},
],
},
{
filename: 'test.ts',
code: `
declare function foo(arg: { meth: () => number }): void
foo({
meth() {
return 1;
},
})
foo({
meth: function () {
return 1;
},
})
foo({
meth: () => {
return 1;
},
})
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
},
],
invalid: [
{
Expand All @@ -289,7 +315,7 @@ new Accumulator().accumulate(() => 1);
function test() {
return;
}
`,
`,
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -304,7 +330,7 @@ function test() {
var fn = function() {
return 1;
};
`,
`,
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -317,7 +343,7 @@ var fn = function() {
filename: 'test.ts',
code: `
var arrowFn = () => 'test';
`,
`,
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -340,7 +366,7 @@ class Test {
}
arrow = () => 'arrow';
}
`,
`,
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -361,21 +387,23 @@ class Test {
},
{
filename: 'test.ts',
code: `function test() {
return;
}`,
code: `
function test() {
return;
}
`,
options: [{ allowExpressions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 1,
line: 2,
column: 1,
},
],
},
{
filename: 'test.ts',
code: `const foo = () => {};`,
code: 'const foo = () => {};',
options: [{ allowExpressions: true }],
errors: [
{
Expand All @@ -387,7 +415,7 @@ class Test {
},
{
filename: 'test.ts',
code: `const foo = function() {};`,
code: 'const foo = function() {};',
options: [{ allowExpressions: true }],
errors: [
{
Expand All @@ -399,7 +427,7 @@ class Test {
},
{
filename: 'test.ts',
code: `var arrowFn = () => 'test';`,
code: "var arrowFn = () => 'test';",
options: [{ allowTypedFunctionExpressions: true }],
errors: [
{
Expand All @@ -411,7 +439,7 @@ class Test {
},
{
filename: 'test.ts',
code: `var funcExpr = function() { return 'test'; };`,
code: "var funcExpr = function() { return 'test'; };",
options: [{ allowTypedFunctionExpressions: true }],
errors: [
{
Expand All @@ -424,7 +452,7 @@ class Test {

{
filename: 'test.ts',
code: `const x = (() => {}) as Foo`,
code: 'const x = (() => {}) as Foo',
options: [{ allowTypedFunctionExpressions: false }],
errors: [
{
Expand Down Expand Up @@ -467,84 +495,72 @@ const x: Foo = {
},
{
filename: 'test.ts',
code: `
() => () => {};
`,
code: '() => () => {};',
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
line: 1,
column: 7,
},
],
},
{
filename: 'test.ts',
code: `
() => function () {};
`,
code: '() => function () {};',
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
line: 1,
column: 7,
},
],
},
{
filename: 'test.ts',
code: `
() => { return () => {} };
`,
code: '() => { return () => {} };',
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
line: 1,
column: 16,
},
],
},
{
filename: 'test.ts',
code: `
() => { return function () {} };
`,
code: '() => { return function () {} };',
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
line: 1,
column: 16,
},
],
},
{
filename: 'test.ts',
code: `
function fn() { return () => {} };
`,
code: 'function fn() { return () => {} };',
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
line: 1,
column: 24,
},
],
},
{
filename: 'test.ts',
code: `
function fn() { return function () {} };
`,
code: 'function fn() { return function () {} };',
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
line: 1,
column: 24,
},
],
Expand Down Expand Up @@ -574,14 +590,12 @@ function FunctionDeclaration() {
},
{
filename: 'test.ts',
code: `
() => () => { return () => { return; } };
`,
code: '() => () => { return () => { return; } };',
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
line: 1,
column: 22,
},
],
Expand Down Expand Up @@ -651,10 +665,41 @@ new Accumulator().accumulate(() => 1);
},
],
},
{
filename: 'test.ts',
code: '(() => true)()',
options: [
{
allowTypedFunctionExpressions: false,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 1,
column: 2,
},
],
},
{
filename: 'test.ts',
code: `
(() => true)()
declare function foo(arg: { meth: () => number }): void
foo({
meth() {
return 1;
},
})
foo({
meth: function () {
return 1;
},
})
foo({
meth: () => {
return 1;
},
})
`,
options: [
{
Expand All @@ -664,8 +709,18 @@ new Accumulator().accumulate(() => 1);
errors: [
{
messageId: 'missingReturnType',
line: 2,
column: 2,
line: 4,
column: 7,
},
{
messageId: 'missingReturnType',
line: 9,
column: 9,
},
{
messageId: 'missingReturnType',
line: 14,
column: 9,
},
],
},
Expand Down