8000 chore: enable unicorn/no-array-reduce by abrahamguo · Pull Request #9640 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

chore: enable unicorn/no-array-reduce #9640

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 50 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
1bb8c2b
enable rule
abrahamguo Jul 25, 2024
739a61d
member-ordering
abrahamguo Jul 25, 2024
5c75d88
parse-options
abrahamguo Jul 25, 2024
77f83b0
no-type-alias
abrahamguo Jul 25, 2024
cac19fc
no-base-to-string
abrahamguo Jul 25, 2024
1aaf5f3
semi
abrahamguo Jul 25, 2024
7ce8860
no-inferrable-types
abrahamguo Jul 25, 2024
5159c62
no-duplicate-type-constituents
abrahamguo Jul 25, 2024
49ddaa0
configs.test
abrahamguo Jul 25, 2024
97c54c8
func-call-spacing.test
abrahamguo Jul 25, 2024
1798fa2
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into n…
abrahamguo Jul 25, 2024
13fb7d8
prefer-nullish-coalescing.test
abrahamguo Jul 25, 2024
2c61ea4
no-explicit-any.test
abrahamguo Jul 25, 2024
1236c79
no-unsafe-assignment
8000 abrahamguo Jul 25, 2024
052b1eb
semi.test
abrahamguo Jul 25, 2024
9ca9249
prefer-nullish-coalescing.test
abrahamguo Jul 25, 2024
b8e3944
type-annotation-spacing.test
abrahamguo Jul 25, 2024
ff9812f
indent.test
abrahamguo Jul 25, 2024
a6e57ad
parser
abrahamguo Jul 25, 2024
7b5953e
configs.test
abrahamguo Jul 25, 2024
072ba30
resolveProjectList
abrahamguo Jul 25, 2024
867feb6
generate-configs
abrahamguo Jul 25, 2024
88c8064
deepMerge
abrahamguo Jul 25, 2024
cd96c13
jsonSchema
abrahamguo Jul 25, 2024
b5b6b4b
ConfigTypeScript
abrahamguo Jul 25, 2024
b2c0ed5
generate-sponsors
abrahamguo Jul 25, 2024
c46f70c
generate-configs
abrahamguo Jul 25, 2024
9b4c6ad
fix semi
abrahamguo Jul 25, 2024
ea3935b
review except for flatMap
abrahamguo Jul 27, 2024
c29870e
no longer using flatMap as filter
abrahamguo Jul 27, 2024
dc47bf1
fix variable name
abrahamguo Jul 27, 2024
37c76df
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into n…
abrahamguo Aug 2, 2024
9df95fd
additional fix
abrahamguo Aug 2, 2024
b91620f
prettier
abrahamguo Aug 2, 2024
4217764
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into n…
abrahamguo Aug 5, 2024
dc33d9e
refactor member-ordering
abrahamguo Aug 5, 2024
36c4d39
move comment
abrahamguo Aug 5, 2024
669ee31
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into n…
abrahamguo Aug 7, 2024
fda2a0d
revert no-duplicate-type-constituents
abrahamguo Aug 7, 2024
4d9c2f6
ConfigTypeScript
abrahamguo Aug 7, 2024
2adb2c8
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into n…
abrahamguo Aug 8, 2024
297ce7e
revert generate-sponsors
abrahamguo Aug 8, 2024
7ca4fac
revert
abrahamguo Aug 8, 2024
c7d5cf8
remove lint rule
abrahamguo Aug 8, 2024
859841c
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into n…
abrahamguo Aug 8, 2024
f74c256
Merge branch 'main' into no-array-reduce
JoshuaKGoldberg Aug 19, 2024
b1d5c2b
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into n…
abrahamguo Aug 19, 2024
1216fee
Merge branch 'no-array-reduce' of github.com:abrahamguo/typescript-es…
abrahamguo Aug 19, 2024
be32707
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into n…
abrahamguo Aug 27, 2024
6b79534
remove Object.groupBy because lib was downleveled
abrahamguo Aug 27, 2024
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
100 changes: 49 additions & 51 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,75 +299,73 @@ export const defaultOrder: MemberType[] = [
'method',
];

const allMemberTypes = Array.from(
(
[
'readonly-signature',
'signature',
'readonly-field',
'field',
'method',
'call-signature',
'constructor',
'accessor',
'get',
'set',
'static-initialization',
] as const
).reduce<Set<MemberType>>((all, type) => {
all.add(type);

(['public', 'protected', 'private', '#private'] as const).forEach(
accessibility => {
if (
const allMemberTypes = [
Copy link
Member

Choose a reason for hiding this comment

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

Wow this is a giant one. Definitely an improvement.

...new Set(
(
[
'readonly-signature',
'signature',
'readonly-field',
'field',
'method',
'call-signature',
'constructor',
'accessor',
'get',
'set',
'static-initialization',
] as const
).flatMap(type => [
type,

...(['public', 'protected', 'private', '#private'] as const)
.flatMap<MemberType>(accessibility => [
type !== 'readonly-signature' &&
type !== 'signature' &&
type !== 'static-initialization' &&
type !== 'call-signature' &&
!(type === 'constructor' && accessibility === '#private')
) {
all.add(`${accessibility}-${type}`); // e.g. `public-field`
}
? `${accessibility}-${type}` // e.g. `public-field`
: [],

// Only class instance fields, methods, accessors, get and set can have decorators attached to them
if (
// Only class instance fields, methods, accessors, get and set can have decorators attached to them
accessibility !== '#private' &&
(type === 'readonly-field' ||
type === 'field' ||
type === 'method' ||
type === 'accessor' ||
type === 'get' ||
type === 'set')
) {
all.add(`${accessibility}-decorated-${type}`);
all.add(`decorated-${type}`);
}
? [`${accessibility}-decorated-${type}`, `decorated-${type}`]
: [],

if (
type !== 'constructor' &&
type !== 'readonly-signature' &&
type !== 'signature' &&
type !== 'call-signature'
) {
// There is no `static-constructor` or `instance-constructor` or `abstract-constructor`
if (accessibility === '#private' || accessibility === 'private') {
(['static', 'instance'] as const).forEach(scope => {
all.add(`${scope}-${type}`);
all.add(`${accessibility}-${scope}-${type}`);
});
} else {
(['static', 'instance', 'abstract'] as const).forEach(scope => {
all.add(`${scope}-${type}`);
all.add(`${accessibility}-${scope}-${type}`);
});
}
}
},
);

return all;
}, new Set<MemberType>()),
);
? (
[
'static',
'instance',
// There is no `static-constructor` or `instance-constructor` or `abstract-constructor`
...(accessibility === '#private' ||
accessibility === 'private'
? []
: (['abstract'] as const)),
] as const
).flatMap(
scope =>
[
`${scope}-${type}`,
`${accessibility}-${scope}-${type}`,
] as const,
)
: [],
])
.flat(),
]),
),
];

const functionExpressions = [
AST_NODE_TYPES.FunctionExpression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,12 @@ function normalizeOption(option: Selector): NormalizedSelector[] {
function parseOptions(context: Context): ParsedOptions {
const normalizedOptions = context.options.flatMap(normalizeOption);

const result = getEnumNames(Selectors).reduce((acc, k) => {
acc[k] = createValidator(k, context, normalizedOptions);
return acc;
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
}, {} as ParsedOptions);

return result;
return Object.fromEntries(
getEnumNames(Selectors).map(k => [
k,
createValidator(k, context, normalizedOptions),
]),
) as ParsedOptions;
}

export { parseOptions };
5 changes: 1 addition & 4 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,7 @@ export default createRule<Options, MessageIds>({
node.type === AST_NODE_TYPES.TSUnionType ||
node.type === AST_NODE_TYPES.TSIntersectionType
) {
return node.types.reduce<TypeWithLabel[]>((acc, type) => {
acc.push(...getTypes(type, node.type));
return acc;
}, []);
return node.types.flatMap(type => getTypes(type, node.type));
}
return [{ node, compositionType }];
}
Expand Down
51 changes: 33 additions & 18 deletions packages/eslint-plugin/tests/configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ const EXTENSION_RULES = Object.entries(rules)
] as const,
);

function entriesToObject<T = unknown>(value: [string, T][]): Record<string, T> {
return value.reduce<Record<string, T>>((accum, [k, v]) => {
accum[k] = v;
return accum;
}, {});
}

function filterRules(
values: Record<string, string | unknown[]>,
): [string, string | unknown[]][] {
Expand Down Expand Up @@ -118,7 +111,9 @@ describe('all.ts', () => {
excludeDeprecated: true,
});

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -135,7 +130,9 @@ describe('disable-type-checked.ts', () => {
.filter(([, rule]) => rule.meta.docs?.requiresTypeChecking)
.map(([name]) => [`${RULE_NAME_PREFIX}${name}`, 'off']);

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});
});

