Skip to content
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

Clean up expect type code for diagnostics #895

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-cobras-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@definitelytyped/eslint-plugin": patch
---

Clean up expect type code for diagnostics
14 changes: 5 additions & 9 deletions packages/eslint-plugin/src/rules/expect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isDeclarationPath } from "@definitelytyped/utils";
import { createRule, findTypesPackage, findUp } from "../util";
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import type * as ts from "typescript";
Expand Down Expand Up @@ -74,11 +73,6 @@ Then re-run.`,
},
defaultOptions: [{}],
create(context) {
const inEditor = !context.options[0]?.versionsToTest?.length;
if (inEditor && (isDeclarationPath(context.filename) || !context.sourceCode.text.includes("$ExpectType"))) {
return {};
}

const tsconfigPath = findUp(context.filename, (dir) => {
const tsconfig = path.join(dir, "tsconfig.json");
return fs.existsSync(tsconfig) ? tsconfig : undefined;
Expand Down Expand Up @@ -115,9 +109,11 @@ Then re-run.`,
};

let versionsToTest = context.options[0]?.versionsToTest;
let reportDiagnostics = true;
if (!versionsToTest?.length) {
// In the editor, just use the built-in install of TypeScript.
versionsToTest = [{ versionName: "", path: require.resolve("typescript") }];
reportDiagnostics = false;
}

for (const version of versionsToTest) {
Expand All @@ -132,6 +128,7 @@ Then re-run.`,
version.versionName,
/*nextHigherVersion*/ undefined,
dirPath,
reportDiagnostics,
);
}

Expand Down Expand Up @@ -212,6 +209,7 @@ function walk(
versionName: string,
nextHigherVersion: string | undefined,
dirPath: string,
reportDiagnostics: boolean,
): void {
const sourceFile = program.getSourceFile(fileName)!;
if (!sourceFile) {
Expand All @@ -226,9 +224,7 @@ function walk(

const checker = program.getTypeChecker();

if (versionName) {
// If we're using the built-in version of TS, then we're in the editor and tsserver will report diagnostics.

if (reportDiagnostics) {
// Don't care about emit errors.
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
for (const diagnostic of diagnostics) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
types/expect-diagnostics-versioned/expect-diagnostics-tests.ts
5:7 error TypeScript@x.y compile error:
Type 'number' is not assignable to type 'string' @definitelytyped/expect

✖ 1 problem (1 error, 0 warnings)

==== types/expect-diagnostics-versioned/expect-diagnostics-tests.ts ====

// eslint-disable-next-line @definitelytyped/no-relative-import-in-test
import * as expect from ".";

// This should cause a diagnostic.
const blah: string = expect.foo;
~~~~
!!! @definitelytyped/expect: TypeScript@x.y compile error:
!!! : Type 'number' is not assignable to type 'string'.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
types/expect-diagnostics-versioned/index.d.ts
4:22 error TypeScript@x.y compile error:
Generic type 'Array<T>' requires 1 type argument(s) @definitelytyped/expect

✖ 1 problem (1 error, 0 warnings)

==== types/expect-diagnostics-versioned/index.d.ts ====

export const foo = 1234;

// This should cause a diagnostic.
export const badSet: Array<string, number, number, number>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! @definitelytyped/expect: TypeScript@x.y compile error:
!!! : Generic type 'Array<T>' requires 1 type argument(s).
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
No errors

==== types/expect-diagnostics/expect-diagnostics-tests.ts ====

// eslint-disable-next-line @definitelytyped/no-relative-import-in-test
import * as expect from ".";

// This should cause a diagnostic.
const blah: string = expect.foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
No errors

==== types/expect-diagnostics/index.d.ts ====

export const foo = 1234;

// This should cause a diagnostic.
export const badSet: Array<string, number, number, number>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
overrides: [
{
files: ["*.ts", "*.cts", "*.mts", "*.tsx"],
rules: {
"@definitelytyped/expect": ["error", { versionsToTest: [{ versionName: "x.y", path: "typescript" }] }],
},
},
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line @definitelytyped/no-relative-import-in-test
import * as expect from ".";

// This should cause a diagnostic.
const blah: string = expect.foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const foo = 1234;

// This should cause a diagnostic.
export const badSet: Array<string, number, number, number>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@types/expect-diagnostics",
"version": "2.0.9999",
"owners": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"expect-diagnostics-tests.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line @definitelytyped/no-relative-import-in-test
import * as expect from ".";

// This should cause a diagnostic.
const blah: string = expect.foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const foo = 1234;

// This should cause a diagnostic.
export const badSet: Array<string, number, number, number>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@types/expect-diagnostics",
"version": "2.0.9999",
"owners": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"expect-diagnostics-tests.ts"
]
}
Loading