Skip to content

Commit bdba10a

Browse files
nx-cloud[bot]llwt
andauthored
fix(eslint): restore generic type arguments
Co-authored-by: llwt <llwt@users.noreply.github.com>
1 parent e2ac3e2 commit bdba10a

1 file changed

Lines changed: 47 additions & 25 deletions

File tree

  • packages/eslint/src/generators/utils/flat-config

packages/eslint/src/generators/utils/flat-config/ast-utils.ts

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function removeOverridesFromLintConfig(content: string): string {
5151
}
5252

5353
// TODO Change name
54-
function findExportDefault(source: ts.SourceFile): ts.NodeArray {
54+
function findExportDefault(source: ts.SourceFile): ts.NodeArray<ts.Node> {
5555
return ts.forEachChild(source, function analyze(node) {
5656
if (
5757
ts.isExportAssignment(node) &&
@@ -62,7 +62,7 @@ function findExportDefault(source: ts.SourceFile): ts.NodeArray {
6262
});
6363
}
6464

65-
function findModuleExports(source: ts.SourceFile): ts.NodeArray {
65+
function findModuleExports(source: ts.SourceFile): ts.NodeArray<ts.Node> {
6666
return ts.forEachChild(source, function analyze(node) {
6767
if (
6868
ts.isExpressionStatement(node) &&
@@ -170,7 +170,7 @@ function isOverride(node: ts.Node): boolean {
170170

171171
export function hasOverride(
172172
content: string,
173-
lookup: (override: Linter.ConfigOverride) => boolean
173+
lookup: (override: Linter.ConfigOverride<Linter.RulesRecord>) => boolean
174174
): boolean {
175175
const source = ts.createSourceFile(
176176
'',
@@ -187,7 +187,7 @@ export function hasOverride(
187187
}
188188
for (const node of exportsArray) {
189189
if (isOverride(node)) {
190-
let data: Partial;
190+
let data: Partial<Record<string, unknown>>;
191191

192192
if (ts.isObjectLiteralExpression(node)) {
193193
data = extractPropertiesFromObjectLiteral(node);
@@ -201,7 +201,9 @@ export function hasOverride(
201201
}
202202
}
203203

204-
if (lookup(data as Linter.ConfigOverride)) {
204+
if (
205+
lookup(data as unknown as Linter.ConfigOverride<Linter.RulesRecord>)
206+
) {
205207
return true;
206208
}
207209
}
@@ -247,7 +249,7 @@ function extractLiteralValue(node: ts.Node): unknown {
247249
}
248250

249251
if (ts.isObjectLiteralExpression(node)) {
250-
const obj: Record = {};
252+
const obj: Record<string, unknown> = {};
251253
for (const prop of node.properties) {
252254
if (ts.isPropertyAssignment(prop)) {
253255
const name = prop.name.getText().replace(/['"]/g, '');
@@ -279,8 +281,8 @@ function extractLiteralValue(node: ts.Node): unknown {
279281
*/
280282
function extractPropertiesFromObjectLiteral(
281283
node: ts.ObjectLiteralExpression
282-
): Partial {
283-
const result: Record = {};
284+
): Partial<Record<string, unknown>> {
285+
const result: Record<string, unknown> = {};
284286
for (const prop of node.properties) {
285287
if (ts.isPropertyAssignment(prop)) {
286288
const name = prop.name.getText().replace(/['"]/g, '');
@@ -291,7 +293,7 @@ function extractPropertiesFromObjectLiteral(
291293
}
292294
}
293295

294-
return result as Partial;
296+
return result as Partial<Record<string, unknown>>;
295297
}
296298

297299
/**
@@ -315,7 +317,10 @@ function findPropertyNode(
315317
/**
316318
* Find properties that are added, changed, or removed.
317319
*/
318-
function findChangedProperties(original: Record, updated: Record): string[] {
320+
function findChangedProperties(
321+
original: Record<string, unknown>,
322+
updated: Record<string, unknown>
323+
): string[] {
319324
const changed: string[] = [];
320325
// Check modified/added properties
321326
for (const key of Object.keys(updated)) {
@@ -356,8 +361,10 @@ function serializeValue(value: unknown, format: 'mjs' | 'cjs'): string {
356361
export function replaceOverride(
357362
content: string,
358363
root: string,
359-
lookup: (override: Linter.ConfigOverride) => boolean,
360-
update?: (override: Partial) => Partial
364+
lookup: (override: Linter.ConfigOverride<Linter.RulesRecord>) => boolean,
365+
update?: (
366+
override: Partial<Linter.ConfigOverride<Linter.RulesRecord>>
367+
) => Partial<Linter.ConfigOverride<Linter.RulesRecord>>
361368
): string {
362369
const source = ts.createSourceFile(
363370
'',
@@ -393,17 +400,19 @@ export function replaceOverride(
393400

394401
// Use AST-based extraction to handle variable references (e.g., plugins: { 'abc': abc })
395402
const data = extractPropertiesFromObjectLiteral(objectLiteralNode);
396-
if (lookup(data as Linter.ConfigOverride)) {
403+
if (lookup(data as unknown as Linter.ConfigOverride<Linter.RulesRecord>)) {
397404
// Deep clone before update (update functions may mutate nested objects)
398405
const originalData = structuredClone(data);
399406

400-
let updatedData = update?.(data);
407+
let updatedData = update?.(
408+
data as Partial<Linter.ConfigOverride<Linter.RulesRecord>>
409+
);
401410
if (updatedData) {
402411
updatedData = mapFilePaths(updatedData);
403412

404413
const changedProps = findChangedProperties(
405-
originalData as Record,
406-
updatedData as Record
414+
originalData as Record<string, unknown>,
415+
updatedData as Record<string, unknown>
407416
);
408417

409418
for (const propName of changedProps) {
@@ -632,7 +641,7 @@ function addCJSImportToFlatConfig(
632641
variable: string | string[],
633642
imp: string
634643
): string {
635-
const foundBindingVars: ts.NodeArray = ts.forEachChild(
644+
const foundBindingVars: ts.NodeArray<ts.BindingElement> = ts.forEachChild(
636645
source,
637646
function analyze(node) {
638647
// we can only combine object binding patterns
@@ -1356,10 +1365,12 @@ const compat = new FlatCompat({
13561365
* Optionally add flat compat initialization
13571366
*/
13581367
export function createNodeList(
1359-
importsMap: Map,
1368+
importsMap: Map<string, string>,
13601369
exportElements: ts.Expression[],
13611370
format: 'mjs' | 'cjs'
1362-
): ts.NodeArray {
1371+
): ts.NodeArray<
1372+
ts.VariableStatement | ts.Identifier | ts.ExpressionStatement | ts.SourceFile
1373+
> {
13631374
const importsList = [];
13641375

13651376
Array.from(importsMap.entries()).forEach(([imp, varName]) => {
@@ -1455,7 +1466,14 @@ export function generatePluginExtendsElementWithCompatFixup(
14551466
/**
14561467
* Stringifies TS nodes to file content string
14571468
*/
1458-
export function stringifyNodeList(nodes: ts.NodeArray): string {
1469+
export function stringifyNodeList(
1470+
nodes: ts.NodeArray<
1471+
| ts.VariableStatement
1472+
| ts.Identifier
1473+
| ts.ExpressionStatement
1474+
| ts.SourceFile
1475+
>
1476+
): string {
14591477
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
14601478
const resultFile = ts.createSourceFile(
14611479
'',
@@ -1571,7 +1589,9 @@ function convertGlobPattern(pattern: string): string {
15711589
// FROM: https://github.com/eslint/rewrite/blob/e2a7ec809db20e638abbad250d105ddbde88a8d5/packages/migrate-config/src/migrate-config.js#L38
15721590
const keysToCopy = ['settings', 'rules', 'processor'];
15731591

1574-
export function overrideNeedsCompat(override: Partial) {
1592+
export function overrideNeedsCompat(
1593+
override: Partial<Linter.ConfigOverride<Linter.RulesRecord>>
1594+
) {
15751595
return override.env || override.extends || override.plugins;
15761596
}
15771597

@@ -1580,7 +1600,7 @@ export function overrideNeedsCompat(override: Partial) {
15801600
* based on a given legacy eslintrc JSON override object
15811601
*/
15821602
export function generateFlatOverride(
1583-
_override: Partial & {
1603+
_override: Partial<Linter.ConfigOverride<Linter.RulesRecord>> & {
15841604
ignores?: Linter.FlatConfig['ignores'];
15851605
},
15861606
format: 'mjs' | 'cjs'
@@ -1772,7 +1792,7 @@ export function generateFlatOverride(
17721792
}
17731793

17741794
function generateESMParserImport(
1775-
override: Partial & {
1795+
override: Partial<Linter.ConfigOverride<Linter.RulesRecord>> & {
17761796
ignores?: Linter.FlatConfig['ignores'];
17771797
}
17781798
): ts.PropertyAssignment {
@@ -1795,7 +1815,7 @@ function generateESMParserImport(
17951815
}
17961816

17971817
function generateCJSParserImport(
1798-
override: Partial & {
1818+
override: Partial<Linter.ConfigOverride<Linter.RulesRecord>> & {
17991819
ignores?: Linter.FlatConfig['ignores'];
18001820
}
18011821
): ts.PropertyAssignment {
@@ -1831,7 +1851,9 @@ export function generateFlatPredefinedConfig(
18311851
return spread ? ts.factory.createSpreadElement(node) : node;
18321852
}
18331853

1834-
export function mapFilePaths<T extends Partial>(_override: T) {
1854+
export function mapFilePaths<
1855+
T extends Partial<Linter.ConfigOverride<Linter.RulesRecord>>
1856+
>(_override: T) {
18351857
const override: T = {
18361858
..._override,
18371859
};

0 commit comments

Comments
 (0)