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

fix(eslint-plugin): [explicit-module-boundary-types] and [explicit-function-return-type] don't report onas const satisfies#10315

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
Show file tree
Hide file tree
Changes fromall commits
Commits
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
29 changes: 17 additions & 12 deletionspackages/eslint-plugin/src/util/explicitReturnTypeUtils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -176,15 +176,12 @@ function doesImmediatelyReturnFunctionExpression({
/**
* Checks if a function belongs to:
* ```
* () => ({ action: 'xxx' } as const)
* ({ action: 'xxx' } as const)
* ```
*/
function returnsConstAssertionDirectly(
node: TSESTree.ArrowFunctionExpression,
): boolean {
const { body } = node;
if (isTypeAssertion(body)) {
const { typeAnnotation } = body;
function isConstAssertion(node: TSESTree.Node): boolean {
if (isTypeAssertion(node)) {
const { typeAnnotation } = node;
if (typeAnnotation.type === AST_NODE_TYPES.TSTypeReference) {
const { typeName } = typeAnnotation;
if (
Expand DownExpand Up@@ -256,11 +253,19 @@ function isValidFunctionExpressionReturnType(
}

// https://github.com/typescript-eslint/typescript-eslint/issues/653
return (
options.allowDirectConstAssertionInArrowFunctions === true &&
node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
returnsConstAssertionDirectly(node)
);
if (
!options.allowDirectConstAssertionInArrowFunctions ||
node.type !== AST_NODE_TYPES.ArrowFunctionExpression
) {
return false;
}

let body = node.body;
while (body.type === AST_NODE_TYPES.TSSatisfiesExpression) {
body = body.expression;
}

return isConstAssertion(body);
}

/**
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -443,6 +443,53 @@ const func = (value: number) => x as const;
},
{
code: `
interface R {
type: string;
value: number;
}

const func = (value: number) => ({ type: 'X', value }) as const satisfies R;
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: true,
},
],
},
{
code: `
interface R {
type: string;
value: number;
}

const func = (value: number) =>
({ type: 'X', value }) as const satisfies R satisfies R;
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: true,
},
],
},
{
code: `
interface R {
type: string;
value: number;
}

const func = (value: number) =>
({ type: 'X', value }) as const satisfies R satisfies R satisfies R;
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: true,
},
],
},
{
code: `
new Promise(resolve => {});
new Foo(1, () => {});
`,
Expand DownExpand Up@@ -1731,6 +1778,30 @@ const func = (value: number) => ({ type: 'X', value }) as const;
},
],
},
{
code: `
interface R {
type: string;
value: number;
}

const func = (value: number) => ({ type: 'X', value }) as const satisfies R;
`,
errors: [
{
column: 30,
endColumn: 32,
endLine: 7,
line: 7,
messageId: 'missingReturnType',
},
],
options: [
{
allowDirectConstAssertionInArrowFunctions: false,
},
],
},
{
code: 'const log = (message: string) => void console.log(message);',
errors: [
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -367,6 +367,54 @@ export const func4 = (value: number) => x as const;
},
{
code: `
interface R {
type: string;
value: number;
}

export const func = (value: number) =>
({ type: 'X', value }) as const satisfies R;
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: true,
},
],
},
{
code: `
interface R {
type: string;
value: number;
}

export const func = (value: number) =>
({ type: 'X', value }) as const satisfies R satisfies R;
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: true,
},
],
},
{
code: `
interface R {
type: string;
value: number;
}

export const func = (value: number) =>
({ type: 'X', value }) as const satisfies R satisfies R satisfies R;
`,
options: [
{
allowDirectConstAssertionInArrowFunctions: true,
},
],
},
{
code: `
export const func1 = (value: string) => value;
export const func2 = (value: number) => ({ type: 'X', value });
`,
Expand DownExpand Up@@ -1204,6 +1252,31 @@ export const func = (value: number) => ({ type: 'X', value }) as const;
},
{
code: `
interface R {
type: string;
value: number;
}

export const func = (value: number) =>
({ type: 'X', value }) as const satisfies R;
`,
errors: [
{
column: 37,
endColumn: 39,
endLine: 7,
line: 7,
messageId: 'missingReturnType',
},
],
options: [
{
allowDirectConstAssertionInArrowFunctions: false,
},
],
},
{
code: `
export class Test {
constructor() {}
get prop() {
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp