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 allowedNames#4440

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
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
15 commits
Select commitHold shift + click to select a range
5e2c4fd
feat(eslint-plugin): [explicit-function-return-type] add allowedNames…
islandryuJan 14, 2022
f931a38
feat(eslint-plugin): [explicit-function-return-type] afix typecheck
islandryuJan 14, 2022
edd64e8
Merge branch 'main'
islandryuJan 24, 2022
b383ff6
Update packages/eslint-plugin/src/rules/explicit-function-return-type.ts
islandryuJan 24, 2022
f48f8d7
Merge branch 'featAddallowedNames' of https://github.com/islandryu/ty…
islandryuJan 24, 2022
f03d6e2
feat(eslint-plugin): [explicit-function-return-type] Change to allowe…
islandryuJan 26, 2022
2728975
feat(eslint-plugin): [explicit-function-return-type] Change to allowe…
islandryuJan 26, 2022
690986d
fix(eslint-plugin): [explicit-function-return-type] fix for codecov
islandryuJan 26, 2022
1e38d1d
fix(eslint-plugin): [explicit-function-return-type] add test
islandryuJan 26, 2022
d8d39e6
fix(eslint-plugin): [explicit-function-return-type] add test
islandryuJan 26, 2022
39d6715
fix(eslint-plugin): [explicit-function-return-type] fix for codecov
islandryuJan 29, 2022
a79ac5f
Merge branch 'main' into featAddallowedNames
Jan 29, 2022
8d66aa2
Merge branch 'main' into featAddallowedNames
islandryuJan 31, 2022
545b2ab
fix(eslint-plugin): [explicit-function-return-type] Change allowedNam…
islandryuFeb 1, 2022
65103ff
Merge branch 'featAddallowedNames' of https://github.com/islandryu/ty…
islandryuFeb 1, 2022
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@@ -77,6 +77,10 @@ type Options = {
allowDirectConstAssertionInArrowFunctions?: boolean;
// if true, concise arrow functions that start with the void keyword will not be checked
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
/**
* An array of function/method names that will not have their arguments or their return values checked.
*/
allowedNames?: string[];
};

