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): [no-misused-spread] add suggestions#10719

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 12 commits intotypescript-eslint:mainfromyeonjuan:fix/10642
Feb 24, 2025
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
74 changes: 72 additions & 2 deletionspackages/eslint-plugin/src/rules/no-misused-spread.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';
import type {TSESLint,TSESTree } from '@typescript-eslint/utils';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

Expand All@@ -9,11 +10,13 @@ import {
createRule,
getConstrainedTypeAtLocation,
getParserServices,
getWrappingFixer,
isBuiltinSymbolLike,
isPromiseLike,
isTypeFlagSet,
readonlynessOptionsSchema,
typeMatchesSomeSpecifier,
isHigherPrecedenceThanAwait,
} from '../util';

type Options = [
Expand All@@ -23,14 +26,16 @@ type Options = [
];

type MessageIds =
| 'addAwait'
| 'noArraySpreadInObject'
| 'noClassDeclarationSpreadInObject'
| 'noClassInstanceSpreadInObject'
| 'noFunctionSpreadInObject'
| 'noIterableSpreadInObject'
| 'noMapSpreadInObject'
| 'noPromiseSpreadInObject'
| 'noStringSpread';
| 'noStringSpread'
| 'replaceMapSpreadInObject';

export default createRule<Options, MessageIds>({
name: 'no-misused-spread',
Expand All@@ -42,7 +47,9 @@ export default createRule<Options, MessageIds>({
recommended: 'strict',
requiresTypeChecking: true,
},
hasSuggestions: true,
messages: {
addAwait: 'Add await operator.',
noArraySpreadInObject:
'Using the spread operator on an array in an object will result in a list of indices.',
noClassDeclarationSpreadInObject:
Expand All@@ -64,6 +71,8 @@ export default createRule<Options, MessageIds>({
'Consider using `Intl.Segmenter` for locale-aware string decomposition.',
"Otherwise, if you don't need to preserve emojis or other non-Ascii characters, disable this lint rule on this line or configure the 'allow' rule option.",
].join('\n'),
replaceMapSpreadInObject:
'Replace map spread in object with `Object.fromEntries()`',
},
schema: [
{
Expand DownExpand Up@@ -104,6 +113,65 @@ export default createRule<Options, MessageIds>({
}
}

function getMapSpreadSuggestions(
node: TSESTree.JSXSpreadAttribute | TSESTree.SpreadElement,
type: ts.Type,
): TSESLint.ReportSuggestionArray<MessageIds> | null {
const types = tsutils.unionTypeParts(type);
if (types.some(t => !isMap(services.program, t))) {
return null;
}

if (
node.parent.type === AST_NODE_TYPES.ObjectExpression &&
node.parent.properties.length === 1
) {
return [
{
messageId: 'replaceMapSpreadInObject',
fix: getWrappingFixer({
node: node.parent,
innerNode: node.argument,
sourceCode: context.sourceCode,
wrap: code => `Object.fromEntries(${code})`,
}),
},
];
}

return [
{
messageId: 'replaceMapSpreadInObject',
fix: getWrappingFixer({
node: node.argument,
sourceCode: context.sourceCode,
wrap: code => `Object.fromEntries(${code})`,
}),
},
];
}

function getPromiseSpreadSuggestions(
node: TSESTree.Expression,
): TSESLint.ReportSuggestionArray<MessageIds> {
const isHighPrecendence = isHigherPrecedenceThanAwait(
services.esTreeNodeToTSNodeMap.get(node),
);

return [
{
messageId: 'addAwait',
fix: fixer =>
isHighPrecendence
? fixer.insertTextBefore(node, 'await ')
: [
fixer.insertTextBefore(node, 'await ('),
fixer.insertTextAfter(node, ')'),
],
},
];
}

function checkObjectSpread(
node: TSESTree.JSXSpreadAttribute | TSESTree.SpreadElement,
): void {
Expand All@@ -117,6 +185,7 @@ export default createRule<Options, MessageIds>({
context.report({
node,
messageId: 'noPromiseSpreadInObject',
suggest: getPromiseSpreadSuggestions(node.argument),
});

return;
Expand All@@ -135,6 +204,7 @@ export default createRule<Options, MessageIds>({
context.report({
node,
messageId: 'noMapSpreadInObject',
suggest: getMapSpreadSuggestions(node, type),
});

return;
Expand Down
14 changes: 1 addition & 13 deletionspackages/eslint-plugin/src/rules/return-await.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,8 +12,8 @@ import {
isAwaitKeyword,
needsToBeAwaited,
nullThrows,
isHigherPrecedenceThanAwait,
} from '../util';
import { getOperatorPrecedence } from '../util/getOperatorPrecedence';

type FunctionNode =
| TSESTree.ArrowFunctionExpression
Expand DownExpand Up@@ -278,18 +278,6 @@ export default createRule({
];
}

function isHigherPrecedenceThanAwait(node: ts.Node): boolean {
const operator = ts.isBinaryExpression(node)
? node.operatorToken.kind
: ts.SyntaxKind.Unknown;
const nodePrecedence = getOperatorPrecedence(node.kind, operator);
const awaitPrecedence = getOperatorPrecedence(
ts.SyntaxKind.AwaitExpression,
ts.SyntaxKind.Unknown,
);
return nodePrecedence > awaitPrecedence;
}

function test(node: TSESTree.Expression, expression: ts.Node): void {
let child: ts.Node;

Expand Down
3 changes: 2 additions & 1 deletionpackages/eslint-plugin/src/util/index.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,8 +26,9 @@ export * from './scopeUtils';
export * from './types';
export * from './getConstraintInfo';
export * from './getValueOfLiteralType';
export * from './truthinessAndNullishUtils';
export * from './isHigherPrecedenceThanAwait';
export * from './skipChainExpression';
export * from './truthinessAndNullishUtils';

// this is done for convenience - saves migrating all of the old rules
export * from '@typescript-eslint/type-utils';
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';

import { getOperatorPrecedence } from './getOperatorPrecedence';

export function isHigherPrecedenceThanAwait(tsNode: ts.Node): boolean {
const operator = ts.isBinaryExpression(tsNode)
? tsNode.operatorToken.kind
: ts.SyntaxKind.Unknown;
const nodePrecedence = getOperatorPrecedence(tsNode.kind, operator);
const awaitPrecedence = getOperatorPrecedence(
ts.SyntaxKind.AwaitExpression,
ts.SyntaxKind.Unknown,
);
return nodePrecedence > awaitPrecedence;
}
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp