Skip to content

Commit

Permalink
Switch expect to using settings for versionsToTest (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Mar 4, 2024
1 parent 465dbce commit 5dae397
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .changeset/thirty-pugs-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@definitelytyped/eslint-plugin": patch
"@definitelytyped/dtslint": patch
---

Switch expect to using settings for versionsToTest
8 changes: 7 additions & 1 deletion packages/dtslint/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,17 @@ function getEslintOptions(
const allFiles = ["*.ts", "*.cts", "*.mts", "*.tsx"];

const overrideConfig: ESLint.Options["overrideConfig"] = {
settings: {
dt: {
versionsToTest,
},
},
overrides: [
{
files: allFiles,
rules: {
"@definitelytyped/expect": ["error", { versionsToTest }],
// This prevents anyone from disabling this rule.
"@definitelytyped/expect": ["error"],
},
},
],
Expand Down
55 changes: 35 additions & 20 deletions packages/eslint-plugin/src/rules/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,7 @@ import { ReportDescriptorMessageData } from "@typescript-eslint/utils/ts-eslint"
type TSModule = typeof ts;
const builtinTypeScript = require("typescript") as TSModule;

type Options = [
{
versionsToTest?: {
readonly versionName: string;
readonly path: string;
}[];
},
];
type MessageIds =
| "noTsconfig"
| "twoAssertions"
| "failure"
| "diagnostic"
| "programContents"
| "noMatch"
| "needInstall";

const rule = createRule<Options, MessageIds>({
const rule = createRule({
name: "expect",
meta: {
type: "problem",
Expand Down Expand Up @@ -108,9 +91,9 @@ Then re-run.`,
existing.versions.add(versionName);
};

let versionsToTest = context.options[0]?.versionsToTest;
let reportDiagnostics = true;
if (!versionsToTest?.length) {
let { versionsToTest } = getSettings(context);
if (!versionsToTest) {
// In the editor, just use the built-in install of TypeScript.
versionsToTest = [{ versionName: "", path: require.resolve("typescript") }];
reportDiagnostics = false;
Expand Down Expand Up @@ -145,6 +128,36 @@ Then re-run.`,
},
});

interface VersionToTest {
readonly versionName: string;
readonly path: string;
}

interface Settings {
readonly versionsToTest?: readonly VersionToTest[];
}

function getSettings(context: Parameters<(typeof rule)["create"]>[0]): Settings {
const dt = context.settings.dt;
if (!dt || typeof dt !== "object") {
return {};
}

let versionsToTest = (dt as Record<string, unknown>).versionsToTest;
versionsToTest ??= undefined;
if (!Array.isArray(versionsToTest)) {
throw new Error("Invalid versionsToTest");
}

for (const version of versionsToTest) {
if (typeof version !== "object" || typeof version.versionName !== "string" || typeof version.path !== "string") {
throw new Error("Invalid version to test");
}
}

return { versionsToTest };
}

const programCache = new WeakMap<ts.Program, Map<string, ts.Program>>();
/** Maps a ts.Program to one created with the version specified in `options`. */
function getProgram(configFile: string, ts: TSModule, versionName: string, lintProgram: ts.Program): ts.Program {
Expand Down Expand Up @@ -186,6 +199,8 @@ function createProgram(configFile: string, ts: TSModule): ts.Program {
return ts.createProgram(parsed.fileNames, parsed.options, host);
}

type MessageIds = keyof (typeof rule)["meta"]["messages"];

interface ReporterInfo {
versionName: string;
messageId: MessageIds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module.exports = {
overrides: [
{
files: ["*.ts", "*.cts", "*.mts", "*.tsx"],
rules: {
"@definitelytyped/expect": ["error", { versionsToTest: [{ versionName: "x.y", path: "typescript" }] }],
},
},
]
settings: {
dt: {
versionsToTest: [{ versionName: "x.y", path: "typescript" }]
}
},
};

0 comments on commit 5dae397

Please sign in to comment.