const defaults = {
Expand All@@ -85,6 +89,7 @@ const defaults = {
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
allowedNames: [],
};
```

Expand DownExpand Up@@ -262,6 +267,21 @@ const log = (message: string) => {
var log = (message: string) => void console.log(message);
```

### `allowedNames`

You may pass function/method names you would like this rule to ignore, like so:

```json
{
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}
```

## 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@@ -13,6 +13,7 @@ type Options = [
allowHigherOrderFunctions?: boolean;
allowDirectConstAssertionInArrowFunctions?: boolean;
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
allowedNames?: string[];
},
];
type MessageIds = 'missingReturnType';
Expand DownExpand Up@@ -48,6 +49,12 @@ export default util.createRule<Options, MessageIds>({
allowConciseArrowFunctionExpressionsStartingWithVoid: {
type: 'boolean',
},
allowedNames: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
Expand All@@ -60,11 +67,64 @@ export default util.createRule<Options, MessageIds>({
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
allowedNames: [],
},
],
create(context, [options]) {
const sourceCode = context.getSourceCode();
function isAllowedName(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression
| TSESTree.FunctionDeclaration,
): boolean {
if (!options.allowedNames || !options.allowedNames.length) {
return false;
}

if (
node.type === AST_NODE_TYPES.ArrowFunctionExpression ||
node.type === AST_NODE_TYPES.FunctionExpression
) {
const parent = node.parent;
let funcName;
if (node.id?.name) {
funcName = node.id.name;
} else if (parent) {
switch (parent.type) {
case AST_NODE_TYPES.VariableDeclarator: {
if (parent.id.type === AST_NODE_TYPES.Identifier) {
funcName = parent.id.name;
}
break;
}
case AST_NODE_TYPES.MethodDefinition:
case AST_NODE_TYPES.PropertyDefinition:
case AST_NODE_TYPES.Property: {
if (
parent.key.type === AST_NODE_TYPES.Identifier &&
parent.computed === false
) {
funcName = parent.key.name;
}
break;
}
}
}
if (!!funcName && !!options.allowedNames.includes(funcName)) {
return true;
}
}
if (
node.type === AST_NODE_TYPES.FunctionDeclaration &&
node.id &&
node.id.type === AST_NODE_TYPES.Identifier &&
!!options.allowedNames.includes(node.id.name)
) {
return true;
}
return false;
}
return {
'ArrowFunctionExpression, FunctionExpression'(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
Expand All@@ -79,6 +139,10 @@ export default util.createRule<Options, MessageIds>({
return;
}

if (isAllowedName(node)) {
return;
}

if (
options.allowTypedFunctionExpressions &&
(isValidFunctionExpressionReturnType(node, options) ||
Expand All@@ -96,6 +160,9 @@ export default util.createRule<Options, MessageIds>({
);
},
FunctionDeclaration(node): void {
if (isAllowedName(node)) {
return;
}
if (options.allowTypedFunctionExpressions && node.returnType) {
return;
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -400,6 +400,99 @@ new Foo(1, () => {});
code: 'const log = (message: string) => void console.log(message);',
options: [{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }],
},
{
filename: 'test.ts',
options: [
{
allowedNames: ['test1', 'test2'],
},
],
code: `
function test1() {
return;
}

const foo = function test2() {
return;
};
`,
},
{
filename: 'test.ts',
options: [
{
allowedNames: ['test1', 'test2'],
},
],
code: `
const test1 = function () {
return;
};
const foo = function () {
return function test2() {};
};
`,
},
{
filename: 'test.ts',
options: [
{
allowedNames: ['test1', 'test2'],
},
],
code: `
const test1 = () => {
return;
};
export const foo = {
test2() {
return 0;
},
};
`,
},
{
filename: 'test.ts',
code: `
class Test {
constructor() {}
get prop() {
return 1;
}
set prop() {}
method() {
return;
}
arrow = () => 'arrow';
private method() {
return;
}
}
`,
options: [
{
allowedNames: ['prop', 'method', 'arrow'],
},
],
},
{
filename: 'test.ts',
code: `
const x = {
arrowFn: () => {
return;
},
fn: function () {
return;
},
};
`,
options: [
{
allowedNames: ['arrowFn', 'fn'],
},
],
},
{
filename: 'test.ts',
code: `
Expand DownExpand Up@@ -1226,5 +1319,100 @@ const func = (value: number) => ({ type: 'X', value } as const);
},
],
},
{
filename: 'test.ts',
options: [
{
allowedNames: ['test', '1'],
},
],
code: `
function hoge() {
return;
}
const foo = () => {
return;
};
const baz = function () {
return;
};
let [test, test] = function () {
return;
};
class X {
[test] = function () {
return;
};
}
const x = {
1: function () {
reutrn;
},
};
`,
errors: [
{
messageId: 'missingReturnType',
line: 2,
endLine: 2,
column: 1,
endColumn: 16,
},
{
messageId: 'missingReturnType',
line: 5,
endLine: 5,
column: 13,
endColumn: 18,
},
{
messageId: 'missingReturnType',
line: 8,
endLine: 8,
column: 13,
endColumn: 24,
},
{
messageId: 'missingReturnType',
line: 11,
endLine: 11,
column: 20,
endColumn: 31,
},
{
line: 15,
column: 12,
messageId: 'missingReturnType',
endLine: 15,
endColumn: 23,
},
{
messageId: 'missingReturnType',
line: 20,
endLine: 20,
column: 6,
endColumn: 17,
},
],
},
{
filename: 'test.ts',
code: `
const ignoredName = 'notIgnoredName';
class Foo {
[ignoredName]() {}
}
`,
options: [{ allowedNames: ['ignoredName'] }],
errors: [
{
messageId: 'missingReturnType',
line: 4,
endLine: 4,
column: 3,
endColumn: 18,
},
],
},
],
});

[8]ページ先頭

©2009-2025 Movatter.jp