Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Flip typesVersions #306

Merged
merged 5 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"prepublishOnly": "npm run build && npm run test && npm run lint"
},
"dependencies": {
"@definitelytyped/header-parser": "latest",
"@definitelytyped/typescript-versions": "latest",
"@definitelytyped/utils": "latest",
"@definitelytyped/header-parser": "0.0.49-next.0",
"@definitelytyped/typescript-versions": "0.0.47-next.0",
"@definitelytyped/utils": "0.0.47-next.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update these after shipping 0.0.47 and before merging this PR.

"dts-critic": "latest",
"fs-extra": "^6.0.1",
"json-stable-stringify": "^1.0.1",
Expand Down
59 changes: 29 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { parseTypeScriptVersionLine } from "@definitelytyped/header-parser";
import { AllTypeScriptVersion, TypeScriptVersion } from "@definitelytyped/typescript-versions";
import { readdir, readFile, stat } from "fs-extra";
import { basename, dirname, join as joinPaths, resolve } from "path";
import assert = require("assert");

import { cleanTypeScriptInstalls, installAllTypeScriptVersions, installTypeScriptNext } from "@definitelytyped/utils";
import { checkPackageJson, checkTsconfig } from "./checks";
import { checkTslintJson, lint, TsVersion } from "./lint";
import { assertDefined, last, mapDefinedAsync, withoutPrefix } from "./util";
import { mapDefinedAsync, withoutPrefix } from "./util";

async function main(): Promise<void> {
const args = process.argv.slice(2);
Expand Down Expand Up @@ -153,35 +154,33 @@ async function runTests(

if (onlyTestTsNext || tsLocal) {
const tsVersion = tsLocal ? "local" : TypeScriptVersion.latest;
if (typesVersions.length === 0) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only-next/only-local case is now much simpler because it doesn't have to search for the latest version inside a ts* directory

await testTypesVersion(dirPath, tsVersion, tsVersion, isOlderVersion, dt, indexText, expectOnly, tsLocal);
} else {
const latestTypesVersion = last(typesVersions);
const versionPath = joinPaths(dirPath, `ts${latestTypesVersion}`);
const versionIndexText = await readFile(joinPaths(versionPath, "index.d.ts"), "utf-8");
await testTypesVersion(
versionPath, tsVersion, tsVersion,
isOlderVersion, dt, versionIndexText, expectOnly, tsLocal, /*inTypesVersionDirectory*/ true);
}
await testTypesVersion(dirPath, tsVersion, tsVersion, isOlderVersion, dt, indexText, expectOnly, tsLocal, /*isLatest*/ true);
} else {
await testTypesVersion(dirPath, undefined, getTsVersion(0), isOlderVersion, dt, indexText, expectOnly, undefined);
for (let i = 0; i < typesVersions.length; i++) {
const version = typesVersions[i];
const versionPath = joinPaths(dirPath, `ts${version}`);
const versionIndexText = await readFile(joinPaths(versionPath, "index.d.ts"), "utf-8");
await testTypesVersion(
versionPath, version, getTsVersion(i + 1), isOlderVersion, dt, versionIndexText,
expectOnly, undefined, /*inTypesVersionDirectory*/ true);
}

function getTsVersion(i: number): TsVersion {
return i === typesVersions.length
? TypeScriptVersion.latest
: assertDefined(TypeScriptVersion.previous(typesVersions[i]));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The all-versions code is completely rewritten -- it now generates low/hi ranges and iterates over them.

Also, we only care about the // Minimum Typescript Version comment from the root index.d.ts, so just need to read that once upfront.

// For example, typesVersions of [3.2, 3.5, 3.6] will have
// associated ts3.2, ts3.5, ts3.6 directories, for
// <=3.2, <=3.5, <=3.6 respectively; the root level is for 3.7 and above.
// so this code needs to generate ranges [lowest-3.2, 3.3-3.5, 3.6-3.6, 3.7-latest]
const lows = [TypeScriptVersion.lowest, ...typesVersions.map(next)]
const his = [...typesVersions, TypeScriptVersion.latest]
assert.strictEqual(lows.length, his.length)
for (let i = 0; i < lows.length; i++) {
const low = lows[i];
const hi = his[i];
const isLatest = hi === TypeScriptVersion.latest;
let versionPath = isLatest ? dirPath : joinPaths(dirPath, `ts${hi}`);
let versionIndexText = isLatest ? indexText : await readFile(joinPaths(versionPath, "index.d.ts"), "utf-8");
console.log('testing from', low, 'to', hi, 'in', versionPath)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how noisy this will be in normal use of dtslint, but it surely makes the standalone output more readable.

await testTypesVersion(versionPath, low, hi, isOlderVersion, dt, versionIndexText, expectOnly, undefined, isLatest);
}
}
}

function next(v: TypeScriptVersion): TypeScriptVersion | undefined {
const index = TypeScriptVersion.supported.indexOf(v);
assert.notStrictEqual(index, -1);
return index === TypeScriptVersion.supported.length - 1 ? undefined : TypeScriptVersion.supported[index + 1];
}

async function testTypesVersion(
dirPath: string,
lowVersion: TsVersion | undefined,
Expand All @@ -191,21 +190,21 @@ async function testTypesVersion(
indexText: string,
expectOnly: boolean,
tsLocal: string | undefined,
inTypesVersionDirectory?: boolean,
isLatest: boolean,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testTypesVersion is now called in many fewer places, so I

  1. Shifted undefined checking out, to only places it applies.
  2. Shifted min-version checking out, to only places it applies.
  3. Made the last parameter required.

): Promise<void> {
const minVersionFromComment = getTypeScriptVersionFromComment(indexText);
if (minVersionFromComment !== undefined && inTypesVersionDirectory) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headers are already not allowed in subdirectories, so this check isn't needed.

throw new Error(`Already in the \`ts${lowVersion}\` directory, don't need \`// Minimum TypeScript Version\`.`);
if (minVersionFromComment !== undefined && !isLatest) {
throw new Error(`Already in the \`ts${maxVersion}\` directory, don't need \`// Minimum TypeScript Version\`.`);
}
const minVersion = lowVersion
|| minVersionFromComment && TypeScriptVersion.isSupported(minVersionFromComment) && minVersionFromComment
|| TypeScriptVersion.lowest;

await checkTslintJson(dirPath, dt);
await checkTsconfig(dirPath, dt
? { relativeBaseUrl: ".." + (isOlderVersion ? "/.." : "") + (inTypesVersionDirectory ? "/.." : "") + "/" }
? { relativeBaseUrl: ".." + (isOlderVersion ? "/.." : "") + (isLatest ? "" : "/..") + "/" }
: undefined);
const err = await lint(dirPath, minVersion, maxVersion, !!inTypesVersionDirectory, expectOnly, tsLocal);
const err = await lint(dirPath, minVersion, maxVersion, isLatest, expectOnly, tsLocal);
if (err) {
throw new Error(err);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function lint(
dirPath: string,
minVersion: TsVersion,
maxVersion: TsVersion,
inTypesVersionDirectory: boolean,
isLatest: boolean,
expectOnly: boolean,
tsLocal: string | undefined): Promise<string | undefined> {
const tsconfigPath = joinPaths(dirPath, "tsconfig.json");
Expand Down Expand Up @@ -49,7 +49,7 @@ export async function lint(
// External dependencies should have been handled by `testDependencies`;
// typesVersions should be handled in a separate lint
if (!isExternalDependency(file, dirPath, lintProgram) &&
(inTypesVersionDirectory || !isTypesVersionPath(fileName, dirPath))) {
(!isLatest || !isTypesVersionPath(fileName, dirPath))) {
linter.lint(fileName, text, config);
}
}
Expand Down