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

refactor(types): remove @ts-expect-error in loaderRunner and StatsFactory #7864

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 9 additions & 6 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4511,10 +4511,10 @@ interface GlobalPassOption {
type GotHandler<T = any> = (result: any | null, callback: (error: Error | null) => void) => void;

// @public (undocumented)
type GroupConfig = {
type GroupConfig<T, R = T> = {
getKeys: (arg0: any) => string[] | undefined;
createGroup: <T, R>(arg0: string, arg1: (T | R)[], arg2: T[]) => R;
getOptions?: (<T>(arg0: string, arg1: T[]) => GroupOptions) | undefined;
createGroup: (key: string, arg1: (T | R)[], arg2: T[]) => R;
getOptions?: ((key: string, arg1: T[]) => GroupOptions) | undefined;
};

// @public
Expand Down Expand Up @@ -4577,7 +4577,7 @@ type Hooks = Readonly<{
extract: HookMap<SyncBailHook<[Object, any, StatsFactoryContext], undefined>>;
filter: HookMap<SyncBailHook<[any, StatsFactoryContext, number, number], undefined>>;
filterSorted: HookMap<SyncBailHook<[any, StatsFactoryContext, number, number], undefined>>;
groupResults: HookMap<SyncBailHook<[GroupConfig[], StatsFactoryContext], undefined>>;
groupResults: HookMap<SyncBailHook<[GroupConfig<any>[], StatsFactoryContext], undefined>>;
filterResults: HookMap<SyncBailHook<[any, StatsFactoryContext, number, number], undefined>>;
sort: HookMap<SyncBailHook<[
((arg1: any, arg2: any) => number)[],
Expand Down Expand Up @@ -5890,11 +5890,11 @@ export interface LoaderContext<OptionsType = {}> {
// (undocumented)
addMissingDependency(missing: string): void;
// (undocumented)
async(): (err?: Error | null, content?: string | Buffer, sourceMap?: string | SourceMap, additionalData?: AdditionalData) => void;
async(): LoaderContextCallback;
// (undocumented)
cacheable(cacheable?: boolean): void;
// (undocumented)
callback(err?: Error | null, content?: string | Buffer, sourceMap?: string | SourceMap, additionalData?: AdditionalData): void;
callback: LoaderContextCallback;
// (undocumented)
clearDependencies(): void;
// (undocumented)
Expand Down Expand Up @@ -5978,6 +5978,9 @@ export interface LoaderContext<OptionsType = {}> {
version: 2;
}

// @public (undocumented)
type LoaderContextCallback = (err?: Error | null, content?: string | Buffer, sourceMap?: string | SourceMap, additionalData?: AdditionalData) => void;

// @public (undocumented)
export type LoaderDefinition<OptionsType = {}, ContextAdditions = {}> = LoaderDefinitionFunction<OptionsType, ContextAdditions> & {
raw?: false;
Expand Down
21 changes: 9 additions & 12 deletions packages/rspack/src/config/adapterRuleUse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,21 @@ export interface AdditionalData {
[index: string]: any;
}

export type LoaderContextCallback = (
err?: Error | null,
content?: string | Buffer,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
) => void;

export interface LoaderContext<OptionsType = {}> {
version: 2;
resource: string;
resourcePath: string;
resourceQuery: string;
resourceFragment: string;
async(): (
err?: Error | null,
content?: string | Buffer,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
) => void;
callback(
err?: Error | null,
content?: string | Buffer,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
): void;
async(): LoaderContextCallback;
callback: LoaderContextCallback;
cacheable(cacheable?: boolean): void;
sourceMap: boolean;
rootContext: string;
Expand Down
59 changes: 28 additions & 31 deletions packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { NonErrorEmittedError, type RspackError } from "../RspackError";
import {
BUILTIN_LOADER_PREFIX,
type LoaderContext,
type LoaderContextCallback,
isUseSimpleSourceMap,
isUseSourceMap
} from "../config/adapterRuleUse";
Expand Down Expand Up @@ -236,45 +237,44 @@ const runSyncOrAsync = promisify(function runSyncOrAsync(
fn: Function,
context: LoaderContext,
args: any[],
callback: (err: Error | null, args: any[]) => void
callback: (err: Error | null | undefined, args: any[]) => void
) {
let isSync = true;
let isDone = false;
let isError = false; // internal error
let reportedError = false;
// @ts-expect-error loader-runner leverages `arguments` to achieve the same functionality.
context.async = function async() {
if (isDone) {
if (reportedError) return; // ignore
if (reportedError) return undefined as any; // ignore
SoonIter marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("async(): The callback was already called.");
}
isSync = false;
return innerCallback;
};
const innerCallback = (context.callback = (err, ...args) => {
const innerCallback: LoaderContextCallback = (err, ...args) => {
if (isDone) {
if (reportedError) return; // ignore
throw new Error("callback(): The callback was already called.");
}
isDone = true;
isSync = false;
try {
// @ts-expect-error
callback(err, args);
} catch (e) {
isError = true;
throw e;
}
});
};
context.callback = innerCallback;

try {
const result = (function LOADER_EXECUTION() {
return fn.apply(context, args);
})();
if (isSync) {
isDone = true;
if (result === undefined) {
// @ts-expect-error
callback();
callback(null, []);
SoonIter marked this conversation as resolved.
Show resolved Hide resolved
return;
}
if (
Expand Down Expand Up @@ -306,8 +306,7 @@ const runSyncOrAsync = promisify(function runSyncOrAsync(
}
isDone = true;
reportedError = true;
// @ts-expect-error
callback(e);
callback(e as Error, []);
}
});

Expand Down Expand Up @@ -585,26 +584,27 @@ export async function runLoaders(
loaderContext.resolve = function resolve(context, request, callback) {
resolver.resolve({}, context, request, getResolveContext(), callback);
};
// @ts-expect-error TODO

loaderContext.getResolve = function getResolve(options) {
const child = options ? resolver.withOptions(options as any) : resolver;
return (context, request, callback) => {
if (callback) {
child.resolve({}, context, request, getResolveContext(), callback);
} else {
return new Promise((resolve, reject) => {
child.resolve(
{},
context,
request,
getResolveContext(),
(err, result) => {
if (err) reject(err);
else resolve(result);
}
);
});
// TODO: return value may be undefined
return undefined as unknown as Promise<any>;
}
return new Promise((resolve, reject) => {
child.resolve(
{},
context,
request,
getResolveContext(),
(err, result) => {
if (err) reject(err);
else resolve(result);
}
);
});
};
};
loaderContext.getLogger = function getLogger(name) {
Expand Down Expand Up @@ -655,7 +655,7 @@ export async function runLoaders(
sourceMap?,
assetInfo?
) {
let source: Source;
let source: Source | undefined = undefined;
if (sourceMap) {
if (
typeof sourceMap === "string" &&
Expand Down Expand Up @@ -683,13 +683,10 @@ export async function runLoaders(
content
);
}
// @ts-expect-error
compiler._lastCompilation.__internal__emit_asset_from_loader(
compiler._lastCompilation!.__internal__emit_asset_from_loader(
name,
// @ts-expect-error
source,
// @ts-expect-error
assetInfo,
source!,
assetInfo!,
context._moduleIdentifier
);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/node/NodeEnvironmentPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* Copyright (c) JS Foundation and other contributors
* https://github.com/webpack/webpack/blob/main/LICENSE
*/
// @ts-expect-error
import CachedInputFileSystem from "enhanced-resolve/lib/CachedInputFileSystem";
// @ts-expect-error we directly import from enhanced-resolve inner js file to improve performance
import CachedInputFileSystem from "enhanced-resolve/lib/CachedInputFileSystem.js";
import fs from "graceful-fs";

import type { Compiler } from "..";
Expand Down
Loading
Loading