tstyche 7.2.0 → 7.2.2
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/api.d.ts +5 -5
- package/dist/api.js +239 -246
- package/package.json +5 -5
package/dist/api.d.ts
CHANGED
|
@@ -79,14 +79,14 @@ declare class TestTree {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
declare class DiagnosticOrigin {
|
|
82
|
-
|
|
82
|
+
expectNode: ExpectNode | undefined;
|
|
83
83
|
end: number;
|
|
84
84
|
sourceFile: ts.SourceFile | JsonSourceFile;
|
|
85
85
|
start: number;
|
|
86
|
-
constructor(start: number, end: number, sourceFile: ts.SourceFile | JsonSourceFile,
|
|
87
|
-
static fromAbilityDiagnostic(diagnostic: ts.DiagnosticWithLocation,
|
|
88
|
-
static fromAssertion(
|
|
89
|
-
static fromNode(node: ts.Node,
|
|
86
|
+
constructor(start: number, end: number, sourceFile: ts.SourceFile | JsonSourceFile, expectNode?: ExpectNode);
|
|
87
|
+
static fromAbilityDiagnostic(diagnostic: ts.DiagnosticWithLocation, expectNode: ExpectNode): DiagnosticOrigin;
|
|
88
|
+
static fromAssertion(expectNode: ExpectNode): DiagnosticOrigin;
|
|
89
|
+
static fromNode(node: ts.Node, expectNode?: ExpectNode): DiagnosticOrigin;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
declare class Diagnostic {
|
package/dist/api.js
CHANGED
|
@@ -112,25 +112,25 @@ function isDiagnosticWithLocation(diagnostic) {
|
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
class DiagnosticOrigin {
|
|
115
|
-
|
|
115
|
+
expectNode;
|
|
116
116
|
end;
|
|
117
117
|
sourceFile;
|
|
118
118
|
start;
|
|
119
|
-
constructor(start, end, sourceFile,
|
|
119
|
+
constructor(start, end, sourceFile, expectNode) {
|
|
120
120
|
this.start = start;
|
|
121
121
|
this.end = end;
|
|
122
122
|
this.sourceFile = sourceFile;
|
|
123
|
-
this.
|
|
123
|
+
this.expectNode = expectNode;
|
|
124
124
|
}
|
|
125
|
-
static fromAbilityDiagnostic(diagnostic,
|
|
126
|
-
return new DiagnosticOrigin(diagnostic.start, getTextSpanEnd(diagnostic), diagnostic.file,
|
|
125
|
+
static fromAbilityDiagnostic(diagnostic, expectNode) {
|
|
126
|
+
return new DiagnosticOrigin(diagnostic.start, getTextSpanEnd(diagnostic), diagnostic.file, expectNode);
|
|
127
127
|
}
|
|
128
|
-
static fromAssertion(
|
|
129
|
-
const node =
|
|
130
|
-
return new DiagnosticOrigin(node.getStart(), node.getEnd(), node.getSourceFile(),
|
|
128
|
+
static fromAssertion(expectNode) {
|
|
129
|
+
const node = expectNode.matcherNameNode.name;
|
|
130
|
+
return new DiagnosticOrigin(node.getStart(), node.getEnd(), node.getSourceFile(), expectNode);
|
|
131
131
|
}
|
|
132
|
-
static fromNode(node,
|
|
133
|
-
return new DiagnosticOrigin(node.getStart(), node.getEnd(), node.getSourceFile(),
|
|
132
|
+
static fromNode(node, expectNode) {
|
|
133
|
+
return new DiagnosticOrigin(node.getStart(), node.getEnd(), node.getSourceFile(), expectNode);
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
|
|
@@ -517,17 +517,20 @@ class Environment {
|
|
|
517
517
|
return Path.resolve(os.homedir(), ".local", "share", "TSTyche");
|
|
518
518
|
}
|
|
519
519
|
static #resolveTypeScriptModule() {
|
|
520
|
-
let specifier = "typescript";
|
|
521
520
|
if (process.env["TSTYCHE_TYPESCRIPT_MODULE"] != null) {
|
|
522
|
-
specifier = process.env["TSTYCHE_TYPESCRIPT_MODULE"];
|
|
521
|
+
const specifier = process.env["TSTYCHE_TYPESCRIPT_MODULE"];
|
|
522
|
+
if (specifier !== "") {
|
|
523
|
+
return import.meta.resolve(specifier);
|
|
524
|
+
}
|
|
525
|
+
return;
|
|
523
526
|
}
|
|
524
527
|
let resolvedModule;
|
|
525
528
|
try {
|
|
526
|
-
resolvedModule = import.meta.resolve(
|
|
529
|
+
resolvedModule = import.meta.resolve("typescript");
|
|
527
530
|
}
|
|
528
531
|
catch {
|
|
529
532
|
}
|
|
530
|
-
return resolvedModule;
|
|
533
|
+
return resolvedModule?.endsWith("lib/typescript.js") ? resolvedModule : undefined;
|
|
531
534
|
}
|
|
532
535
|
}
|
|
533
536
|
|
|
@@ -609,7 +612,7 @@ class Fetcher {
|
|
|
609
612
|
}
|
|
610
613
|
async get(request, diagnostic, options) {
|
|
611
614
|
let delay = 3000;
|
|
612
|
-
request.headers.set("User-Agent", `tstyche
|
|
615
|
+
request.headers.set("User-Agent", `tstyche/7.2.2 ${process.platform} ${process.arch}`);
|
|
613
616
|
for (let attempt = 0; attempt <= this.#retries; attempt++) {
|
|
614
617
|
const isLastAttempt = attempt === this.#retries;
|
|
615
618
|
const suppressErrors = options?.suppressErrors || !isLastAttempt;
|
|
@@ -2357,8 +2360,8 @@ function CodeFrameText({ diagnosticCategory, diagnosticOrigin, options }) {
|
|
|
2357
2360
|
}
|
|
2358
2361
|
}
|
|
2359
2362
|
let breadcrumbs;
|
|
2360
|
-
if (showBreadcrumbs && diagnosticOrigin.
|
|
2361
|
-
breadcrumbs = jsx(BreadcrumbsText, { ancestor: diagnosticOrigin.
|
|
2363
|
+
if (showBreadcrumbs && diagnosticOrigin.expectNode != null) {
|
|
2364
|
+
breadcrumbs = jsx(BreadcrumbsText, { ancestor: diagnosticOrigin.expectNode.parent });
|
|
2362
2365
|
}
|
|
2363
2366
|
const location = (jsx(Line, { children: [" ".repeat(gutterWidth + 2), jsx(Text, { color: "90", children: " at " }), jsx(Text, { color: "36", children: Path.relative("", diagnosticOrigin.sourceFile.fileName) }), jsx(Text, { color: "90", children: `:${firstMarkedLine + 1}:${firstMarkedCharacter + 1}` }), breadcrumbs] }));
|
|
2364
2367
|
return (jsx(Text, { children: [codeFrame, jsx(Line, {}), location] }));
|
|
@@ -3382,12 +3385,21 @@ function deepCompareKeys(a, b, keys) {
|
|
|
3382
3385
|
}
|
|
3383
3386
|
return true;
|
|
3384
3387
|
}
|
|
3385
|
-
function
|
|
3386
|
-
return
|
|
3388
|
+
function isCapitaizedIdentifierLike(node, compiler) {
|
|
3389
|
+
return isIdentifierLike(node, compiler) && /^[A-Z_$]/.test(node.getText()[0]);
|
|
3387
3390
|
}
|
|
3388
|
-
function
|
|
3391
|
+
function isChildOfExpressionStatement(node, compiler) {
|
|
3389
3392
|
return compiler.isExpressionStatement(node.parent);
|
|
3390
3393
|
}
|
|
3394
|
+
function isIdentifierLike(node, compiler) {
|
|
3395
|
+
return (compiler.isIdentifier(node) ||
|
|
3396
|
+
compiler.isPropertyAccessExpression(node) ||
|
|
3397
|
+
compiler.isTypeReferenceNode(node) ||
|
|
3398
|
+
compiler.isExpressionWithTypeArguments(node));
|
|
3399
|
+
}
|
|
3400
|
+
function belongsToArgumentList(node, compiler) {
|
|
3401
|
+
return compiler.isCallExpression(node.parent) && node.parent.arguments.some((argument) => argument === node);
|
|
3402
|
+
}
|
|
3391
3403
|
|
|
3392
3404
|
class AbilityLayer {
|
|
3393
3405
|
#compiler;
|
|
@@ -3397,26 +3409,21 @@ class AbilityLayer {
|
|
|
3397
3409
|
this.#compiler = compiler;
|
|
3398
3410
|
this.#editor = editor;
|
|
3399
3411
|
}
|
|
3400
|
-
#belongsToNode(node, diagnostic) {
|
|
3401
|
-
return diagnosticBelongsToNode(diagnostic, node.matcherNode) || diagnosticBelongsToNode(diagnostic, node.source);
|
|
3402
|
-
}
|
|
3403
3412
|
close(diagnostics) {
|
|
3404
3413
|
if (diagnostics.length > 0 && this.#nodes.length > 0) {
|
|
3405
3414
|
this.#nodes.reverse();
|
|
3406
3415
|
for (const diagnostic of diagnostics) {
|
|
3407
|
-
this.#
|
|
3416
|
+
for (const node of this.#nodes) {
|
|
3417
|
+
if (diagnosticBelongsToNode(diagnostic, node.matcherNode) ||
|
|
3418
|
+
diagnosticBelongsToNode(diagnostic, node.source)) {
|
|
3419
|
+
node.abilityDiagnostics.add(diagnostic);
|
|
3420
|
+
break;
|
|
3421
|
+
}
|
|
3422
|
+
}
|
|
3408
3423
|
}
|
|
3409
3424
|
}
|
|
3410
3425
|
this.#nodes = [];
|
|
3411
3426
|
}
|
|
3412
|
-
#mapToNodes(diagnostic) {
|
|
3413
|
-
for (const node of this.#nodes) {
|
|
3414
|
-
if (this.#belongsToNode(node, diagnostic)) {
|
|
3415
|
-
node.abilityDiagnostics.add(diagnostic);
|
|
3416
|
-
break;
|
|
3417
|
-
}
|
|
3418
|
-
}
|
|
3419
|
-
}
|
|
3420
3427
|
visitExpect(expect) {
|
|
3421
3428
|
const expectStart = expect.node.getStart();
|
|
3422
3429
|
const expectExpressionEnd = expect.node.expression.getEnd();
|
|
@@ -3430,6 +3437,7 @@ class AbilityLayer {
|
|
|
3430
3437
|
const targetNode = expect.target?.[0];
|
|
3431
3438
|
if (!sourceNode ||
|
|
3432
3439
|
!targetNode ||
|
|
3440
|
+
!isCapitaizedIdentifierLike(sourceNode, this.#compiler) ||
|
|
3433
3441
|
!this.#compiler.isObjectLiteralExpression(targetNode) ||
|
|
3434
3442
|
!targetNode.properties.every((property) => (this.#compiler.isPropertyAssignment(property) &&
|
|
3435
3443
|
(this.#compiler.isIdentifier(property.name) || this.#compiler.isStringLiteral(property.name))) ||
|
|
@@ -3437,38 +3445,40 @@ class AbilityLayer {
|
|
|
3437
3445
|
return;
|
|
3438
3446
|
}
|
|
3439
3447
|
const sourceText = sourceNode.getText();
|
|
3440
|
-
if (
|
|
3448
|
+
if (belongsToArgumentList(sourceNode, this.#compiler)) {
|
|
3441
3449
|
this.#editor
|
|
3442
3450
|
.eraseTrailingComma(expect.source)
|
|
3443
3451
|
.erase(expectStart, matcherNodeEnd)
|
|
3444
|
-
.update(expectStart, matcherNameEnd,
|
|
3452
|
+
.update(expectStart, matcherNameEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler) ? ";" : "")
|
|
3445
3453
|
.update(sourceNode.getStart() - 1, sourceNode.getEnd(), `<${sourceText}`);
|
|
3446
3454
|
}
|
|
3447
3455
|
else {
|
|
3448
3456
|
const id = ["SC", expectStart, Date.now().toString(36)].join("_");
|
|
3449
3457
|
this.#editor
|
|
3450
3458
|
.erase(expectStart, matcherNodeEnd)
|
|
3451
|
-
.update(expectStart, matcherNameEnd,
|
|
3459
|
+
.update(expectStart, matcherNameEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler)
|
|
3452
3460
|
? `;const ${id} = undefined as any as ${sourceText};<${id}`
|
|
3453
3461
|
: `const ${id} = undefined as any as ${sourceText};<${id}`);
|
|
3454
3462
|
}
|
|
3455
3463
|
for (const property of targetNode.properties) {
|
|
3456
3464
|
if (this.#compiler.isPropertyAssignment(property)) {
|
|
3457
3465
|
this.#editor
|
|
3458
|
-
.update(property.name.getStart(), property.name.getEnd()
|
|
3459
|
-
? ` ${property.name.getText().slice(1, -1)}
|
|
3460
|
-
: property.name.getText()
|
|
3461
|
-
.
|
|
3462
|
-
.update(property.initializer.getStart(), property.initializer.getEnd(), property.initializer.getText()
|
|
3466
|
+
.update(property.name.getStart(), property.name.getEnd(), this.#compiler.isStringLiteral(property.name)
|
|
3467
|
+
? ` ${property.name.getText().slice(1, -1)} `
|
|
3468
|
+
: property.name.getText())
|
|
3469
|
+
.insert(property.initializer.getStart(), "={")
|
|
3470
|
+
.update(property.initializer.getStart(), property.initializer.getEnd(), property.initializer.getText())
|
|
3471
|
+
.insert(property.initializer.getEnd(), "}");
|
|
3463
3472
|
continue;
|
|
3464
3473
|
}
|
|
3465
3474
|
if (this.#compiler.isSpreadAssignment(property)) {
|
|
3466
3475
|
this.#editor
|
|
3467
|
-
.
|
|
3468
|
-
.update(property.getStart(), property.getEnd(), property.getText()
|
|
3476
|
+
.insert(property.getStart(), "{")
|
|
3477
|
+
.update(property.getStart(), property.getEnd(), property.getText())
|
|
3478
|
+
.insert(property.getEnd(), "}");
|
|
3469
3479
|
}
|
|
3470
3480
|
}
|
|
3471
|
-
this.#editor.
|
|
3481
|
+
this.#editor.insert(matcherNodeEnd, "/>");
|
|
3472
3482
|
break;
|
|
3473
3483
|
}
|
|
3474
3484
|
case "toBeApplicable":
|
|
@@ -3481,15 +3491,15 @@ class AbilityLayer {
|
|
|
3481
3491
|
if (!sourceNode) {
|
|
3482
3492
|
return;
|
|
3483
3493
|
}
|
|
3484
|
-
if (
|
|
3494
|
+
if (belongsToArgumentList(sourceNode, this.#compiler)) {
|
|
3485
3495
|
this.#editor
|
|
3486
3496
|
.eraseTrailingComma(expect.source)
|
|
3487
|
-
.update(expectStart, expectExpressionEnd,
|
|
3497
|
+
.update(expectStart, expectExpressionEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler) ? ";" : "")
|
|
3488
3498
|
.erase(expectEnd, matcherNameEnd);
|
|
3489
3499
|
}
|
|
3490
3500
|
else {
|
|
3491
3501
|
const sourceText = sourceNode.getFullText();
|
|
3492
|
-
this.#editor.update(expectStart, matcherNameEnd,
|
|
3502
|
+
this.#editor.update(expectStart, matcherNameEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler)
|
|
3493
3503
|
? `;(undefined as any as ${sourceText})`
|
|
3494
3504
|
: `(undefined as any as ${sourceText})`);
|
|
3495
3505
|
}
|
|
@@ -3501,15 +3511,15 @@ class AbilityLayer {
|
|
|
3501
3511
|
if (!sourceNode) {
|
|
3502
3512
|
return;
|
|
3503
3513
|
}
|
|
3504
|
-
if (
|
|
3514
|
+
if (belongsToArgumentList(sourceNode, this.#compiler)) {
|
|
3505
3515
|
this.#editor
|
|
3506
3516
|
.eraseTrailingComma(expect.source)
|
|
3507
|
-
.update(expectStart, expectExpressionEnd,
|
|
3517
|
+
.update(expectStart, expectExpressionEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler) ? "; new" : "new")
|
|
3508
3518
|
.erase(expectEnd, matcherNameEnd);
|
|
3509
3519
|
}
|
|
3510
3520
|
else {
|
|
3511
3521
|
const sourceText = sourceNode.getFullText();
|
|
3512
|
-
this.#editor.update(expectStart, matcherNameEnd,
|
|
3522
|
+
this.#editor.update(expectStart, matcherNameEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler)
|
|
3513
3523
|
? `;new (undefined as any as ${sourceText})`
|
|
3514
3524
|
: `new (undefined as any as ${sourceText})`);
|
|
3515
3525
|
}
|
|
@@ -3519,13 +3529,13 @@ class AbilityLayer {
|
|
|
3519
3529
|
this.#nodes.push(expect);
|
|
3520
3530
|
const sourceNode = expect.source[0];
|
|
3521
3531
|
const targetNode = expect.target?.[0];
|
|
3522
|
-
if (!sourceNode) {
|
|
3532
|
+
if (!sourceNode || !targetNode || !isIdentifierLike(sourceNode, this.#compiler)) {
|
|
3523
3533
|
return;
|
|
3524
3534
|
}
|
|
3525
|
-
if (
|
|
3535
|
+
if (belongsToArgumentList(sourceNode, this.#compiler)) {
|
|
3526
3536
|
this.#editor
|
|
3527
3537
|
.eraseTrailingComma(expect.source)
|
|
3528
|
-
.update(expectStart, expectExpressionEnd,
|
|
3538
|
+
.update(expectStart, expectExpressionEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler) ? ";" : "")
|
|
3529
3539
|
.erase(expectEnd, matcherNodeEnd);
|
|
3530
3540
|
if (this.#compiler.isExpressionWithTypeArguments(sourceNode)) {
|
|
3531
3541
|
this.#editor.erase(sourceNode.expression.getEnd(), sourceNode.getEnd());
|
|
@@ -3535,15 +3545,13 @@ class AbilityLayer {
|
|
|
3535
3545
|
const sourceText = this.#compiler.isTypeReferenceNode(sourceNode)
|
|
3536
3546
|
? sourceNode.typeName.getFullText()
|
|
3537
3547
|
: sourceNode.getFullText();
|
|
3538
|
-
this.#editor.update(expectStart, matcherNodeEnd,
|
|
3548
|
+
this.#editor.update(expectStart, matcherNodeEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler)
|
|
3539
3549
|
? `;undefined as any as ${sourceText}`
|
|
3540
3550
|
: `undefined as any as ${sourceText}`);
|
|
3541
3551
|
}
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
this.#editor.update(targetNode.getFullStart(), targetNode.getEnd(), `<${targetText}>`.padStart(targetNode.getFullWidth()));
|
|
3546
|
-
}
|
|
3552
|
+
const targetText = targetNode.getText().slice(1, -1);
|
|
3553
|
+
if (targetText.trim().length > 0) {
|
|
3554
|
+
this.#editor.update(targetNode.getFullStart(), targetNode.getEnd(), `<${targetText}>`.padStart(targetNode.getFullWidth()));
|
|
3547
3555
|
}
|
|
3548
3556
|
break;
|
|
3549
3557
|
}
|
|
@@ -3554,15 +3562,15 @@ class AbilityLayer {
|
|
|
3554
3562
|
if (!sourceNode || !targetNode) {
|
|
3555
3563
|
return;
|
|
3556
3564
|
}
|
|
3557
|
-
if (
|
|
3565
|
+
if (belongsToArgumentList(sourceNode, this.#compiler)) {
|
|
3558
3566
|
this.#editor
|
|
3559
3567
|
.eraseTrailingComma(expect.source)
|
|
3560
|
-
.update(expectStart, expectExpressionEnd,
|
|
3568
|
+
.update(expectStart, expectExpressionEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler) ? ";" : "")
|
|
3561
3569
|
.erase(expectEnd, matcherNodeEnd);
|
|
3562
3570
|
}
|
|
3563
3571
|
else {
|
|
3564
3572
|
const sourceText = sourceNode.getFullText();
|
|
3565
|
-
this.#editor.update(expectStart, matcherNodeEnd,
|
|
3573
|
+
this.#editor.update(expectStart, matcherNodeEnd, isChildOfExpressionStatement(expect.matcherNode, this.#compiler)
|
|
3566
3574
|
? `;(undefined as any as ${sourceText})`
|
|
3567
3575
|
: `(undefined as any as ${sourceText})`);
|
|
3568
3576
|
}
|
|
@@ -3687,6 +3695,10 @@ class TextEditor {
|
|
|
3687
3695
|
}
|
|
3688
3696
|
return this;
|
|
3689
3697
|
}
|
|
3698
|
+
insert(position, text) {
|
|
3699
|
+
this.update(position, position, text);
|
|
3700
|
+
return this;
|
|
3701
|
+
}
|
|
3690
3702
|
#getErased(start, end) {
|
|
3691
3703
|
if (this.#text.indexOf("\n", start) >= end) {
|
|
3692
3704
|
return " ".repeat(end - start);
|
|
@@ -4391,7 +4403,7 @@ class Reject {
|
|
|
4391
4403
|
}
|
|
4392
4404
|
if (this.#typeChecker.getTypeAtLocation(node).flags & this.#compiler.TypeFlags[capitalize(rejectedType)]) {
|
|
4393
4405
|
const text = [
|
|
4394
|
-
|
|
4406
|
+
belongsToArgumentList(node, this.#compiler)
|
|
4395
4407
|
? RejectDiagnosticText.argumentCannotBeOfType(rejectedType)
|
|
4396
4408
|
: RejectDiagnosticText.typeArgumentCannotBeOfType(rejectedType),
|
|
4397
4409
|
...RejectDiagnosticText.typeWasRejected(rejectedType),
|
|
@@ -4412,7 +4424,7 @@ class Ensure {
|
|
|
4412
4424
|
this.#compiler = compiler;
|
|
4413
4425
|
}
|
|
4414
4426
|
argument(node, enclosingNode, onDiagnostics) {
|
|
4415
|
-
if (!node || !
|
|
4427
|
+
if (!node || !belongsToArgumentList(node, this.#compiler)) {
|
|
4416
4428
|
this.#emitDiagnostic("An argument must be provided.", enclosingNode, onDiagnostics);
|
|
4417
4429
|
return false;
|
|
4418
4430
|
}
|
|
@@ -4425,16 +4437,33 @@ class Ensure {
|
|
|
4425
4437
|
}
|
|
4426
4438
|
return true;
|
|
4427
4439
|
}
|
|
4440
|
+
jsxSetup(program, node, onDiagnostics) {
|
|
4441
|
+
const diagnosticText = [];
|
|
4442
|
+
if (!program.getCompilerOptions().jsx) {
|
|
4443
|
+
diagnosticText.push("The matcher requires the 'jsx' compiler option to be configured.");
|
|
4444
|
+
}
|
|
4445
|
+
if (node.getSourceFile().languageVariant !== this.#compiler.LanguageVariant.JSX) {
|
|
4446
|
+
diagnosticText.push("The matcher requires a '.tsx' file extension.");
|
|
4447
|
+
}
|
|
4448
|
+
if (diagnosticText.length > 0) {
|
|
4449
|
+
this.#emitDiagnostic(diagnosticText, node, onDiagnostics);
|
|
4450
|
+
return false;
|
|
4451
|
+
}
|
|
4452
|
+
return true;
|
|
4453
|
+
}
|
|
4428
4454
|
typeArgument(node, enclosingNode, onDiagnostics) {
|
|
4429
|
-
if (!node ||
|
|
4455
|
+
if (!node || belongsToArgumentList(node, this.#compiler)) {
|
|
4430
4456
|
this.#emitDiagnostic("A type argument must be provided.", enclosingNode, onDiagnostics);
|
|
4431
4457
|
return false;
|
|
4432
4458
|
}
|
|
4433
4459
|
return true;
|
|
4434
4460
|
}
|
|
4435
4461
|
#emitDiagnostic(text, enclosingNode, onDiagnostics) {
|
|
4462
|
+
if (!Array.isArray(text)) {
|
|
4463
|
+
text = [text];
|
|
4464
|
+
}
|
|
4436
4465
|
const origin = DiagnosticOrigin.fromNode(enclosingNode);
|
|
4437
|
-
onDiagnostics(
|
|
4466
|
+
onDiagnostics(text.map((text) => Diagnostic.error(text, origin)));
|
|
4438
4467
|
}
|
|
4439
4468
|
}
|
|
4440
4469
|
|
|
@@ -4526,57 +4555,22 @@ class ExpectDiagnosticText {
|
|
|
4526
4555
|
}
|
|
4527
4556
|
}
|
|
4528
4557
|
|
|
4529
|
-
class
|
|
4530
|
-
|
|
4531
|
-
#compiler;
|
|
4532
|
-
#signatureCache = new Map();
|
|
4558
|
+
class MatcherBase {
|
|
4559
|
+
compiler;
|
|
4533
4560
|
typeChecker;
|
|
4534
|
-
constructor(compiler, program
|
|
4535
|
-
this
|
|
4561
|
+
constructor(compiler, program) {
|
|
4562
|
+
this.compiler = compiler;
|
|
4536
4563
|
this.typeChecker = program.getTypeChecker();
|
|
4537
|
-
this.assertionNode = assertionNode;
|
|
4538
|
-
}
|
|
4539
|
-
checkIsAssignableFrom(sourceNode, targetNode) {
|
|
4540
|
-
return this.#checkIsRelatedTo(targetNode, sourceNode);
|
|
4541
|
-
}
|
|
4542
|
-
checkIsAssignableTo(sourceNode, targetNode) {
|
|
4543
|
-
return this.#checkIsRelatedTo(sourceNode, targetNode);
|
|
4544
|
-
}
|
|
4545
|
-
#checkIsRelatedTo(sourceNode, targetNode) {
|
|
4546
|
-
const sourceType = this.getType(sourceNode);
|
|
4547
|
-
const targetType = this.getType(targetNode);
|
|
4548
|
-
return this.typeChecker.isTypeAssignableTo(sourceType, targetType);
|
|
4549
|
-
}
|
|
4550
|
-
extendsObjectType(type) {
|
|
4551
|
-
const nonPrimitiveType = "getNonPrimitiveType" in this.typeChecker
|
|
4552
|
-
? this.typeChecker.getNonPrimitiveType()
|
|
4553
|
-
: { flags: this.#compiler.TypeFlags.NonPrimitive };
|
|
4554
|
-
return this.typeChecker.isTypeAssignableTo(type, nonPrimitiveType);
|
|
4555
|
-
}
|
|
4556
|
-
getSignatures(node) {
|
|
4557
|
-
let signatures = this.#signatureCache.get(node);
|
|
4558
|
-
if (!signatures) {
|
|
4559
|
-
const type = this.getType(node);
|
|
4560
|
-
signatures = type.getCallSignatures();
|
|
4561
|
-
if (signatures.length === 0) {
|
|
4562
|
-
signatures = type.getConstructSignatures();
|
|
4563
|
-
}
|
|
4564
|
-
}
|
|
4565
|
-
return signatures;
|
|
4566
|
-
}
|
|
4567
|
-
getTypeText(node) {
|
|
4568
|
-
return this.typeChecker.typeToString(this.getType(node));
|
|
4569
4564
|
}
|
|
4570
4565
|
getType(node) {
|
|
4571
4566
|
return this.typeChecker.getTypeAtLocation(node);
|
|
4572
4567
|
}
|
|
4568
|
+
getTypeText(node, typeChecker) {
|
|
4569
|
+
return typeChecker.typeToString(typeChecker.getTypeAtLocation(node));
|
|
4570
|
+
}
|
|
4573
4571
|
}
|
|
4574
4572
|
|
|
4575
|
-
class AbilityMatcherBase {
|
|
4576
|
-
compiler;
|
|
4577
|
-
constructor(compiler) {
|
|
4578
|
-
this.compiler = compiler;
|
|
4579
|
-
}
|
|
4573
|
+
class AbilityMatcherBase extends MatcherBase {
|
|
4580
4574
|
getArgumentCountText(nodes) {
|
|
4581
4575
|
if (nodes.length === 0) {
|
|
4582
4576
|
return "without arguments";
|
|
@@ -4592,19 +4586,19 @@ class AbilityMatcherBase {
|
|
|
4592
4586
|
}
|
|
4593
4587
|
return `with the given type argument${targetNode.elements.length === 1 ? "" : "s"}`;
|
|
4594
4588
|
}
|
|
4595
|
-
explain(
|
|
4596
|
-
const isExpression =
|
|
4589
|
+
explain(expectNode, sourceNode, targetNode, getArgumentCountText) {
|
|
4590
|
+
const isExpression = belongsToArgumentList(sourceNode, this.compiler);
|
|
4597
4591
|
const argumentCountText = getArgumentCountText?.();
|
|
4598
4592
|
const diagnostics = [];
|
|
4599
|
-
if (
|
|
4600
|
-
for (const diagnostic of
|
|
4593
|
+
if (expectNode.abilityDiagnostics.size > 0) {
|
|
4594
|
+
for (const diagnostic of expectNode.abilityDiagnostics) {
|
|
4601
4595
|
const text = [this.explainNotText(isExpression, argumentCountText), getDiagnosticMessageText(diagnostic)];
|
|
4602
4596
|
let origin;
|
|
4603
4597
|
if (isDiagnosticWithLocation(diagnostic) && diagnosticBelongsToNode(diagnostic, targetNode)) {
|
|
4604
|
-
origin = DiagnosticOrigin.fromAbilityDiagnostic(diagnostic,
|
|
4598
|
+
origin = DiagnosticOrigin.fromAbilityDiagnostic(diagnostic, expectNode);
|
|
4605
4599
|
}
|
|
4606
4600
|
else {
|
|
4607
|
-
origin = DiagnosticOrigin.fromAssertion(
|
|
4601
|
+
origin = DiagnosticOrigin.fromAssertion(expectNode);
|
|
4608
4602
|
}
|
|
4609
4603
|
let related;
|
|
4610
4604
|
if (diagnostic.relatedInformation != null) {
|
|
@@ -4614,7 +4608,7 @@ class AbilityMatcherBase {
|
|
|
4614
4608
|
}
|
|
4615
4609
|
}
|
|
4616
4610
|
else {
|
|
4617
|
-
const origin = DiagnosticOrigin.fromAssertion(
|
|
4611
|
+
const origin = DiagnosticOrigin.fromAssertion(expectNode);
|
|
4618
4612
|
diagnostics.push(Diagnostic.error(this.explainText(isExpression, argumentCountText), origin));
|
|
4619
4613
|
}
|
|
4620
4614
|
return diagnostics;
|
|
@@ -4624,12 +4618,15 @@ class AbilityMatcherBase {
|
|
|
4624
4618
|
class ToAcceptProps extends AbilityMatcherBase {
|
|
4625
4619
|
explainText = ExpectDiagnosticText.acceptsProps;
|
|
4626
4620
|
explainNotText = ExpectDiagnosticText.doesNotAcceptProps;
|
|
4627
|
-
match(
|
|
4621
|
+
match(expectNode, sourceNode, targetNode, onDiagnostics) {
|
|
4628
4622
|
const diagnostics = [];
|
|
4629
|
-
const
|
|
4630
|
-
if (
|
|
4631
|
-
|
|
4632
|
-
const
|
|
4623
|
+
const sourceType = this.getType(sourceNode);
|
|
4624
|
+
if (!isCapitaizedIdentifierLike(sourceNode, this.compiler) ||
|
|
4625
|
+
!(sourceType.getCallSignatures().length > 0 || sourceType.getConstructSignatures().length > 0)) {
|
|
4626
|
+
const expectedText = !isIdentifierLike(sourceNode, this.compiler)
|
|
4627
|
+
? "an identifier of a JSX component"
|
|
4628
|
+
: "an identifier that begins with an uppercase letter";
|
|
4629
|
+
const text = belongsToArgumentList(sourceNode, this.compiler)
|
|
4633
4630
|
? ExpectDiagnosticText.argumentMustBe(expectedText)
|
|
4634
4631
|
: ExpectDiagnosticText.typeArgumentMustBe(expectedText);
|
|
4635
4632
|
const origin = DiagnosticOrigin.fromNode(sourceNode);
|
|
@@ -4662,8 +4659,8 @@ class ToAcceptProps extends AbilityMatcherBase {
|
|
|
4662
4659
|
return;
|
|
4663
4660
|
}
|
|
4664
4661
|
return {
|
|
4665
|
-
explain: () => this.explain(
|
|
4666
|
-
isMatch:
|
|
4662
|
+
explain: () => this.explain(expectNode, sourceNode, targetNode),
|
|
4663
|
+
isMatch: expectNode.abilityDiagnostics.size === 0,
|
|
4667
4664
|
};
|
|
4668
4665
|
}
|
|
4669
4666
|
}
|
|
@@ -5156,7 +5153,7 @@ class Structure {
|
|
|
5156
5153
|
return result;
|
|
5157
5154
|
}
|
|
5158
5155
|
#memoize(a, b, compare) {
|
|
5159
|
-
const key = [a.id, b.id].sort().join(":");
|
|
5156
|
+
const key = [a.id, b.id].sort((a, b) => a - b).join(":");
|
|
5160
5157
|
const result = this.#memoizeCache.get(key);
|
|
5161
5158
|
if (result !== undefined) {
|
|
5162
5159
|
return result !== 2;
|
|
@@ -5180,14 +5177,14 @@ class Structure {
|
|
|
5180
5177
|
}
|
|
5181
5178
|
}
|
|
5182
5179
|
|
|
5183
|
-
class RelationMatcherBase {
|
|
5184
|
-
explain(
|
|
5185
|
-
const sourceTypeText =
|
|
5186
|
-
const targetTypeText =
|
|
5187
|
-
const text =
|
|
5180
|
+
class RelationMatcherBase extends MatcherBase {
|
|
5181
|
+
explain(expectNode, sourceNode, targetNode) {
|
|
5182
|
+
const sourceTypeText = this.getTypeText(sourceNode, this.typeChecker);
|
|
5183
|
+
const targetTypeText = this.getTypeText(targetNode, this.typeChecker);
|
|
5184
|
+
const text = expectNode.isNot
|
|
5188
5185
|
? this.explainText(sourceTypeText, targetTypeText)
|
|
5189
5186
|
: this.explainNotText(sourceTypeText, targetTypeText);
|
|
5190
|
-
const origin = DiagnosticOrigin.fromNode(targetNode,
|
|
5187
|
+
const origin = DiagnosticOrigin.fromNode(targetNode, expectNode);
|
|
5191
5188
|
return [Diagnostic.error(text, origin)];
|
|
5192
5189
|
}
|
|
5193
5190
|
}
|
|
@@ -5195,42 +5192,38 @@ class RelationMatcherBase {
|
|
|
5195
5192
|
class ToBe extends RelationMatcherBase {
|
|
5196
5193
|
#structure;
|
|
5197
5194
|
constructor(compiler, program) {
|
|
5198
|
-
super();
|
|
5195
|
+
super(compiler, program);
|
|
5199
5196
|
this.#structure = new Structure(compiler, program);
|
|
5200
5197
|
}
|
|
5201
5198
|
explainText = ExpectDiagnosticText.isTheSame;
|
|
5202
5199
|
explainNotText = ExpectDiagnosticText.isNotTheSame;
|
|
5203
|
-
match(
|
|
5200
|
+
match(expectNode, sourceNode, targetNode) {
|
|
5204
5201
|
return {
|
|
5205
|
-
explain: () => this.explain(
|
|
5206
|
-
isMatch: this.#structure.compare(
|
|
5202
|
+
explain: () => this.explain(expectNode, sourceNode, targetNode),
|
|
5203
|
+
isMatch: this.#structure.compare(this.getType(sourceNode), this.getType(targetNode)),
|
|
5207
5204
|
};
|
|
5208
5205
|
}
|
|
5209
5206
|
}
|
|
5210
5207
|
|
|
5211
|
-
class ToBeApplicable {
|
|
5212
|
-
#compiler;
|
|
5213
|
-
constructor(compiler) {
|
|
5214
|
-
this.#compiler = compiler;
|
|
5215
|
-
}
|
|
5208
|
+
class ToBeApplicable extends MatcherBase {
|
|
5216
5209
|
#resolveTargetText(node) {
|
|
5217
5210
|
let text = "";
|
|
5218
5211
|
switch (node.kind) {
|
|
5219
|
-
case this
|
|
5212
|
+
case this.compiler.SyntaxKind.ClassDeclaration:
|
|
5220
5213
|
text = "class";
|
|
5221
5214
|
break;
|
|
5222
|
-
case this
|
|
5215
|
+
case this.compiler.SyntaxKind.MethodDeclaration:
|
|
5223
5216
|
text = "method";
|
|
5224
5217
|
break;
|
|
5225
|
-
case this
|
|
5226
|
-
text = node.modifiers?.some((modifier) => modifier.kind === this
|
|
5218
|
+
case this.compiler.SyntaxKind.PropertyDeclaration:
|
|
5219
|
+
text = node.modifiers?.some((modifier) => modifier.kind === this.compiler.SyntaxKind.AccessorKeyword)
|
|
5227
5220
|
? "accessor"
|
|
5228
5221
|
: "field";
|
|
5229
5222
|
break;
|
|
5230
|
-
case this
|
|
5223
|
+
case this.compiler.SyntaxKind.GetAccessor:
|
|
5231
5224
|
text = "getter";
|
|
5232
5225
|
break;
|
|
5233
|
-
case this
|
|
5226
|
+
case this.compiler.SyntaxKind.SetAccessor:
|
|
5234
5227
|
text = "setter";
|
|
5235
5228
|
break;
|
|
5236
5229
|
}
|
|
@@ -5239,12 +5232,12 @@ class ToBeApplicable {
|
|
|
5239
5232
|
}
|
|
5240
5233
|
return text;
|
|
5241
5234
|
}
|
|
5242
|
-
#explain(
|
|
5243
|
-
const targetText = this.#resolveTargetText(
|
|
5235
|
+
#explain(expectNode) {
|
|
5236
|
+
const targetText = this.#resolveTargetText(expectNode.matcherNode.parent);
|
|
5244
5237
|
const diagnostics = [];
|
|
5245
|
-
if (
|
|
5246
|
-
const origin = DiagnosticOrigin.fromAssertion(
|
|
5247
|
-
for (const diagnostic of
|
|
5238
|
+
if (expectNode.abilityDiagnostics.size > 0) {
|
|
5239
|
+
const origin = DiagnosticOrigin.fromAssertion(expectNode);
|
|
5240
|
+
for (const diagnostic of expectNode.abilityDiagnostics) {
|
|
5248
5241
|
const text = [ExpectDiagnosticText.cannotBeApplied(targetText), getDiagnosticMessageText(diagnostic)];
|
|
5249
5242
|
let related;
|
|
5250
5243
|
if (diagnostic.relatedInformation != null) {
|
|
@@ -5254,16 +5247,16 @@ class ToBeApplicable {
|
|
|
5254
5247
|
}
|
|
5255
5248
|
}
|
|
5256
5249
|
else {
|
|
5257
|
-
const origin = DiagnosticOrigin.fromAssertion(
|
|
5250
|
+
const origin = DiagnosticOrigin.fromAssertion(expectNode);
|
|
5258
5251
|
diagnostics.push(Diagnostic.error(ExpectDiagnosticText.canBeApplied(targetText), origin));
|
|
5259
5252
|
}
|
|
5260
5253
|
return diagnostics;
|
|
5261
5254
|
}
|
|
5262
|
-
match(
|
|
5263
|
-
const type =
|
|
5255
|
+
match(expectNode, sourceNode, onDiagnostics) {
|
|
5256
|
+
const type = this.getType(sourceNode);
|
|
5264
5257
|
if (type.getCallSignatures().length === 0) {
|
|
5265
5258
|
const expectedText = "of a function type";
|
|
5266
|
-
const text =
|
|
5259
|
+
const text = belongsToArgumentList(sourceNode, this.compiler)
|
|
5267
5260
|
? ExpectDiagnosticText.argumentMustBe(expectedText)
|
|
5268
5261
|
: ExpectDiagnosticText.typeArgumentMustBe(expectedText);
|
|
5269
5262
|
const origin = DiagnosticOrigin.fromNode(sourceNode);
|
|
@@ -5271,8 +5264,8 @@ class ToBeApplicable {
|
|
|
5271
5264
|
return;
|
|
5272
5265
|
}
|
|
5273
5266
|
return {
|
|
5274
|
-
explain: () => this.#explain(
|
|
5275
|
-
isMatch:
|
|
5267
|
+
explain: () => this.#explain(expectNode),
|
|
5268
|
+
isMatch: expectNode.abilityDiagnostics.size === 0,
|
|
5276
5269
|
};
|
|
5277
5270
|
}
|
|
5278
5271
|
}
|
|
@@ -5280,10 +5273,10 @@ class ToBeApplicable {
|
|
|
5280
5273
|
class ToBeAssignableFrom extends RelationMatcherBase {
|
|
5281
5274
|
explainText = ExpectDiagnosticText.isAssignableFrom;
|
|
5282
5275
|
explainNotText = ExpectDiagnosticText.isNotAssignableFrom;
|
|
5283
|
-
match(
|
|
5276
|
+
match(expectNode, sourceNode, targetNode) {
|
|
5284
5277
|
return {
|
|
5285
|
-
explain: () => this.explain(
|
|
5286
|
-
isMatch:
|
|
5278
|
+
explain: () => this.explain(expectNode, sourceNode, targetNode),
|
|
5279
|
+
isMatch: this.typeChecker.isTypeAssignableTo(this.getType(targetNode), this.getType(sourceNode)),
|
|
5287
5280
|
};
|
|
5288
5281
|
}
|
|
5289
5282
|
}
|
|
@@ -5291,10 +5284,10 @@ class ToBeAssignableFrom extends RelationMatcherBase {
|
|
|
5291
5284
|
class ToBeAssignableTo extends RelationMatcherBase {
|
|
5292
5285
|
explainText = ExpectDiagnosticText.isAssignableTo;
|
|
5293
5286
|
explainNotText = ExpectDiagnosticText.isNotAssignableTo;
|
|
5294
|
-
match(
|
|
5287
|
+
match(expectNode, sourceNode, targetNode) {
|
|
5295
5288
|
return {
|
|
5296
|
-
explain: () => this.explain(
|
|
5297
|
-
isMatch:
|
|
5289
|
+
explain: () => this.explain(expectNode, sourceNode, targetNode),
|
|
5290
|
+
isMatch: this.typeChecker.isTypeAssignableTo(this.getType(sourceNode), this.getType(targetNode)),
|
|
5298
5291
|
};
|
|
5299
5292
|
}
|
|
5300
5293
|
}
|
|
@@ -5302,11 +5295,11 @@ class ToBeAssignableTo extends RelationMatcherBase {
|
|
|
5302
5295
|
class ToBeCallableWith extends AbilityMatcherBase {
|
|
5303
5296
|
explainText = ExpectDiagnosticText.isCallable;
|
|
5304
5297
|
explainNotText = ExpectDiagnosticText.isNotCallable;
|
|
5305
|
-
match(
|
|
5306
|
-
const sourceType =
|
|
5298
|
+
match(expectNode, sourceNode, targetNodes, onDiagnostics) {
|
|
5299
|
+
const sourceType = this.getType(sourceNode);
|
|
5307
5300
|
if (sourceType.getCallSignatures().length === 0) {
|
|
5308
5301
|
const text = [];
|
|
5309
|
-
if (
|
|
5302
|
+
if (belongsToArgumentList(sourceNode, this.compiler)) {
|
|
5310
5303
|
text.push(ExpectDiagnosticText.argumentMustBe("a callable expression"));
|
|
5311
5304
|
}
|
|
5312
5305
|
else {
|
|
@@ -5320,8 +5313,8 @@ class ToBeCallableWith extends AbilityMatcherBase {
|
|
|
5320
5313
|
return;
|
|
5321
5314
|
}
|
|
5322
5315
|
return {
|
|
5323
|
-
explain: () => this.explain(
|
|
5324
|
-
isMatch:
|
|
5316
|
+
explain: () => this.explain(expectNode, sourceNode, targetNodes, () => this.getArgumentCountText(targetNodes)),
|
|
5317
|
+
isMatch: expectNode.abilityDiagnostics.size === 0,
|
|
5325
5318
|
};
|
|
5326
5319
|
}
|
|
5327
5320
|
}
|
|
@@ -5329,11 +5322,11 @@ class ToBeCallableWith extends AbilityMatcherBase {
|
|
|
5329
5322
|
class ToBeConstructableWith extends AbilityMatcherBase {
|
|
5330
5323
|
explainText = ExpectDiagnosticText.isConstructable;
|
|
5331
5324
|
explainNotText = ExpectDiagnosticText.isNotConstructable;
|
|
5332
|
-
match(
|
|
5333
|
-
const sourceType =
|
|
5325
|
+
match(expectNode, sourceNode, targetNodes, onDiagnostics) {
|
|
5326
|
+
const sourceType = this.getType(sourceNode);
|
|
5334
5327
|
if (sourceType.getConstructSignatures().length === 0) {
|
|
5335
5328
|
const text = [];
|
|
5336
|
-
if (
|
|
5329
|
+
if (belongsToArgumentList(sourceNode, this.compiler)) {
|
|
5337
5330
|
text.push(ExpectDiagnosticText.argumentMustBe("a constructable expression"));
|
|
5338
5331
|
}
|
|
5339
5332
|
else {
|
|
@@ -5347,8 +5340,8 @@ class ToBeConstructableWith extends AbilityMatcherBase {
|
|
|
5347
5340
|
return;
|
|
5348
5341
|
}
|
|
5349
5342
|
return {
|
|
5350
|
-
explain: () => this.explain(
|
|
5351
|
-
isMatch:
|
|
5343
|
+
explain: () => this.explain(expectNode, sourceNode, targetNodes, () => this.getArgumentCountText(targetNodes)),
|
|
5344
|
+
isMatch: expectNode.abilityDiagnostics.size === 0,
|
|
5352
5345
|
};
|
|
5353
5346
|
}
|
|
5354
5347
|
}
|
|
@@ -5356,13 +5349,10 @@ class ToBeConstructableWith extends AbilityMatcherBase {
|
|
|
5356
5349
|
class ToBeInstantiableWith extends AbilityMatcherBase {
|
|
5357
5350
|
explainText = ExpectDiagnosticText.isInstantiable;
|
|
5358
5351
|
explainNotText = ExpectDiagnosticText.isNotInstantiable;
|
|
5359
|
-
match(
|
|
5360
|
-
if (!(this.compiler
|
|
5361
|
-
this.compiler.isPropertyAccessExpression(sourceNode) ||
|
|
5362
|
-
this.compiler.isTypeReferenceNode(sourceNode) ||
|
|
5363
|
-
this.compiler.isExpressionWithTypeArguments(sourceNode))) {
|
|
5352
|
+
match(expectNode, sourceNode, targetNode, onDiagnostics) {
|
|
5353
|
+
if (!isIdentifierLike(sourceNode, this.compiler)) {
|
|
5364
5354
|
let text;
|
|
5365
|
-
if (
|
|
5355
|
+
if (belongsToArgumentList(sourceNode, this.compiler)) {
|
|
5366
5356
|
text = ExpectDiagnosticText.argumentMustBe("an instantiable expression");
|
|
5367
5357
|
}
|
|
5368
5358
|
else {
|
|
@@ -5379,49 +5369,51 @@ class ToBeInstantiableWith extends AbilityMatcherBase {
|
|
|
5379
5369
|
return;
|
|
5380
5370
|
}
|
|
5381
5371
|
return {
|
|
5382
|
-
explain: () => this.explain(
|
|
5383
|
-
isMatch:
|
|
5372
|
+
explain: () => this.explain(expectNode, sourceNode, targetNode, () => this.getTypeArgumentCountText(targetNode)),
|
|
5373
|
+
isMatch: expectNode.abilityDiagnostics.size === 0,
|
|
5384
5374
|
};
|
|
5385
5375
|
}
|
|
5386
5376
|
}
|
|
5387
5377
|
|
|
5388
|
-
class ToHaveProperty {
|
|
5389
|
-
#
|
|
5390
|
-
|
|
5391
|
-
|
|
5392
|
-
}
|
|
5393
|
-
#explain(matchWorker, sourceNode, targetNode) {
|
|
5394
|
-
const sourceTypeText = matchWorker.getTypeText(sourceNode);
|
|
5395
|
-
const targetType = matchWorker.getType(targetNode);
|
|
5378
|
+
class ToHaveProperty extends MatcherBase {
|
|
5379
|
+
#explain(expectNode, sourceNode, targetNode) {
|
|
5380
|
+
const sourceTypeText = this.getTypeText(sourceNode, this.typeChecker);
|
|
5381
|
+
const targetType = this.getType(targetNode);
|
|
5396
5382
|
let propertyNameText;
|
|
5397
|
-
if (targetType.flags & (this
|
|
5383
|
+
if (targetType.flags & (this.compiler.TypeFlags.StringLiteral | this.compiler.TypeFlags.NumberLiteral)) {
|
|
5398
5384
|
propertyNameText = targetType.value.toString();
|
|
5399
5385
|
}
|
|
5400
5386
|
else {
|
|
5401
|
-
propertyNameText = `[${this
|
|
5387
|
+
propertyNameText = `[${this.compiler.unescapeLeadingUnderscores(targetType.symbol.escapedName)}]`;
|
|
5402
5388
|
}
|
|
5403
|
-
const origin = DiagnosticOrigin.fromNode(targetNode,
|
|
5404
|
-
return
|
|
5389
|
+
const origin = DiagnosticOrigin.fromNode(targetNode, expectNode);
|
|
5390
|
+
return expectNode.isNot
|
|
5405
5391
|
? [Diagnostic.error(ExpectDiagnosticText.hasProperty(sourceTypeText, propertyNameText), origin)]
|
|
5406
5392
|
: [Diagnostic.error(ExpectDiagnosticText.doesNotHaveProperty(sourceTypeText, propertyNameText), origin)];
|
|
5407
5393
|
}
|
|
5408
|
-
|
|
5394
|
+
#extendsObjectType(type) {
|
|
5395
|
+
const nonPrimitiveType = "getNonPrimitiveType" in this.typeChecker
|
|
5396
|
+
? this.typeChecker.getNonPrimitiveType()
|
|
5397
|
+
: { flags: this.compiler.TypeFlags.NonPrimitive };
|
|
5398
|
+
return this.typeChecker.isTypeAssignableTo(type, nonPrimitiveType);
|
|
5399
|
+
}
|
|
5400
|
+
match(expectNode, sourceNode, targetNode, onDiagnostics) {
|
|
5409
5401
|
const diagnostics = [];
|
|
5410
|
-
const sourceType =
|
|
5411
|
-
if (sourceType.flags & (this
|
|
5412
|
-
!
|
|
5402
|
+
const sourceType = this.getType(sourceNode);
|
|
5403
|
+
if (sourceType.flags & (this.compiler.TypeFlags.Any | this.compiler.TypeFlags.Never) ||
|
|
5404
|
+
!this.#extendsObjectType(sourceType)) {
|
|
5413
5405
|
const expectedText = "of an object type";
|
|
5414
|
-
const text =
|
|
5406
|
+
const text = belongsToArgumentList(sourceNode, this.compiler)
|
|
5415
5407
|
? ExpectDiagnosticText.argumentMustBe(expectedText)
|
|
5416
5408
|
: ExpectDiagnosticText.typeArgumentMustBe(expectedText);
|
|
5417
5409
|
const origin = DiagnosticOrigin.fromNode(sourceNode);
|
|
5418
5410
|
diagnostics.push(Diagnostic.error(text, origin));
|
|
5419
5411
|
}
|
|
5420
|
-
const targetType =
|
|
5412
|
+
const targetType = this.getType(targetNode);
|
|
5421
5413
|
if (!(targetType.flags &
|
|
5422
|
-
(this
|
|
5423
|
-
this
|
|
5424
|
-
this
|
|
5414
|
+
(this.compiler.TypeFlags.StringLiteral |
|
|
5415
|
+
this.compiler.TypeFlags.NumberLiteral |
|
|
5416
|
+
this.compiler.TypeFlags.UniqueESSymbol))) {
|
|
5425
5417
|
const expectedText = "a string, number or symbol";
|
|
5426
5418
|
const text = ExpectDiagnosticText.argumentMustBe(expectedText);
|
|
5427
5419
|
const origin = DiagnosticOrigin.fromNode(targetNode);
|
|
@@ -5432,8 +5424,8 @@ class ToHaveProperty {
|
|
|
5432
5424
|
return;
|
|
5433
5425
|
}
|
|
5434
5426
|
return {
|
|
5435
|
-
explain: () => this.#explain(
|
|
5436
|
-
isMatch:
|
|
5427
|
+
explain: () => this.#explain(expectNode, sourceNode, targetNode),
|
|
5428
|
+
isMatch: expectNode.abilityDiagnostics.size === 0,
|
|
5437
5429
|
};
|
|
5438
5430
|
}
|
|
5439
5431
|
}
|
|
@@ -5443,30 +5435,30 @@ class ToRaiseError {
|
|
|
5443
5435
|
constructor(compiler) {
|
|
5444
5436
|
this.#compiler = compiler;
|
|
5445
5437
|
}
|
|
5446
|
-
#explain(
|
|
5447
|
-
const isExpression =
|
|
5448
|
-
const origin = DiagnosticOrigin.fromAssertion(
|
|
5449
|
-
if (
|
|
5438
|
+
#explain(expectNode, sourceNode, targetNodes) {
|
|
5439
|
+
const isExpression = belongsToArgumentList(sourceNode, this.#compiler);
|
|
5440
|
+
const origin = DiagnosticOrigin.fromAssertion(expectNode);
|
|
5441
|
+
if (expectNode.diagnostics.size === 0) {
|
|
5450
5442
|
const text = ExpectDiagnosticText.didNotRaiseError(isExpression);
|
|
5451
5443
|
return [Diagnostic.error(text, origin)];
|
|
5452
5444
|
}
|
|
5453
|
-
if (
|
|
5454
|
-
const count =
|
|
5445
|
+
if (expectNode.diagnostics.size !== targetNodes.length) {
|
|
5446
|
+
const count = expectNode.diagnostics.size;
|
|
5455
5447
|
const text = ExpectDiagnosticText.raisedError(isExpression, count, targetNodes.length);
|
|
5456
5448
|
const related = [
|
|
5457
5449
|
Diagnostic.error(ExpectDiagnosticText.raisedTypeError(count)),
|
|
5458
|
-
...Diagnostic.fromDiagnostics([...
|
|
5450
|
+
...Diagnostic.fromDiagnostics([...expectNode.diagnostics]),
|
|
5459
5451
|
];
|
|
5460
5452
|
return [Diagnostic.error(text, origin).add({ related })];
|
|
5461
5453
|
}
|
|
5462
|
-
return [...
|
|
5454
|
+
return [...expectNode.diagnostics].reduce((accumulator, diagnostic, index) => {
|
|
5463
5455
|
const targetNode = targetNodes[index];
|
|
5464
5456
|
const isMatch = this.#matchExpectedError(diagnostic, targetNode);
|
|
5465
|
-
if (
|
|
5466
|
-
const text =
|
|
5457
|
+
if (expectNode.isNot ? isMatch : !isMatch) {
|
|
5458
|
+
const text = expectNode.isNot
|
|
5467
5459
|
? ExpectDiagnosticText.raisedMatchingError(isExpression)
|
|
5468
5460
|
: ExpectDiagnosticText.didNotRaiseMatchingError(isExpression);
|
|
5469
|
-
const origin = DiagnosticOrigin.fromNode(targetNode,
|
|
5461
|
+
const origin = DiagnosticOrigin.fromNode(targetNode, expectNode);
|
|
5470
5462
|
const related = [
|
|
5471
5463
|
Diagnostic.error(ExpectDiagnosticText.raisedTypeError()),
|
|
5472
5464
|
...Diagnostic.fromDiagnostics([diagnostic]),
|
|
@@ -5476,7 +5468,7 @@ class ToRaiseError {
|
|
|
5476
5468
|
return accumulator;
|
|
5477
5469
|
}, []);
|
|
5478
5470
|
}
|
|
5479
|
-
match(
|
|
5471
|
+
match(expectNode, sourceNode, targetNodes, onDiagnostics) {
|
|
5480
5472
|
const diagnostics = [];
|
|
5481
5473
|
for (const targetNode of targetNodes) {
|
|
5482
5474
|
if (!(this.#compiler.isStringLiteralLike(targetNode) ||
|
|
@@ -5494,15 +5486,15 @@ class ToRaiseError {
|
|
|
5494
5486
|
}
|
|
5495
5487
|
let isMatch;
|
|
5496
5488
|
if (targetNodes.length === 0) {
|
|
5497
|
-
isMatch =
|
|
5489
|
+
isMatch = expectNode.diagnostics.size > 0;
|
|
5498
5490
|
}
|
|
5499
5491
|
else {
|
|
5500
5492
|
isMatch =
|
|
5501
|
-
|
|
5502
|
-
[...
|
|
5493
|
+
expectNode.diagnostics.size === targetNodes.length &&
|
|
5494
|
+
[...expectNode.diagnostics].every((diagnostic, index) => this.#matchExpectedError(diagnostic, targetNodes[index]));
|
|
5503
5495
|
}
|
|
5504
5496
|
return {
|
|
5505
|
-
explain: () => this.#explain(
|
|
5497
|
+
explain: () => this.#explain(expectNode, sourceNode, targetNodes),
|
|
5506
5498
|
isMatch,
|
|
5507
5499
|
};
|
|
5508
5500
|
}
|
|
@@ -5523,7 +5515,6 @@ class ToRaiseError {
|
|
|
5523
5515
|
}
|
|
5524
5516
|
|
|
5525
5517
|
class ExpectService {
|
|
5526
|
-
#compiler;
|
|
5527
5518
|
#ensure;
|
|
5528
5519
|
#program;
|
|
5529
5520
|
#reject;
|
|
@@ -5538,29 +5529,31 @@ class ExpectService {
|
|
|
5538
5529
|
toHaveProperty;
|
|
5539
5530
|
toRaiseError;
|
|
5540
5531
|
constructor(compiler, program, resolvedConfig) {
|
|
5541
|
-
this.#compiler = compiler;
|
|
5542
5532
|
this.#program = program;
|
|
5543
5533
|
this.#ensure = new Ensure(compiler);
|
|
5544
5534
|
this.#reject = new Reject(compiler, program, resolvedConfig);
|
|
5545
|
-
this.toAcceptProps = new ToAcceptProps(compiler);
|
|
5535
|
+
this.toAcceptProps = new ToAcceptProps(compiler, program);
|
|
5546
5536
|
this.toBe = new ToBe(compiler, program);
|
|
5547
|
-
this.toBeApplicable = new ToBeApplicable(compiler);
|
|
5548
|
-
this.toBeAssignableFrom = new ToBeAssignableFrom();
|
|
5549
|
-
this.toBeAssignableTo = new ToBeAssignableTo();
|
|
5550
|
-
this.toBeCallableWith = new ToBeCallableWith(compiler);
|
|
5551
|
-
this.toBeConstructableWith = new ToBeConstructableWith(compiler);
|
|
5552
|
-
this.toBeInstantiableWith = new ToBeInstantiableWith(compiler);
|
|
5553
|
-
this.toHaveProperty = new ToHaveProperty(compiler);
|
|
5537
|
+
this.toBeApplicable = new ToBeApplicable(compiler, program);
|
|
5538
|
+
this.toBeAssignableFrom = new ToBeAssignableFrom(compiler, program);
|
|
5539
|
+
this.toBeAssignableTo = new ToBeAssignableTo(compiler, program);
|
|
5540
|
+
this.toBeCallableWith = new ToBeCallableWith(compiler, program);
|
|
5541
|
+
this.toBeConstructableWith = new ToBeConstructableWith(compiler, program);
|
|
5542
|
+
this.toBeInstantiableWith = new ToBeInstantiableWith(compiler, program);
|
|
5543
|
+
this.toHaveProperty = new ToHaveProperty(compiler, program);
|
|
5554
5544
|
this.toRaiseError = new ToRaiseError(compiler);
|
|
5555
5545
|
}
|
|
5556
|
-
match(
|
|
5557
|
-
const matcherNameText =
|
|
5558
|
-
if (
|
|
5546
|
+
match(expectNode, onDiagnostics) {
|
|
5547
|
+
const matcherNameText = expectNode.matcherNameNode.name.text;
|
|
5548
|
+
if (matcherNameText === "toAcceptProps" &&
|
|
5549
|
+
!this.#ensure.jsxSetup(this.#program, expectNode.matcherNameNode.name, onDiagnostics)) {
|
|
5550
|
+
return;
|
|
5551
|
+
}
|
|
5552
|
+
if (!this.#ensure.argumentOrTypeArgument(expectNode.source[0], expectNode.node.expression, onDiagnostics)) {
|
|
5559
5553
|
return;
|
|
5560
5554
|
}
|
|
5561
|
-
|
|
5562
|
-
|
|
5563
|
-
this.#reject.argumentType([assertionNode.source[0], assertionNode.target?.[0]], onDiagnostics)) {
|
|
5555
|
+
if (!(matcherNameText === "toBeInstantiableWith" || (matcherNameText === "toRaiseError" && !expectNode.isNot)) &&
|
|
5556
|
+
this.#reject.argumentType([expectNode.source[0], expectNode.target?.[0]], onDiagnostics)) {
|
|
5564
5557
|
return;
|
|
5565
5558
|
}
|
|
5566
5559
|
switch (matcherNameText) {
|
|
@@ -5568,35 +5561,35 @@ class ExpectService {
|
|
|
5568
5561
|
case "toBe":
|
|
5569
5562
|
case "toBeAssignableFrom":
|
|
5570
5563
|
case "toBeAssignableTo":
|
|
5571
|
-
if (!this.#ensure.argumentOrTypeArgument(
|
|
5564
|
+
if (!this.#ensure.argumentOrTypeArgument(expectNode.target?.[0], expectNode.matcherNameNode.name, onDiagnostics)) {
|
|
5572
5565
|
return;
|
|
5573
5566
|
}
|
|
5574
|
-
return this[matcherNameText].match(
|
|
5567
|
+
return this[matcherNameText].match(expectNode, expectNode.source[0], expectNode.target[0], onDiagnostics);
|
|
5575
5568
|
case "toBeApplicable":
|
|
5576
|
-
return this.toBeApplicable.match(
|
|
5569
|
+
return this.toBeApplicable.match(expectNode, expectNode.source[0], onDiagnostics);
|
|
5577
5570
|
case "toBeCallableWith":
|
|
5578
5571
|
case "toBeConstructableWith":
|
|
5579
5572
|
case "toRaiseError":
|
|
5580
|
-
return this[matcherNameText].match(
|
|
5573
|
+
return this[matcherNameText].match(expectNode, expectNode.source[0], expectNode.target, onDiagnostics);
|
|
5581
5574
|
case "toBeInstantiableWith": {
|
|
5582
|
-
if (!this.#ensure.typeArgument(
|
|
5575
|
+
if (!this.#ensure.typeArgument(expectNode.target?.[0], expectNode.matcherNameNode.name, onDiagnostics)) {
|
|
5583
5576
|
return;
|
|
5584
5577
|
}
|
|
5585
|
-
return this.toBeInstantiableWith.match(
|
|
5578
|
+
return this.toBeInstantiableWith.match(expectNode, expectNode.source[0], expectNode.target[0], onDiagnostics);
|
|
5586
5579
|
}
|
|
5587
5580
|
case "toHaveProperty":
|
|
5588
|
-
if (!this.#ensure.argument(
|
|
5581
|
+
if (!this.#ensure.argument(expectNode.target?.[0], expectNode.matcherNameNode.name, onDiagnostics)) {
|
|
5589
5582
|
return;
|
|
5590
5583
|
}
|
|
5591
|
-
return this.toHaveProperty.match(
|
|
5584
|
+
return this.toHaveProperty.match(expectNode, expectNode.source[0], expectNode.target[0], onDiagnostics);
|
|
5592
5585
|
default:
|
|
5593
|
-
this.#onMatcherIsNotSupported(matcherNameText,
|
|
5586
|
+
this.#onMatcherIsNotSupported(matcherNameText, expectNode, onDiagnostics);
|
|
5594
5587
|
}
|
|
5595
5588
|
return;
|
|
5596
5589
|
}
|
|
5597
|
-
#onMatcherIsNotSupported(matcherNameText,
|
|
5590
|
+
#onMatcherIsNotSupported(matcherNameText, expectNode, onDiagnostics) {
|
|
5598
5591
|
const text = ExpectDiagnosticText.matcherIsNotSupported(matcherNameText);
|
|
5599
|
-
const origin = DiagnosticOrigin.fromNode(
|
|
5592
|
+
const origin = DiagnosticOrigin.fromNode(expectNode.matcherNameNode.name);
|
|
5600
5593
|
onDiagnostics(Diagnostic.error(text, origin));
|
|
5601
5594
|
}
|
|
5602
5595
|
}
|
|
@@ -5908,7 +5901,7 @@ class FileRunner {
|
|
|
5908
5901
|
class Runner {
|
|
5909
5902
|
#eventEmitter = new EventEmitter();
|
|
5910
5903
|
#resolvedConfig;
|
|
5911
|
-
static version = "7.2.
|
|
5904
|
+
static version = "7.2.2";
|
|
5912
5905
|
constructor(resolvedConfig) {
|
|
5913
5906
|
this.#resolvedConfig = resolvedConfig;
|
|
5914
5907
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tstyche",
|
|
3
|
-
"version": "7.2.
|
|
3
|
+
"version": "7.2.2",
|
|
4
4
|
"description": "Everything You Need for Type Testing.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -36,15 +36,15 @@
|
|
|
36
36
|
"dist/**/*",
|
|
37
37
|
"schemas/*.json"
|
|
38
38
|
],
|
|
39
|
-
"
|
|
40
|
-
"
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=22"
|
|
41
41
|
},
|
|
42
42
|
"peerDependenciesMeta": {
|
|
43
43
|
"typescript": {
|
|
44
44
|
"optional": true
|
|
45
45
|
}
|
|
46
46
|
},
|
|
47
|
-
"
|
|
48
|
-
"
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"typescript": ">=5.4"
|
|
49
49
|
}
|
|
50
50
|
}
|