Expand All @@ -151,7 +148,9 @@ describe('recommended.ts', () => {
recommendations: ['recommended'],
});

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -168,7 +167,9 @@ describe('recommended-type-checked.ts', () => {
recommendations: ['recommended'],
});

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -186,7 +187,9 @@ describe('recommended-type-checked-only.ts', () => {
recommendations: ['recommended'],
}).filter(([ruleName]) => ruleName);

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -205,7 +208,9 @@ describe('strict.ts', () => {
recommendations: ['recommended', 'strict'],
});

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -222,7 +227,9 @@ describe('strict-type-checked.ts', () => {
excludeDeprecated: true,
recommendations: ['recommended', 'strict'],
});
expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -241,7 +248,9 @@ describe('strict-type-checked-only.ts', () => {
recommendations: ['recommended', 'strict'],
}).filter(([ruleName]) => ruleName);

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -259,7 +268,9 @@ describe('stylistic.ts', () => {
recommendations: ['stylistic'],
});

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -275,7 +286,9 @@ describe('stylistic-type-checked.ts', () => {
});

it('contains all stylistic rules, excluding deprecated ones', () => {
expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand All @@ -293,7 +306,9 @@ describe('stylistic-type-checked-only.ts', () => {
recommendations: ['stylistic'],
}).filter(([ruleName]) => ruleName);

expect(entriesToObject(ruleConfigs)).toEqual(entriesToObject(configRules));
expect(Object.fromEntries(ruleConfigs)).toEqual(
Object.fromEntries(configRules),
);
});

itHasBaseRulesOverriden(unfilteredConfigRules);
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ ruleTester.run('no-base-to-string', rule, {
...literalList.map(i => `\`\${${i}}\`;`),

// operator + +=
...literalListWrapped
.map(l => literalListWrapped.map(r => `${l} + ${r};`))
.reduce((pre, cur) => [...pre, ...cur]),
...literalListWrapped.flatMap(l =>
literalListWrapped.map(r => `${l} + ${r};`),
),

// toString()
...literalListWrapped.map(i => `${i === '1' ? `(${i})` : i}.toString();`),
Expand Down
65 changes: 32 additions & 33 deletions packages/eslint-plugin/tests/rules/no-explicit-any.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ const test = <T extends Partial<never>>() => {};
],
},
] as RuleInvalidTestCase[]
).reduce<RuleInvalidTestCase[]>((acc, testCase) => {
).flatMap(testCase => {
const suggestions = (code: string): RuleSuggestionOutput[] => [
{
messageId: 'suggestUnknown',
Expand All @@ -1209,38 +1209,37 @@ const test = <T extends Partial<never>>() => {};
output: code.replace(/any/, 'never'),
},
];
acc.push({
...testCase,
errors: testCase.errors.map(e => ({
...e,
suggestions: e.suggestions ?? suggestions(testCase.code),
})),
});
const options = testCase.options ?? [];
const code = `// fixToUnknown: true\n${testCase.code}`;
acc.push({
code,
output: code.replaceAll('any', 'unknown'),
options: [{ ...options[0], fixToUnknown: true }],
errors: testCase.errors.map(err => {
if (err.line === undefined) {
return err;
}

return {
...err,
line: err.line + 1,
suggestions:
err.suggestions?.map(
(s): RuleSuggestionOutput => ({
...s,
output: `// fixToUnknown: true\n${s.output}`,
}),
) ?? suggestions(code),
};
}),
});
return [
{
...testCase,
errors: testCase.errors.map(e => ({
...e,
suggestions: e.suggestions ?? suggestions(testCase.code),
})),
},
{
code,
output: code.replaceAll('any', 'unknown'),
options: [{ ...testCase.options?.[0], fixToUnknown: true }],
errors: testCase.errors.map(err => {
if (err.line === undefined) {
return err;
}

return acc;
}, []),
return {
...err,
line: err.line + 1,
suggestions:
err.suggestions?.map(
(s): RuleSuggestionOutput => ({
...s,
output: `// fixToUnknown: true\n${s.output}`,
}),
) ?? suggestions(code),
};
}),
},
];
}),
});
Loading
Loading
0