Skip to content

Commit

Permalink
feat(eslint-plugin): add support for object props in CallExpressions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher authored and JamesHenry committed Jul 24, 2019
1 parent fd6be42 commit 8141f01
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 53 deletions.
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
19 changes: 10 additions & 9 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
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 @@ -273,6 +273,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 @@ -281,7 +307,7 @@ new Accumulator().accumulate(() => 1);
function test() {
return;
}
`,
`,
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -296,7 +322,7 @@ function test() {
var fn = function() {
return 1;
};
`,
`,
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -309,7 +335,7 @@ var fn = function() {
filename: 'test.ts',
code: `
var arrowFn = () => 'test';
`,
`,
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -332,7 +358,7 @@ class Test {
}
arrow = () => 'arrow';
}
`,
`,
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -353,21 +379,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 @@ -379,7 +407,7 @@ class Test {
},
{
filename: 'test.ts',
code: `const foo = function() {};`,
code: 'const foo = function() {};',
options: [{ allowExpressions: true }],
errors: [
{
Expand All @@ -391,7 +419,7 @@ class Test {
},
{
filename: 'test.ts',
code: `var arrowFn = () => 'test';`,
code: "var arrowFn = () => 'test';",
options: [{ allowTypedFunctionExpressions: true }],
errors: [
{
Expand All @@ -403,7 +431,7 @@ class Test {
},
{
filename: 'test.ts',
code: `var funcExpr = function() { return 'test'; };`,
code: "var funcExpr = function() { return 'test'; };",
options: [{ allowTypedFunctionExpressions: true }],
errors: [
{
Expand All @@ -416,7 +444,7 @@ class Test {

{
filename: 'test.ts',
code: `const x = (() => {}) as Foo`,
code: 'const x = (() => {}) as Foo',
options: [{ allowTypedFunctionExpressions: false }],
errors: [
{
Expand Down Expand Up @@ -459,84 +487,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 @@ -566,14 +582,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 @@ -643,10 +657,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 @@ -656,8 +701,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

0 comments on commit 8141f01

Please sign in to comment.