Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [explicit-function-return-type] add allowIIFEs option#6237

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 16 commits intotypescript-eslint:mainfromyeonjuan:fix/#6115
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
dd7e799
feat(eslint-plugin): [explicit-function-return-type] add allowIIFEs o…
yeonjuanDec 17, 2022
3f8a981
remove useless code
yeonjuanDec 17, 2022
a242fb7
fix tests
yeonjuanDec 17, 2022
75a520d
apply reviews
yeonjuanDec 18, 2022
28f2a6a
remove useless lint
yeonjuanDec 18, 2022
0739ae5
add tc
yeonjuanDec 18, 2022
d3e168c
fix
yeonjuanDec 18, 2022
47cf0c5
Merge branch 'main' into fix/#6115
yeonjuanDec 23, 2022
a6fba09
add test cases
yeonjuanDec 26, 2022
9c7da1b
Merge branch 'fix/#6115-temp' into fix/#6115
yeonjuanDec 26, 2022
10f64e0
Merge branch 'main' into fix/#6115
yeonjuanFeb 12, 2023
7affa5b
fix
yeonjuanFeb 12, 2023
2657eb6
fix
yeonjuanFeb 12, 2023
0af3c21
add test cases
yeonjuanFeb 12, 2023
1c702dc
add test case
yeonjuanFeb 12, 2023
f18a367
Update packages/eslint-plugin/tests/rules/explicit-function-return-ty…
JoshuaKGoldbergFeb 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -257,6 +257,26 @@ You may pass function/method names you would like this rule to ignore, like so:
}
```

### `allowIIFE`

Examples of code for this rule with `{ allowIIFE: true }`:

#### ❌ Incorrect

```ts
var func = () => 'foo';
```

#### ✅ Correct

```ts
var foo = (() => 'foo')();

var bar = (function () {
return 'bar';
})();
```

## When Not To Use It

If you don't wish to prevent calling code from using function return values in unexpected ways, then
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,6 +17,7 @@ type Options = [
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
allowFunctionsWithoutTypeParameters?: boolean;
allowedNames?: string[];
allowIIFEs?: boolean;
},
];
type MessageIds = 'missingReturnType';
Expand DownExpand Up@@ -75,6 +76,11 @@ export default util.createRule<Options, MessageIds>({
},
type: 'array',
},
allowIIFEs: {
description:
'Whether to ignore immediately invoked function expressions (IIFEs).',
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All@@ -88,6 +94,7 @@ export default util.createRule<Options, MessageIds>({
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
allowedNames: [],
allowIIFEs: false,
},
],
create(context, [options]) {
Expand All@@ -102,6 +109,10 @@ export default util.createRule<Options, MessageIds>({
return true;
}

if (options.allowIIFEs && isIIFE(node)) {
return true;
}

if (!options.allowedNames?.length) {
return false;
}
Expand DownExpand Up@@ -149,6 +160,16 @@ export default util.createRule<Options, MessageIds>({
}
return false;
}

function isIIFE(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression
| TSESTree.FunctionDeclaration,
): boolean {
return node.parent!.type === AST_NODE_TYPES.CallExpression;
}

return {
'ArrowFunctionExpression, FunctionExpression'(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -595,6 +595,132 @@ const x: Bar<Foo> = arg1 => arg2 => arg1 + arg2;
},
],
},
{
filename: 'test.ts',
code: `
let foo = function (): number {
return 1;
};
`,
options: [
{
allowIIFEs: true,
},
],
},
{
filename: 'test.ts',
code: `
const foo = (function () {
return 1;
})();
`,
options: [
{
allowIIFEs: true,
},
],
},
{
filename: 'test.ts',
code: `
const foo = (() => {
return 1;
})();
`,
options: [
{
allowIIFEs: true,
},
],
},
{
filename: 'test.ts',
code: `
const foo = ((arg: number): number => {
return arg;
})(0);
`,
options: [
{
allowIIFEs: true,
},
],
},
{
filename: 'test.ts',
code: `
const foo = (() => (() => 'foo')())();
`,
options: [
{
allowIIFEs: true,
},
],
},
{
filename: 'test.ts',
code: `
let foo = (() => (): string => {
return 'foo';
})()();
`,
options: [
{
allowIIFEs: true,
},
],
},
{
filename: 'test.ts',
code: `
let foo = (() => (): string => {
return 'foo';
})();
`,
options: [
{
allowIIFEs: true,
allowHigherOrderFunctions: false,
},
],
},
{
filename: 'test.ts',
code: `
let foo = (() => (): string => {
return 'foo';
})()();
`,
options: [
{
allowIIFEs: true,
allowHigherOrderFunctions: true,
},
],
},
{
filename: 'test.ts',
code: `
let foo = (() => (): void => {})()();
`,
options: [
{
allowIIFEs: true,
},
],
},
{
filename: 'test.ts',
code: `
let foo = (() => (() => {})())();
`,
options: [
{
allowIIFEs: true,
},
],
},
],
invalid: [
{
Expand DownExpand Up@@ -1477,5 +1603,93 @@ class Foo {
},
],
},
{
filename: 'test.ts',
code: `
const foo = (function () {
return 'foo';
})();
`,
options: [
{
allowIIFEs: false,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 2,
endLine: 2,
column: 14,
endColumn: 25,
},
],
},
{
filename: 'test.ts',
code: `
const foo = (function () {
return () => {
return 1;
};
})();
`,
options: [
{
allowIIFEs: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 3,
endLine: 3,
column: 10,
endColumn: 15,
},
],
},
{
filename: 'test.ts',
code: `
let foo = function () {
return 'foo';
};
`,
options: [
{
allowIIFEs: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 2,
endLine: 2,
column: 11,
endColumn: 22,
},
],
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Let's add a bit more test coverage:

  • IIFEs with return types
  • Multiple levels of IIFEs (e.g.(() => () => {})()())
  • Arguments & parameters

{
filename: 'test.ts',
code: `
let foo = (() => () => {})()();
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@JoshuaKGoldberg
How should we handle multiple levels of IIFEs? For now, it allows only if the function is called directly.

constfoo=(()=>()=>"foo")()();// errorconstbar=(()=>(()=>"foo")())();// pass

Should both cases be passed when theallowIIFEs option is true?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@yeonjuan The first one IMO is not an "IIFE" by definition—(() => () => "foo")() is an expression that happens to evaluate to a function, not a function expression itself. It's fine if we don't handle it.

yeonjuan, bradzacher, and JoshuaKGoldberg reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I would agree with that assessment.
The inner function is not an IIFE, just the outer function.

`,
options: [
{
allowIIFEs: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 2,
endLine: 2,
column: 18,
endColumn: 23,
},
],
},
],
});

[8]ページ先頭

©2009-2025 Movatter.jp