corsa-oxlint 1.10.0 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/checker.js CHANGED
@@ -74,6 +74,10 @@ function createTypeChecker(context) {
74
74
  return this.getTypeOfSymbol(symbol) ?? this.getDeclaredTypeOfSymbol(symbol) ?? this.getTypeAtLocation(node);
75
75
  },
76
76
  typeToString(type, enclosingDeclaration, flags) {
77
+ if (flags === void 0) {
78
+ const syntheticText = syntheticTypeText(type);
79
+ if (syntheticText !== void 0) return syntheticText;
80
+ }
77
81
  return sessionForContext(context).session.typeToString(type, flags);
78
82
  },
79
83
  getBaseTypeOfLiteralType(type) {
@@ -95,7 +99,8 @@ function createTypeChecker(context) {
95
99
  return sessionForContext(context).session.getTypePredicateOfSignature(signature);
96
100
  },
97
101
  getBaseTypes(type) {
98
- return sessionForContext(context).session.getBaseTypes(type);
102
+ const bases = sessionForContext(context).session.getBaseTypes(type);
103
+ return bases.length > 0 ? bases : constructorBaseTypesFromType(context, type, this);
99
104
  },
100
105
  getImplementedTypes(node) {
101
106
  if ("pos" in node) return implementedTypesFromCorsaNode(context, node, this);
@@ -115,10 +120,12 @@ function createTypeChecker(context) {
115
120
  return implementedTypesFromTypeAndBases(context, type, this);
116
121
  },
117
122
  getTypeArguments(type) {
123
+ const syntheticArguments = type.syntheticTypeArguments;
124
+ if (syntheticArguments) return syntheticArguments;
118
125
  return sessionForContext(context).session.getTypeArguments(type);
119
126
  },
120
127
  getTypesOfType(type) {
121
- return sessionForContext(context).session.getTypesOfType(type);
128
+ return syntheticTypeConstituents(type) ?? sessionForContext(context).session.getTypesOfType(type);
122
129
  },
123
130
  getTargetOfType(type) {
124
131
  return sessionForContext(context).session.getTargetOfType(type);
@@ -148,7 +155,7 @@ function createTypeChecker(context) {
148
155
  return sessionForContext(context).session.getBaseTypeOfType(type);
149
156
  },
150
157
  getConstraintOfType(type) {
151
- return sessionForContext(context).session.getConstraintOfType(type);
158
+ return sessionForContext(context).session.getConstraintOfType(type) ?? constraintFromTypeParameterSource(context, type, this);
152
159
  },
153
160
  isUnionType(type) {
154
161
  return (type.flags & typeFlags.union) !== 0;
@@ -181,16 +188,366 @@ function pathsReferToSameFile(left, right) {
181
188
  const normalizedRight = right.replaceAll("\\", "/").toLowerCase();
182
189
  return normalizedLeft === normalizedRight || normalizedLeft.endsWith(`/${normalizedRight}`) || normalizedRight.endsWith(`/${normalizedLeft}`);
183
190
  }
191
+ function syntheticTypeText(type) {
192
+ return type.syntheticText ?? (isSyntheticCompoundType(type) ? type.texts[0] : void 0);
193
+ }
194
+ function syntheticTypeConstituents(type) {
195
+ return isSyntheticCompoundType(type) ? type.syntheticTypes : void 0;
196
+ }
197
+ function isSyntheticCompoundType(type) {
198
+ return type.id.startsWith("synthetic-compound:") && Array.isArray(type.syntheticTypes);
199
+ }
200
+ function syntheticCompoundType(kind, types, checker) {
201
+ const uniqueTypes = uniqueTypesById(types);
202
+ if (uniqueTypes.length === 0) return;
203
+ if (uniqueTypes.length === 1) return uniqueTypes[0];
204
+ const separator = kind === "union" ? " | " : " & ";
205
+ const text = uniqueTypes.map((type) => safeTypeToString(checker, type)).filter((part) => part !== void 0 && part.length > 0).join(separator);
206
+ if (!text) return;
207
+ return {
208
+ __corsaOxlintKind: "type",
209
+ id: `synthetic-compound:${kind}:${uniqueTypes.map((type) => type.id).join(separator)}`,
210
+ flags: kind === "union" ? typeFlags.union : typeFlags.intersection,
211
+ texts: [text],
212
+ syntheticTypes: uniqueTypes
213
+ };
214
+ }
215
+ function syntheticSourceType(type, text, typeArguments) {
216
+ return {
217
+ ...type,
218
+ texts: [text],
219
+ syntheticText: text,
220
+ syntheticTypeArguments: typeArguments
221
+ };
222
+ }
223
+ function uniqueTypesById(types) {
224
+ const seen = /* @__PURE__ */ new Set();
225
+ const unique = [];
226
+ for (const type of types) {
227
+ if (seen.has(type.id)) continue;
228
+ seen.add(type.id);
229
+ unique.push(type);
230
+ }
231
+ return unique;
232
+ }
184
233
  function typeOfNewExpression(context, node, checker) {
185
234
  const callee = childNode(node, "callee");
186
235
  if (!callee) return;
187
236
  const calleeType = checker.getTypeAtLocation(callee);
188
237
  if (!calleeType) return;
189
238
  const constructSignature = checker.getSignaturesOfType(calleeType, 1)[0];
190
- const type = constructSignature ? checker.getReturnTypeOfSignature(constructSignature) ?? calleeType : calleeType;
239
+ const type = constructSignature ? checker.getReturnTypeOfSignature(constructSignature) ?? constructorInstanceTypeFromType(context, calleeType, checker) ?? calleeType : constructorInstanceTypeFromType(context, calleeType, checker) ?? calleeType;
191
240
  sessionForContext(context).session.rememberTypeLookupFromType(type, calleeType);
192
241
  return type;
193
242
  }
243
+ function constructorBaseTypesFromType(context, type, checker) {
244
+ const constituentBases = constructorBaseTypesFromConstituents(context, type, checker);
245
+ if (constituentBases.length > 0) return constituentBases;
246
+ const instanceType = constructorInstanceTypeFromType(context, type, checker);
247
+ if (!instanceType) return [];
248
+ if (isSuperclassConstructorLookup(context, type)) return [instanceType];
249
+ const instanceBases = sessionForContext(context).session.getBaseTypes(instanceType);
250
+ return instanceBases.length > 0 ? instanceBases : [instanceType];
251
+ }
252
+ function constructorBaseTypesFromConstituents(context, type, checker) {
253
+ if (!checker.isIntersectionType(type) && !checker.isUnionType(type)) return [];
254
+ const bases = [];
255
+ const seen = /* @__PURE__ */ new Set();
256
+ for (const constituent of checker.getTypesOfType(type)) {
257
+ const directBases = sessionForContext(context).session.getBaseTypes(constituent);
258
+ const constituentBases = directBases.length > 0 ? directBases : constructorBaseTypesFromType(context, constituent, checker);
259
+ for (const base of constituentBases) {
260
+ if (seen.has(base.id)) continue;
261
+ seen.add(base.id);
262
+ bases.push(base);
263
+ }
264
+ }
265
+ return bases;
266
+ }
267
+ function constructorInstanceTypeFromType(context, type, checker) {
268
+ const constituentInstance = constructorInstanceTypeFromConstituents(context, type, checker);
269
+ if (constituentInstance) return constituentInstance;
270
+ return constructorInstanceTypeFromScalar(context, type, checker);
271
+ }
272
+ function constructorInstanceTypeFromConstituents(context, type, checker) {
273
+ const kind = compoundTypeKind(type, checker);
274
+ if (!kind) return;
275
+ const constituents = checker.getTypesOfType(type);
276
+ if (constituents.length === 0) return;
277
+ const instances = [];
278
+ for (const constituent of constituents) {
279
+ const instance = constructorInstanceTypeFromType(context, constituent, checker);
280
+ if (!instance) return;
281
+ instances.push(instance);
282
+ }
283
+ return syntheticCompoundType(kind, instances, checker);
284
+ }
285
+ function constructorInstanceTypeFromScalar(context, type, checker) {
286
+ const text = safeTypeToString(checker, type);
287
+ if (!text || !/\btypeof\s+/.test(text)) return;
288
+ const symbol = checker.getSymbolOfType(type);
289
+ const declared = symbol ? checker.getDeclaredTypeOfSymbol(symbol) : void 0;
290
+ if (declared && declared.id !== type.id && !safeTypeToString(checker, declared)?.includes("typeof ")) return declared;
291
+ const names = constructorTypeNames(text);
292
+ return names.length === 1 ? typeFromClassName(context, names[0], checker, type) : void 0;
293
+ }
294
+ function compoundTypeKind(type, checker) {
295
+ if (checker.isUnionType(type)) return "union";
296
+ return checker.isIntersectionType(type) ? "intersection" : void 0;
297
+ }
298
+ function constructorTypeNames(text) {
299
+ const names = [];
300
+ for (const match of text.matchAll(/\btypeof\s+([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)/g)) if (match[1]) names.push(match[1]);
301
+ return names;
302
+ }
303
+ function typeFromClassName(context, name, checker, relatedType) {
304
+ const session = sessionForContext(context).session;
305
+ const lookup = session.getTypeLookupSource(relatedType);
306
+ const sourceText = lookup ? lookup.sourceText ?? session.getSourceTextForPath(lookup.fileName) : context.sourceCode.text;
307
+ const fileName = lookup?.fileName ?? context.filename;
308
+ if (!sourceText) return;
309
+ const simpleName = lastQualifiedNamePart(name);
310
+ const position = classIdentifierPosition(sourceText, name, lookup?.position);
311
+ if (position === void 0) return;
312
+ return checker.getTypeAtLocation({
313
+ fileName,
314
+ pos: position,
315
+ end: position + simpleName.length,
316
+ range: [position, position + simpleName.length]
317
+ });
318
+ }
319
+ function classIdentifierPosition(sourceText, qualifiedName, anchorPosition) {
320
+ const parts = qualifiedName.split(".");
321
+ return classIdentifierPositionInRange(sourceText, parts.at(-1), (parts.length > 1 ? namespaceBodyRange(sourceText, parts.slice(0, -1), anchorPosition) : void 0) ?? [0, sourceText.length]);
322
+ }
323
+ function classIdentifierPositionInRange(sourceText, name, range) {
324
+ const pattern = new RegExp(`\\b(?:export\\s+)?(?:declare\\s+)?class\\s+${escapeRegExp(name)}\\b`, "g");
325
+ pattern.lastIndex = range[0];
326
+ let match;
327
+ while (match = pattern.exec(sourceText)) {
328
+ if (match.index >= range[1]) return;
329
+ return match.index + match[0].lastIndexOf(name);
330
+ }
331
+ }
332
+ function namespaceBodyRange(sourceText, names, anchorPosition) {
333
+ let range = [0, sourceText.length];
334
+ for (const name of names) {
335
+ const nextRange = namespaceBodyRangeForName(sourceText, name, range, anchorPosition);
336
+ if (!nextRange) return;
337
+ range = nextRange;
338
+ }
339
+ return range;
340
+ }
341
+ function namespaceBodyRangeForName(sourceText, name, range, anchorPosition) {
342
+ const pattern = new RegExp(`\\b(?:export\\s+)?namespace\\s+${escapeRegExp(name)}\\s*\\{`, "g");
343
+ pattern.lastIndex = range[0];
344
+ let fallback;
345
+ let match;
346
+ while (match = pattern.exec(sourceText)) {
347
+ if (match.index >= range[1]) break;
348
+ const bodyStart = sourceText.indexOf("{", match.index) + 1;
349
+ const bodyEnd = matchingBraceEnd(sourceText, bodyStart - 1);
350
+ if (bodyStart <= 0 || bodyEnd === void 0 || bodyEnd > range[1]) continue;
351
+ const bodyRange = [bodyStart, bodyEnd];
352
+ if (anchorPosition !== void 0 && anchorPosition >= bodyRange[0] && anchorPosition <= bodyRange[1]) return bodyRange;
353
+ fallback ??= bodyRange;
354
+ }
355
+ return fallback;
356
+ }
357
+ function lastQualifiedNamePart(name) {
358
+ return name.slice(name.lastIndexOf(".") + 1);
359
+ }
360
+ function matchingBraceEnd(sourceText, openBracePosition) {
361
+ let depth = 0;
362
+ for (let index = openBracePosition; index < sourceText.length; index += 1) {
363
+ const char = sourceText[index];
364
+ if (char === "{") depth += 1;
365
+ else if (char === "}") {
366
+ depth -= 1;
367
+ if (depth === 0) return index;
368
+ }
369
+ }
370
+ }
371
+ function isSuperclassConstructorLookup(context, type) {
372
+ const session = sessionForContext(context).session;
373
+ const lookup = session.getTypeLookupSource(type);
374
+ if (!lookup) return false;
375
+ const sourceText = lookup.sourceText ?? session.getSourceTextForPath(lookup.fileName);
376
+ if (!sourceText) return false;
377
+ return /\bextends\s*$/.test(sourceText.slice(Math.max(0, lookup.position - 32), lookup.position));
378
+ }
379
+ function constraintFromTypeParameterSource(context, type, checker) {
380
+ const name = safeTypeToString(checker, type)?.trim();
381
+ if (!name || !/^[A-Za-z_$][\w$]*$/.test(name)) return;
382
+ const session = sessionForContext(context).session;
383
+ const lookup = session.getTypeLookupSource(type);
384
+ const sourceText = lookup ? lookup.sourceText ?? session.getSourceTextForPath(lookup.fileName) : context.sourceCode.text;
385
+ const fileName = lookup?.fileName ?? context.filename;
386
+ if (!sourceText) return;
387
+ const range = typeParameterConstraintRange(sourceText, name, lookup?.position);
388
+ if (!range) return;
389
+ const constraintNode = {
390
+ fileName,
391
+ pos: range[0],
392
+ end: range[1],
393
+ range
394
+ };
395
+ const symbol = checker.getSymbolAtLocation(constraintNode);
396
+ const constraintType = (symbol ? checker.getDeclaredTypeOfSymbol(symbol) ?? checker.getTypeOfSymbol(symbol) : void 0) ?? checker.getTypeAtLocation(constraintNode);
397
+ if (!constraintType) return;
398
+ const constraintText = sourceText.slice(range[0], range[1]).trim();
399
+ const argumentTypes = typeArgumentRanges(sourceText, range[0], range[1]).map((argumentRange) => typeFromSourceRange(fileName, argumentRange, checker)).filter((argument) => argument !== void 0);
400
+ return argumentTypes.length > 0 ? syntheticSourceType(constraintType, constraintText, argumentTypes) : constraintType;
401
+ }
402
+ function typeFromSourceRange(fileName, range, checker) {
403
+ const node = {
404
+ fileName,
405
+ pos: range[0],
406
+ end: range[1],
407
+ range
408
+ };
409
+ const symbol = checker.getSymbolAtLocation(node);
410
+ return (symbol ? checker.getDeclaredTypeOfSymbol(symbol) ?? checker.getTypeOfSymbol(symbol) : void 0) ?? checker.getTypeAtLocation(node);
411
+ }
412
+ function typeParameterConstraintRange(sourceText, name, lookupPosition) {
413
+ const candidate = typeParameterConstraintCandidate(sourceText, name, lookupPosition);
414
+ return candidate ? [candidate.constraintStart, candidate.constraintEnd] : void 0;
415
+ }
416
+ function typeParameterConstraintCandidate(sourceText, name, lookupPosition) {
417
+ const pattern = new RegExp(`\\b${escapeRegExp(name)}\\s+extends\\s+`, "g");
418
+ let selected;
419
+ let selectedScore = Number.NEGATIVE_INFINITY;
420
+ let match;
421
+ while (match = pattern.exec(sourceText)) {
422
+ const nameStart = match.index;
423
+ const nameEnd = nameStart + name.length;
424
+ const constraintStart = match.index + match[0].length;
425
+ const constraintEnd = scanTypeParameterConstraintEnd(sourceText, constraintStart);
426
+ if (constraintEnd <= constraintStart) continue;
427
+ const candidate = {
428
+ nameStart,
429
+ nameEnd,
430
+ constraintStart,
431
+ constraintEnd,
432
+ declarationRange: declarationRangeForTypeParameter(sourceText, nameStart, constraintEnd)
433
+ };
434
+ if (lookupPosition !== void 0 && lookupPosition >= nameStart && lookupPosition <= nameEnd) return candidate;
435
+ const score = typeParameterConstraintCandidateScore(candidate, lookupPosition);
436
+ if (score > selectedScore) {
437
+ selected = candidate;
438
+ selectedScore = score;
439
+ }
440
+ }
441
+ return selected;
442
+ }
443
+ function typeParameterConstraintCandidateScore(candidate, lookupPosition) {
444
+ if (lookupPosition === void 0) return -candidate.nameStart;
445
+ const declarationRange = candidate.declarationRange;
446
+ if (declarationRange && lookupPosition >= declarationRange[0] && lookupPosition <= declarationRange[1]) return declarationRange[0];
447
+ return candidate.nameStart <= lookupPosition ? candidate.nameStart - 1e6 : -2e6;
448
+ }
449
+ function declarationRangeForTypeParameter(sourceText, nameStart, constraintEnd) {
450
+ const listStart = sourceText.lastIndexOf("<", nameStart);
451
+ if (listStart === -1) return;
452
+ const listEnd = scanTypeParameterListEnd(sourceText, listStart);
453
+ if (listEnd === void 0 || listEnd < constraintEnd) return;
454
+ const bodyStart = sourceText.indexOf("{", listEnd);
455
+ if (bodyStart === -1) return [listStart, listEnd + 1];
456
+ const bodyEnd = matchingBraceEnd(sourceText, bodyStart);
457
+ return bodyEnd === void 0 ? [listStart, sourceText.length] : [listStart, bodyEnd + 1];
458
+ }
459
+ function scanTypeParameterListEnd(sourceText, listStart) {
460
+ let depth = 0;
461
+ for (let index = listStart; index < sourceText.length; index += 1) {
462
+ const char = sourceText[index];
463
+ if (char === "<") depth += 1;
464
+ else if (char === ">") {
465
+ depth -= 1;
466
+ if (depth === 0) return index;
467
+ }
468
+ }
469
+ }
470
+ function scanTypeParameterConstraintEnd(sourceText, start) {
471
+ let end = start;
472
+ let angleDepth = 0;
473
+ let bracketDepth = 0;
474
+ let parenDepth = 0;
475
+ let braceDepth = 0;
476
+ while (end < sourceText.length) {
477
+ const char = sourceText[end];
478
+ if (char === "<") angleDepth += 1;
479
+ else if (char === ">") {
480
+ if (angleDepth === 0) break;
481
+ angleDepth -= 1;
482
+ } else if (char === "[") bracketDepth += 1;
483
+ else if (char === "]") bracketDepth = Math.max(0, bracketDepth - 1);
484
+ else if (char === "(") parenDepth += 1;
485
+ else if (char === ")") parenDepth = Math.max(0, parenDepth - 1);
486
+ else if (char === "{") braceDepth += 1;
487
+ else if (char === "}") {
488
+ if (braceDepth === 0) break;
489
+ braceDepth -= 1;
490
+ } else if (angleDepth === 0 && bracketDepth === 0 && parenDepth === 0 && braceDepth === 0 && (char === "," || char === "=")) break;
491
+ end += 1;
492
+ }
493
+ while (end > start && /\s/.test(sourceText[end - 1])) end -= 1;
494
+ return end;
495
+ }
496
+ function typeArgumentRanges(sourceText, start, end) {
497
+ const open = sourceText.indexOf("<", start);
498
+ if (open === -1 || open >= end) return [];
499
+ const close = matchingAngleEnd(sourceText, open, end);
500
+ if (close === void 0) return [];
501
+ const ranges = [];
502
+ let argumentStart = open + 1;
503
+ let angleDepth = 0;
504
+ let bracketDepth = 0;
505
+ let parenDepth = 0;
506
+ let braceDepth = 0;
507
+ for (let index = open + 1; index < close; index += 1) {
508
+ const char = sourceText[index];
509
+ if (char === "<") angleDepth += 1;
510
+ else if (char === ">") angleDepth = Math.max(0, angleDepth - 1);
511
+ else if (char === "[") bracketDepth += 1;
512
+ else if (char === "]") bracketDepth = Math.max(0, bracketDepth - 1);
513
+ else if (char === "(") parenDepth += 1;
514
+ else if (char === ")") parenDepth = Math.max(0, parenDepth - 1);
515
+ else if (char === "{") braceDepth += 1;
516
+ else if (char === "}") braceDepth = Math.max(0, braceDepth - 1);
517
+ else if (char === "," && angleDepth === 0 && bracketDepth === 0 && parenDepth === 0 && braceDepth === 0) {
518
+ appendTrimmedRange(ranges, sourceText, argumentStart, index);
519
+ argumentStart = index + 1;
520
+ }
521
+ }
522
+ appendTrimmedRange(ranges, sourceText, argumentStart, close);
523
+ return ranges;
524
+ }
525
+ function matchingAngleEnd(sourceText, openAnglePosition, limit) {
526
+ let depth = 0;
527
+ for (let index = openAnglePosition; index < limit; index += 1) {
528
+ const char = sourceText[index];
529
+ if (char === "<") depth += 1;
530
+ else if (char === ">") {
531
+ depth -= 1;
532
+ if (depth === 0) return index;
533
+ }
534
+ }
535
+ }
536
+ function appendTrimmedRange(ranges, sourceText, start, end) {
537
+ while (start < end && /\s/.test(sourceText[start])) start += 1;
538
+ while (end > start && /\s/.test(sourceText[end - 1])) end -= 1;
539
+ if (end > start) ranges.push([start, end]);
540
+ }
541
+ function safeTypeToString(checker, type) {
542
+ try {
543
+ return checker.typeToString(type);
544
+ } catch {
545
+ return;
546
+ }
547
+ }
548
+ function escapeRegExp(text) {
549
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
550
+ }
194
551
  function nodeForTypeLookup(node) {
195
552
  if ("pos" in node) return node;
196
553
  switch (node.type) {