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

Forbid explicit any, migrate to unknown or proper typing #16219

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ module.exports = {
],
'@typescript-eslint/no-unused-vars': ['warn', { args: 'none' }],
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-explicit-any': [
'error',
{ fixToUnknown: true, ignoreRestArgs: true }
],
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/interface-name-prefix': 'off',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
coverage/
.eslintcache
.stylelintcache
.mypy_cache

dev_mode/listings
dev_mode/schemas
Expand Down
6 changes: 3 additions & 3 deletions builder/src/build-labextension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ commander

let lastHash: string | null = null;

function compilerCallback(err: any, stats: any) {
function compilerCallback(err: unknown, stats: unknown) {
if (!options.watch || err) {
// Do not keep cache anymore
compiler.purgeInputFileSystem();
Expand Down Expand Up @@ -128,9 +128,9 @@ commander
compiler.watch(config[0].watchOptions || {}, compilerCallback);
console.error('\nwebpack is watching the files…\n');
} else {
compiler.run((err: any, stats: any) => {
compiler.run((err: unknown, stats: unknown) => {
if (compiler.close) {
compiler.close((err2: any) => {
compiler.close((err2: unknown) => {
compilerCallback(err || err2, stats);
});
} else {
Expand Down
3 changes: 2 additions & 1 deletion builder/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export namespace Build {
test: /\.svg/,
type: 'asset/inline',
generator: {
dataUrl: (content: any) => miniSVGDataURI(content.toString())
dataUrl: (content: unknown) =>
miniSVGDataURI(content.toString())
}
},
{
Expand Down
14 changes: 7 additions & 7 deletions builder/src/extensionConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ function generateConfig({

const coreData = require(path.join(corePath, 'package.json'));

let shared: any = {};
let shared: unknown = {};

// Start with core package versions.
const coreDeps: any = {
const coreDeps: unknown = {
...coreData.dependencies,
...(coreData.resolutions ?? {})
};
Expand Down Expand Up @@ -165,8 +165,8 @@ function generateConfig({
);

class CleanupPlugin {
apply(compiler: any) {
compiler.hooks.done.tap('Cleanup', (stats: any) => {
apply(compiler: unknown) {
compiler.hooks.done.tap('Cleanup', (stats: unknown) => {
const newlyCreatedAssets = stats.compilation.assets;

// Clear out any remoteEntry files that are stale
Expand All @@ -189,7 +189,7 @@ function generateConfig({

// Find the remoteEntry file and add it to the package.json metadata
const data = fs.readJSONSync(path.join(outputPath, 'package.json'));
const _build: any = {
const _build: unknown = {
load: path.join('static', newEntry)
};
if (exposes['./extension'] !== undefined) {
Expand Down Expand Up @@ -251,7 +251,7 @@ function generateConfig({
filename += '?v=[contenthash]';
}

const rules: any = [{ test: /\.html$/, type: 'asset/resource' }];
const rules: unknown = [{ test: /\.html$/, type: 'asset/resource' }];

if (mode === 'development') {
rules.push({
Expand Down Expand Up @@ -286,7 +286,7 @@ function generateConfig({

if (mode === 'development') {
const logPath = path.join(outputPath, 'build_log.json');
function regExpReplacer(key: any, value: any) {
function regExpReplacer(key: unknown, value: unknown) {
if (value instanceof RegExp) {
return value.toString();
} else {
Expand Down
28 changes: 14 additions & 14 deletions builder/src/webpack-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ export namespace WPPlugin {
* (the non-exported) webpack.IgnoringWatchFileSystem
*/
class FilterIgnoringWatchFileSystem {
constructor(wfs: any, ignored: (path: string) => boolean) {
constructor(wfs: unknown, ignored: (path: string) => boolean) {
this.wfs = wfs;

// ignored should be a callback function that filters the build files
this.ignored = ignored;
}

watch(
files: any,
dirs: any,
missing: any,
startTime: any,
options: any,
callback: any,
callbackUndelayed: any
files: unknown,
dirs: unknown,
missing: unknown,
startTime: unknown,
options: unknown,
callback: unknown,
callbackUndelayed: unknown
) {
files = Array.from(files);
dirs = Array.from(dirs);
Expand All @@ -83,11 +83,11 @@ export namespace WPPlugin {
startTime,
options,
(
err: any,
fileTimestamps: any,
dirTimestamps: any,
changedFiles: any,
removedFiles: any
err: unknown,
fileTimestamps: unknown,
dirTimestamps: unknown,
changedFiles: unknown,
removedFiles: unknown
) => {
if (err) return callback(err);
for (const path of ignoredFiles) {
Expand Down Expand Up @@ -130,7 +130,7 @@ export namespace WPPlugin {
}

ignored: (path: string) => boolean;
wfs: any;
wfs: unknown;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion builder/src/webpack.config.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const rules = [
issuer: /\.css$/,
type: 'asset',
generator: {
dataUrl: (content: any) => miniSVGDataURI(content.toString())
dataUrl: (content: unknown) => miniSVGDataURI(content.toString())
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion buildutils/src/bump-js-major.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ commander
.arguments('<package> [others...]')
.option('--force', 'Force the upgrade')
.option('--dry-run', 'Show what would be executed')
.action((pkg: string, others: Array<string>, options: any) => {
.action((pkg: string, others: Array<string>, options: unknown) => {
utils.exitOnUncaughtException();
others.push(pkg);
const toBump: Set<string> = new Set();
Expand Down
2 changes: 1 addition & 1 deletion buildutils/src/bumpversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ commander
.option('--force', 'Force the upgrade')
.option('--skip-commit', 'Whether to skip commit changes')
.arguments('<spec>')
.action((spec: any, opts: any) => {
.action((spec: unknown, opts: unknown) => {
utils.exitOnUncaughtException();

// Get the previous version.
Expand Down
2 changes: 1 addition & 1 deletion buildutils/src/clean-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ for (let i = 0; i < packageConfig.length; i++) {
function handlePackage(packagePath: string): void {
// Read in the package.json.
const packageJSONPath = path.join(packagePath, 'package.json');
let data: any;
let data: unknown;
try {
data = require(packageJSONPath);
} catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions buildutils/src/ensure-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export async function ensurePackage(
const sourceFile = ts.createSourceFile(
fileName,
fs.readFileSync(fileName).toString(),
(ts.ScriptTarget as any).ES6,
(ts.ScriptTarget as unknown).ES6,
/* setParentNodes */ true
);
imports = imports.concat(getImports(sourceFile));
Expand Down Expand Up @@ -819,7 +819,7 @@ export interface IEnsurePackageOptions {
/**
* The package data.
*/
data: any;
data: unknown;

/**
* The cache of dependency versions by package.
Expand Down Expand Up @@ -930,7 +930,7 @@ function getImports(sourceFile: ts.SourceFile): string[] {
const imports: string[] = [];
handleNode(sourceFile);

function handleNode(node: any): void {
function handleNode(node: unknown): void {
switch (node.kind) {
case ts.SyntaxKind.ImportDeclaration:
imports.push(node.moduleSpecifier.text);
Expand Down
20 changes: 10 additions & 10 deletions buildutils/src/ensure-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {

type Dict<T> = { [key: string]: T };

type CoreData = Map<string, any>;
type CoreData = Map<string, unknown>;

// URL config for this branch
// Source and target branches
Expand Down Expand Up @@ -305,7 +305,7 @@ const SKIP_CSS: Dict<string[]> = {
]
};

const pkgData: Dict<any> = {};
const pkgData: Dict<unknown> = {};
const pkgPaths: Dict<string> = {};
const pkgNames: Dict<string> = {};
const depCache: Dict<string> = {};
Expand Down Expand Up @@ -467,11 +467,11 @@ function ensureMetaPackage(): string[] {
* Get the core data for the given core paths.
*/
function getCoreData(corePaths: string[]): CoreData {
const coreData = new Map<string, any>();
const coreData = new Map<string, unknown>();

corePaths.forEach(pkgPath => {
const dataPath = path.join(pkgPath, 'package.json');
let data: any;
let data: unknown;
try {
data = utils.readJSONFile(dataPath);
} catch (e) {
Expand All @@ -487,7 +487,7 @@ function getCoreData(corePaths: string[]): CoreData {
/**
* Ensure a core package.
*/
function ensureCorePackage(corePackage: any, corePaths: string[]) {
function ensureCorePackage(corePackage: unknown, corePaths: string[]) {
corePackage.jupyterlab.extensions = {};
corePackage.dependencies = {};

Expand All @@ -496,7 +496,7 @@ function ensureCorePackage(corePackage: any, corePaths: string[]) {

corePaths.forEach(pkgPath => {
const dataPath = path.join(pkgPath, 'package.json');
let data: any;
let data: unknown;
try {
data = utils.readJSONFile(dataPath);
} catch (e) {
Expand Down Expand Up @@ -642,7 +642,7 @@ function ensureJupyterlab(): string[] {
corePackage.jupyterlab.linkedPackages = {};
utils.getLernaPaths().forEach(pkgPath => {
const dataPath = path.join(pkgPath, 'package.json');
let data: any;
let data: unknown;
try {
data = utils.readJSONFile(dataPath);
} catch (e) {
Expand Down Expand Up @@ -726,7 +726,7 @@ export async function ensureIntegrity(): Promise<boolean> {
// Gather all of our package data and other metadata.
paths.forEach(pkgPath => {
// Read in the package.json.
let data: any;
let data: unknown;
try {
data = utils.readJSONFile(path.join(pkgPath, 'package.json'));
} catch (e) {
Expand Down Expand Up @@ -760,7 +760,7 @@ export async function ensureIntegrity(): Promise<boolean> {
return;
}

const depData = graph.getNodeData(depName) as any;
const depData = graph.getNodeData(depName) as unknown;
if (typeof depData.style === 'string') {
cssData[depName] = [depData.style];
}
Expand Down Expand Up @@ -846,7 +846,7 @@ export async function ensureIntegrity(): Promise<boolean> {

// Handle the top level package.
const corePath = path.resolve('.', 'package.json');
const coreData: any = utils.readJSONFile(corePath);
const coreData: unknown = utils.readJSONFile(corePath);
if (utils.writePackageData(corePath, coreData)) {
messages['top'] = ['Update package.json'];
}
Expand Down
2 changes: 1 addition & 1 deletion buildutils/src/get-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function getDependency(name: string): Promise<string> {
utils.getLernaPaths().forEach(pkgRoot => {
// Read in the package.json.
const packagePath = path.join(pkgRoot, 'package.json');
let data: any;
let data: unknown;
try {
data = utils.readJSONFile(packagePath);
} catch (e) {
Expand Down
10 changes: 5 additions & 5 deletions buildutils/src/local-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
}

// Assign as `any`` for compatibility with spawn `OpenMode`` options
const out: any = fs.openSync(log_file, 'a');
const err: any = fs.openSync(log_file, 'a');
const out: unknown = fs.openSync(log_file, 'a');
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
const err: unknown = fs.openSync(log_file, 'a');
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

const options = { cwd: out_dir, detached: true, stdio: ['ignore', out, err] };

Expand All @@ -111,7 +111,7 @@
}
await delay(100);
if (fs.existsSync(log_file)) {
content = fs.readFileSync(log_file, { encoding: 'utf-8' });

Check failure

Code scanning / CodeQL

Potential file system race condition High

The file may have changed since it
was checked
.
The file may have changed since it
was checked
.
}
delays -= 100;
}
Expand Down Expand Up @@ -278,7 +278,7 @@
.command('start')
.option('--port <port>', 'Port to use for the registry')
.option('--path <path>', 'Path to use for the registry')
.action(async (options: any) => {
.action(async (options: unknown) => {
utils.exitOnUncaughtException();
const out_dir = options.path || DEFAULT_OUT_DIR;
await startLocalRegistry(out_dir, options.port || DEFAULT_PORT);
Expand All @@ -287,7 +287,7 @@
program
.command('stop')
.option('--path <path>', 'Path to use for the registry')
.action(async (options: any) => {
.action(async (options: unknown) => {
utils.exitOnUncaughtException();
const out_dir = options.path || DEFAULT_OUT_DIR;
await stopLocalRegistry(out_dir);
Expand All @@ -296,7 +296,7 @@
program
.command('publish-dists')
.option('--path <path>', 'Path to the directory with npm tar balls')
.action((options: any) => {
.action((options: unknown) => {
utils.exitOnUncaughtException();
publishPackages(options.path || process.cwd());
});
Expand Down
2 changes: 1 addition & 1 deletion buildutils/src/patch-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ commander
.option('--force', 'Force the upgrade')
.option('--all', 'Patch all JS packages instead of the changed ones')
.option('--skip-commit', 'Whether to skip commit changes')
.action((options: any) => {
.action((options: unknown) => {
utils.exitOnUncaughtException();

// Make sure we can patch release.
Expand Down
2 changes: 1 addition & 1 deletion buildutils/src/prepare-python-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as utils from './utils';
// Specify the program signature.
commander
.description('Prepare the Python package for release')
.action(async (options: any) => {
.action(async (options: unknown) => {
utils.exitOnUncaughtException();

const distDir = './dist';
Expand Down
2 changes: 1 addition & 1 deletion buildutils/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ commander
.option('--skip-tags', 'publish assets but do not handle tags')
.option('--yes', 'Publish without confirmation')
.option('--dry-run', 'Do not actually push any assets')
.action(async (options: any) => {
.action(async (options: unknown) => {
utils.exitOnUncaughtException();

// No-op if we're in release helper dry run
Expand Down
2 changes: 1 addition & 1 deletion buildutils/src/remove-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ handlePackage(path.resolve('.'));
function handlePackage(packagePath: string): void {
// Read in the package.json.
packagePath = path.join(packagePath, 'package.json');
let data: any;
let data: unknown;
try {
data = utils.readJSONFile(packagePath);
} catch (e) {
Expand Down