Skip to content

Commit

Permalink
[infra] Port scripts from TS -> JS (DefinitelyTyped#61102)
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko authored Jul 5, 2022
1 parent fe6bfed commit 9755814
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 195 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*.cmd
*.pdb
*.suo
*.js
*.cjs
*.user
*.cache
*.cs
Expand All @@ -31,9 +29,6 @@ _infrastructure/tests/build
.idea
*.iml

*.js.map
!*.js/

# npm
node_modules
package-lock.json
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"node": ">=7.8.0"
},
"scripts": {
"compile-scripts": "tsc -p scripts",
"not-needed": "node scripts/not-needed.js",
"update-codeowners": "node scripts/update-codeowners.js",
"test-all": "node node_modules/@definitelytyped/dtslint-runner/dist/index.js --path .",
Expand Down
38 changes: 16 additions & 22 deletions scripts/fix-tslint.ts → scripts/fix-tslint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Usage: ts-node fix-tslint.ts

/// <reference types="node" />

import * as fs from 'node:fs';
Expand All @@ -25,29 +23,25 @@ for (const dirName of fs.readdirSync(home)) {
}
}

function fixTslint(dir: URL): void {
/**
* @param {URL} dir
*/
function fixTslint(dir) {
const target = new URL('tslint.json', dir);
if (!fs.existsSync(target)) return;
let json = JSON.parse(fs.readFileSync(target, 'utf-8'));
json = fix(json);
const text = Object.keys(json).length === 1 ? '{ "extends": "@definitelytyped/dtslint/dt.json" }' : JSON.stringify(json, undefined, 4);
fs.writeFileSync(target, text + "\n", "utf-8");
const json = JSON.parse(fs.readFileSync(target, 'utf-8'));
json.rules = fixRules(json.rules);
const text =
Object.keys(json).length === 1
? '{ "extends": "@definitelytyped/dtslint/dt.json" }'
: JSON.stringify(json, undefined, 4);
fs.writeFileSync(target, text + '\n', 'utf-8');
}

function fix(config: any): any {
const out: any = {};
for (const key in config) {
let value = config[key];
out[key] = key === "rules" ? fixRules(value) : value;
}
return out;
}

function fixRules(rules: any): any {
const out: any = {};
for (const key in rules) {
out[key] = rules[key];
}
return out;
/**
* @param {{}} rules
*/
function fixRules(rules) {
return Object.fromEntries(Object.entries(rules).map(([key, value]) => [key, value]));
}

36 changes: 12 additions & 24 deletions scripts/generate-tsconfigs.ts → scripts/generate-tsconfigs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Usage: ts-node generate-tsconfigs.ts

/// <reference types="node" />

import * as fs from 'node:fs';
Expand All @@ -24,30 +22,20 @@ for (const dirName of fs.readdirSync(home)) {
}
}

function fixTsconfig(dir: URL): void {
/**
* @param {URL} dir
*/
function fixTsconfig(dir) {
const target = new URL('tsconfig.json', dir);
let json = JSON.parse(fs.readFileSync(target, 'utf-8'));
json = fix(json);
const json = JSON.parse(fs.readFileSync(target, 'utf-8'));
json.compilerOptions = fixCompilerOptions(json.compilerOptions);
fs.writeFileSync(target, JSON.stringify(json, undefined, 4), 'utf-8');
}

function fix(config: any): any {
const out: any = {};
for (const key in config) {
let value = config[key];
if (key === 'compilerOptions') {
value = fixCompilerOptions(value);
}
out[key] = value;
}
return out;
}

function fixCompilerOptions(config: any): any {
const out: any = {};
for (const key in config) {
out[key] = config[key];
// Do something interesting here
}
return out;
/**
* @param {{}} compilerOptions
*/
function fixCompilerOptions(compilerOptions) {
// Do something interesting here
return Object.fromEntries(Object.entries(compilerOptions).map(([key, value]) => [key, value]));
}
10 changes: 5 additions & 5 deletions scripts/ghostbuster.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-check
import { flatMap, mapDefined } from "@definitelytyped/utils";
import * as os from "node:os";
import fsExtra from 'fs-extra';
import fsExtra from "fs-extra";
const { writeFileSync, readFileSync, readdirSync, existsSync } = fsExtra;
import hp from "@definitelytyped/header-parser";
import { Octokit } from "@octokit/core";
Expand Down Expand Up @@ -71,7 +69,7 @@ function getAllHeaders() {
parsed = hp.parseHeaderOrFail(indexContent);
} catch (e) {}
if (parsed) {
headers[index] = { ...parsed, raw: indexContent };
headers[/** @type {never} */ (index)] = { ...parsed, raw: indexContent };
}
}
});
Expand All @@ -87,6 +85,7 @@ async function fetchGhosts(users) {
const maxPageSize = 2000;
const pages = Math.ceil(users.size / maxPageSize);
const userArray = Array.from(users);
/** @type string[] */
const ghosts = [];
for (let page = 0; page < pages; page++) {
const startIndex = page * maxPageSize;
Expand Down Expand Up @@ -124,7 +123,8 @@ async function tryGQL(fn) {
const result = await fn();
if (result.data) return result.data;
return result;
} catch (resultWithErrors) {
// @ts-expect-error
} catch (/** @type {{}} */ resultWithErrors) {
if (resultWithErrors.data) {
return resultWithErrors.data;
}
Expand Down
8 changes: 5 additions & 3 deletions scripts/tsconfig.json → scripts/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"exclude": ["fix-tslint.ts", "update-config"],
"exclude": ["close-old-issues.js", "fix-tslint.js"],
"compilerOptions": {
"noUnusedLocals": true,
"target": "es6",
"module": "esnext",
"strict": true,
Expand All @@ -9,7 +10,8 @@
"resolveJsonModule": true,
"typeRoots": ["../types"],
"types": [],
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true
"checkJs": true,
"forceConsistentCasingInFileNames": true,
"lib": ["es2019"]
}
}
3 changes: 3 additions & 0 deletions scripts/not-needed.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ notNeededPackages.packages[typingsPackageName] = { libraryName, asOfVersion };
notNeededPackages.packages = Object.fromEntries(Object.entries(notNeededPackages.packages).sort());
fs.writeFileSync('notNeededPackages.json', JSON.stringify(notNeededPackages, undefined, 4) + '\n', 'utf-8');

/**
* @param {string} dir
*/
function rmdirRecursive(dir) {
for (let entry of fs.readdirSync(dir)) {
entry = path.join(dir, entry);
Expand Down
37 changes: 20 additions & 17 deletions scripts/support-window.ts → scripts/support-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ const supported = Object.entries(data)

const x = scaleTime()
.domain([
min(
supported,
(d) =>
// Clip 1/4 of the earliest supported version. Cuts off the
// release date (unimportant?) but gives the visual impression
// of additional, unsupported versions?
new Date(
Number(d.releaseDate) +
((d.endDate as never) - (d.releaseDate as never)) / 4
)
)!,
max(supported, (d) => d.endDate)!,
/** @type {never} */ (
min(
supported,
(d) =>
// Clip 1/4 of the earliest supported version. Cuts off the
// release date (unimportant?) but gives the visual impression
// of additional, unsupported versions?
new Date(
Number(d.releaseDate) +
// prettier-ignore
(/** @type {never} */ (d.endDate) - /** @type {never} */ (d.releaseDate)) / 4
)
)
),
/** @type {never} */ (max(supported, (d) => d.endDate)),
])
.nice()
.range([margin.left, width - margin.right]);
Expand Down Expand Up @@ -80,7 +83,7 @@ svg
.data(supported)
.join("rect")
.attr("x", (d) => x(d.releaseDate))
.attr("y", (d) => y(d.version)!)
.attr("y", (d) => /** @type {never} */ (y(d.version)))
.attr("width", (d) => x(d.endDate) - x(d.releaseDate))
.attr("height", y.bandwidth())
.attr("fill", (d, i) => (i % 2 === 0 ? "#3178c6" : "#235a97"));
Expand All @@ -106,7 +109,7 @@ texts
.attr("fill", "#ffffff")
.append("text")
.attr("x", (d) => x(d.releaseDate) + (x(d.endDate) - x(d.releaseDate)) / 2)
.attr("y", (d) => y(d.version)!)
.attr("y", (d) => /** @type {never} */ (y(d.version)))
.attr("dy", "0.35em")
.text((d) => d.version);
texts
Expand All @@ -116,7 +119,7 @@ texts
.data(supported)
.join("text")
.attr("x", (d) => x(d.releaseDate) + (x(d.endDate) - x(d.releaseDate)) / 4)
.attr("y", (d) => y(d.version)!)
.attr("y", (d) => /** @type {never} */ (y(d.version)))
.attr("dy", "0.35em")
.text((d) => formatDate(d.releaseDate));
texts
Expand All @@ -129,7 +132,7 @@ texts
"x",
(d) => x(d.releaseDate) + ((x(d.endDate) - x(d.releaseDate)) * 3) / 4
)
.attr("y", (d) => y(d.version)!)
.attr("y", (d) => /** @type {never} */ (y(d.version)))
.attr("dy", "0.35em")
.text((d) => formatDate(d.endDate));
process.stdout.write(serialize(svg.node()!));
process.stdout.write(serialize(/** @type {never} */ (svg.node())));
73 changes: 73 additions & 0 deletions scripts/update-config/LintPackage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as fs from "node:fs";
import * as stringify from "json-stable-stringify";
import * as path from "node:path";
import { Configuration as Config, Linter } from "tslint";
import { isExternalDependency } from "./dependencies";

/**
* Represents a package from the linter's perspective.
* For example, `DefinitelyTyped/types/react` and `DefinitelyTyped/types/react/v15` are different
* packages.
*/
export class LintPackage {
/** @type {import("typescript").SourceFile[]} */
#files = [];
#rootDir;
#program;

/**
* @param {string} rootDir
*/
constructor(rootDir) {
this.#rootDir = rootDir;
this.#program = Linter.createProgram(path.join(this.#rootDir, "tsconfig.json"));
}

config() {
return Config.readConfigurationFile(path.join(this.#rootDir, "tslint.json"));
}

/**
* @param {string} filePath
*/
addFile(filePath) {
const file = this.#program.getSourceFile(filePath);
if (file) {
this.#files.push(file);
}
}

/**
* @param {import("tslint").ILinterOptions} opts
* @param {import("tslint").Configuration.IConfigurationFile} config
*/
lint(opts, config) {
const linter = new Linter(opts, this.#program);
for (const file of this.#files) {
if (ignoreFile(file, this.#rootDir, this.#program)) {
continue;
}
linter.lint(file.fileName, file.text, config);
}
return linter.getResult();
}

/**
* @param {import("tslint").Configuration.RawConfigFile} config
*/
updateConfig(config) {
fs.writeFileSync(path.join(this.#rootDir, "tslint.json"), stringify(config, { space: 4 }), {
encoding: "utf8",
flag: "w",
});
}
}

/**
* @param {import("typescript").SourceFile} file
* @param {string} dirPath
* @param {import("typescript").Program} program
*/
function ignoreFile(file, dirPath, program) {
return program.isSourceFileDefaultLibrary(file) || isExternalDependency(file, path.resolve(dirPath), program);
}
53 changes: 0 additions & 53 deletions scripts/update-config/LintPackage.ts

This file was deleted.

Loading

0 comments on commit 9755814

Please sign in to comment.