From 71d60fafb4fea446ef3b600cdb0613c035473997 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Sun, 21 May 2023 21:58:37 +0300 Subject: [PATCH 01/12] refactor(types): more --- lib/PrefetchPlugin.js | 4 ++++ lib/debug/ProfilingPlugin.js | 11 +++++++++++ types.d.ts | 14 +++++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/PrefetchPlugin.js b/lib/PrefetchPlugin.js index fb1454cbe9c..4f09fc0c3dc 100644 --- a/lib/PrefetchPlugin.js +++ b/lib/PrefetchPlugin.js @@ -10,6 +10,10 @@ const PrefetchDependency = require("./dependencies/PrefetchDependency"); /** @typedef {import("./Compiler")} Compiler */ class PrefetchPlugin { + /** + * @param {string} context context or request if context is not set + * @param {string} [request] request + */ constructor(context, request) { if (request) { this.context = context; diff --git a/lib/debug/ProfilingPlugin.js b/lib/debug/ProfilingPlugin.js index 8fa4cad066e..cc896fe2c8b 100644 --- a/lib/debug/ProfilingPlugin.js +++ b/lib/debug/ProfilingPlugin.js @@ -17,6 +17,7 @@ const createSchemaValidation = require("../util/create-schema-validation"); const { dirname, mkdirpSync } = require("../util/fs"); /** @typedef {import("../../declarations/plugins/debug/ProfilingPlugin").ProfilingPluginOptions} ProfilingPluginOptions */ +/** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */ const validate = createSchemaValidation( @@ -72,6 +73,11 @@ class Profiler { ]); } + /** + * @param {string} method method name + * @param {object} [params] params + * @returns {Promise} Promise for the result + */ sendCommand(method, params) { if (this.hasSession()) { return new Promise((res, rej) => { @@ -203,6 +209,11 @@ class ProfilingPlugin { this.outputPath = options.outputPath || "events.json"; } + /** + * Apply the plugin + * @param {Compiler} compiler the compiler instance + * @returns {void} + */ apply(compiler) { const tracer = createTrace( compiler.intermediateFileSystem, diff --git a/types.d.ts b/types.d.ts index edf023557e6..6403e52b16f 100644 --- a/types.d.ts +++ b/types.d.ts @@ -9322,9 +9322,9 @@ declare interface PossibleFileSystemError { syscall?: string; } declare class PrefetchPlugin { - constructor(context?: any, request?: any); - context: any; - request: any; + constructor(context: string, request?: string); + context: null | string; + request: string; /** * Apply the plugin @@ -9366,14 +9366,18 @@ declare class Profiler { inspector: any; hasSession(): boolean; startProfiling(): Promise | Promise<[any, any, any]>; - sendCommand(method?: any, params?: any): Promise; + sendCommand(method: string, params?: object): Promise; destroy(): Promise; stopProfiling(): Promise<{ profile: any }>; } declare class ProfilingPlugin { constructor(options?: ProfilingPluginOptions); outputPath: string; - apply(compiler?: any): void; + + /** + * Apply the plugin + */ + apply(compiler: Compiler): void; static Profiler: typeof Profiler; } declare interface ProfilingPluginOptions { From 055737dc0ce38bacd10a8fbafedb137cfdd8a9f2 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Sun, 21 May 2023 23:31:30 +0300 Subject: [PATCH 02/12] refactor(types): more --- lib/css/CssParser.js | 2 ++ lib/dependencies/AMDDefineDependency.js | 8 ++++++++ lib/dependencies/AMDRequireArrayDependency.js | 5 +++++ lib/dependencies/AMDRequireContextDependency.js | 6 ++++++ lib/dependencies/AMDRequireDependenciesBlock.js | 6 ++++++ lib/dependencies/AMDRequireDependency.js | 7 +++++++ lib/dependencies/AMDRequireItemDependency.js | 6 ++++++ lib/dependencies/CachedConstDependency.js | 6 ++++++ lib/dependencies/CommonJsExportRequireDependency.js | 11 +++++++++++ lib/dependencies/CommonJsExportsDependency.js | 8 ++++++++ lib/dependencies/CommonJsExportsParserPlugin.js | 3 +-- lib/dependencies/CommonJsFullRequireDependency.js | 3 ++- lib/dependencies/CommonJsPlugin.js | 7 +++++++ lib/dependencies/CommonJsSelfReferenceDependency.js | 8 ++++++++ lib/dependencies/ConstDependency.js | 3 ++- lib/dependencies/ContextDependencyHelpers.js | 6 +++--- lib/dependencies/CreateScriptUrlDependency.js | 3 ++- lib/dependencies/CriticalDependencyWarning.js | 3 +++ lib/dependencies/CssImportDependency.js | 3 ++- lib/dependencies/CssLocalIdentifierDependency.js | 3 ++- lib/dependencies/CssSelfLocalIdentifierDependency.js | 3 ++- lib/dependencies/CssUrlDependency.js | 3 ++- lib/dependencies/DelegatedSourceDependency.js | 3 +++ lib/dependencies/ExportsInfoDependency.js | 6 ++++++ lib/dependencies/HarmonyAcceptDependency.js | 3 ++- lib/dependencies/HarmonyAcceptImportDependency.js | 3 +++ .../HarmonyEvaluatedImportSpecifierDependency.js | 11 +++++++++++ lib/dependencies/HarmonyExportExpressionDependency.js | 7 +++++++ .../HarmonyExportImportedSpecifierDependency.js | 3 ++- lib/dependencies/HarmonyExportSpecifierDependency.js | 4 ++++ lib/dependencies/HarmonyImportDependency.js | 3 ++- lib/dependencies/HarmonyImportSideEffectDependency.js | 6 ++++++ lib/dependencies/HarmonyImportSpecifierDependency.js | 11 +++++++++++ lib/dependencies/ImportContextDependency.js | 6 ++++++ lib/dependencies/ImportDependency.js | 3 ++- lib/dependencies/ImportEagerDependency.js | 3 ++- lib/dependencies/ImportMetaHotAcceptDependency.js | 6 ++++++ lib/dependencies/ImportMetaHotDeclineDependency.js | 6 ++++++ lib/dependencies/ImportWeakDependency.js | 3 ++- lib/dependencies/LocalModuleDependency.js | 7 +++++++ lib/dependencies/ModuleHotAcceptDependency.js | 6 ++++++ lib/dependencies/ModuleHotDeclineDependency.js | 6 ++++++ lib/dependencies/PrefetchDependency.js | 3 +++ lib/dependencies/ProvidedDependency.js | 3 ++- lib/dependencies/PureExpressionDependency.js | 3 ++- lib/dependencies/RequireContextDependency.js | 6 ++++++ lib/dependencies/RequireEnsureDependenciesBlock.js | 4 ++++ lib/dependencies/RequireEnsureDependency.js | 6 ++++++ lib/dependencies/RequireEnsureItemDependency.js | 3 +++ lib/dependencies/RequireEnsurePlugin.js | 7 +++++++ lib/dependencies/RequireHeaderDependency.js | 4 ++++ lib/dependencies/RequireIncludeDependency.js | 5 +++++ .../RequireIncludeDependencyParserPlugin.js | 3 +++ lib/dependencies/RequireIncludePlugin.js | 7 +++++++ lib/dependencies/RequireResolveContextDependency.js | 8 ++++++++ lib/dependencies/RequireResolveDependency.js | 6 ++++++ lib/dependencies/RequireResolveHeaderDependency.js | 4 ++++ lib/dependencies/URLDependency.js | 5 +++-- lib/dependencies/UnsupportedDependency.js | 5 +++++ .../WebAssemblyExportImportedDependency.js | 6 ++++++ lib/dependencies/WebpackIsIncludedDependency.js | 5 +++++ lib/dependencies/WorkerDependency.js | 3 ++- lib/dependencies/getFunctionExpression.js | 7 +++++++ lib/javascript/JavascriptParser.js | 5 ++++- types.d.ts | 9 ++++----- 65 files changed, 307 insertions(+), 29 deletions(-) diff --git a/lib/css/CssParser.js b/lib/css/CssParser.js index 20bf8b88bac..4d26a977e43 100644 --- a/lib/css/CssParser.js +++ b/lib/css/CssParser.js @@ -19,6 +19,8 @@ const walkCssTokens = require("./walkCssTokens"); /** @typedef {import("../Parser").ParserState} ParserState */ /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */ +/** @typedef {[number, number]} Range */ + const CC_LEFT_CURLY = "{".charCodeAt(0); const CC_RIGHT_CURLY = "}".charCodeAt(0); const CC_COLON = ":".charCodeAt(0); diff --git a/lib/dependencies/AMDDefineDependency.js b/lib/dependencies/AMDDefineDependency.js index 019f3908c46..b9b403c34f4 100644 --- a/lib/dependencies/AMDDefineDependency.js +++ b/lib/dependencies/AMDDefineDependency.js @@ -12,6 +12,7 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ @@ -107,6 +108,13 @@ const DEFINITIONS = { }; class AMDDefineDependency extends NullDependency { + /** + * @param {Range} range range + * @param {Range} arrayRange array range + * @param {Range} functionRange function range + * @param {Range} objectRange object range + * @param {boolean} namedModule true, when define is called with a name + */ constructor(range, arrayRange, functionRange, objectRange, namedModule) { super(); this.range = range; diff --git a/lib/dependencies/AMDRequireArrayDependency.js b/lib/dependencies/AMDRequireArrayDependency.js index 123ce345657..96dc54bdf45 100644 --- a/lib/dependencies/AMDRequireArrayDependency.js +++ b/lib/dependencies/AMDRequireArrayDependency.js @@ -12,10 +12,15 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class AMDRequireArrayDependency extends NullDependency { + /** + * @param {TODO} depsArray deps array + * @param {Range} range range + */ constructor(depsArray, range) { super(); diff --git a/lib/dependencies/AMDRequireContextDependency.js b/lib/dependencies/AMDRequireContextDependency.js index eab91c33d2f..f040cb6e861 100644 --- a/lib/dependencies/AMDRequireContextDependency.js +++ b/lib/dependencies/AMDRequireContextDependency.js @@ -8,10 +8,16 @@ const makeSerializable = require("../util/makeSerializable"); const ContextDependency = require("./ContextDependency"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class AMDRequireContextDependency extends ContextDependency { + /** + * @param {TODO} options options + * @param {Range} range range + * @param {Range} valueRange value range + */ constructor(options, range, valueRange) { super(options); diff --git a/lib/dependencies/AMDRequireDependenciesBlock.js b/lib/dependencies/AMDRequireDependenciesBlock.js index eebe0bce263..615660c3c9e 100644 --- a/lib/dependencies/AMDRequireDependenciesBlock.js +++ b/lib/dependencies/AMDRequireDependenciesBlock.js @@ -8,7 +8,13 @@ const AsyncDependenciesBlock = require("../AsyncDependenciesBlock"); const makeSerializable = require("../util/makeSerializable"); +/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */ + class AMDRequireDependenciesBlock extends AsyncDependenciesBlock { + /** + * @param {DependencyLocation} loc location info + * @param {string=} request request + */ constructor(loc, request) { super(null, loc, request); } diff --git a/lib/dependencies/AMDRequireDependency.js b/lib/dependencies/AMDRequireDependency.js index 3710bee239a..93167b872b9 100644 --- a/lib/dependencies/AMDRequireDependency.js +++ b/lib/dependencies/AMDRequireDependency.js @@ -13,10 +13,17 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class AMDRequireDependency extends NullDependency { + /** + * @param {Range} outerRange outer range + * @param {Range} arrayRange array range + * @param {Range} functionRange function range + * @param {Range} errorCallbackRange error callback range + */ constructor(outerRange, arrayRange, functionRange, errorCallbackRange) { super(); diff --git a/lib/dependencies/AMDRequireItemDependency.js b/lib/dependencies/AMDRequireItemDependency.js index c21d87b641e..2218eba3e5d 100644 --- a/lib/dependencies/AMDRequireItemDependency.js +++ b/lib/dependencies/AMDRequireItemDependency.js @@ -9,7 +9,13 @@ const makeSerializable = require("../util/makeSerializable"); const ModuleDependency = require("./ModuleDependency"); const ModuleDependencyTemplateAsRequireId = require("./ModuleDependencyTemplateAsRequireId"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ + class AMDRequireItemDependency extends ModuleDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + */ constructor(request, range) { super(request); diff --git a/lib/dependencies/CachedConstDependency.js b/lib/dependencies/CachedConstDependency.js index b94dc0bbb96..827ffd3eb91 100644 --- a/lib/dependencies/CachedConstDependency.js +++ b/lib/dependencies/CachedConstDependency.js @@ -18,11 +18,17 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../DependencyTemplates")} DependencyTemplates */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ class CachedConstDependency extends NullDependency { + /** + * @param {TODO} expression expression + * @param {Range} range range + * @param {string} identifier identifier + */ constructor(expression, range, identifier) { super(); diff --git a/lib/dependencies/CommonJsExportRequireDependency.js b/lib/dependencies/CommonJsExportRequireDependency.js index cf2cfe7aec8..33c36d81e1d 100644 --- a/lib/dependencies/CommonJsExportRequireDependency.js +++ b/lib/dependencies/CommonJsExportRequireDependency.js @@ -22,15 +22,26 @@ const processExportInfo = require("./processExportInfo"); /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../Module")} Module */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ +/** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */ const idsSymbol = Symbol("CommonJsExportRequireDependency.ids"); const EMPTY_OBJECT = {}; class CommonJsExportRequireDependency extends ModuleDependency { + /** + * @param {Range} range range + * @param {Range} valueRange value range + * @param {CommonJSDependencyBaseKeywords} base base + * @param {string[]} names names + * @param {string} request request + * @param {string[]} ids ids + * @param {boolean} resultUsed true, when the result is used + */ constructor(range, valueRange, base, names, request, ids, resultUsed) { super(request); this.range = range; diff --git a/lib/dependencies/CommonJsExportsDependency.js b/lib/dependencies/CommonJsExportsDependency.js index ab53f20f273..5c1681da613 100644 --- a/lib/dependencies/CommonJsExportsDependency.js +++ b/lib/dependencies/CommonJsExportsDependency.js @@ -16,12 +16,20 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ +/** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */ const EMPTY_OBJECT = {}; class CommonJsExportsDependency extends NullDependency { + /** + * @param {Range} range range + * @param {Range} valueRange value range + * @param {CommonJSDependencyBaseKeywords} base base + * @param {string[]} names names + */ constructor(range, valueRange, base, names) { super(); this.range = range; diff --git a/lib/dependencies/CommonJsExportsParserPlugin.js b/lib/dependencies/CommonJsExportsParserPlugin.js index 7b5bb050758..f72a2237ba0 100644 --- a/lib/dependencies/CommonJsExportsParserPlugin.js +++ b/lib/dependencies/CommonJsExportsParserPlugin.js @@ -18,7 +18,6 @@ const ModuleDecoratorDependency = require("./ModuleDecoratorDependency"); /** @typedef {import("estree").AssignmentExpression} AssignmentExpression */ /** @typedef {import("estree").CallExpression} CallExpression */ -/** @typedef {import("estree").Expression} ExpressionNode */ /** @typedef {import("estree").Expression} Expression */ /** @typedef {import("estree").Super} Super */ @@ -95,7 +94,7 @@ const isFalsyLiteral = expr => { /** * @param {JavascriptParser} parser the parser - * @param {ExpressionNode} expr expression + * @param {Expression} expr expression * @returns {{ argument: BasicEvaluatedExpression, ids: string[] } | undefined} parsed call */ const parseRequireCall = (parser, expr) => { diff --git a/lib/dependencies/CommonJsFullRequireDependency.js b/lib/dependencies/CommonJsFullRequireDependency.js index 43b2195d939..f8ceb13a46a 100644 --- a/lib/dependencies/CommonJsFullRequireDependency.js +++ b/lib/dependencies/CommonJsFullRequireDependency.js @@ -16,6 +16,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ @@ -23,7 +24,7 @@ const ModuleDependency = require("./ModuleDependency"); class CommonJsFullRequireDependency extends ModuleDependency { /** * @param {string} request the request string - * @param {[number, number]} range location in source code + * @param {Range} range location in source code * @param {string[]} names accessed properties on module */ constructor(request, range, names) { diff --git a/lib/dependencies/CommonJsPlugin.js b/lib/dependencies/CommonJsPlugin.js index 2b05f2b3426..34fbc710fca 100644 --- a/lib/dependencies/CommonJsPlugin.js +++ b/lib/dependencies/CommonJsPlugin.js @@ -34,9 +34,16 @@ const { } = require("../javascript/JavascriptParserHelpers"); const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependency"); +/** @typedef {import("../Compiler")} Compiler */ + const PLUGIN_NAME = "CommonJsPlugin"; class CommonJsPlugin { + /** + * Apply the plugin + * @param {Compiler} compiler the compiler instance + * @returns {void} + */ apply(compiler) { compiler.hooks.compilation.tap( PLUGIN_NAME, diff --git a/lib/dependencies/CommonJsSelfReferenceDependency.js b/lib/dependencies/CommonJsSelfReferenceDependency.js index 094604f7bfc..2aef8cec7f3 100644 --- a/lib/dependencies/CommonJsSelfReferenceDependency.js +++ b/lib/dependencies/CommonJsSelfReferenceDependency.js @@ -17,11 +17,19 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ +/** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */ class CommonJsSelfReferenceDependency extends NullDependency { + /** + * @param {Range} range range + * @param {CommonJSDependencyBaseKeywords} base base + * @param {string[]} names names + * @param {boolean} call is a call + */ constructor(range, base, names, call) { super(); this.range = range; diff --git a/lib/dependencies/ConstDependency.js b/lib/dependencies/ConstDependency.js index f118155cc34..1c9dcfe61fd 100644 --- a/lib/dependencies/ConstDependency.js +++ b/lib/dependencies/ConstDependency.js @@ -15,6 +15,7 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -22,7 +23,7 @@ const NullDependency = require("./NullDependency"); class ConstDependency extends NullDependency { /** * @param {string} expression the expression - * @param {number|[number, number]} range the source range + * @param {number|Range} range the source range * @param {string[]=} runtimeRequirements runtime requirements */ constructor(expression, range, runtimeRequirements) { diff --git a/lib/dependencies/ContextDependencyHelpers.js b/lib/dependencies/ContextDependencyHelpers.js index 97d059bcb4f..6e2a8f74f70 100644 --- a/lib/dependencies/ContextDependencyHelpers.js +++ b/lib/dependencies/ContextDependencyHelpers.js @@ -38,12 +38,12 @@ const splitContextFromPrefix = prefix => { }; /** @typedef {Partial>} PartialContextDependencyOptions */ - -/** @typedef {{ new(options: ContextDependencyOptions, range: [number, number], valueRange: [number, number], ...args: any[]): ContextDependency }} ContextDependencyConstructor */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ +/** @typedef {{ new(options: ContextDependencyOptions, range: Range, valueRange: [number, number], ...args: any[]): ContextDependency }} ContextDependencyConstructor */ /** * @param {ContextDependencyConstructor} Dep the Dependency class - * @param {[number, number]} range source range + * @param {Range} range source range * @param {BasicEvaluatedExpression} param context param * @param {EsTreeNode} expr expr * @param {Pick} options options for context creation diff --git a/lib/dependencies/CreateScriptUrlDependency.js b/lib/dependencies/CreateScriptUrlDependency.js index e2d27bf19f5..9abb5c50cf4 100644 --- a/lib/dependencies/CreateScriptUrlDependency.js +++ b/lib/dependencies/CreateScriptUrlDependency.js @@ -12,12 +12,13 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class CreateScriptUrlDependency extends NullDependency { /** - * @param {[number, number]} range range + * @param {Range} range range */ constructor(range) { super(); diff --git a/lib/dependencies/CriticalDependencyWarning.js b/lib/dependencies/CriticalDependencyWarning.js index 4501e6868ad..653bcc15c88 100644 --- a/lib/dependencies/CriticalDependencyWarning.js +++ b/lib/dependencies/CriticalDependencyWarning.js @@ -9,6 +9,9 @@ const WebpackError = require("../WebpackError"); const makeSerializable = require("../util/makeSerializable"); class CriticalDependencyWarning extends WebpackError { + /** + * @param {string} message message + */ constructor(message) { super(); diff --git a/lib/dependencies/CssImportDependency.js b/lib/dependencies/CssImportDependency.js index d27311f3744..fab838869e9 100644 --- a/lib/dependencies/CssImportDependency.js +++ b/lib/dependencies/CssImportDependency.js @@ -17,6 +17,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */ /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ +/** @typedef {import("../css/CssParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -29,7 +30,7 @@ class CssImportDependency extends ModuleDependency { * \@import url("landscape.css") layer(forms) screen and (orientation: landscape) screen and (orientation: landscape); * * @param {string} request request - * @param {[number, number]} range range of the argument + * @param {Range} range range of the argument * @param {string | undefined} layer layer * @param {string | undefined} supports list of supports conditions * @param {string | undefined} media list of media conditions diff --git a/lib/dependencies/CssLocalIdentifierDependency.js b/lib/dependencies/CssLocalIdentifierDependency.js index 2cf301e2c34..3a282068ab7 100644 --- a/lib/dependencies/CssLocalIdentifierDependency.js +++ b/lib/dependencies/CssLocalIdentifierDependency.js @@ -13,13 +13,14 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */ /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../css/CssParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class CssLocalIdentifierDependency extends NullDependency { /** * @param {string} name name - * @param {[number, number]} range range + * @param {Range} range range * @param {string=} prefix prefix */ constructor(name, range, prefix = "") { diff --git a/lib/dependencies/CssSelfLocalIdentifierDependency.js b/lib/dependencies/CssSelfLocalIdentifierDependency.js index b2c43405068..246cf1b1b2d 100644 --- a/lib/dependencies/CssSelfLocalIdentifierDependency.js +++ b/lib/dependencies/CssSelfLocalIdentifierDependency.js @@ -14,6 +14,7 @@ const CssLocalIdentifierDependency = require("./CssLocalIdentifierDependency"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../css/CssParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ @@ -21,7 +22,7 @@ const CssLocalIdentifierDependency = require("./CssLocalIdentifierDependency"); class CssSelfLocalIdentifierDependency extends CssLocalIdentifierDependency { /** * @param {string} name name - * @param {[number, number]} range range + * @param {Range} range range * @param {string=} prefix prefix * @param {Set=} declaredSet set of declared names (will only be active when in declared set) */ diff --git a/lib/dependencies/CssUrlDependency.js b/lib/dependencies/CssUrlDependency.js index c8d89450570..92ff0a9b83e 100644 --- a/lib/dependencies/CssUrlDependency.js +++ b/lib/dependencies/CssUrlDependency.js @@ -18,6 +18,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */ /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -28,7 +29,7 @@ const getRawDataUrlModule = memoize(() => require("../asset/RawDataUrlModule")); class CssUrlDependency extends ModuleDependency { /** * @param {string} request request - * @param {[number, number]} range range of the argument + * @param {Range} range range of the argument * @param {"string" | "url"} urlType dependency type e.g. url() or string */ constructor(request, range, urlType) { diff --git a/lib/dependencies/DelegatedSourceDependency.js b/lib/dependencies/DelegatedSourceDependency.js index 238c62d00de..737f60e7727 100644 --- a/lib/dependencies/DelegatedSourceDependency.js +++ b/lib/dependencies/DelegatedSourceDependency.js @@ -9,6 +9,9 @@ const makeSerializable = require("../util/makeSerializable"); const ModuleDependency = require("./ModuleDependency"); class DelegatedSourceDependency extends ModuleDependency { + /** + * @param {string} request the request string + */ constructor(request) { super(request); } diff --git a/lib/dependencies/ExportsInfoDependency.js b/lib/dependencies/ExportsInfoDependency.js index e36f4fedff9..df8b1302be2 100644 --- a/lib/dependencies/ExportsInfoDependency.js +++ b/lib/dependencies/ExportsInfoDependency.js @@ -16,6 +16,7 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../Module")} Module */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -84,6 +85,11 @@ const getProperty = (moduleGraph, module, exportName, property, runtime) => { }; class ExportsInfoDependency extends NullDependency { + /** + * @param {Range} range + * @param {string | null} exportName + * @param {string | null} property + */ constructor(range, exportName, property) { super(); this.range = range; diff --git a/lib/dependencies/HarmonyAcceptDependency.js b/lib/dependencies/HarmonyAcceptDependency.js index 4d270120d82..00a367d82c7 100644 --- a/lib/dependencies/HarmonyAcceptDependency.js +++ b/lib/dependencies/HarmonyAcceptDependency.js @@ -13,13 +13,14 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("./HarmonyAcceptImportDependency")} HarmonyAcceptImportDependency */ class HarmonyAcceptDependency extends NullDependency { /** - * @param {[number, number]} range expression range + * @param {Range} range expression range * @param {HarmonyAcceptImportDependency[]} dependencies import dependencies * @param {boolean} hasCallback true, if the range wraps an existing callback */ diff --git a/lib/dependencies/HarmonyAcceptImportDependency.js b/lib/dependencies/HarmonyAcceptImportDependency.js index 9fbffac0fb1..bcf01ef01ae 100644 --- a/lib/dependencies/HarmonyAcceptImportDependency.js +++ b/lib/dependencies/HarmonyAcceptImportDependency.js @@ -14,6 +14,9 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ class HarmonyAcceptImportDependency extends HarmonyImportDependency { + /** + * @param {string} request the request string + */ constructor(request) { super(request, NaN); this.weak = true; diff --git a/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js b/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js index 5f5ebc9aca3..742c9eee786 100644 --- a/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +++ b/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js @@ -12,6 +12,8 @@ const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDepend /** @typedef {import("../ChunkGraph")} ChunkGraph */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Assertions} Assertions */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ @@ -23,6 +25,15 @@ const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDepend * a.x !== undefined; // if x value statically analyzable */ class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDependency { + /** + * @param {string} request the request string + * @param {number} sourceOrder source order + * @param {TODO} ids ids + * @param {TODO} name name + * @param {Range} range location in source code + * @param {Assertions} assertions assertions + * @param {string} operator operator + */ constructor(request, sourceOrder, ids, name, range, assertions, operator) { super(request, sourceOrder, ids, name, range, false, assertions); this.operator = operator; diff --git a/lib/dependencies/HarmonyExportExpressionDependency.js b/lib/dependencies/HarmonyExportExpressionDependency.js index d9aa8d83f7e..fb094399998 100644 --- a/lib/dependencies/HarmonyExportExpressionDependency.js +++ b/lib/dependencies/HarmonyExportExpressionDependency.js @@ -18,10 +18,17 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class HarmonyExportExpressionDependency extends NullDependency { + /** + * @param {Range} range range + * @param {Range} rangeStatement range statement + * @param {string} prefix prefix + * @param {TODO} declarationId declaration id + */ constructor(range, rangeStatement, prefix, declarationId) { super(); this.range = range; diff --git a/lib/dependencies/HarmonyExportImportedSpecifierDependency.js b/lib/dependencies/HarmonyExportImportedSpecifierDependency.js index 5a6f78cb86c..c1ed2cdd0e7 100644 --- a/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +++ b/lib/dependencies/HarmonyExportImportedSpecifierDependency.js @@ -36,6 +36,7 @@ const processExportInfo = require("./processExportInfo"); /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ /** @typedef {import("../WebpackError")} WebpackError */ +/** @typedef {import("../javascript/JavascriptParser").Assertions} Assertions */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -332,7 +333,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency { * @param {ReadonlyArray | Iterable} otherStarExports other star exports in the module before this import * @param {number} exportPresenceMode mode of checking export names * @param {HarmonyStarExportsList} allStarExports all star exports in the module - * @param {Record=} assertions import assertions + * @param {Assertions=} assertions import assertions */ constructor( request, diff --git a/lib/dependencies/HarmonyExportSpecifierDependency.js b/lib/dependencies/HarmonyExportSpecifierDependency.js index d749b6dab69..4e742935942 100644 --- a/lib/dependencies/HarmonyExportSpecifierDependency.js +++ b/lib/dependencies/HarmonyExportSpecifierDependency.js @@ -19,6 +19,10 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class HarmonyExportSpecifierDependency extends NullDependency { + /** + * @param {TODO} id id + * @param {TODO} name name + */ constructor(id, name) { super(); this.id = id; diff --git a/lib/dependencies/HarmonyImportDependency.js b/lib/dependencies/HarmonyImportDependency.js index e0544043f58..b4a65530610 100644 --- a/lib/dependencies/HarmonyImportDependency.js +++ b/lib/dependencies/HarmonyImportDependency.js @@ -24,6 +24,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ /** @typedef {import("../WebpackError")} WebpackError */ +/** @typedef {import("../javascript/JavascriptParser").Assertions} Assertions */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -55,7 +56,7 @@ class HarmonyImportDependency extends ModuleDependency { * * @param {string} request request string * @param {number} sourceOrder source order - * @param {Record=} assertions import assertions + * @param {Assertions=} assertions import assertions */ constructor(request, sourceOrder, assertions) { super(request); diff --git a/lib/dependencies/HarmonyImportSideEffectDependency.js b/lib/dependencies/HarmonyImportSideEffectDependency.js index 2a7cbd933f8..5bd775266d8 100644 --- a/lib/dependencies/HarmonyImportSideEffectDependency.js +++ b/lib/dependencies/HarmonyImportSideEffectDependency.js @@ -16,10 +16,16 @@ const HarmonyImportDependency = require("./HarmonyImportDependency"); /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */ /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ +/** @typedef {import("../javascript/JavascriptParser").Assertions} Assertions */ /** @typedef {import("../util/Hash")} Hash */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ class HarmonyImportSideEffectDependency extends HarmonyImportDependency { + /** + * @param {TODO} request the request string + * @param {number} sourceOrder source order + * @param {Assertions=} assertions assertions + */ constructor(request, sourceOrder, assertions) { super(request, sourceOrder, assertions); } diff --git a/lib/dependencies/HarmonyImportSpecifierDependency.js b/lib/dependencies/HarmonyImportSpecifierDependency.js index 3b7a1d15f1d..e3445e1c0f9 100644 --- a/lib/dependencies/HarmonyImportSpecifierDependency.js +++ b/lib/dependencies/HarmonyImportSpecifierDependency.js @@ -23,6 +23,8 @@ const HarmonyImportDependency = require("./HarmonyImportDependency"); /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */ /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ /** @typedef {import("../WebpackError")} WebpackError */ +/** @typedef {import("../javascript/JavascriptParser").Assertions} Assertions */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -33,6 +35,15 @@ const idsSymbol = Symbol("HarmonyImportSpecifierDependency.ids"); const { ExportPresenceModes } = HarmonyImportDependency; class HarmonyImportSpecifierDependency extends HarmonyImportDependency { + /** + * @param {TODO} request request + * @param {number} sourceOrder source order + * @param {string[]} ids ids + * @param {string} name name + * @param {Range} range range + * @param {TODO} exportPresenceMode export presence mode + * @param {Assertions=} assertions assertions + */ constructor( request, sourceOrder, diff --git a/lib/dependencies/ImportContextDependency.js b/lib/dependencies/ImportContextDependency.js index f94a86fcede..a1811e722bc 100644 --- a/lib/dependencies/ImportContextDependency.js +++ b/lib/dependencies/ImportContextDependency.js @@ -9,10 +9,16 @@ const makeSerializable = require("../util/makeSerializable"); const ContextDependency = require("./ContextDependency"); const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class ImportContextDependency extends ContextDependency { + /** + * @param {TODO} options options + * @param {Range} range range + * @param {Range} valueRange value range + */ constructor(options, range, valueRange) { super(options); diff --git a/lib/dependencies/ImportDependency.js b/lib/dependencies/ImportDependency.js index 98ff9b2654e..11d9a97734a 100644 --- a/lib/dependencies/ImportDependency.js +++ b/lib/dependencies/ImportDependency.js @@ -14,6 +14,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ @@ -21,7 +22,7 @@ const ModuleDependency = require("./ModuleDependency"); class ImportDependency extends ModuleDependency { /** * @param {string} request the request - * @param {[number, number]} range expression range + * @param {Range} range expression range * @param {string[][]=} referencedExports list of referenced exports */ constructor(request, range, referencedExports) { diff --git a/lib/dependencies/ImportEagerDependency.js b/lib/dependencies/ImportEagerDependency.js index 2aa9a570385..e9daa524512 100644 --- a/lib/dependencies/ImportEagerDependency.js +++ b/lib/dependencies/ImportEagerDependency.js @@ -13,11 +13,12 @@ const ImportDependency = require("./ImportDependency"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ class ImportEagerDependency extends ImportDependency { /** * @param {string} request the request - * @param {[number, number]} range expression range + * @param {Range} range expression range * @param {string[][]=} referencedExports list of referenced exports */ constructor(request, range, referencedExports) { diff --git a/lib/dependencies/ImportMetaHotAcceptDependency.js b/lib/dependencies/ImportMetaHotAcceptDependency.js index 66329d7fcbb..70d8199338d 100644 --- a/lib/dependencies/ImportMetaHotAcceptDependency.js +++ b/lib/dependencies/ImportMetaHotAcceptDependency.js @@ -9,7 +9,13 @@ const makeSerializable = require("../util/makeSerializable"); const ModuleDependency = require("./ModuleDependency"); const ModuleDependencyTemplateAsId = require("./ModuleDependencyTemplateAsId"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ + class ImportMetaHotAcceptDependency extends ModuleDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + */ constructor(request, range) { super(request); this.range = range; diff --git a/lib/dependencies/ImportMetaHotDeclineDependency.js b/lib/dependencies/ImportMetaHotDeclineDependency.js index b9d1a5a57f5..c6c35a250ce 100644 --- a/lib/dependencies/ImportMetaHotDeclineDependency.js +++ b/lib/dependencies/ImportMetaHotDeclineDependency.js @@ -9,7 +9,13 @@ const makeSerializable = require("../util/makeSerializable"); const ModuleDependency = require("./ModuleDependency"); const ModuleDependencyTemplateAsId = require("./ModuleDependencyTemplateAsId"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ + class ImportMetaHotDeclineDependency extends ModuleDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + */ constructor(request, range) { super(request); diff --git a/lib/dependencies/ImportWeakDependency.js b/lib/dependencies/ImportWeakDependency.js index fc141965488..4e19c50268e 100644 --- a/lib/dependencies/ImportWeakDependency.js +++ b/lib/dependencies/ImportWeakDependency.js @@ -13,11 +13,12 @@ const ImportDependency = require("./ImportDependency"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ class ImportWeakDependency extends ImportDependency { /** * @param {string} request the request - * @param {[number, number]} range expression range + * @param {Range} range expression range * @param {string[][]=} referencedExports list of referenced exports */ constructor(request, range, referencedExports) { diff --git a/lib/dependencies/LocalModuleDependency.js b/lib/dependencies/LocalModuleDependency.js index 4e576ee881c..93aee8d21e4 100644 --- a/lib/dependencies/LocalModuleDependency.js +++ b/lib/dependencies/LocalModuleDependency.js @@ -11,10 +11,17 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ +/** @typedef {import("./LocalModule")} LocalModule */ class LocalModuleDependency extends NullDependency { + /** + * @param {LocalModule} localModule local module + * @param {Range} range range + * @param {boolean} callNew true, when the local module should be called with new + */ constructor(localModule, range, callNew) { super(); diff --git a/lib/dependencies/ModuleHotAcceptDependency.js b/lib/dependencies/ModuleHotAcceptDependency.js index 9ccf8a3033b..1916a7e2563 100644 --- a/lib/dependencies/ModuleHotAcceptDependency.js +++ b/lib/dependencies/ModuleHotAcceptDependency.js @@ -9,7 +9,13 @@ const makeSerializable = require("../util/makeSerializable"); const ModuleDependency = require("./ModuleDependency"); const ModuleDependencyTemplateAsId = require("./ModuleDependencyTemplateAsId"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ + class ModuleHotAcceptDependency extends ModuleDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + */ constructor(request, range) { super(request); this.range = range; diff --git a/lib/dependencies/ModuleHotDeclineDependency.js b/lib/dependencies/ModuleHotDeclineDependency.js index c5edb770e7b..70423774b4e 100644 --- a/lib/dependencies/ModuleHotDeclineDependency.js +++ b/lib/dependencies/ModuleHotDeclineDependency.js @@ -9,7 +9,13 @@ const makeSerializable = require("../util/makeSerializable"); const ModuleDependency = require("./ModuleDependency"); const ModuleDependencyTemplateAsId = require("./ModuleDependencyTemplateAsId"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ + class ModuleHotDeclineDependency extends ModuleDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + */ constructor(request, range) { super(request); diff --git a/lib/dependencies/PrefetchDependency.js b/lib/dependencies/PrefetchDependency.js index 3c19dae2efc..59e22c59a79 100644 --- a/lib/dependencies/PrefetchDependency.js +++ b/lib/dependencies/PrefetchDependency.js @@ -8,6 +8,9 @@ const ModuleDependency = require("./ModuleDependency"); class PrefetchDependency extends ModuleDependency { + /** + * @param {string} request the request string + */ constructor(request) { super(request); } diff --git a/lib/dependencies/ProvidedDependency.js b/lib/dependencies/ProvidedDependency.js index 5fb2f3d5957..17bce4caba1 100644 --- a/lib/dependencies/ProvidedDependency.js +++ b/lib/dependencies/ProvidedDependency.js @@ -18,6 +18,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../DependencyTemplates")} DependencyTemplates */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -37,7 +38,7 @@ class ProvidedDependency extends ModuleDependency { * @param {string} request request * @param {string} identifier identifier * @param {string[]} ids ids - * @param {[number, number]} range range + * @param {Range} range range */ constructor(request, identifier, ids, range) { super(request); diff --git a/lib/dependencies/PureExpressionDependency.js b/lib/dependencies/PureExpressionDependency.js index 180141ed511..0b7372fe7f7 100644 --- a/lib/dependencies/PureExpressionDependency.js +++ b/lib/dependencies/PureExpressionDependency.js @@ -17,13 +17,14 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ class PureExpressionDependency extends NullDependency { /** - * @param {[number, number]} range the source range + * @param {Range} range the source range */ constructor(range) { super(); diff --git a/lib/dependencies/RequireContextDependency.js b/lib/dependencies/RequireContextDependency.js index 21c8f06eb6d..9aa883f2edb 100644 --- a/lib/dependencies/RequireContextDependency.js +++ b/lib/dependencies/RequireContextDependency.js @@ -9,7 +9,13 @@ const makeSerializable = require("../util/makeSerializable"); const ContextDependency = require("./ContextDependency"); const ModuleDependencyTemplateAsRequireId = require("./ModuleDependencyTemplateAsRequireId"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ + class RequireContextDependency extends ContextDependency { + /** + * @param {TODO} options options + * @param {Range} range range + */ constructor(options, range) { super(options); diff --git a/lib/dependencies/RequireEnsureDependenciesBlock.js b/lib/dependencies/RequireEnsureDependenciesBlock.js index 1928dbe0f94..51c0984c4ae 100644 --- a/lib/dependencies/RequireEnsureDependenciesBlock.js +++ b/lib/dependencies/RequireEnsureDependenciesBlock.js @@ -9,6 +9,10 @@ const AsyncDependenciesBlock = require("../AsyncDependenciesBlock"); const makeSerializable = require("../util/makeSerializable"); class RequireEnsureDependenciesBlock extends AsyncDependenciesBlock { + /** + * @param {TODO} chunkName chunk name + * @param {TODO} loc location info + */ constructor(chunkName, loc) { super(chunkName, loc, null); } diff --git a/lib/dependencies/RequireEnsureDependency.js b/lib/dependencies/RequireEnsureDependency.js index c552faab993..464a009c5dc 100644 --- a/lib/dependencies/RequireEnsureDependency.js +++ b/lib/dependencies/RequireEnsureDependency.js @@ -13,10 +13,16 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class RequireEnsureDependency extends NullDependency { + /** + * @param {Range} range range + * @param {Range} contentRange content range + * @param {Range} errorHandlerRange error handler range + */ constructor(range, contentRange, errorHandlerRange) { super(); diff --git a/lib/dependencies/RequireEnsureItemDependency.js b/lib/dependencies/RequireEnsureItemDependency.js index 70d2df1f0ed..f9a465a55c9 100644 --- a/lib/dependencies/RequireEnsureItemDependency.js +++ b/lib/dependencies/RequireEnsureItemDependency.js @@ -10,6 +10,9 @@ const ModuleDependency = require("./ModuleDependency"); const NullDependency = require("./NullDependency"); class RequireEnsureItemDependency extends ModuleDependency { + /** + * @param {string} request the request string + */ constructor(request) { super(request); } diff --git a/lib/dependencies/RequireEnsurePlugin.js b/lib/dependencies/RequireEnsurePlugin.js index 01099071d16..7ee964523f9 100644 --- a/lib/dependencies/RequireEnsurePlugin.js +++ b/lib/dependencies/RequireEnsurePlugin.js @@ -19,9 +19,16 @@ const { toConstantDependency } = require("../javascript/JavascriptParserHelpers"); +/** @typedef {import("../Compiler")} Compiler */ + const PLUGIN_NAME = "RequireEnsurePlugin"; class RequireEnsurePlugin { + /** + * Apply the plugin + * @param {Compiler} compiler the compiler instance + * @returns {void} + */ apply(compiler) { compiler.hooks.compilation.tap( PLUGIN_NAME, diff --git a/lib/dependencies/RequireHeaderDependency.js b/lib/dependencies/RequireHeaderDependency.js index 79022636825..264a92aabf4 100644 --- a/lib/dependencies/RequireHeaderDependency.js +++ b/lib/dependencies/RequireHeaderDependency.js @@ -12,10 +12,14 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class RequireHeaderDependency extends NullDependency { + /** + * @param {Range} range range + */ constructor(range) { super(); if (!Array.isArray(range)) throw new Error("range must be valid"); diff --git a/lib/dependencies/RequireIncludeDependency.js b/lib/dependencies/RequireIncludeDependency.js index 35c0f45404c..f0ab47c1a20 100644 --- a/lib/dependencies/RequireIncludeDependency.js +++ b/lib/dependencies/RequireIncludeDependency.js @@ -14,9 +14,14 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ class RequireIncludeDependency extends ModuleDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + */ constructor(request, range) { super(request); diff --git a/lib/dependencies/RequireIncludeDependencyParserPlugin.js b/lib/dependencies/RequireIncludeDependencyParserPlugin.js index d74db996c62..6e95b239dbb 100644 --- a/lib/dependencies/RequireIncludeDependencyParserPlugin.js +++ b/lib/dependencies/RequireIncludeDependencyParserPlugin.js @@ -14,6 +14,9 @@ const makeSerializable = require("../util/makeSerializable"); const RequireIncludeDependency = require("./RequireIncludeDependency"); module.exports = class RequireIncludeDependencyParserPlugin { + /** + * @param {boolean} warn true: warn about deprecation, false: don't warn + */ constructor(warn) { this.warn = warn; } diff --git a/lib/dependencies/RequireIncludePlugin.js b/lib/dependencies/RequireIncludePlugin.js index 056be3a6415..dc8e55251ee 100644 --- a/lib/dependencies/RequireIncludePlugin.js +++ b/lib/dependencies/RequireIncludePlugin.js @@ -12,9 +12,16 @@ const { const RequireIncludeDependency = require("./RequireIncludeDependency"); const RequireIncludeDependencyParserPlugin = require("./RequireIncludeDependencyParserPlugin"); +/** @typedef {import("../Compiler")} Compiler */ + const PLUGIN_NAME = "RequireIncludePlugin"; class RequireIncludePlugin { + /** + * Apply the plugin + * @param {Compiler} compiler the compiler instance + * @returns {void} + */ apply(compiler) { compiler.hooks.compilation.tap( PLUGIN_NAME, diff --git a/lib/dependencies/RequireResolveContextDependency.js b/lib/dependencies/RequireResolveContextDependency.js index 1d2454a4f15..dd82e922094 100644 --- a/lib/dependencies/RequireResolveContextDependency.js +++ b/lib/dependencies/RequireResolveContextDependency.js @@ -9,10 +9,18 @@ const makeSerializable = require("../util/makeSerializable"); const ContextDependency = require("./ContextDependency"); const ContextDependencyTemplateAsId = require("./ContextDependencyTemplateAsId"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ +/** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */ class RequireResolveContextDependency extends ContextDependency { + /** + * @param {ContextDependencyOptions} options options + * @param {Range} range range + * @param {Range} valueRange value range + * @param {TODO} context context + */ constructor(options, range, valueRange, context) { super(options, context); diff --git a/lib/dependencies/RequireResolveDependency.js b/lib/dependencies/RequireResolveDependency.js index e3f0917ecb4..b4f014b54c5 100644 --- a/lib/dependencies/RequireResolveDependency.js +++ b/lib/dependencies/RequireResolveDependency.js @@ -12,9 +12,15 @@ const ModuleDependencyAsId = require("./ModuleDependencyTemplateAsId"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ class RequireResolveDependency extends ModuleDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + * @param {TODO} context context + */ constructor(request, range, context) { super(request); diff --git a/lib/dependencies/RequireResolveHeaderDependency.js b/lib/dependencies/RequireResolveHeaderDependency.js index 6425d2e5942..9cb9c2428f4 100644 --- a/lib/dependencies/RequireResolveHeaderDependency.js +++ b/lib/dependencies/RequireResolveHeaderDependency.js @@ -11,10 +11,14 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class RequireResolveHeaderDependency extends NullDependency { + /** + * @param {Range} range range + */ constructor(range) { super(); diff --git a/lib/dependencies/URLDependency.js b/lib/dependencies/URLDependency.js index 06ed7d9b107..b6f615593c4 100644 --- a/lib/dependencies/URLDependency.js +++ b/lib/dependencies/URLDependency.js @@ -22,6 +22,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */ /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -32,8 +33,8 @@ const getRawDataUrlModule = memoize(() => require("../asset/RawDataUrlModule")); class URLDependency extends ModuleDependency { /** * @param {string} request request - * @param {[number, number]} range range of the arguments of new URL( |> ... <| ) - * @param {[number, number]} outerRange range of the full |> new URL(...) <| + * @param {Range} range range of the arguments of new URL( |> ... <| ) + * @param {Range} outerRange range of the full |> new URL(...) <| * @param {boolean=} relative use relative urls instead of absolute with base uri */ constructor(request, range, outerRange, relative) { diff --git a/lib/dependencies/UnsupportedDependency.js b/lib/dependencies/UnsupportedDependency.js index 0f4ee2195e6..6796634c9b4 100644 --- a/lib/dependencies/UnsupportedDependency.js +++ b/lib/dependencies/UnsupportedDependency.js @@ -11,10 +11,15 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class UnsupportedDependency extends NullDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + */ constructor(request, range) { super(); diff --git a/lib/dependencies/WebAssemblyExportImportedDependency.js b/lib/dependencies/WebAssemblyExportImportedDependency.js index f62f311b997..c2452ecd1c6 100644 --- a/lib/dependencies/WebAssemblyExportImportedDependency.js +++ b/lib/dependencies/WebAssemblyExportImportedDependency.js @@ -17,6 +17,12 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ class WebAssemblyExportImportedDependency extends ModuleDependency { + /** + * @param {string} exportName export name + * @param {string} request request + * @param {string} name name + * @param {TODO} valueType value type + */ constructor(exportName, request, name, valueType) { super(request); /** @type {string} */ diff --git a/lib/dependencies/WebpackIsIncludedDependency.js b/lib/dependencies/WebpackIsIncludedDependency.js index f3406ebd90d..ba41e4e162e 100644 --- a/lib/dependencies/WebpackIsIncludedDependency.js +++ b/lib/dependencies/WebpackIsIncludedDependency.js @@ -15,9 +15,14 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ class WebpackIsIncludedDependency extends ModuleDependency { + /** + * @param {string} request the request string + * @param {Range} range location in source code + */ constructor(request, range) { super(request); diff --git a/lib/dependencies/WorkerDependency.js b/lib/dependencies/WorkerDependency.js index 4c183691f8f..812653d8471 100644 --- a/lib/dependencies/WorkerDependency.js +++ b/lib/dependencies/WorkerDependency.js @@ -18,6 +18,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ /** @typedef {import("../Entrypoint")} Entrypoint */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ @@ -26,7 +27,7 @@ const ModuleDependency = require("./ModuleDependency"); class WorkerDependency extends ModuleDependency { /** * @param {string} request request - * @param {[number, number]} range range + * @param {Range} range range * @param {Object} workerDependencyOptions options * @param {string} workerDependencyOptions.publicPath public path for the worker */ diff --git a/lib/dependencies/getFunctionExpression.js b/lib/dependencies/getFunctionExpression.js index 14fd1396e60..e792b9b96c6 100644 --- a/lib/dependencies/getFunctionExpression.js +++ b/lib/dependencies/getFunctionExpression.js @@ -5,6 +5,13 @@ "use strict"; +/** @typedef {import("estree").Expression} Expression */ +/** @typedef {import("estree").SpreadElement} SpreadElement */ + +/** + * @param {Expression} expr expressions + * @returns {{fn: Expression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined }} + */ module.exports = expr => { // if ( diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index d02cb75d4f7..9878a153385 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -53,6 +53,7 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").ThisExpression} ThisExpressionNode */ /** @typedef {import("estree").UnaryExpression} UnaryExpressionNode */ /** @typedef {import("estree").VariableDeclarator} VariableDeclaratorNode */ +/** @typedef {Record} Assertions */ /** @template T @typedef {import("tapable").AsArray} AsArray */ /** @typedef {import("../Parser").ParserState} ParserState */ /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */ @@ -102,6 +103,8 @@ class VariableInfo { * @property {boolean} inTry */ +/** @typedef {[number, number]} Range */ + /** * Helper function for joining two ranges into a single range. This is useful * when working with AST nodes, as it allows you to combine the ranges of child nodes @@ -1380,7 +1383,7 @@ class JavascriptParser extends Parser { : "" + argExpr.number; const newString = value + (stringSuffix ? stringSuffix.string : ""); - const newRange = /** @type {[number, number]} */ ([ + const newRange = /** @type {Range} */ ([ argExpr.range[0], (stringSuffix || argExpr).range[1] ]); diff --git a/types.d.ts b/types.d.ts index 6403e52b16f..44755b1ecd5 100644 --- a/types.d.ts +++ b/types.d.ts @@ -227,6 +227,9 @@ declare interface ArgumentConfig { type: "string" | "number" | "boolean" | "path" | "enum" | "RegExp" | "reset"; values?: any[]; } +declare interface Assertions { + [index: string]: any; +} declare interface Asset { /** * the filename of the asset @@ -4618,11 +4621,7 @@ declare interface HandleModuleCreationOptions { connectOrigin?: boolean; } declare class HarmonyImportDependency extends ModuleDependency { - constructor( - request: string, - sourceOrder: number, - assertions?: Record - ); + constructor(request: string, sourceOrder: number, assertions?: Assertions); sourceOrder: number; getImportVar(moduleGraph: ModuleGraph): string; getImportStatement( From 824333a2cb158c86e1fa6e4b7c6608976bd39cb8 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Sun, 21 May 2023 23:35:30 +0300 Subject: [PATCH 03/12] refactor(types): more --- lib/dependencies/CssUrlDependency.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/dependencies/CssUrlDependency.js b/lib/dependencies/CssUrlDependency.js index 92ff0a9b83e..5054fde6202 100644 --- a/lib/dependencies/CssUrlDependency.js +++ b/lib/dependencies/CssUrlDependency.js @@ -74,6 +74,10 @@ class CssUrlDependency extends ModuleDependency { } } +/** + * @param {string} str string + * @returns {string} string in quotes if needed + */ const cssEscapeString = str => { let countWhiteOrBracket = 0; let countQuotation = 0; From 7cba20d4621d7ef0e2b53a5bff71144487f41d22 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 00:06:31 +0300 Subject: [PATCH 04/12] refactor: types fix --- lib/dependencies/ExportsInfoDependency.js | 6 +++--- lib/dependencies/getFunctionExpression.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/dependencies/ExportsInfoDependency.js b/lib/dependencies/ExportsInfoDependency.js index df8b1302be2..227e086a151 100644 --- a/lib/dependencies/ExportsInfoDependency.js +++ b/lib/dependencies/ExportsInfoDependency.js @@ -86,9 +86,9 @@ const getProperty = (moduleGraph, module, exportName, property, runtime) => { class ExportsInfoDependency extends NullDependency { /** - * @param {Range} range - * @param {string | null} exportName - * @param {string | null} property + * @param {Range} range range + * @param {string | null} exportName export name + * @param {string | null} property property */ constructor(range, exportName, property) { super(); diff --git a/lib/dependencies/getFunctionExpression.js b/lib/dependencies/getFunctionExpression.js index e792b9b96c6..f485cdb7d21 100644 --- a/lib/dependencies/getFunctionExpression.js +++ b/lib/dependencies/getFunctionExpression.js @@ -10,7 +10,7 @@ /** * @param {Expression} expr expressions - * @returns {{fn: Expression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined }} + * @returns {{fn: Expression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined }} function expression with additional information */ module.exports = expr => { // From 13f7bed151142c960e24224bdc594d100705e1dd Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 00:17:19 +0300 Subject: [PATCH 05/12] refactor: types --- lib/SelfModuleFactory.js | 12 ++++++++++++ lib/dependencies/ExportsInfoDependency.js | 2 +- lib/dependencies/getFunctionExpression.js | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/SelfModuleFactory.js b/lib/SelfModuleFactory.js index b2430a44097..ee47365af27 100644 --- a/lib/SelfModuleFactory.js +++ b/lib/SelfModuleFactory.js @@ -5,11 +5,23 @@ "use strict"; +/** @typedef {import("./ModuleFactory").ModuleFactoryCreateData} ModuleFactoryCreateData */ +/** @typedef {import("./ModuleFactory").ModuleFactoryResult} ModuleFactoryResult */ +/** @typedef {import("./ModuleGraph")} ModuleGraph */ + class SelfModuleFactory { + /** + * @param {ModuleGraph} moduleGraph module graph + */ constructor(moduleGraph) { this.moduleGraph = moduleGraph; } + /** + * @param {ModuleFactoryCreateData} data data object + * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @returns {void} + */ create(data, callback) { const module = this.moduleGraph.getParentModule(data.dependencies[0]); callback(null, { diff --git a/lib/dependencies/ExportsInfoDependency.js b/lib/dependencies/ExportsInfoDependency.js index 227e086a151..add9c168757 100644 --- a/lib/dependencies/ExportsInfoDependency.js +++ b/lib/dependencies/ExportsInfoDependency.js @@ -87,7 +87,7 @@ const getProperty = (moduleGraph, module, exportName, property, runtime) => { class ExportsInfoDependency extends NullDependency { /** * @param {Range} range range - * @param {string | null} exportName export name + * @param {TODO} exportName export name * @param {string | null} property property */ constructor(range, exportName, property) { diff --git a/lib/dependencies/getFunctionExpression.js b/lib/dependencies/getFunctionExpression.js index f485cdb7d21..f99123180e4 100644 --- a/lib/dependencies/getFunctionExpression.js +++ b/lib/dependencies/getFunctionExpression.js @@ -10,7 +10,7 @@ /** * @param {Expression} expr expressions - * @returns {{fn: Expression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined }} function expression with additional information + * @returns {{fn: TODO, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined }} function expression with additional information */ module.exports = expr => { // From d58b0c9c26ddc7eee299953043c363f0c7b82881 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 00:47:28 +0300 Subject: [PATCH 06/12] refactor: types more --- lib/DllEntryPlugin.js | 5 +++++ lib/dependencies/AMDDefineDependencyParserPlugin.js | 6 ++++++ .../AMDRequireDependenciesBlockParserPlugin.js | 6 ++++++ lib/dependencies/CachedConstDependency.js | 2 +- lib/dependencies/CommonJsExportsParserPlugin.js | 1 + lib/dependencies/CssUrlDependency.js | 7 ++++++- lib/dependencies/HarmonyDetectionParserPlugin.js | 6 ++++++ lib/util/makeSerializable.js | 7 +++++++ 8 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/DllEntryPlugin.js b/lib/DllEntryPlugin.js index 529eb0de9e2..1d8f1d50fd2 100644 --- a/lib/DllEntryPlugin.js +++ b/lib/DllEntryPlugin.js @@ -10,6 +10,11 @@ const DllEntryDependency = require("./dependencies/DllEntryDependency"); const EntryDependency = require("./dependencies/EntryDependency"); class DllEntryPlugin { + /** + * @param {string} context context + * @param {string[]} entries entry names + * @param {TODO} options options + */ constructor(context, entries, options) { this.context = context; this.entries = entries; diff --git a/lib/dependencies/AMDDefineDependencyParserPlugin.js b/lib/dependencies/AMDDefineDependencyParserPlugin.js index 7d1c7e9e041..6c82cecf246 100644 --- a/lib/dependencies/AMDDefineDependencyParserPlugin.js +++ b/lib/dependencies/AMDDefineDependencyParserPlugin.js @@ -16,6 +16,8 @@ const DynamicExports = require("./DynamicExports"); const LocalModuleDependency = require("./LocalModuleDependency"); const { addLocalModule, getLocalModule } = require("./LocalModulesHelpers"); +/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ + const isBoundFunctionExpression = expr => { if (expr.type !== "CallExpression") return false; if (expr.callee.type !== "MemberExpression") return false; @@ -43,6 +45,10 @@ class AMDDefineDependencyParserPlugin { this.options = options; } + /** + * @param {JavascriptParser} parser the parser + * @returns {void} + */ apply(parser) { parser.hooks.call .for("define") diff --git a/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js b/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js index f49f55b4ff1..061e5c92c55 100644 --- a/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +++ b/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js @@ -19,6 +19,8 @@ const { getLocalModule } = require("./LocalModulesHelpers"); const UnsupportedDependency = require("./UnsupportedDependency"); const getFunctionExpression = require("./getFunctionExpression"); +/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ + class AMDRequireDependenciesBlockParserPlugin { constructor(options) { this.options = options; @@ -50,6 +52,10 @@ class AMDRequireDependenciesBlockParserPlugin { return bindThis; } + /** + * @param {JavascriptParser} parser the parser + * @returns {void} + */ apply(parser) { parser.hooks.call .for("require") diff --git a/lib/dependencies/CachedConstDependency.js b/lib/dependencies/CachedConstDependency.js index 827ffd3eb91..e02aa8af1ed 100644 --- a/lib/dependencies/CachedConstDependency.js +++ b/lib/dependencies/CachedConstDependency.js @@ -25,7 +25,7 @@ const NullDependency = require("./NullDependency"); class CachedConstDependency extends NullDependency { /** - * @param {TODO} expression expression + * @param {string} expression expression * @param {Range} range range * @param {string} identifier identifier */ diff --git a/lib/dependencies/CommonJsExportsParserPlugin.js b/lib/dependencies/CommonJsExportsParserPlugin.js index f72a2237ba0..c664947a92e 100644 --- a/lib/dependencies/CommonJsExportsParserPlugin.js +++ b/lib/dependencies/CommonJsExportsParserPlugin.js @@ -133,6 +133,7 @@ class CommonJsExportsParserPlugin { /** * @param {JavascriptParser} parser the parser + * @returns {void} */ apply(parser) { const enableStructuredExports = () => { diff --git a/lib/dependencies/CssUrlDependency.js b/lib/dependencies/CssUrlDependency.js index 5054fde6202..deed1098c5b 100644 --- a/lib/dependencies/CssUrlDependency.js +++ b/lib/dependencies/CssUrlDependency.js @@ -125,6 +125,7 @@ CssUrlDependency.Template = class CssUrlDependencyTemplate extends ( ) { const dep = /** @type {CssUrlDependency} */ (dependency); + /** @type {string | undefined} */ let newValue; switch (dep.urlType) { @@ -148,7 +149,11 @@ CssUrlDependency.Template = class CssUrlDependencyTemplate extends ( break; } - source.replace(dep.range[0], dep.range[1] - 1, newValue); + source.replace( + dep.range[0], + dep.range[1] - 1, + /** @type {string} */ (newValue) + ); } }; diff --git a/lib/dependencies/HarmonyDetectionParserPlugin.js b/lib/dependencies/HarmonyDetectionParserPlugin.js index 923fff73614..2fd1435911d 100644 --- a/lib/dependencies/HarmonyDetectionParserPlugin.js +++ b/lib/dependencies/HarmonyDetectionParserPlugin.js @@ -10,12 +10,18 @@ const DynamicExports = require("./DynamicExports"); const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency"); const HarmonyExports = require("./HarmonyExports"); +/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ + module.exports = class HarmonyDetectionParserPlugin { constructor(options) { const { topLevelAwait = false } = options || {}; this.topLevelAwait = topLevelAwait; } + /** + * @param {JavascriptParser} parser the parser + * @returns {void} + */ apply(parser) { parser.hooks.program.tap("HarmonyDetectionParserPlugin", ast => { const isStrictHarmony = diff --git a/lib/util/makeSerializable.js b/lib/util/makeSerializable.js index 55593c19f71..c1d777963ab 100644 --- a/lib/util/makeSerializable.js +++ b/lib/util/makeSerializable.js @@ -6,6 +6,8 @@ const { register } = require("./serialization"); +/** @typedef {import("../serialization/ObjectMiddleware").Constructor} Constructor */ + class ClassSerializer { constructor(Constructor) { this.Constructor = Constructor; @@ -25,6 +27,11 @@ class ClassSerializer { } } +/** + * @param {Constructor} Constructor the constructor + * @param {string} request the request which will be required when deserializing + * @param {string | null} [name] the name to make multiple serializer unique when sharing a request + */ module.exports = (Constructor, request, name = null) => { register(Constructor, request, name, new ClassSerializer(Constructor)); }; From 78075cac672d30eae313f52aedf0ae509fb62601 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 01:28:57 +0300 Subject: [PATCH 07/12] refactor: types more --- .../HarmonyExportExpressionDependency.js | 3 +- .../HarmonyExportHeaderDependency.js | 5 +++ ...ImportMetaContextDependencyParserPlugin.js | 45 +++++++++++++------ lib/dependencies/ImportParserPlugin.js | 5 +++ lib/javascript/JavascriptParser.js | 36 ++++++++------- types.d.ts | 9 ++-- 6 files changed, 70 insertions(+), 33 deletions(-) diff --git a/lib/dependencies/HarmonyExportExpressionDependency.js b/lib/dependencies/HarmonyExportExpressionDependency.js index fb094399998..db78aecce12 100644 --- a/lib/dependencies/HarmonyExportExpressionDependency.js +++ b/lib/dependencies/HarmonyExportExpressionDependency.js @@ -27,7 +27,7 @@ class HarmonyExportExpressionDependency extends NullDependency { * @param {Range} range range * @param {Range} rangeStatement range statement * @param {string} prefix prefix - * @param {TODO} declarationId declaration id + * @param {string | { range: Range, prefix: string, suffix: string }} [declarationId] declaration id */ constructor(range, rangeStatement, prefix, declarationId) { super(); @@ -151,6 +151,7 @@ HarmonyExportExpressionDependency.Template = class HarmonyExportDependencyTempla `/* harmony default export */ ${dep.prefix}` ); } else { + /** @type {string} */ let content; const name = ConcatenationScope.DEFAULT_EXPORT; if (runtimeTemplate.supportsConst()) { diff --git a/lib/dependencies/HarmonyExportHeaderDependency.js b/lib/dependencies/HarmonyExportHeaderDependency.js index 5aa80545abb..bbed777a4bb 100644 --- a/lib/dependencies/HarmonyExportHeaderDependency.js +++ b/lib/dependencies/HarmonyExportHeaderDependency.js @@ -11,10 +11,15 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class HarmonyExportHeaderDependency extends NullDependency { + /** + * @param {Range} range range + * @param {Range} rangeStatement range statement + */ constructor(range, rangeStatement) { super(); this.range = range; diff --git a/lib/dependencies/ImportMetaContextDependencyParserPlugin.js b/lib/dependencies/ImportMetaContextDependencyParserPlugin.js index 6019bf550b1..db958b86d76 100644 --- a/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +++ b/lib/dependencies/ImportMetaContextDependencyParserPlugin.js @@ -11,13 +11,20 @@ const { } = require("../javascript/JavascriptParserHelpers"); const ImportMetaContextDependency = require("./ImportMetaContextDependency"); -/** @typedef {import("estree").Expression} ExpressionNode */ -/** @typedef {import("estree").ObjectExpression} ObjectExpressionNode */ +/** @typedef {import("estree").Expression} Expression */ +/** @typedef {import("estree").ObjectExpression} ObjectExpression */ +/** @typedef {import("estree").Property} Property */ +/** @typedef {import("estree").SourceLocation} SourceLocation */ /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ /** @typedef {import("../ContextModule").ContextModuleOptions} ContextModuleOptions */ /** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */ /** @typedef {Pick&{groupOptions: RawChunkGroupOptions, exports?: ContextModuleOptions["referencedExports"]}} ImportMetaContextOptions */ +/** + * @param {TODO} prop property + * @param {string} expect except message + * @returns {WebpackError} error + */ function createPropertyParseError(prop, expect) { return createError( `Parsing import.meta.webpackContext options failed. Unknown value for property ${JSON.stringify( @@ -27,6 +34,11 @@ function createPropertyParseError(prop, expect) { ); } +/** + * @param {string} msg message + * @param {SourceLocation} loc location + * @returns {WebpackError} error + */ function createError(msg, loc) { const error = new WebpackError(msg); error.name = "ImportMetaContextError"; @@ -35,6 +47,10 @@ function createError(msg, loc) { } module.exports = class ImportMetaContextDependencyParserPlugin { + /** + * @param {JavascriptParser} parser the parser + * @returns {void} + */ apply(parser) { parser.hooks.evaluateIdentifier .for("import.meta.webpackContext") @@ -52,7 +68,9 @@ module.exports = class ImportMetaContextDependencyParserPlugin { if (expr.arguments.length < 1 || expr.arguments.length > 2) return; const [directoryNode, optionsNode] = expr.arguments; if (optionsNode && optionsNode.type !== "ObjectExpression") return; - const requestExpr = parser.evaluateExpression(directoryNode); + const requestExpr = parser.evaluateExpression( + /** @type {Expression} */ (directoryNode) + ); if (!requestExpr.isString()) return; const request = requestExpr.string; const errors = []; @@ -71,7 +89,8 @@ module.exports = class ImportMetaContextDependencyParserPlugin { /** @type {ContextModuleOptions["referencedExports"]} */ let exports; if (optionsNode) { - for (const prop of optionsNode.properties) { + for (const prop of /** @type {ObjectExpression} */ (optionsNode) + .properties) { if (prop.type !== "Property" || prop.key.type !== "Identifier") { errors.push( createError( @@ -84,7 +103,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { switch (prop.key.name) { case "regExp": { const regExpExpr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (!regExpExpr.isRegExp()) { errors.push(createPropertyParseError(prop, "RegExp")); @@ -95,7 +114,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { } case "include": { const regExpExpr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (!regExpExpr.isRegExp()) { errors.push(createPropertyParseError(prop, "RegExp")); @@ -106,7 +125,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { } case "exclude": { const regExpExpr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (!regExpExpr.isRegExp()) { errors.push(createPropertyParseError(prop, "RegExp")); @@ -117,7 +136,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { } case "mode": { const modeExpr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (!modeExpr.isString()) { errors.push(createPropertyParseError(prop, "string")); @@ -130,7 +149,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { } case "chunkName": { const expr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (!expr.isString()) { errors.push(createPropertyParseError(prop, "string")); @@ -141,7 +160,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { } case "exports": { const expr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (expr.isString()) { exports = [[expr.string]]; @@ -176,7 +195,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { } case "prefetch": { const expr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (expr.isBoolean()) { groupOptions.prefetchOrder = 0; @@ -189,7 +208,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { } case "preload": { const expr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (expr.isBoolean()) { groupOptions.preloadOrder = 0; @@ -202,7 +221,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin { } case "recursive": { const recursiveExpr = parser.evaluateExpression( - /** @type {ExpressionNode} */ (prop.value) + /** @type {Expression} */ (prop.value) ); if (!recursiveExpr.isBoolean()) { errors.push(createPropertyParseError(prop, "boolean")); diff --git a/lib/dependencies/ImportParserPlugin.js b/lib/dependencies/ImportParserPlugin.js index a3aba061d36..aa10eebd5ef 100644 --- a/lib/dependencies/ImportParserPlugin.js +++ b/lib/dependencies/ImportParserPlugin.js @@ -17,6 +17,7 @@ const ImportWeakDependency = require("./ImportWeakDependency"); /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */ /** @typedef {import("../ContextModule").ContextMode} ContextMode */ +/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ class ImportParserPlugin { /** @@ -26,6 +27,10 @@ class ImportParserPlugin { this.options = options; } + /** + * @param {JavascriptParser} parser the parser + * @returns {void} + */ apply(parser) { const exportsFromEnumerable = enumerable => Array.from(enumerable, e => [e]); diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index 9878a153385..ac39c534cd2 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -17,17 +17,21 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("acorn").Options} AcornOptions */ /** @typedef {import("estree").ArrayExpression} ArrayExpressionNode */ +/** @typedef {import("estree").AssignmentExpression} AssignmentExpression */ /** @typedef {import("estree").BinaryExpression} BinaryExpressionNode */ /** @typedef {import("estree").BlockStatement} BlockStatementNode */ /** @typedef {import("estree").SequenceExpression} SequenceExpressionNode */ /** @typedef {import("estree").CallExpression} CallExpressionNode */ -/** @typedef {import("estree").ClassDeclaration} ClassDeclarationNode */ -/** @typedef {import("estree").ClassExpression} ClassExpressionNode */ +/** @typedef {import("estree").BaseCallExpression} BaseCallExpression */ +/** @typedef {import("estree").StaticBlock} StaticBlock */ +/** @typedef {import("estree").ImportExpression} ImportExpression */ +/** @typedef {import("estree").ClassDeclaration} ClassDeclaration */ +/** @typedef {import("estree").ClassExpression} ClassExpression */ /** @typedef {import("estree").Comment} CommentNode */ /** @typedef {import("estree").ConditionalExpression} ConditionalExpressionNode */ /** @typedef {import("estree").Declaration} DeclarationNode */ /** @typedef {import("estree").PrivateIdentifier} PrivateIdentifierNode */ -/** @typedef {import("estree").PropertyDefinition} PropertyDefinitionNode */ +/** @typedef {import("estree").PropertyDefinition} PropertyDefinition */ /** @typedef {import("estree").Expression} ExpressionNode */ /** @typedef {import("estree").Identifier} IdentifierNode */ /** @typedef {import("estree").IfStatement} IfStatementNode */ @@ -37,7 +41,7 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").ChainExpression} ChainExpressionNode */ /** @typedef {import("estree").MemberExpression} MemberExpressionNode */ /** @typedef {import("estree").MetaProperty} MetaPropertyNode */ -/** @typedef {import("estree").MethodDefinition} MethodDefinitionNode */ +/** @typedef {import("estree").MethodDefinition} MethodDefinition */ /** @typedef {import("estree").ModuleDeclaration} ModuleDeclarationNode */ /** @typedef {import("estree").NewExpression} NewExpressionNode */ /** @typedef {import("estree").Node} AnyNode */ @@ -237,14 +241,14 @@ class JavascriptParser extends Parser { statement: new SyncBailHook(["statement"]), /** @type {SyncBailHook<[IfStatementNode], boolean | void>} */ statementIf: new SyncBailHook(["statement"]), - /** @type {SyncBailHook<[ExpressionNode, ClassExpressionNode | ClassDeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExpressionNode, ClassExpression | ClassDeclaration], boolean | void>} */ classExtendsExpression: new SyncBailHook([ "expression", "classDefinition" ]), - /** @type {SyncBailHook<[MethodDefinitionNode | PropertyDefinitionNode, ClassExpressionNode | ClassDeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[MethodDefinition | PropertyDefinition | StaticBlock, ClassExpression | ClassDeclaration], boolean | void>} */ classBodyElement: new SyncBailHook(["element", "classDefinition"]), - /** @type {SyncBailHook<[ExpressionNode, MethodDefinitionNode | PropertyDefinitionNode, ClassExpressionNode | ClassDeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExpressionNode, MethodDefinition | PropertyDefinition, ClassExpression | ClassDeclaration], boolean | void>} */ classBodyValue: new SyncBailHook([ "expression", "element", @@ -302,19 +306,19 @@ class JavascriptParser extends Parser { canRename: new HookMap(() => new SyncBailHook(["initExpression"])), /** @type {HookMap>} */ rename: new HookMap(() => new SyncBailHook(["initExpression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ assign: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ assignMemberChain: new HookMap( () => new SyncBailHook(["expression", "members"]) ), /** @type {HookMap>} */ typeof: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {SyncBailHook<[ExpressionNode], boolean | void>} */ + /** @type {SyncBailHook<[ImportExpression], boolean | void>} */ importCall: new SyncBailHook(["expression"]), /** @type {SyncBailHook<[ExpressionNode], boolean | void>} */ topLevelAwait: new SyncBailHook(["expression"]), - /** @type {HookMap>} */ + /** @type {HookMap>} */ call: new HookMap(() => new SyncBailHook(["expression"])), /** Something like "a.b()" */ /** @type {HookMap>} */ @@ -1554,7 +1558,7 @@ class JavascriptParser extends Parser { } /** - * @param {ClassExpressionNode | ClassDeclarationNode} classy a class node + * @param {ClassExpression | ClassDeclaration} classy a class node * @returns {void} */ walkClass(classy) { @@ -2925,6 +2929,9 @@ class JavascriptParser extends Parser { this.scope.topLevelScope = wasTopLevel; } + /** + * @param {ImportExpression} expression import expression + */ walkImportExpression(expression) { let result = this.hooks.importCall.call(expression); if (result === true) return; @@ -3641,9 +3648,8 @@ class JavascriptParser extends Parser { return false; } const items = - /** @type {(MethodDefinitionNode | PropertyDefinitionNode)[]} */ ( - expr.body.body - ); + /** @type {(MethodDefinition | PropertyDefinition)[]} */ + (expr.body.body); return items.every( item => (!item.computed || diff --git a/types.d.ts b/types.d.ts index 44755b1ecd5..6b708af0b82 100644 --- a/types.d.ts +++ b/types.d.ts @@ -13,6 +13,7 @@ import { AssignmentPattern, AssignmentProperty, AwaitExpression, + BaseCallExpression, BigIntLiteral, BinaryExpression, BlockStatement, @@ -5208,7 +5209,7 @@ declare class JavascriptParser extends Parser { >; classBodyElement: SyncBailHook< [ - MethodDefinition | PropertyDefinition, + StaticBlock | MethodDefinition | PropertyDefinition, ClassExpression | ClassDeclaration ], boolean | void @@ -5279,9 +5280,9 @@ declare class JavascriptParser extends Parser { SyncBailHook<[AssignmentExpression, string[]], boolean | void> >; typeof: HookMap>; - importCall: SyncBailHook<[Expression], boolean | void>; + importCall: SyncBailHook<[ImportExpression], boolean | void>; topLevelAwait: SyncBailHook<[Expression], boolean | void>; - call: HookMap>; + call: HookMap>; callMemberChain: HookMap< SyncBailHook<[CallExpression, string[], boolean[]], boolean | void> >; @@ -5464,7 +5465,7 @@ declare class JavascriptParser extends Parser { walkTaggedTemplateExpression(expression?: any): void; walkClassExpression(expression?: any): void; walkChainExpression(expression: ChainExpression): void; - walkImportExpression(expression?: any): void; + walkImportExpression(expression: ImportExpression): void; walkCallExpression(expression?: any): void; walkMemberExpression(expression?: any): void; walkMemberExpressionWithExpressionName( From 0401bd4a2c315f86401d6b9129c70d753e43f133 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 01:58:24 +0300 Subject: [PATCH 08/12] refactor: types more --- .../CommonJsRequireContextDependency.js | 8 ++++++++ lib/dependencies/CommonJsRequireDependency.js | 7 +++++++ lib/dependencies/LocalModule.js | 16 ++++++++++++++++ lib/dependencies/LocalModulesHelpers.js | 18 ++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/lib/dependencies/CommonJsRequireContextDependency.js b/lib/dependencies/CommonJsRequireContextDependency.js index ebe731b2799..66dc594852e 100644 --- a/lib/dependencies/CommonJsRequireContextDependency.js +++ b/lib/dependencies/CommonJsRequireContextDependency.js @@ -9,10 +9,18 @@ const makeSerializable = require("../util/makeSerializable"); const ContextDependency = require("./ContextDependency"); const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class CommonJsRequireContextDependency extends ContextDependency { + /** + * @param {TODO} options options for the context module + * @param {Range} range location in source code + * @param {Range} valueRange location of the require call + * @param {boolean} inShorthand true, if the require call is in shorthand notation + * @param {string} context context + */ constructor(options, range, valueRange, inShorthand, context) { super(options, context); diff --git a/lib/dependencies/CommonJsRequireDependency.js b/lib/dependencies/CommonJsRequireDependency.js index 03d0a251a13..09545a86e5e 100644 --- a/lib/dependencies/CommonJsRequireDependency.js +++ b/lib/dependencies/CommonJsRequireDependency.js @@ -9,7 +9,14 @@ const makeSerializable = require("../util/makeSerializable"); const ModuleDependency = require("./ModuleDependency"); const ModuleDependencyTemplateAsId = require("./ModuleDependencyTemplateAsId"); +/** @typedef {import("../javascript/JavascriptParser").Range} Range */ + class CommonJsRequireDependency extends ModuleDependency { + /** + * @param {string} request request + * @param {Range=} range location in source code + * @param {string=} context request context + */ constructor(request, range, context) { super(request); this.range = range; diff --git a/lib/dependencies/LocalModule.js b/lib/dependencies/LocalModule.js index 8516594b31c..c7e23380587 100644 --- a/lib/dependencies/LocalModule.js +++ b/lib/dependencies/LocalModule.js @@ -7,7 +7,14 @@ const makeSerializable = require("../util/makeSerializable"); +/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ +/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ + class LocalModule { + /** + * @param {string} name name + * @param {number} idx index + */ constructor(name, idx) { this.name = name; this.idx = idx; @@ -18,10 +25,16 @@ class LocalModule { this.used = true; } + /** + * @returns {string} variable name + */ variableName() { return "__WEBPACK_LOCAL_MODULE_" + this.idx + "__"; } + /** + * @param {ObjectSerializerContext} context context + */ serialize(context) { const { write } = context; @@ -30,6 +43,9 @@ class LocalModule { write(this.used); } + /** + * @param {ObjectDeserializerContext} context context + */ deserialize(context) { const { read } = context; diff --git a/lib/dependencies/LocalModulesHelpers.js b/lib/dependencies/LocalModulesHelpers.js index bc2eb8a9d76..21157ebb8a9 100644 --- a/lib/dependencies/LocalModulesHelpers.js +++ b/lib/dependencies/LocalModulesHelpers.js @@ -7,6 +7,13 @@ const LocalModule = require("./LocalModule"); +/** @typedef {import("../javascript/JavascriptParser").ParserState} ParserState */ + +/** + * @param {string} parent parent module + * @param {string} mod module to resolve + * @returns {string} resolved module + */ const lookup = (parent, mod) => { if (mod.charAt(0) !== ".") return mod; @@ -26,6 +33,11 @@ const lookup = (parent, mod) => { return path.join("/"); }; +/** + * @param {ParserState} state parser state + * @param {string} name name + * @returns {LocalModule} local module + */ exports.addLocalModule = (state, name) => { if (!state.localModules) { state.localModules = []; @@ -35,6 +47,12 @@ exports.addLocalModule = (state, name) => { return m; }; +/** + * @param {ParserState} state parser state + * @param {string} name name + * @param {string} [namedModule] named module + * @returns {LocalModule | null} local module or null + */ exports.getLocalModule = (state, name, namedModule) => { if (!state.localModules) return null; if (namedModule) { From e719e60340700ae5316e9e8aa15174e0b403e038 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 03:03:05 +0300 Subject: [PATCH 09/12] refactor: types more --- lib/dependencies/AMDPlugin.js | 7 + lib/dependencies/CommonJsPlugin.js | 7 + .../HarmonyExportDependencyParserPlugin.js | 3 + lib/dependencies/HarmonyModulesPlugin.js | 7 + lib/dependencies/ImportMetaContextPlugin.js | 7 + lib/dependencies/ImportPlugin.js | 7 + lib/dependencies/RequireContextPlugin.js | 7 + lib/dependencies/RequireEnsurePlugin.js | 7 + lib/dependencies/RequireIncludePlugin.js | 7 + lib/dependencies/SystemPlugin.js | 11 +- lib/dependencies/URLPlugin.js | 12 +- lib/dependencies/WorkerPlugin.js | 7 +- lib/javascript/JavascriptParser.js | 155 ++++++++++++++++-- types.d.ts | 74 ++++----- 14 files changed, 259 insertions(+), 59 deletions(-) diff --git a/lib/dependencies/AMDPlugin.js b/lib/dependencies/AMDPlugin.js index 09e8159f321..f9178162008 100644 --- a/lib/dependencies/AMDPlugin.js +++ b/lib/dependencies/AMDPlugin.js @@ -32,8 +32,10 @@ const ConstDependency = require("./ConstDependency"); const LocalModuleDependency = require("./LocalModuleDependency"); const UnsupportedDependency = require("./UnsupportedDependency"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "AMDPlugin"; @@ -125,6 +127,11 @@ class AMDPlugin { ); }); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { if (parserOptions.amd !== undefined && !parserOptions.amd) return; diff --git a/lib/dependencies/CommonJsPlugin.js b/lib/dependencies/CommonJsPlugin.js index 34fbc710fca..64400e0b3a4 100644 --- a/lib/dependencies/CommonJsPlugin.js +++ b/lib/dependencies/CommonJsPlugin.js @@ -34,7 +34,9 @@ const { } = require("../javascript/JavascriptParserHelpers"); const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependency"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "CommonJsPlugin"; @@ -169,6 +171,11 @@ class CommonJsPlugin { ); }); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { if (parserOptions.commonjs !== undefined && !parserOptions.commonjs) return; diff --git a/lib/dependencies/HarmonyExportDependencyParserPlugin.js b/lib/dependencies/HarmonyExportDependencyParserPlugin.js index 2beefc0fe22..35fba801a32 100644 --- a/lib/dependencies/HarmonyExportDependencyParserPlugin.js +++ b/lib/dependencies/HarmonyExportDependencyParserPlugin.js @@ -21,6 +21,9 @@ const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDepe const { HarmonyStarExportsList } = HarmonyExportImportedSpecifierDependency; module.exports = class HarmonyExportDependencyParserPlugin { + /** + * @param {import("../../declarations/WebpackOptions").JavascriptParserOptions} options options + */ constructor(options) { this.exportPresenceMode = options.reexportExportsPresence !== undefined diff --git a/lib/dependencies/HarmonyModulesPlugin.js b/lib/dependencies/HarmonyModulesPlugin.js index b2914ef6f80..4b68a18cea9 100644 --- a/lib/dependencies/HarmonyModulesPlugin.js +++ b/lib/dependencies/HarmonyModulesPlugin.js @@ -25,7 +25,9 @@ const HarmonyExportDependencyParserPlugin = require("./HarmonyExportDependencyPa const HarmonyImportDependencyParserPlugin = require("./HarmonyImportDependencyParserPlugin"); const HarmonyTopLevelThisParserPlugin = require("./HarmonyTopLevelThisParserPlugin"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "HarmonyModulesPlugin"; @@ -113,6 +115,11 @@ class HarmonyModulesPlugin { new HarmonyAcceptImportDependency.Template() ); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { // TODO webpack 6: rename harmony to esm or module if (parserOptions.harmony !== undefined && !parserOptions.harmony) diff --git a/lib/dependencies/ImportMetaContextPlugin.js b/lib/dependencies/ImportMetaContextPlugin.js index 9c9f8853ff2..ed9ac05da53 100644 --- a/lib/dependencies/ImportMetaContextPlugin.js +++ b/lib/dependencies/ImportMetaContextPlugin.js @@ -13,8 +13,10 @@ const ContextElementDependency = require("./ContextElementDependency"); const ImportMetaContextDependency = require("./ImportMetaContextDependency"); const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDependencyParserPlugin"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "ImportMetaContextPlugin"; @@ -41,6 +43,11 @@ class ImportMetaContextPlugin { normalModuleFactory ); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { if ( parserOptions.importMetaContext !== undefined && diff --git a/lib/dependencies/ImportPlugin.js b/lib/dependencies/ImportPlugin.js index 8bf1e774e8a..4ee51a8f748 100644 --- a/lib/dependencies/ImportPlugin.js +++ b/lib/dependencies/ImportPlugin.js @@ -16,7 +16,9 @@ const ImportEagerDependency = require("./ImportEagerDependency"); const ImportParserPlugin = require("./ImportParserPlugin"); const ImportWeakDependency = require("./ImportWeakDependency"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "ImportPlugin"; @@ -66,6 +68,11 @@ class ImportPlugin { new ImportContextDependency.Template() ); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { if (parserOptions.import !== undefined && !parserOptions.import) return; diff --git a/lib/dependencies/RequireContextPlugin.js b/lib/dependencies/RequireContextPlugin.js index 77bb4b685e2..f556d383051 100644 --- a/lib/dependencies/RequireContextPlugin.js +++ b/lib/dependencies/RequireContextPlugin.js @@ -14,8 +14,10 @@ const ContextElementDependency = require("./ContextElementDependency"); const RequireContextDependency = require("./RequireContextDependency"); const RequireContextDependencyParserPlugin = require("./RequireContextDependencyParserPlugin"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ /** @type {ResolveOptions} */ const EMPTY_RESOLVE_OPTIONS = {}; @@ -46,6 +48,11 @@ class RequireContextPlugin { normalModuleFactory ); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { if ( parserOptions.requireContext !== undefined && diff --git a/lib/dependencies/RequireEnsurePlugin.js b/lib/dependencies/RequireEnsurePlugin.js index 7ee964523f9..5a1dc31dbdd 100644 --- a/lib/dependencies/RequireEnsurePlugin.js +++ b/lib/dependencies/RequireEnsurePlugin.js @@ -19,7 +19,9 @@ const { toConstantDependency } = require("../javascript/JavascriptParserHelpers"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "RequireEnsurePlugin"; @@ -47,6 +49,11 @@ class RequireEnsurePlugin { new RequireEnsureDependency.Template() ); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { if ( parserOptions.requireEnsure !== undefined && diff --git a/lib/dependencies/RequireIncludePlugin.js b/lib/dependencies/RequireIncludePlugin.js index dc8e55251ee..af5bd0215fd 100644 --- a/lib/dependencies/RequireIncludePlugin.js +++ b/lib/dependencies/RequireIncludePlugin.js @@ -12,7 +12,9 @@ const { const RequireIncludeDependency = require("./RequireIncludeDependency"); const RequireIncludeDependencyParserPlugin = require("./RequireIncludeDependencyParserPlugin"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "RequireIncludePlugin"; @@ -35,6 +37,11 @@ class RequireIncludePlugin { new RequireIncludeDependency.Template() ); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { if (parserOptions.requireInclude === false) return; const warn = parserOptions.requireInclude === undefined; diff --git a/lib/dependencies/SystemPlugin.js b/lib/dependencies/SystemPlugin.js index 184c812d36c..eab589cdea4 100644 --- a/lib/dependencies/SystemPlugin.js +++ b/lib/dependencies/SystemPlugin.js @@ -20,7 +20,9 @@ const makeSerializable = require("../util/makeSerializable"); const ConstDependency = require("./ConstDependency"); const SystemRuntimeModule = require("./SystemRuntimeModule"); +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "SystemPlugin"; @@ -46,6 +48,11 @@ class SystemPlugin { compilation.addRuntimeModule(chunk, new SystemRuntimeModule()); }); + /** + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} + */ const handler = (parser, parserOptions) => { if (parserOptions.system === undefined || !parserOptions.system) { return; @@ -105,7 +112,9 @@ class SystemPlugin { return parser.hooks.importCall.call({ type: "ImportExpression", - source: expr.arguments[0], + source: + /** @type {import("estree").Literal} */ + (expr.arguments[0]), loc: expr.loc, range: expr.range }); diff --git a/lib/dependencies/URLPlugin.js b/lib/dependencies/URLPlugin.js index 6f099b971fc..cfb094a0189 100644 --- a/lib/dependencies/URLPlugin.js +++ b/lib/dependencies/URLPlugin.js @@ -16,9 +16,11 @@ const InnerGraph = require("../optimize/InnerGraph"); const URLDependency = require("./URLDependency"); /** @typedef {import("estree").NewExpression} NewExpressionNode */ +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../NormalModule")} NormalModule */ /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ const PLUGIN_NAME = "URLPlugin"; @@ -43,9 +45,11 @@ class URLPlugin { const getUrl = module => { return pathToFileURL(module.resource); }; + /** - * @param {JavascriptParser} parser parser - * @param {object} parserOptions options + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} */ const parserCallback = (parser, parserOptions) => { if (parserOptions.url === false) return; @@ -77,9 +81,7 @@ class URLPlugin { ) return; - const request = parser.evaluateExpression(arg1).asString(); - - return request; + return parser.evaluateExpression(arg1).asString(); }; parser.hooks.canRename.for("URL").tap(PLUGIN_NAME, approve); diff --git a/lib/dependencies/WorkerPlugin.js b/lib/dependencies/WorkerPlugin.js index b426836f697..03979386480 100644 --- a/lib/dependencies/WorkerPlugin.js +++ b/lib/dependencies/WorkerPlugin.js @@ -30,11 +30,13 @@ const WorkerDependency = require("./WorkerDependency"); /** @typedef {import("estree").Pattern} Pattern */ /** @typedef {import("estree").Property} Property */ /** @typedef {import("estree").SpreadElement} SpreadElement */ +/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Entrypoint").EntryOptions} EntryOptions */ /** @typedef {import("../Parser").ParserState} ParserState */ /** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */ /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ +/** @typedef {import("../javascript/JavascriptParser")} Parser */ /** @typedef {import("./HarmonyImportDependencyParserPlugin").HarmonySettings} HarmonySettings */ const getUrl = module => { @@ -169,8 +171,9 @@ class WorkerPlugin { }; /** - * @param {JavascriptParser} parser the parser - * @param {object} parserOptions options + * @param {Parser} parser parser parser + * @param {JavascriptParserOptions} parserOptions parserOptions + * @returns {void} */ const parserPlugin = (parser, parserOptions) => { if (parserOptions.worker === false) return; diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index ac39c534cd2..43e82052e5f 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -26,6 +26,9 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").StaticBlock} StaticBlock */ /** @typedef {import("estree").ImportExpression} ImportExpression */ /** @typedef {import("estree").ClassDeclaration} ClassDeclaration */ +/** @typedef {import("estree").ForStatement} ForStatement */ +/** @typedef {import("estree").SwitchStatement} SwitchStatement */ +/** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */ /** @typedef {import("estree").ClassExpression} ClassExpression */ /** @typedef {import("estree").Comment} CommentNode */ /** @typedef {import("estree").ConditionalExpression} ConditionalExpressionNode */ @@ -34,22 +37,33 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").PropertyDefinition} PropertyDefinition */ /** @typedef {import("estree").Expression} ExpressionNode */ /** @typedef {import("estree").Identifier} IdentifierNode */ -/** @typedef {import("estree").IfStatement} IfStatementNode */ -/** @typedef {import("estree").LabeledStatement} LabeledStatementNode */ +/** @typedef {import("estree").VariableDeclaration} VariableDeclaration */ +/** @typedef {import("estree").IfStatement} IfStatement */ +/** @typedef {import("estree").LabeledStatement} LabeledStatement */ /** @typedef {import("estree").Literal} LiteralNode */ /** @typedef {import("estree").LogicalExpression} LogicalExpressionNode */ /** @typedef {import("estree").ChainExpression} ChainExpressionNode */ /** @typedef {import("estree").MemberExpression} MemberExpressionNode */ /** @typedef {import("estree").MetaProperty} MetaPropertyNode */ +/** @typedef {import("estree").VariableDeclarator} VariableDeclarator */ +/** @typedef {import("estree").ForInStatement} ForInStatement */ +/** @typedef {import("estree").ForOfStatement} ForOfStatement */ +/** @typedef {import("estree").ReturnStatement} ReturnStatement */ +/** @typedef {import("estree").WithStatement} WithStatement */ +/** @typedef {import("estree").ThrowStatement} ThrowStatement */ /** @typedef {import("estree").MethodDefinition} MethodDefinition */ /** @typedef {import("estree").ModuleDeclaration} ModuleDeclarationNode */ /** @typedef {import("estree").NewExpression} NewExpressionNode */ +/** @typedef {import("estree").WhileStatement} WhileStatement */ +/** @typedef {import("estree").ExpressionStatement} ExpressionStatement */ +/** @typedef {import("estree").FunctionDeclaration} FunctionDeclaration */ +/** @typedef {import("estree").DoWhileStatement} DoWhileStatement */ +/** @typedef {import("estree").TryStatement} TryStatement */ /** @typedef {import("estree").Node} AnyNode */ /** @typedef {import("estree").Program} ProgramNode */ /** @typedef {import("estree").Statement} StatementNode */ /** @typedef {import("estree").ImportDeclaration} ImportDeclarationNode */ -/** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclarationNode */ -/** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclarationNode */ +/** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclaration */ /** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclarationNode */ /** @typedef {import("estree").Super} SuperNode */ /** @typedef {import("estree").TaggedTemplateExpression} TaggedTemplateExpressionNode */ @@ -239,7 +253,7 @@ class JavascriptParser extends Parser { blockPreStatement: new SyncBailHook(["declaration"]), /** @type {SyncBailHook<[StatementNode | ModuleDeclarationNode], boolean | void>} */ statement: new SyncBailHook(["statement"]), - /** @type {SyncBailHook<[IfStatementNode], boolean | void>} */ + /** @type {SyncBailHook<[IfStatement], boolean | void>} */ statementIf: new SyncBailHook(["statement"]), /** @type {SyncBailHook<[ExpressionNode, ClassExpression | ClassDeclaration], boolean | void>} */ classExtendsExpression: new SyncBailHook([ @@ -254,7 +268,7 @@ class JavascriptParser extends Parser { "element", "classDefinition" ]), - /** @type {HookMap>} */ + /** @type {HookMap>} */ label: new HookMap(() => new SyncBailHook(["statement"])), /** @type {SyncBailHook<[ImportDeclarationNode, ImportSource], boolean | void>} */ import: new SyncBailHook(["statement", "source"]), @@ -265,22 +279,22 @@ class JavascriptParser extends Parser { "exportName", "identifierName" ]), - /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode], boolean | void>} */ export: new SyncBailHook(["statement"]), - /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode, ImportSource], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode, ImportSource], boolean | void>} */ exportImport: new SyncBailHook(["statement", "source"]), - /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode, DeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode, DeclarationNode], boolean | void>} */ exportDeclaration: new SyncBailHook(["statement", "declaration"]), - /** @type {SyncBailHook<[ExportDefaultDeclarationNode, DeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportDefaultDeclaration, DeclarationNode], boolean | void>} */ exportExpression: new SyncBailHook(["statement", "declaration"]), - /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode, string, string, number | undefined], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode, string, string, number | undefined], boolean | void>} */ exportSpecifier: new SyncBailHook([ "statement", "identifierName", "exportName", "index" ]), - /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode, ImportSource, string, string, number | undefined], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode, ImportSource, string, string, number | undefined], boolean | void>} */ exportImportSpecifier: new SyncBailHook([ "statement", "source", @@ -1801,6 +1815,9 @@ class JavascriptParser extends Parser { this.walkExpression(statement.expression); } + /** + * @param {IfStatement} statement if statement + */ preWalkIfStatement(statement) { this.preWalkStatement(statement.consequent); if (statement.alternate) { @@ -1808,6 +1825,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {IfStatement} statement if statement + */ walkIfStatement(statement) { const result = this.hooks.statementIf.call(statement); if (result === undefined) { @@ -1825,10 +1845,16 @@ class JavascriptParser extends Parser { } } + /** + * @param {LabeledStatement} statement with statement + */ preWalkLabeledStatement(statement) { this.preWalkStatement(statement.body); } + /** + * @param {LabeledStatement} statement with statement + */ walkLabeledStatement(statement) { const hook = this.hooks.label.get(statement.label.name); if (hook !== undefined) { @@ -1838,42 +1864,69 @@ class JavascriptParser extends Parser { this.walkNestedStatement(statement.body); } + /** + * @param {WithStatement} statement with statement + */ preWalkWithStatement(statement) { this.preWalkStatement(statement.body); } + /** + * @param {WithStatement} statement with statement + */ walkWithStatement(statement) { this.walkExpression(statement.object); this.walkNestedStatement(statement.body); } + /** + * @param {SwitchStatement} statement switch statement + */ preWalkSwitchStatement(statement) { this.preWalkSwitchCases(statement.cases); } + /** + * @param {SwitchStatement} statement switch statement + */ walkSwitchStatement(statement) { this.walkExpression(statement.discriminant); this.walkSwitchCases(statement.cases); } + /** + * @param {ReturnStatement | ThrowStatement} statement return or throw statement + */ walkTerminatingStatement(statement) { if (statement.argument) this.walkExpression(statement.argument); } + /** + * @param {ReturnStatement} statement return statement + */ walkReturnStatement(statement) { this.walkTerminatingStatement(statement); } + /** + * @param {ThrowStatement} statement return statement + */ walkThrowStatement(statement) { this.walkTerminatingStatement(statement); } + /** + * @param {TryStatement} statement try statement + */ preWalkTryStatement(statement) { this.preWalkStatement(statement.block); if (statement.handler) this.preWalkCatchClause(statement.handler); if (statement.finalizer) this.preWalkStatement(statement.finalizer); } + /** + * @param {TryStatement} statement try statement + */ walkTryStatement(statement) { if (this.scope.inTry) { this.walkStatement(statement.block); @@ -1886,24 +1939,39 @@ class JavascriptParser extends Parser { if (statement.finalizer) this.walkStatement(statement.finalizer); } + /** + * @param {WhileStatement} statement while statement + */ preWalkWhileStatement(statement) { this.preWalkStatement(statement.body); } + /** + * @param {WhileStatement} statement while statement + */ walkWhileStatement(statement) { this.walkExpression(statement.test); this.walkNestedStatement(statement.body); } + /** + * @param {DoWhileStatement} statement do while statement + */ preWalkDoWhileStatement(statement) { this.preWalkStatement(statement.body); } + /** + * @param {DoWhileStatement} statement do while statement + */ walkDoWhileStatement(statement) { this.walkNestedStatement(statement.body); this.walkExpression(statement.test); } + /** + * @param {ForStatement} statement for statement + */ preWalkForStatement(statement) { if (statement.init) { if (statement.init.type === "VariableDeclaration") { @@ -1913,6 +1981,9 @@ class JavascriptParser extends Parser { this.preWalkStatement(statement.body); } + /** + * @param {ForStatement} statement for statement + */ walkForStatement(statement) { this.inBlockScope(() => { if (statement.init) { @@ -1943,6 +2014,9 @@ class JavascriptParser extends Parser { }); } + /** + * @param {ForInStatement} statement for statement + */ preWalkForInStatement(statement) { if (statement.left.type === "VariableDeclaration") { this.preWalkVariableDeclaration(statement.left); @@ -1950,6 +2024,9 @@ class JavascriptParser extends Parser { this.preWalkStatement(statement.body); } + /** + * @param {ForInStatement} statement for statement + */ walkForInStatement(statement) { this.inBlockScope(() => { if (statement.left.type === "VariableDeclaration") { @@ -1982,6 +2059,9 @@ class JavascriptParser extends Parser { this.preWalkStatement(statement.body); } + /** + * @param {ForOfStatement} statement for statement + */ walkForOfStatement(statement) { this.inBlockScope(() => { if (statement.left.type === "VariableDeclaration") { @@ -2004,13 +2084,18 @@ class JavascriptParser extends Parser { }); } - // Declarations + /** + * @param {FunctionDeclaration} statement function declaration + */ preWalkFunctionDeclaration(statement) { if (statement.id) { this.defineVariable(statement.id.name); } } + /** + * @param {FunctionDeclaration} statement function declaration + */ walkFunctionDeclaration(statement) { const wasTopLevel = this.scope.topLevelScope; this.scope.topLevelScope = false; @@ -2031,6 +2116,9 @@ class JavascriptParser extends Parser { this.scope.topLevelScope = wasTopLevel; } + /** + * @param {ExpressionStatement} statement expression statement + */ blockPreWalkExpressionStatement(statement) { const expression = statement.expression; switch (expression.type) { @@ -2039,6 +2127,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {AssignmentExpression} expression assignment expression + */ preWalkAssignmentExpression(expression) { if ( expression.left.type !== "ObjectPattern" || @@ -2179,6 +2270,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {ExportNamedDeclaration} statement the statement + */ walkExportNamedDeclaration(statement) { if (statement.declaration) { this.walkStatement(statement.declaration); @@ -2246,11 +2340,17 @@ class JavascriptParser extends Parser { this.hooks.exportImportSpecifier.call(statement, source, null, name, 0); } + /** + * @param {VariableDeclaration} statement variable declaration + */ preWalkVariableDeclaration(statement) { if (statement.kind !== "var") return; this._preWalkVariableDeclaration(statement, this.hooks.varDeclarationVar); } + /** + * @param {VariableDeclaration} statement variable declaration + */ blockPreWalkVariableDeclaration(statement) { if (statement.kind === "var") return; const hookMap = @@ -2260,6 +2360,10 @@ class JavascriptParser extends Parser { this._preWalkVariableDeclaration(statement, hookMap); } + /** + * @param {VariableDeclaration} statement variable declaration + * @param {TODO} hookMap map of hooks + */ _preWalkVariableDeclaration(statement, hookMap) { for (const declarator of statement.declarations) { switch (declarator.type) { @@ -2306,6 +2410,9 @@ class JavascriptParser extends Parser { return ids; } + /** + * @param {VariableDeclarator} declarator variable declarator + */ preWalkVariableDeclarator(declarator) { if ( !declarator.init || @@ -3454,6 +3561,10 @@ class JavascriptParser extends Parser { .setExpression(expression); } + /** + * @param {ExpressionNode} expression expression + * @returns {string} parsed string + */ parseString(expression) { switch (expression.type) { case "BinaryExpression": @@ -3712,6 +3823,10 @@ class JavascriptParser extends Parser { return !evaluated.couldHaveSideEffects(); } + /** + * @param {Range} range range + * @returns {TODO[]} comments in the range + */ getComments(range) { const [rangeStart, rangeEnd] = range; const compare = (comment, needle) => comment.range[0] - needle; @@ -3799,6 +3914,9 @@ class JavascriptParser extends Parser { this.scope.definitions.set(name, newInfo); } + /** + * @param {string} name variable name + */ defineVariable(name) { const oldInfo = this.scope.definitions.get(name); // Don't redefine variable in same scope to keep existing tags @@ -3807,10 +3925,17 @@ class JavascriptParser extends Parser { this.scope.definitions.set(name, this.scope); } + /** + * @param {string} name variable name + */ undefineVariable(name) { this.scope.definitions.delete(name); } + /** + * @param {string} name variable name + * @returns {boolean} true, when variable is defined + */ isVariableDefined(name) { const info = this.scope.definitions.get(name); if (info === undefined) return false; @@ -3857,6 +3982,10 @@ class JavascriptParser extends Parser { return new VariableInfo(this.scope, undefined, tagInfo); } + /** + * @param {Range} range range of the comment + * @returns {TODO} TODO + */ parseCommentOptions(range) { const comments = this.getComments(range); if (comments.length === 0) { diff --git a/types.d.ts b/types.d.ts index 6b708af0b82..b97293bd69d 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5393,43 +5393,43 @@ declare class JavascriptParser extends Parser { preWalkBlockStatement(statement?: any): void; walkBlockStatement(statement?: any): void; walkExpressionStatement(statement?: any): void; - preWalkIfStatement(statement?: any): void; - walkIfStatement(statement?: any): void; - preWalkLabeledStatement(statement?: any): void; - walkLabeledStatement(statement?: any): void; - preWalkWithStatement(statement?: any): void; - walkWithStatement(statement?: any): void; - preWalkSwitchStatement(statement?: any): void; - walkSwitchStatement(statement?: any): void; - walkTerminatingStatement(statement?: any): void; - walkReturnStatement(statement?: any): void; - walkThrowStatement(statement?: any): void; - preWalkTryStatement(statement?: any): void; - walkTryStatement(statement?: any): void; - preWalkWhileStatement(statement?: any): void; - walkWhileStatement(statement?: any): void; - preWalkDoWhileStatement(statement?: any): void; - walkDoWhileStatement(statement?: any): void; - preWalkForStatement(statement?: any): void; - walkForStatement(statement?: any): void; - preWalkForInStatement(statement?: any): void; - walkForInStatement(statement?: any): void; + preWalkIfStatement(statement: IfStatement): void; + walkIfStatement(statement: IfStatement): void; + preWalkLabeledStatement(statement: LabeledStatement): void; + walkLabeledStatement(statement: LabeledStatement): void; + preWalkWithStatement(statement: WithStatement): void; + walkWithStatement(statement: WithStatement): void; + preWalkSwitchStatement(statement: SwitchStatement): void; + walkSwitchStatement(statement: SwitchStatement): void; + walkTerminatingStatement(statement: ReturnStatement | ThrowStatement): void; + walkReturnStatement(statement: ReturnStatement): void; + walkThrowStatement(statement: ThrowStatement): void; + preWalkTryStatement(statement: TryStatement): void; + walkTryStatement(statement: TryStatement): void; + preWalkWhileStatement(statement: WhileStatement): void; + walkWhileStatement(statement: WhileStatement): void; + preWalkDoWhileStatement(statement: DoWhileStatement): void; + walkDoWhileStatement(statement: DoWhileStatement): void; + preWalkForStatement(statement: ForStatement): void; + walkForStatement(statement: ForStatement): void; + preWalkForInStatement(statement: ForInStatement): void; + walkForInStatement(statement: ForInStatement): void; preWalkForOfStatement(statement?: any): void; - walkForOfStatement(statement?: any): void; - preWalkFunctionDeclaration(statement?: any): void; - walkFunctionDeclaration(statement?: any): void; - blockPreWalkExpressionStatement(statement?: any): void; - preWalkAssignmentExpression(expression?: any): void; + walkForOfStatement(statement: ForOfStatement): void; + preWalkFunctionDeclaration(statement: FunctionDeclaration): void; + walkFunctionDeclaration(statement: FunctionDeclaration): void; + blockPreWalkExpressionStatement(statement: ExpressionStatement): void; + preWalkAssignmentExpression(expression: AssignmentExpression): void; blockPreWalkImportDeclaration(statement?: any): void; enterDeclaration(declaration?: any, onIdent?: any): void; blockPreWalkExportNamedDeclaration(statement?: any): void; - walkExportNamedDeclaration(statement?: any): void; + walkExportNamedDeclaration(statement: ExportNamedDeclaration): void; blockPreWalkExportDefaultDeclaration(statement?: any): void; walkExportDefaultDeclaration(statement?: any): void; blockPreWalkExportAllDeclaration(statement?: any): void; - preWalkVariableDeclaration(statement?: any): void; - blockPreWalkVariableDeclaration(statement?: any): void; - preWalkVariableDeclarator(declarator?: any): void; + preWalkVariableDeclaration(statement: VariableDeclaration): void; + blockPreWalkVariableDeclaration(statement: VariableDeclaration): void; + preWalkVariableDeclarator(declarator: VariableDeclarator): void; walkVariableDeclaration(statement?: any): void; blockPreWalkClassDeclaration(statement?: any): void; walkClassDeclaration(statement?: any): void; @@ -5526,7 +5526,7 @@ declare class JavascriptParser extends Parser { enterRestElement(pattern?: any, onIdent?: any): void; enterAssignmentPattern(pattern?: any, onIdent?: any): void; evaluateExpression(expression: Expression): BasicEvaluatedExpression; - parseString(expression?: any): any; + parseString(expression: Expression): string; parseCalculatedString(expression?: any): any; evaluate(source: string): BasicEvaluatedExpression; isPure( @@ -5566,21 +5566,19 @@ declare class JavascriptParser extends Parser { | PrivateIdentifier, commentsStartPos: number ): boolean; - getComments(range?: any): any[]; + getComments(range: [number, number]): any[]; isAsiPosition(pos: number): boolean; unsetAsiPosition(pos: number): void; isStatementLevelExpression(expr?: any): boolean; getTagData(name?: any, tag?: any): any; tagVariable(name?: any, tag?: any, data?: any): void; - defineVariable(name?: any): void; - undefineVariable(name?: any): void; - isVariableDefined(name?: any): boolean; + defineVariable(name: string): void; + undefineVariable(name: string): void; + isVariableDefined(name: string): boolean; getVariableInfo(name: string): ExportedVariableInfo; setVariable(name: string, variableInfo: ExportedVariableInfo): void; evaluatedVariable(tagInfo?: any): VariableInfo; - parseCommentOptions( - range?: any - ): { options: null; errors: null } | { options: object; errors: unknown[] }; + parseCommentOptions(range: [number, number]): any; extractMemberExpressionChain(expression: MemberExpression): { members: string[]; object: From 30390058eff62a6ece1d1cbd1761f4882e687cea Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 03:46:58 +0300 Subject: [PATCH 10/12] refactor: types more --- lib/javascript/JavascriptParser.js | 189 ++++++++++++++++++++++------- types.d.ts | 80 ++++++------ 2 files changed, 189 insertions(+), 80 deletions(-) diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index 43e82052e5f..d60f6fe3ca6 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -16,11 +16,10 @@ const memoize = require("../util/memoize"); const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("acorn").Options} AcornOptions */ -/** @typedef {import("estree").ArrayExpression} ArrayExpressionNode */ /** @typedef {import("estree").AssignmentExpression} AssignmentExpression */ -/** @typedef {import("estree").BinaryExpression} BinaryExpressionNode */ +/** @typedef {import("estree").BinaryExpression} BinaryExpression */ /** @typedef {import("estree").BlockStatement} BlockStatementNode */ -/** @typedef {import("estree").SequenceExpression} SequenceExpressionNode */ +/** @typedef {import("estree").SequenceExpression} SequenceExpression */ /** @typedef {import("estree").CallExpression} CallExpressionNode */ /** @typedef {import("estree").BaseCallExpression} BaseCallExpression */ /** @typedef {import("estree").StaticBlock} StaticBlock */ @@ -31,20 +30,34 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */ /** @typedef {import("estree").ClassExpression} ClassExpression */ /** @typedef {import("estree").Comment} CommentNode */ -/** @typedef {import("estree").ConditionalExpression} ConditionalExpressionNode */ +/** @typedef {import("estree").ConditionalExpression} ConditionalExpression */ /** @typedef {import("estree").Declaration} DeclarationNode */ -/** @typedef {import("estree").PrivateIdentifier} PrivateIdentifierNode */ +/** @typedef {import("estree").PrivateIdentifier} PrivateIdentifier */ /** @typedef {import("estree").PropertyDefinition} PropertyDefinition */ /** @typedef {import("estree").Expression} ExpressionNode */ -/** @typedef {import("estree").Identifier} IdentifierNode */ +/** @typedef {import("estree").Identifier} Identifier */ /** @typedef {import("estree").VariableDeclaration} VariableDeclaration */ /** @typedef {import("estree").IfStatement} IfStatement */ /** @typedef {import("estree").LabeledStatement} LabeledStatement */ /** @typedef {import("estree").Literal} LiteralNode */ -/** @typedef {import("estree").LogicalExpression} LogicalExpressionNode */ +/** @typedef {import("estree").LogicalExpression} LogicalExpression */ /** @typedef {import("estree").ChainExpression} ChainExpressionNode */ /** @typedef {import("estree").MemberExpression} MemberExpressionNode */ /** @typedef {import("estree").MetaProperty} MetaPropertyNode */ +/** @typedef {import("estree").Property} Property */ +/** @typedef {import("estree").AssignmentPattern} AssignmentPattern */ +/** @typedef {import("estree").UpdateExpression} UpdateExpression */ +/** @typedef {import("estree").ObjectExpression} ObjectExpression */ +/** @typedef {import("estree").UnaryExpression} UnaryExpression */ +/** @typedef {import("estree").ArrayExpression} ArrayExpression */ +/** @typedef {import("estree").BlockStatement} BlockStatement */ +/** @typedef {import("estree").ArrayPattern} ArrayPattern */ +/** @typedef {import("estree").AwaitExpression} AwaitExpression */ +/** @typedef {import("estree").ThisExpression} ThisExpression */ +/** @typedef {import("estree").RestElement} RestElement */ +/** @typedef {import("estree").ObjectPattern} ObjectPattern */ +/** @typedef {import("estree").SwitchCase} SwitchCase */ +/** @typedef {import("estree").CatchClause} CatchClause */ /** @typedef {import("estree").VariableDeclarator} VariableDeclarator */ /** @typedef {import("estree").ForInStatement} ForInStatement */ /** @typedef {import("estree").ForOfStatement} ForOfStatement */ @@ -52,8 +65,9 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").WithStatement} WithStatement */ /** @typedef {import("estree").ThrowStatement} ThrowStatement */ /** @typedef {import("estree").MethodDefinition} MethodDefinition */ -/** @typedef {import("estree").ModuleDeclaration} ModuleDeclarationNode */ -/** @typedef {import("estree").NewExpression} NewExpressionNode */ +/** @typedef {import("estree").ModuleDeclaration} ModuleDeclaration */ +/** @typedef {import("estree").NewExpression} NewExpression */ +/** @typedef {import("estree").SpreadElement} SpreadElement */ /** @typedef {import("estree").WhileStatement} WhileStatement */ /** @typedef {import("estree").ExpressionStatement} ExpressionStatement */ /** @typedef {import("estree").FunctionDeclaration} FunctionDeclaration */ @@ -62,12 +76,12 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").Node} AnyNode */ /** @typedef {import("estree").Program} ProgramNode */ /** @typedef {import("estree").Statement} StatementNode */ -/** @typedef {import("estree").ImportDeclaration} ImportDeclarationNode */ +/** @typedef {import("estree").ImportDeclaration} ImportDeclaration */ /** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclaration */ /** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclarationNode */ /** @typedef {import("estree").Super} SuperNode */ -/** @typedef {import("estree").TaggedTemplateExpression} TaggedTemplateExpressionNode */ -/** @typedef {import("estree").TemplateLiteral} TemplateLiteralNode */ +/** @typedef {import("estree").TaggedTemplateExpression} TaggedTemplateExpression */ +/** @typedef {import("estree").TemplateLiteral} TemplateLiteral */ /** @typedef {import("estree").ThisExpression} ThisExpressionNode */ /** @typedef {import("estree").UnaryExpression} UnaryExpressionNode */ /** @typedef {import("estree").VariableDeclarator} VariableDeclaratorNode */ @@ -115,7 +129,7 @@ class VariableInfo { * @typedef {Object} ScopeInfo * @property {StackedMap} definitions * @property {boolean | "arrow"} topLevelScope - * @property {boolean} inShorthand + * @property {boolean | string} inShorthand * @property {boolean} isStrict * @property {boolean} isAsmJs * @property {boolean} inTry @@ -224,13 +238,13 @@ class JavascriptParser extends Parser { evaluateTypeof: new HookMap(() => new SyncBailHook(["expression"])), /** @type {HookMap>} */ evaluate: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluateIdentifier: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluateDefinedIdentifier: new HookMap( () => new SyncBailHook(["expression"]) ), - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluateNewExpression: new HookMap( () => new SyncBailHook(["expression"]) ), @@ -242,16 +256,16 @@ class JavascriptParser extends Parser { evaluateCallExpressionMember: new HookMap( () => new SyncBailHook(["expression", "param"]) ), - /** @type {HookMap>} */ + /** @type {HookMap>} */ isPure: new HookMap( () => new SyncBailHook(["expression", "commentsStartPosition"]) ), - /** @type {SyncBailHook<[StatementNode | ModuleDeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[StatementNode | ModuleDeclaration], boolean | void>} */ preStatement: new SyncBailHook(["statement"]), - /** @type {SyncBailHook<[StatementNode | ModuleDeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[StatementNode | ModuleDeclaration], boolean | void>} */ blockPreStatement: new SyncBailHook(["declaration"]), - /** @type {SyncBailHook<[StatementNode | ModuleDeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[StatementNode | ModuleDeclaration], boolean | void>} */ statement: new SyncBailHook(["statement"]), /** @type {SyncBailHook<[IfStatement], boolean | void>} */ statementIf: new SyncBailHook(["statement"]), @@ -270,9 +284,9 @@ class JavascriptParser extends Parser { ]), /** @type {HookMap>} */ label: new HookMap(() => new SyncBailHook(["statement"])), - /** @type {SyncBailHook<[ImportDeclarationNode, ImportSource], boolean | void>} */ + /** @type {SyncBailHook<[ImportDeclaration, ImportSource], boolean | void>} */ import: new SyncBailHook(["statement", "source"]), - /** @type {SyncBailHook<[ImportDeclarationNode, ImportSource, string, string], boolean | void>} */ + /** @type {SyncBailHook<[ImportDeclaration, ImportSource, string, string], boolean | void>} */ importSpecifier: new SyncBailHook([ "statement", "source", @@ -314,7 +328,7 @@ class JavascriptParser extends Parser { varDeclarationConst: new HookMap(() => new SyncBailHook(["declaration"])), /** @type {HookMap>} */ varDeclarationVar: new HookMap(() => new SyncBailHook(["declaration"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ pattern: new HookMap(() => new SyncBailHook(["pattern"])), /** @type {HookMap>} */ canRename: new HookMap(() => new SyncBailHook(["initExpression"])), @@ -363,9 +377,9 @@ class JavascriptParser extends Parser { ), /** @type {SyncBailHook<[ChainExpressionNode], boolean | void>} */ optionalChaining: new SyncBailHook(["optionalChaining"]), - /** @type {HookMap>} */ + /** @type {HookMap>} */ new: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {SyncBailHook<[BinaryExpressionNode], boolean | void>} */ + /** @type {SyncBailHook<[BinaryExpression], boolean | void>} */ binaryExpression: new SyncBailHook(["binaryExpression"]), /** @type {HookMap>} */ expression: new HookMap(() => new SyncBailHook(["expression"])), @@ -434,7 +448,7 @@ class JavascriptParser extends Parser { } }); this.hooks.evaluate.for("NewExpression").tap("JavascriptParser", _expr => { - const expr = /** @type {NewExpressionNode} */ (_expr); + const expr = /** @type {NewExpression} */ (_expr); const callee = expr.callee; if (callee.type !== "Identifier") return; if (callee.name !== "RegExp") { @@ -495,7 +509,7 @@ class JavascriptParser extends Parser { this.hooks.evaluate .for("LogicalExpression") .tap("JavascriptParser", _expr => { - const expr = /** @type {LogicalExpressionNode} */ (_expr); + const expr = /** @type {LogicalExpression} */ (_expr); const left = this.evaluateExpression(expr.left); let returnRight = false; @@ -554,13 +568,13 @@ class JavascriptParser extends Parser { * import("./" + foo + bar); // webpack will auto evaluate this into import("./foobar") * ``` * @param {boolean | number | BigInt | string} value the value to convert to an expression - * @param {BinaryExpressionNode | UnaryExpressionNode} expr the expression being evaluated + * @param {BinaryExpression | UnaryExpressionNode} expr the expression being evaluated * @param {boolean} sideEffects whether the expression has side effects * @returns {BasicEvaluatedExpression} the evaluated expression * @example * * ```js - * const binaryExpr = new BinaryExpressionNode("+", + * const binaryExpr = new BinaryExpression("+", * { type: "Literal", value: 2 }, * { type: "Literal", value: 3 } * ); @@ -605,7 +619,7 @@ class JavascriptParser extends Parser { this.hooks.evaluate .for("BinaryExpression") .tap("JavascriptParser", _expr => { - const expr = /** @type {BinaryExpressionNode} */ (_expr); + const expr = /** @type {BinaryExpression} */ (_expr); /** * Evaluates a binary expression if and only if it is a const operation (e.g. 1 + 2, "a" + "b", etc.). @@ -1086,7 +1100,7 @@ class JavascriptParser extends Parser { .setRange(expr.range); }); this.hooks.evaluate.for("Identifier").tap("JavascriptParser", expr => { - if (/** @type {IdentifierNode} */ (expr).name === "undefined") { + if (/** @type {Identifier} */ (expr).name === "undefined") { return new BasicEvaluatedExpression() .setUndefined() .setRange(expr.range); @@ -1145,9 +1159,7 @@ class JavascriptParser extends Parser { }); }; tapEvaluateWithVariableInfo("Identifier", expr => { - const info = this.getVariableInfo( - /** @type {IdentifierNode} */ (expr).name - ); + const info = this.getVariableInfo(/** @type {Identifier} */ (expr).name); if ( typeof info === "string" || (info instanceof VariableInfo && typeof info.freeName === "string") @@ -1297,7 +1309,7 @@ class JavascriptParser extends Parser { /** * @param {"cooked" | "raw"} kind kind of values to get - * @param {TemplateLiteralNode} templateLiteralExpr TemplateLiteral expr + * @param {TemplateLiteral} templateLiteralExpr TemplateLiteral expr * @returns {{quasis: BasicEvaluatedExpression[], parts: BasicEvaluatedExpression[]}} Simplified template */ const getSimplifiedTemplateResult = (kind, templateLiteralExpr) => { @@ -1348,7 +1360,7 @@ class JavascriptParser extends Parser { this.hooks.evaluate .for("TemplateLiteral") .tap("JavascriptParser", _node => { - const node = /** @type {TemplateLiteralNode} */ (_node); + const node = /** @type {TemplateLiteral} */ (_node); const { quasis, parts } = getSimplifiedTemplateResult("cooked", node); if (parts.length === 1) { @@ -1361,7 +1373,7 @@ class JavascriptParser extends Parser { this.hooks.evaluate .for("TaggedTemplateExpression") .tap("JavascriptParser", _node => { - const node = /** @type {TaggedTemplateExpressionNode} */ (_node); + const node = /** @type {TaggedTemplateExpression} */ (_node); const tag = this.evaluateExpression(node.tag); if (tag.isIdentifier() && tag.identifier === "String.raw") { @@ -1466,7 +1478,7 @@ class JavascriptParser extends Parser { this.hooks.evaluate .for("ConditionalExpression") .tap("JavascriptParser", _expr => { - const expr = /** @type {ConditionalExpressionNode} */ (_expr); + const expr = /** @type {ConditionalExpression} */ (_expr); const condition = this.evaluateExpression(expr.test); const conditionValue = condition.asBool(); @@ -1497,7 +1509,7 @@ class JavascriptParser extends Parser { this.hooks.evaluate .for("ArrayExpression") .tap("JavascriptParser", _expr => { - const expr = /** @type {ArrayExpressionNode} */ (_expr); + const expr = /** @type {ArrayExpression} */ (_expr); const items = expr.elements.map(element => { return ( @@ -1564,6 +1576,10 @@ class JavascriptParser extends Parser { return this.destructuringAssignmentProperties.get(node); } + /** + * @param {ExpressionNode} expr expression + * @returns {string | VariableInfoInterface | undefined} identifier + */ getRenameIdentifier(expr) { const result = this.evaluateExpression(expr); if (result.isIdentifier()) { @@ -1797,10 +1813,16 @@ class JavascriptParser extends Parser { } // Real Statements + /** + * @param {BlockStatement} statement block statement + */ preWalkBlockStatement(statement) { this.preWalkStatements(statement.body); } + /** + * @param {BlockStatement} statement block statement + */ walkBlockStatement(statement) { this.inBlockScope(() => { const body = statement.body; @@ -1811,6 +1833,9 @@ class JavascriptParser extends Parser { }); } + /** + * @param {ExpressionStatement} statement expression statement + */ walkExpressionStatement(statement) { this.walkExpression(statement.expression); } @@ -2435,6 +2460,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {VariableDeclaration} statement variable declaration + */ walkVariableDeclaration(statement) { for (const declarator of statement.declarations) { switch (declarator.type) { @@ -2462,16 +2490,25 @@ class JavascriptParser extends Parser { } } + /** + * @param {ClassDeclaration} statement class declaration + */ blockPreWalkClassDeclaration(statement) { if (statement.id) { this.defineVariable(statement.id.name); } } + /** + * @param {ClassDeclaration} statement class declaration + */ walkClassDeclaration(statement) { this.walkClass(statement); } + /** + * @param {SwitchCase[]} switchCases switch statement + */ preWalkSwitchCases(switchCases) { for (let index = 0, len = switchCases.length; index < len; index++) { const switchCase = switchCases[index]; @@ -2479,6 +2516,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {SwitchCase[]} switchCases switch statement + */ walkSwitchCases(switchCases) { this.inBlockScope(() => { const len = switchCases.length; @@ -2514,10 +2554,16 @@ class JavascriptParser extends Parser { }); } + /** + * @param {CatchClause} catchClause catch clause + */ preWalkCatchClause(catchClause) { this.preWalkStatement(catchClause.body); } + /** + * @param {CatchClause} catchClause catch clause + */ walkCatchClause(catchClause) { this.inBlockScope(() => { // Error binding is optional in catch clause since ECMAScript 2019 @@ -2554,6 +2600,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {AssignmentPattern} pattern assignment pattern + */ walkAssignmentPattern(pattern) { this.walkExpression(pattern.right); this.walkPattern(pattern.left); @@ -2569,6 +2618,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {ArrayPattern} pattern array pattern + */ walkArrayPattern(pattern) { for (let i = 0, len = pattern.elements.length; i < len; i++) { const element = pattern.elements[i]; @@ -2576,6 +2628,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {RestElement} pattern rest element + */ walkRestElement(pattern) { this.walkPattern(pattern.argument); } @@ -2668,12 +2723,18 @@ class JavascriptParser extends Parser { } } + /** + * @param {AwaitExpression} expression await expression + */ walkAwaitExpression(expression) { if (this.scope.topLevelScope === true) this.hooks.topLevelAwait.call(expression); this.walkExpression(expression.argument); } + /** + * @param {ArrayExpression} expression array expression + */ walkArrayExpression(expression) { if (expression.elements) { this.walkExpressions(expression.elements); @@ -2686,6 +2747,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {ObjectExpression} expression object expression + */ walkObjectExpression(expression) { for ( let propIndex = 0, len = expression.properties.length; @@ -2697,6 +2761,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {Property | SpreadElement} prop property or spread element + */ walkProperty(prop) { if (prop.type === "SpreadElement") { this.walkExpression(prop.argument); @@ -2762,7 +2829,7 @@ class JavascriptParser extends Parser { } /** - * @param {SequenceExpressionNode} expression the sequence + * @param {SequenceExpression} expression the sequence */ walkSequenceExpression(expression) { if (!expression.expressions) return; @@ -2786,10 +2853,16 @@ class JavascriptParser extends Parser { } } + /** + * @param {UpdateExpression} expression the update expression + */ walkUpdateExpression(expression) { this.walkExpression(expression.argument); } + /** + * @param {UnaryExpression} expression the unary expression + */ walkUnaryExpression(expression) { if (expression.operator === "typeof") { const result = this.callHooksForExpression( @@ -2815,12 +2888,18 @@ class JavascriptParser extends Parser { this.walkExpression(expression.right); } + /** + * @param {BinaryExpression} expression the binary expression + */ walkBinaryExpression(expression) { if (this.hooks.binaryExpression.call(expression) === undefined) { this.walkLeftRightExpression(expression); } } + /** + * @param {LogicalExpression} expression the logical expression + */ walkLogicalExpression(expression) { const result = this.hooks.expressionLogicalOperator.call(expression); if (result === undefined) { @@ -2832,6 +2911,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {AssignmentExpression} expression assignment expression + */ walkAssignmentExpression(expression) { if (expression.left.type === "Identifier") { const renameIdentifier = this.getRenameIdentifier(expression.right); @@ -2902,6 +2984,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {ConditionalExpression} expression conditional expression + */ walkConditionalExpression(expression) { const result = this.hooks.expressionConditionalOperator.call(expression); if (result === undefined) { @@ -2919,6 +3004,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {NewExpression} expression new expression + */ walkNewExpression(expression) { const result = this.callHooksForExpression( this.hooks.new, @@ -2944,6 +3032,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {TaggedTemplateExpression} expression tagged template expression + */ walkTaggedTemplateExpression(expression) { if (expression.tag) { this.walkExpression(expression.tag); @@ -2953,6 +3044,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {ClassExpression} expression the class expression + */ walkClassExpression(expression) { this.walkClass(expression); } @@ -3123,6 +3217,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {MemberExpressionNode} expression member expression + */ walkMemberExpression(expression) { const exprInfo = this.getMemberExpressionInfo( expression, @@ -3216,10 +3313,16 @@ class JavascriptParser extends Parser { if (expression.computed === true) this.walkExpression(expression.property); } + /** + * @param {ThisExpression} expression this expression + */ walkThisExpression(expression) { this.callHooksForName(this.hooks.expression, "this", expression); } + /** + * @param {Identifier} expression identifier + */ walkIdentifier(expression) { this.callHooksForName(this.hooks.expression, expression.name, expression); } @@ -3741,7 +3844,7 @@ class JavascriptParser extends Parser { } /** - * @param {ExpressionNode | DeclarationNode | PrivateIdentifierNode | null | undefined} expr an expression + * @param {ExpressionNode | DeclarationNode | PrivateIdentifier | null | undefined} expr an expression * @param {number} commentsStartPos source position from which annotation comments are checked * @returns {boolean} true, when the expression is pure */ @@ -3978,6 +4081,10 @@ class JavascriptParser extends Parser { } } + /** + * @param {TagInfo} tagInfo tag info + * @returns {VariableInfo} variable info + */ evaluatedVariable(tagInfo) { return new VariableInfo(this.scope, undefined, tagInfo); } diff --git a/types.d.ts b/types.d.ts index b97293bd69d..e621cd81dea 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5375,7 +5375,9 @@ declare class JavascriptParser extends Parser { destructuringAssignmentPropertiesFor( node: Expression ): undefined | Set; - getRenameIdentifier(expr?: any): undefined | string | VariableInfoInterface; + getRenameIdentifier( + expr: Expression + ): undefined | string | VariableInfoInterface; walkClass(classy: ClassExpression | ClassDeclaration): void; preWalkStatements(statements?: any): void; blockPreWalkStatements(statements?: any): void; @@ -5390,9 +5392,9 @@ declare class JavascriptParser extends Parser { * This enforces the nested statement to never be in ASI position. */ walkNestedStatement(statement: Statement): void; - preWalkBlockStatement(statement?: any): void; - walkBlockStatement(statement?: any): void; - walkExpressionStatement(statement?: any): void; + preWalkBlockStatement(statement: BlockStatement): void; + walkBlockStatement(statement: BlockStatement): void; + walkExpressionStatement(statement: ExpressionStatement): void; preWalkIfStatement(statement: IfStatement): void; walkIfStatement(statement: IfStatement): void; preWalkLabeledStatement(statement: LabeledStatement): void; @@ -5430,44 +5432,44 @@ declare class JavascriptParser extends Parser { preWalkVariableDeclaration(statement: VariableDeclaration): void; blockPreWalkVariableDeclaration(statement: VariableDeclaration): void; preWalkVariableDeclarator(declarator: VariableDeclarator): void; - walkVariableDeclaration(statement?: any): void; - blockPreWalkClassDeclaration(statement?: any): void; - walkClassDeclaration(statement?: any): void; - preWalkSwitchCases(switchCases?: any): void; - walkSwitchCases(switchCases?: any): void; - preWalkCatchClause(catchClause?: any): void; - walkCatchClause(catchClause?: any): void; + walkVariableDeclaration(statement: VariableDeclaration): void; + blockPreWalkClassDeclaration(statement: ClassDeclaration): void; + walkClassDeclaration(statement: ClassDeclaration): void; + preWalkSwitchCases(switchCases: SwitchCase[]): void; + walkSwitchCases(switchCases: SwitchCase[]): void; + preWalkCatchClause(catchClause: CatchClause): void; + walkCatchClause(catchClause: CatchClause): void; walkPattern(pattern?: any): void; - walkAssignmentPattern(pattern?: any): void; + walkAssignmentPattern(pattern: AssignmentPattern): void; walkObjectPattern(pattern?: any): void; - walkArrayPattern(pattern?: any): void; - walkRestElement(pattern?: any): void; + walkArrayPattern(pattern: ArrayPattern): void; + walkRestElement(pattern: RestElement): void; walkExpressions(expressions?: any): void; walkExpression(expression?: any): void; - walkAwaitExpression(expression?: any): void; - walkArrayExpression(expression?: any): void; + walkAwaitExpression(expression: AwaitExpression): void; + walkArrayExpression(expression: ArrayExpression): void; walkSpreadElement(expression?: any): void; - walkObjectExpression(expression?: any): void; - walkProperty(prop?: any): void; + walkObjectExpression(expression: ObjectExpression): void; + walkProperty(prop: Property | SpreadElement): void; walkFunctionExpression(expression?: any): void; walkArrowFunctionExpression(expression?: any): void; walkSequenceExpression(expression: SequenceExpression): void; - walkUpdateExpression(expression?: any): void; - walkUnaryExpression(expression?: any): void; + walkUpdateExpression(expression: UpdateExpression): void; + walkUnaryExpression(expression: UnaryExpression): void; walkLeftRightExpression(expression?: any): void; - walkBinaryExpression(expression?: any): void; - walkLogicalExpression(expression?: any): void; - walkAssignmentExpression(expression?: any): void; - walkConditionalExpression(expression?: any): void; - walkNewExpression(expression?: any): void; + walkBinaryExpression(expression: BinaryExpression): void; + walkLogicalExpression(expression: LogicalExpression): void; + walkAssignmentExpression(expression: AssignmentExpression): void; + walkConditionalExpression(expression: ConditionalExpression): void; + walkNewExpression(expression: NewExpression): void; walkYieldExpression(expression?: any): void; walkTemplateLiteral(expression?: any): void; - walkTaggedTemplateExpression(expression?: any): void; - walkClassExpression(expression?: any): void; + walkTaggedTemplateExpression(expression: TaggedTemplateExpression): void; + walkClassExpression(expression: ClassExpression): void; walkChainExpression(expression: ChainExpression): void; walkImportExpression(expression: ImportExpression): void; walkCallExpression(expression?: any): void; - walkMemberExpression(expression?: any): void; + walkMemberExpression(expression: MemberExpression): void; walkMemberExpressionWithExpressionName( expression?: any, name?: any, @@ -5475,8 +5477,8 @@ declare class JavascriptParser extends Parser { members?: any, onUnhandled?: any ): void; - walkThisExpression(expression?: any): void; - walkIdentifier(expression?: any): void; + walkThisExpression(expression: ThisExpression): void; + walkIdentifier(expression: Identifier): void; walkMetaProperty(metaProperty: MetaProperty): void; callHooksForExpression(hookMap: any, expr: any, ...args: any[]): any; callHooksForExpressionWithFallback( @@ -5577,7 +5579,7 @@ declare class JavascriptParser extends Parser { isVariableDefined(name: string): boolean; getVariableInfo(name: string): ExportedVariableInfo; setVariable(name: string, variableInfo: ExportedVariableInfo): void; - evaluatedVariable(tagInfo?: any): VariableInfo; + evaluatedVariable(tagInfo: TagInfo): VariableInfo; parseCommentOptions(range: [number, number]): any; extractMemberExpressionChain(expression: MemberExpression): { members: string[]; @@ -7761,21 +7763,21 @@ type NodeEstreeIndex = | PropertyDefinition | VariableDeclarator | Program - | AssignmentProperty - | Property + | SwitchCase | CatchClause + | AssignmentPattern + | ArrayPattern + | RestElement + | Property + | SpreadElement + | AssignmentProperty | ClassBody | ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier | ObjectPattern - | ArrayPattern - | RestElement - | AssignmentPattern - | SpreadElement | Super - | SwitchCase | TemplateElement; /** @@ -11079,7 +11081,7 @@ declare interface RuntimeValueOptions { declare interface ScopeInfo { definitions: StackedMap; topLevelScope: boolean | "arrow"; - inShorthand: boolean; + inShorthand: string | boolean; isStrict: boolean; isAsmJs: boolean; inTry: boolean; From 8e63b43b5e74f062a3fef18f6b2f791c39e11e62 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 04:50:50 +0300 Subject: [PATCH 11/12] refactor: types more --- lib/javascript/JavascriptParser.js | 255 +++++++++++++++++---------- types.d.ts | 274 ++++++++++++++++++++++++++--- 2 files changed, 411 insertions(+), 118 deletions(-) diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index d60f6fe3ca6..6c463cf0f2b 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -18,9 +18,9 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("acorn").Options} AcornOptions */ /** @typedef {import("estree").AssignmentExpression} AssignmentExpression */ /** @typedef {import("estree").BinaryExpression} BinaryExpression */ -/** @typedef {import("estree").BlockStatement} BlockStatementNode */ +/** @typedef {import("estree").BlockStatement} BlockStatement */ /** @typedef {import("estree").SequenceExpression} SequenceExpression */ -/** @typedef {import("estree").CallExpression} CallExpressionNode */ +/** @typedef {import("estree").CallExpression} CallExpression */ /** @typedef {import("estree").BaseCallExpression} BaseCallExpression */ /** @typedef {import("estree").StaticBlock} StaticBlock */ /** @typedef {import("estree").ImportExpression} ImportExpression */ @@ -29,28 +29,29 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").SwitchStatement} SwitchStatement */ /** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */ /** @typedef {import("estree").ClassExpression} ClassExpression */ -/** @typedef {import("estree").Comment} CommentNode */ +/** @typedef {import("estree").Comment} Comment */ /** @typedef {import("estree").ConditionalExpression} ConditionalExpression */ -/** @typedef {import("estree").Declaration} DeclarationNode */ +/** @typedef {import("estree").Declaration} Declaration */ /** @typedef {import("estree").PrivateIdentifier} PrivateIdentifier */ /** @typedef {import("estree").PropertyDefinition} PropertyDefinition */ -/** @typedef {import("estree").Expression} ExpressionNode */ +/** @typedef {import("estree").Expression} Expression */ /** @typedef {import("estree").Identifier} Identifier */ /** @typedef {import("estree").VariableDeclaration} VariableDeclaration */ /** @typedef {import("estree").IfStatement} IfStatement */ /** @typedef {import("estree").LabeledStatement} LabeledStatement */ -/** @typedef {import("estree").Literal} LiteralNode */ +/** @typedef {import("estree").Literal} Literal */ /** @typedef {import("estree").LogicalExpression} LogicalExpression */ -/** @typedef {import("estree").ChainExpression} ChainExpressionNode */ -/** @typedef {import("estree").MemberExpression} MemberExpressionNode */ -/** @typedef {import("estree").MetaProperty} MetaPropertyNode */ +/** @typedef {import("estree").ChainExpression} ChainExpression */ +/** @typedef {import("estree").MemberExpression} MemberExpression */ +/** @typedef {import("estree").YieldExpression} YieldExpression */ +/** @typedef {import("estree").MetaProperty} MetaProperty */ /** @typedef {import("estree").Property} Property */ /** @typedef {import("estree").AssignmentPattern} AssignmentPattern */ +/** @typedef {import("estree").Pattern} Pattern */ /** @typedef {import("estree").UpdateExpression} UpdateExpression */ /** @typedef {import("estree").ObjectExpression} ObjectExpression */ /** @typedef {import("estree").UnaryExpression} UnaryExpression */ /** @typedef {import("estree").ArrayExpression} ArrayExpression */ -/** @typedef {import("estree").BlockStatement} BlockStatement */ /** @typedef {import("estree").ArrayPattern} ArrayPattern */ /** @typedef {import("estree").AwaitExpression} AwaitExpression */ /** @typedef {import("estree").ThisExpression} ThisExpression */ @@ -74,17 +75,15 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").DoWhileStatement} DoWhileStatement */ /** @typedef {import("estree").TryStatement} TryStatement */ /** @typedef {import("estree").Node} AnyNode */ -/** @typedef {import("estree").Program} ProgramNode */ -/** @typedef {import("estree").Statement} StatementNode */ +/** @typedef {import("estree").Program} Program */ +/** @typedef {import("estree").Directive} Directive */ +/** @typedef {import("estree").Statement} Statement */ /** @typedef {import("estree").ImportDeclaration} ImportDeclaration */ /** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclaration */ -/** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclarationNode */ -/** @typedef {import("estree").Super} SuperNode */ +/** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclaration */ +/** @typedef {import("estree").Super} Super */ /** @typedef {import("estree").TaggedTemplateExpression} TaggedTemplateExpression */ /** @typedef {import("estree").TemplateLiteral} TemplateLiteral */ -/** @typedef {import("estree").ThisExpression} ThisExpressionNode */ -/** @typedef {import("estree").UnaryExpression} UnaryExpressionNode */ -/** @typedef {import("estree").VariableDeclarator} VariableDeclaratorNode */ /** @typedef {Record} Assertions */ /** @template T @typedef {import("tapable").AsArray} AsArray */ /** @typedef {import("../Parser").ParserState} ParserState */ @@ -115,7 +114,7 @@ class VariableInfo { } /** @typedef {string | ScopeInfo | VariableInfo} ExportedVariableInfo */ -/** @typedef {LiteralNode | string | null | undefined} ImportSource */ +/** @typedef {Literal | string | null | undefined} ImportSource */ /** @typedef {Omit & { sourceType: "module" | "script" | "auto", ecmaVersion?: AcornOptions["ecmaVersion"] }} ParseOptions */ /** @@ -192,7 +191,7 @@ const objectAndMembersToName = (object, membersReversed) => { * [MetaProperties](https://github.com/estree/estree/blob/master/es2015.md#metaproperty) which is * specifically for handling the `new.target` meta property. * - * @param {ExpressionNode | SuperNode} expression expression + * @param {Expression | Super} expression expression * @returns {string | "this" | undefined} name or variable info */ const getRootName = expression => { @@ -234,13 +233,13 @@ class JavascriptParser extends Parser { constructor(sourceType = "auto") { super(); this.hooks = Object.freeze({ - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluateTypeof: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluate: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluateIdentifier: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluateDefinedIdentifier: new HookMap( () => new SyncBailHook(["expression"]) ), @@ -248,35 +247,35 @@ class JavascriptParser extends Parser { evaluateNewExpression: new HookMap( () => new SyncBailHook(["expression"]) ), - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluateCallExpression: new HookMap( () => new SyncBailHook(["expression"]) ), - /** @type {HookMap>} */ + /** @type {HookMap>} */ evaluateCallExpressionMember: new HookMap( () => new SyncBailHook(["expression", "param"]) ), - /** @type {HookMap>} */ + /** @type {HookMap>} */ isPure: new HookMap( () => new SyncBailHook(["expression", "commentsStartPosition"]) ), - /** @type {SyncBailHook<[StatementNode | ModuleDeclaration], boolean | void>} */ + /** @type {SyncBailHook<[Statement | ModuleDeclaration], boolean | void>} */ preStatement: new SyncBailHook(["statement"]), - /** @type {SyncBailHook<[StatementNode | ModuleDeclaration], boolean | void>} */ + /** @type {SyncBailHook<[Statement | ModuleDeclaration], boolean | void>} */ blockPreStatement: new SyncBailHook(["declaration"]), - /** @type {SyncBailHook<[StatementNode | ModuleDeclaration], boolean | void>} */ + /** @type {SyncBailHook<[Statement | ModuleDeclaration], boolean | void>} */ statement: new SyncBailHook(["statement"]), /** @type {SyncBailHook<[IfStatement], boolean | void>} */ statementIf: new SyncBailHook(["statement"]), - /** @type {SyncBailHook<[ExpressionNode, ClassExpression | ClassDeclaration], boolean | void>} */ + /** @type {SyncBailHook<[Expression, ClassExpression | ClassDeclaration], boolean | void>} */ classExtendsExpression: new SyncBailHook([ "expression", "classDefinition" ]), /** @type {SyncBailHook<[MethodDefinition | PropertyDefinition | StaticBlock, ClassExpression | ClassDeclaration], boolean | void>} */ classBodyElement: new SyncBailHook(["element", "classDefinition"]), - /** @type {SyncBailHook<[ExpressionNode, MethodDefinition | PropertyDefinition, ClassExpression | ClassDeclaration], boolean | void>} */ + /** @type {SyncBailHook<[Expression, MethodDefinition | PropertyDefinition, ClassExpression | ClassDeclaration], boolean | void>} */ classBodyValue: new SyncBailHook([ "expression", "element", @@ -293,22 +292,22 @@ class JavascriptParser extends Parser { "exportName", "identifierName" ]), - /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration], boolean | void>} */ export: new SyncBailHook(["statement"]), - /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode, ImportSource], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration, ImportSource], boolean | void>} */ exportImport: new SyncBailHook(["statement", "source"]), - /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode, DeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration, Declaration], boolean | void>} */ exportDeclaration: new SyncBailHook(["statement", "declaration"]), - /** @type {SyncBailHook<[ExportDefaultDeclaration, DeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportDefaultDeclaration, Declaration], boolean | void>} */ exportExpression: new SyncBailHook(["statement", "declaration"]), - /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode, string, string, number | undefined], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration, string, string, number | undefined], boolean | void>} */ exportSpecifier: new SyncBailHook([ "statement", "identifierName", "exportName", "index" ]), - /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclarationNode, ImportSource, string, string, number | undefined], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration, ImportSource, string, string, number | undefined], boolean | void>} */ exportImportSpecifier: new SyncBailHook([ "statement", "source", @@ -316,23 +315,23 @@ class JavascriptParser extends Parser { "exportName", "index" ]), - /** @type {SyncBailHook<[VariableDeclaratorNode, StatementNode], boolean | void>} */ + /** @type {SyncBailHook<[VariableDeclarator, Statement], boolean | void>} */ preDeclarator: new SyncBailHook(["declarator", "statement"]), - /** @type {SyncBailHook<[VariableDeclaratorNode, StatementNode], boolean | void>} */ + /** @type {SyncBailHook<[VariableDeclarator, Statement], boolean | void>} */ declarator: new SyncBailHook(["declarator", "statement"]), - /** @type {HookMap>} */ + /** @type {HookMap>} */ varDeclaration: new HookMap(() => new SyncBailHook(["declaration"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ varDeclarationLet: new HookMap(() => new SyncBailHook(["declaration"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ varDeclarationConst: new HookMap(() => new SyncBailHook(["declaration"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ varDeclarationVar: new HookMap(() => new SyncBailHook(["declaration"])), /** @type {HookMap>} */ pattern: new HookMap(() => new SyncBailHook(["pattern"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ canRename: new HookMap(() => new SyncBailHook(["initExpression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ rename: new HookMap(() => new SyncBailHook(["initExpression"])), /** @type {HookMap>} */ assign: new HookMap(() => new SyncBailHook(["expression"])), @@ -340,21 +339,21 @@ class JavascriptParser extends Parser { assignMemberChain: new HookMap( () => new SyncBailHook(["expression", "members"]) ), - /** @type {HookMap>} */ + /** @type {HookMap>} */ typeof: new HookMap(() => new SyncBailHook(["expression"])), /** @type {SyncBailHook<[ImportExpression], boolean | void>} */ importCall: new SyncBailHook(["expression"]), - /** @type {SyncBailHook<[ExpressionNode], boolean | void>} */ + /** @type {SyncBailHook<[Expression], boolean | void>} */ topLevelAwait: new SyncBailHook(["expression"]), /** @type {HookMap>} */ call: new HookMap(() => new SyncBailHook(["expression"])), /** Something like "a.b()" */ - /** @type {HookMap>} */ + /** @type {HookMap>} */ callMemberChain: new HookMap( () => new SyncBailHook(["expression", "members", "membersOptionals"]) ), /** Something like "a.b().c.d" */ - /** @type {HookMap>} */ + /** @type {HookMap>} */ memberChainOfCallMemberChain: new HookMap( () => new SyncBailHook([ @@ -365,7 +364,7 @@ class JavascriptParser extends Parser { ]) ), /** Something like "a.b().c.d()"" */ - /** @type {HookMap>} */ + /** @type {HookMap>} */ callMemberChainOfCallMemberChain: new HookMap( () => new SyncBailHook([ @@ -375,29 +374,29 @@ class JavascriptParser extends Parser { "members" ]) ), - /** @type {SyncBailHook<[ChainExpressionNode], boolean | void>} */ + /** @type {SyncBailHook<[ChainExpression], boolean | void>} */ optionalChaining: new SyncBailHook(["optionalChaining"]), /** @type {HookMap>} */ new: new HookMap(() => new SyncBailHook(["expression"])), /** @type {SyncBailHook<[BinaryExpression], boolean | void>} */ binaryExpression: new SyncBailHook(["binaryExpression"]), - /** @type {HookMap>} */ + /** @type {HookMap>} */ expression: new HookMap(() => new SyncBailHook(["expression"])), - /** @type {HookMap>} */ + /** @type {HookMap>} */ expressionMemberChain: new HookMap( () => new SyncBailHook(["expression", "members", "membersOptionals"]) ), - /** @type {HookMap>} */ + /** @type {HookMap>} */ unhandledExpressionMemberChain: new HookMap( () => new SyncBailHook(["expression", "members"]) ), - /** @type {SyncBailHook<[ExpressionNode], boolean | void>} */ + /** @type {SyncBailHook<[Expression], boolean | void>} */ expressionConditionalOperator: new SyncBailHook(["expression"]), - /** @type {SyncBailHook<[ExpressionNode], boolean | void>} */ + /** @type {SyncBailHook<[Expression], boolean | void>} */ expressionLogicalOperator: new SyncBailHook(["expression"]), - /** @type {SyncBailHook<[ProgramNode, CommentNode[]], boolean | void>} */ + /** @type {SyncBailHook<[Program, Comment[]], boolean | void>} */ program: new SyncBailHook(["ast", "comments"]), - /** @type {SyncBailHook<[ProgramNode, CommentNode[]], boolean | void>} */ + /** @type {SyncBailHook<[Program, Comment[]], boolean | void>} */ finish: new SyncBailHook(["ast", "comments"]) }); this.sourceType = sourceType; @@ -407,10 +406,10 @@ class JavascriptParser extends Parser { this.state = undefined; this.comments = undefined; this.semicolons = undefined; - /** @type {(StatementNode|ExpressionNode)[]} */ + /** @type {(Statement | ModuleDeclaration | Expression)[]} */ this.statementPath = undefined; this.prevStatement = undefined; - /** @type {WeakMap>} */ + /** @type {WeakMap>} */ this.destructuringAssignmentProperties = undefined; this.currentTagData = undefined; this._initializeEvaluating(); @@ -418,7 +417,7 @@ class JavascriptParser extends Parser { _initializeEvaluating() { this.hooks.evaluate.for("Literal").tap("JavascriptParser", _expr => { - const expr = /** @type {LiteralNode} */ (_expr); + const expr = /** @type {Literal} */ (_expr); switch (typeof expr.value) { case "number": @@ -568,7 +567,7 @@ class JavascriptParser extends Parser { * import("./" + foo + bar); // webpack will auto evaluate this into import("./foobar") * ``` * @param {boolean | number | BigInt | string} value the value to convert to an expression - * @param {BinaryExpression | UnaryExpressionNode} expr the expression being evaluated + * @param {BinaryExpression | UnaryExpression} expr the expression being evaluated * @param {boolean} sideEffects whether the expression has side effects * @returns {BasicEvaluatedExpression} the evaluated expression * @example @@ -970,7 +969,7 @@ class JavascriptParser extends Parser { this.hooks.evaluate .for("UnaryExpression") .tap("JavascriptParser", _expr => { - const expr = /** @type {UnaryExpressionNode} */ (_expr); + const expr = /** @type {UnaryExpression} */ (_expr); /** * Evaluates a UnaryExpression if and only if it is a basic const operator (e.g. +a, -a, ~a). @@ -1108,16 +1107,16 @@ class JavascriptParser extends Parser { }); /** * @param {string} exprType expression type name - * @param {function(ExpressionNode): GetInfoResult | undefined} getInfo get info + * @param {function(Expression): GetInfoResult | undefined} getInfo get info * @returns {void} */ const tapEvaluateWithVariableInfo = (exprType, getInfo) => { - /** @type {ExpressionNode | undefined} */ + /** @type {Expression | undefined} */ let cachedExpression = undefined; /** @type {GetInfoResult | undefined} */ let cachedInfo = undefined; this.hooks.evaluate.for(exprType).tap("JavascriptParser", expr => { - const expression = /** @type {MemberExpressionNode} */ (expr); + const expression = /** @type {MemberExpression} */ (expr); const info = getInfo(expr); if (info !== undefined) { @@ -1187,7 +1186,7 @@ class JavascriptParser extends Parser { } }); this.hooks.evaluate.for("MetaProperty").tap("JavascriptParser", expr => { - const metaProperty = /** @type {MetaPropertyNode} */ (expr); + const metaProperty = /** @type {MetaProperty} */ (expr); return this.callHooksForName( this.hooks.evaluateIdentifier, @@ -1197,13 +1196,13 @@ class JavascriptParser extends Parser { }); tapEvaluateWithVariableInfo("MemberExpression", expr => this.getMemberExpressionInfo( - /** @type {MemberExpressionNode} */ (expr), + /** @type {MemberExpression} */ (expr), ALLOWED_MEMBER_TYPES_EXPRESSION ) ); this.hooks.evaluate.for("CallExpression").tap("JavascriptParser", _expr => { - const expr = /** @type {CallExpressionNode} */ (_expr); + const expr = /** @type {CallExpression} */ (_expr); if ( expr.callee.type === "MemberExpression" && expr.callee.property.type === @@ -1211,7 +1210,7 @@ class JavascriptParser extends Parser { ) { // type Super also possible here const param = this.evaluateExpression( - /** @type {ExpressionNode} */ (expr.callee.object) + /** @type {Expression} */ (expr.callee.object) ); const property = expr.callee.property.type === "Literal" @@ -1526,10 +1525,10 @@ class JavascriptParser extends Parser { this.hooks.evaluate .for("ChainExpression") .tap("JavascriptParser", _expr => { - const expr = /** @type {ChainExpressionNode} */ (_expr); - /** @type {ExpressionNode[]} */ + const expr = /** @type {ChainExpression} */ (_expr); + /** @type {Expression[]} */ const optionalExpressionsStack = []; - /** @type {ExpressionNode|SuperNode} */ + /** @type {Expression|Super} */ let next = expr.expression; while ( @@ -1540,7 +1539,7 @@ class JavascriptParser extends Parser { if (next.optional) { // SuperNode can not be optional optionalExpressionsStack.push( - /** @type {ExpressionNode} */ (next.object) + /** @type {Expression} */ (next.object) ); } next = next.object; @@ -1548,7 +1547,7 @@ class JavascriptParser extends Parser { if (next.optional) { // SuperNode can not be optional optionalExpressionsStack.push( - /** @type {ExpressionNode} */ (next.callee) + /** @type {Expression} */ (next.callee) ); } next = next.callee; @@ -1568,7 +1567,7 @@ class JavascriptParser extends Parser { } /** - * @param {ExpressionNode} node node + * @param {Expression} node node * @returns {Set|undefined} destructured identifiers */ destructuringAssignmentPropertiesFor(node) { @@ -1577,7 +1576,7 @@ class JavascriptParser extends Parser { } /** - * @param {ExpressionNode} expr expression + * @param {Expression} expr expression * @returns {string | VariableInfoInterface | undefined} identifier */ getRenameIdentifier(expr) { @@ -1627,7 +1626,11 @@ class JavascriptParser extends Parser { } } - // Pre walking iterates the scope for variable declarations + /** + * Pre walking iterates the scope for variable declarations + * + * @param {(Statement | ModuleDeclaration)[]} statements statements + */ preWalkStatements(statements) { for (let index = 0, len = statements.length; index < len; index++) { const statement = statements[index]; @@ -1635,7 +1638,11 @@ class JavascriptParser extends Parser { } } - // Block pre walking iterates the scope for block variable declarations + /** + * Block pre walking iterates the scope for block variable declarations + * + * @param {(Statement | ModuleDeclaration)[]} statements statements + */ blockPreWalkStatements(statements) { for (let index = 0, len = statements.length; index < len; index++) { const statement = statements[index]; @@ -1643,7 +1650,11 @@ class JavascriptParser extends Parser { } } - // Walking iterates the statements and expressions and processes them + /** + * Walking iterates the statements and expressions and processes them + * + * @param {(Statement | ModuleDeclaration)[]} statements statements + */ walkStatements(statements) { for (let index = 0, len = statements.length; index < len; index++) { const statement = statements[index]; @@ -1651,6 +1662,11 @@ class JavascriptParser extends Parser { } } + /** + * Walking iterates the statements and expressions and processes them + * + * @param {Statement | ModuleDeclaration} statement statement + */ preWalkStatement(statement) { this.statementPath.push(statement); if (this.hooks.preStatement.call(statement)) { @@ -1701,6 +1717,9 @@ class JavascriptParser extends Parser { this.prevStatement = this.statementPath.pop(); } + /** + * @param {Statement | ModuleDeclaration} statement statement + */ blockPreWalkStatement(statement) { this.statementPath.push(statement); if (this.hooks.blockPreStatement.call(statement)) { @@ -1732,6 +1751,9 @@ class JavascriptParser extends Parser { this.prevStatement = this.statementPath.pop(); } + /** + * @param {Statement | ModuleDeclaration} statement statement + */ walkStatement(statement) { this.statementPath.push(statement); if (this.hooks.statement.call(statement) !== undefined) { @@ -1804,8 +1826,8 @@ class JavascriptParser extends Parser { * Walks a statements that is nested within a parent statement * and can potentially be a non-block statement. * This enforces the nested statement to never be in ASI position. - * @param {StatementNode} statement the nested statement - * @returns {void} + * + * @param {Statement} statement the nested statement */ walkNestedStatement(statement) { this.prevStatement = undefined; @@ -2580,6 +2602,9 @@ class JavascriptParser extends Parser { }); } + /** + * @param {Pattern} pattern pattern + */ walkPattern(pattern) { switch (pattern.type) { case "ArrayPattern": @@ -2635,6 +2660,9 @@ class JavascriptParser extends Parser { this.walkPattern(pattern.argument); } + /** + * @param {(Expression | SpreadElement | null)[]} expressions expressions + */ walkExpressions(expressions) { for (const expression of expressions) { if (expression) { @@ -2741,6 +2769,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {SpreadElement} expression spread element + */ walkSpreadElement(expression) { if (expression.argument) { this.walkExpression(expression.argument); @@ -2883,6 +2914,9 @@ class JavascriptParser extends Parser { this.walkExpression(expression.argument); } + /** + * @param {LogicalExpression | BinaryExpression} expression the expression + */ walkLeftRightExpression(expression) { this.walkExpression(expression.left); this.walkExpression(expression.right); @@ -3020,12 +3054,18 @@ class JavascriptParser extends Parser { } } + /** + * @param {YieldExpression} expression yield expression + */ walkYieldExpression(expression) { if (expression.argument) { this.walkExpression(expression.argument); } } + /** + * @param {TemplateLiteral} expression template literal + */ walkTemplateLiteral(expression) { if (expression.expressions) { this.walkExpressions(expression.expressions); @@ -3052,7 +3092,7 @@ class JavascriptParser extends Parser { } /** - * @param {ChainExpressionNode} expression expression + * @param {ChainExpression} expression expression */ walkChainExpression(expression) { const result = this.hooks.optionalChaining.call(expression); @@ -3218,7 +3258,7 @@ class JavascriptParser extends Parser { } /** - * @param {MemberExpressionNode} expression member expression + * @param {MemberExpression} expression member expression */ walkMemberExpression(expression) { const exprInfo = this.getMemberExpressionInfo( @@ -3328,7 +3368,7 @@ class JavascriptParser extends Parser { } /** - * @param {MetaPropertyNode} metaProperty meta property + * @param {MetaProperty} metaProperty meta property */ walkMetaProperty(metaProperty) { this.hooks.expression.for(getRootName(metaProperty)).call(metaProperty); @@ -3348,7 +3388,7 @@ class JavascriptParser extends Parser { * @template T * @template R * @param {HookMap>} hookMap hooks the should be called - * @param {MemberExpressionNode} expr expression info + * @param {MemberExpression} expr expression info * @param {function(string, string | ScopeInfo | VariableInfo, function(): string[]): any} fallback callback when variable in not handled by hooks * @param {function(string): any} defined callback when variable is defined * @param {AsArray} args args for the hook @@ -3605,12 +3645,20 @@ class JavascriptParser extends Parser { } } + /** + * @param {Identifier} pattern identifier pattern + * @param {TODO} onIdent callback + */ enterIdentifier(pattern, onIdent) { if (!this.callHooksForName(this.hooks.pattern, pattern.name, pattern)) { onIdent(pattern.name, pattern); } } + /** + * @param {ObjectPattern} pattern object pattern + * @param {TODO} onIdent callback + */ enterObjectPattern(pattern, onIdent) { for ( let propIndex = 0, len = pattern.properties.length; @@ -3622,6 +3670,10 @@ class JavascriptParser extends Parser { } } + /** + * @param {ArrayPattern} pattern object pattern + * @param {TODO} onIdent callback + */ enterArrayPattern(pattern, onIdent) { for ( let elementIndex = 0, len = pattern.elements.length; @@ -3629,20 +3681,29 @@ class JavascriptParser extends Parser { elementIndex++ ) { const element = pattern.elements[elementIndex]; + // TODO check on `null`? this.enterPattern(element, onIdent); } } + /** + * @param {RestElement} pattern object pattern + * @param {TODO} onIdent callback + */ enterRestElement(pattern, onIdent) { this.enterPattern(pattern.argument, onIdent); } + /** + * @param {AssignmentPattern} pattern object pattern + * @param {TODO} onIdent callback + */ enterAssignmentPattern(pattern, onIdent) { this.enterPattern(pattern.left, onIdent); } /** - * @param {ExpressionNode} expression expression node + * @param {Expression} expression expression node * @returns {BasicEvaluatedExpression} evaluation result */ evaluateExpression(expression) { @@ -3665,7 +3726,7 @@ class JavascriptParser extends Parser { } /** - * @param {ExpressionNode} expression expression + * @param {Expression} expression expression * @returns {string} parsed string */ parseString(expression) { @@ -3776,7 +3837,7 @@ class JavascriptParser extends Parser { source = source.toString("utf-8"); } if (typeof source === "object") { - ast = /** @type {ProgramNode} */ (source); + ast = /** @type {Program} */ (source); comments = source.comments; } else { comments = []; @@ -3844,7 +3905,7 @@ class JavascriptParser extends Parser { } /** - * @param {ExpressionNode | DeclarationNode | PrivateIdentifier | null | undefined} expr an expression + * @param {Expression | Declaration | PrivateIdentifier | null | undefined} expr an expression * @param {number} commentsStartPos source position from which annotation comments are checked * @returns {boolean} true, when the expression is pure */ @@ -4127,8 +4188,8 @@ class JavascriptParser extends Parser { } /** - * @param {MemberExpressionNode} expression a member expression - * @returns {{ members: string[], object: ExpressionNode | SuperNode, membersOptionals: boolean[] }} member names (reverse order) and remaining object + * @param {MemberExpression} expression a member expression + * @returns {{ members: string[], object: Expression | Super, membersOptionals: boolean[] }} member names (reverse order) and remaining object */ extractMemberExpressionChain(expression) { /** @type {AnyNode} */ @@ -4172,11 +4233,11 @@ class JavascriptParser extends Parser { return { info, name }; } - /** @typedef {{ type: "call", call: CallExpressionNode, calleeName: string, rootInfo: string | VariableInfo, getCalleeMembers: () => string[], name: string, getMembers: () => string[], getMembersOptionals: () => boolean[]}} CallExpressionInfo */ + /** @typedef {{ type: "call", call: CallExpression, calleeName: string, rootInfo: string | VariableInfo, getCalleeMembers: () => string[], name: string, getMembers: () => string[], getMembersOptionals: () => boolean[]}} CallExpressionInfo */ /** @typedef {{ type: "expression", rootInfo: string | VariableInfo, name: string, getMembers: () => string[], getMembersOptionals: () => boolean[]}} ExpressionExpressionInfo */ /** - * @param {MemberExpressionNode} expression a member expression + * @param {MemberExpression} expression a member expression * @param {number} allowedTypes which types should be returned, presented in bit mask * @returns {CallExpressionInfo | ExpressionExpressionInfo | undefined} expression info */ @@ -4233,7 +4294,7 @@ class JavascriptParser extends Parser { } /** - * @param {MemberExpressionNode} expression an expression + * @param {MemberExpression} expression an expression * @returns {{ name: string, rootInfo: ExportedVariableInfo, getMembers: () => string[]}} name info */ getNameForExpression(expression) { @@ -4246,7 +4307,7 @@ class JavascriptParser extends Parser { /** * @param {string} code source code * @param {ParseOptions} options parsing options - * @returns {ProgramNode} parsed ast + * @returns {Program} parsed ast */ static _parse(code, options) { const type = options ? options.sourceType : "module"; @@ -4290,7 +4351,7 @@ class JavascriptParser extends Parser { throw error; } - return /** @type {ProgramNode} */ (ast); + return /** @type {Program} */ (ast); } } diff --git a/types.d.ts b/types.d.ts index e621cd81dea..159b2a46edc 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5368,6 +5368,10 @@ declare class JavascriptParser extends Parser { | ForStatement | ForInStatement | ForOfStatement + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration )[]; prevStatement: any; destructuringAssignmentProperties: WeakMap>; @@ -5379,12 +5383,199 @@ declare class JavascriptParser extends Parser { expr: Expression ): undefined | string | VariableInfoInterface; walkClass(classy: ClassExpression | ClassDeclaration): void; - preWalkStatements(statements?: any): void; - blockPreWalkStatements(statements?: any): void; - walkStatements(statements?: any): void; - preWalkStatement(statement?: any): void; - blockPreWalkStatement(statement?: any): void; - walkStatement(statement?: any): void; + + /** + * Pre walking iterates the scope for variable declarations + */ + preWalkStatements( + statements: ( + | FunctionDeclaration + | VariableDeclaration + | ClassDeclaration + | ExpressionStatement + | BlockStatement + | StaticBlock + | EmptyStatement + | DebuggerStatement + | WithStatement + | ReturnStatement + | LabeledStatement + | BreakStatement + | ContinueStatement + | IfStatement + | SwitchStatement + | ThrowStatement + | TryStatement + | WhileStatement + | DoWhileStatement + | ForStatement + | ForInStatement + | ForOfStatement + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration + )[] + ): void; + + /** + * Block pre walking iterates the scope for block variable declarations + */ + blockPreWalkStatements( + statements: ( + | FunctionDeclaration + | VariableDeclaration + | ClassDeclaration + | ExpressionStatement + | BlockStatement + | StaticBlock + | EmptyStatement + | DebuggerStatement + | WithStatement + | ReturnStatement + | LabeledStatement + | BreakStatement + | ContinueStatement + | IfStatement + | SwitchStatement + | ThrowStatement + | TryStatement + | WhileStatement + | DoWhileStatement + | ForStatement + | ForInStatement + | ForOfStatement + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration + )[] + ): void; + + /** + * Walking iterates the statements and expressions and processes them + */ + walkStatements( + statements: ( + | FunctionDeclaration + | VariableDeclaration + | ClassDeclaration + | ExpressionStatement + | BlockStatement + | StaticBlock + | EmptyStatement + | DebuggerStatement + | WithStatement + | ReturnStatement + | LabeledStatement + | BreakStatement + | ContinueStatement + | IfStatement + | SwitchStatement + | ThrowStatement + | TryStatement + | WhileStatement + | DoWhileStatement + | ForStatement + | ForInStatement + | ForOfStatement + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration + )[] + ): void; + + /** + * Walking iterates the statements and expressions and processes them + */ + preWalkStatement( + statement: + | FunctionDeclaration + | VariableDeclaration + | ClassDeclaration + | ExpressionStatement + | BlockStatement + | StaticBlock + | EmptyStatement + | DebuggerStatement + | WithStatement + | ReturnStatement + | LabeledStatement + | BreakStatement + | ContinueStatement + | IfStatement + | SwitchStatement + | ThrowStatement + | TryStatement + | WhileStatement + | DoWhileStatement + | ForStatement + | ForInStatement + | ForOfStatement + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration + ): void; + blockPreWalkStatement( + statement: + | FunctionDeclaration + | VariableDeclaration + | ClassDeclaration + | ExpressionStatement + | BlockStatement + | StaticBlock + | EmptyStatement + | DebuggerStatement + | WithStatement + | ReturnStatement + | LabeledStatement + | BreakStatement + | ContinueStatement + | IfStatement + | SwitchStatement + | ThrowStatement + | TryStatement + | WhileStatement + | DoWhileStatement + | ForStatement + | ForInStatement + | ForOfStatement + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration + ): void; + walkStatement( + statement: + | FunctionDeclaration + | VariableDeclaration + | ClassDeclaration + | ExpressionStatement + | BlockStatement + | StaticBlock + | EmptyStatement + | DebuggerStatement + | WithStatement + | ReturnStatement + | LabeledStatement + | BreakStatement + | ContinueStatement + | IfStatement + | SwitchStatement + | ThrowStatement + | TryStatement + | WhileStatement + | DoWhileStatement + | ForStatement + | ForInStatement + | ForOfStatement + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration + ): void; /** * Walks a statements that is nested within a parent statement @@ -5439,31 +5630,65 @@ declare class JavascriptParser extends Parser { walkSwitchCases(switchCases: SwitchCase[]): void; preWalkCatchClause(catchClause: CatchClause): void; walkCatchClause(catchClause: CatchClause): void; - walkPattern(pattern?: any): void; + walkPattern(pattern: Pattern): void; walkAssignmentPattern(pattern: AssignmentPattern): void; walkObjectPattern(pattern?: any): void; walkArrayPattern(pattern: ArrayPattern): void; walkRestElement(pattern: RestElement): void; - walkExpressions(expressions?: any): void; + walkExpressions( + expressions: ( + | null + | UnaryExpression + | ArrayExpression + | ArrowFunctionExpression + | AssignmentExpression + | AwaitExpression + | BinaryExpression + | SimpleCallExpression + | NewExpression + | ChainExpression + | ClassExpression + | ConditionalExpression + | FunctionExpression + | Identifier + | ImportExpression + | SimpleLiteral + | RegExpLiteral + | BigIntLiteral + | LogicalExpression + | MemberExpression + | MetaProperty + | ObjectExpression + | SequenceExpression + | TaggedTemplateExpression + | TemplateLiteral + | ThisExpression + | UpdateExpression + | YieldExpression + | SpreadElement + )[] + ): void; walkExpression(expression?: any): void; walkAwaitExpression(expression: AwaitExpression): void; walkArrayExpression(expression: ArrayExpression): void; - walkSpreadElement(expression?: any): void; + walkSpreadElement(expression: SpreadElement): void; walkObjectExpression(expression: ObjectExpression): void; - walkProperty(prop: Property | SpreadElement): void; + walkProperty(prop: SpreadElement | Property): void; walkFunctionExpression(expression?: any): void; walkArrowFunctionExpression(expression?: any): void; walkSequenceExpression(expression: SequenceExpression): void; walkUpdateExpression(expression: UpdateExpression): void; walkUnaryExpression(expression: UnaryExpression): void; - walkLeftRightExpression(expression?: any): void; + walkLeftRightExpression( + expression: BinaryExpression | LogicalExpression + ): void; walkBinaryExpression(expression: BinaryExpression): void; walkLogicalExpression(expression: LogicalExpression): void; walkAssignmentExpression(expression: AssignmentExpression): void; walkConditionalExpression(expression: ConditionalExpression): void; walkNewExpression(expression: NewExpression): void; - walkYieldExpression(expression?: any): void; - walkTemplateLiteral(expression?: any): void; + walkYieldExpression(expression: YieldExpression): void; + walkTemplateLiteral(expression: TemplateLiteral): void; walkTaggedTemplateExpression(expression: TaggedTemplateExpression): void; walkClassExpression(expression: ClassExpression): void; walkChainExpression(expression: ChainExpression): void; @@ -5522,11 +5747,11 @@ declare class JavascriptParser extends Parser { detectMode(statements?: any): void; enterPatterns(patterns?: any, onIdent?: any): void; enterPattern(pattern?: any, onIdent?: any): void; - enterIdentifier(pattern?: any, onIdent?: any): void; - enterObjectPattern(pattern?: any, onIdent?: any): void; - enterArrayPattern(pattern?: any, onIdent?: any): void; - enterRestElement(pattern?: any, onIdent?: any): void; - enterAssignmentPattern(pattern?: any, onIdent?: any): void; + enterIdentifier(pattern: Identifier, onIdent?: any): void; + enterObjectPattern(pattern: ObjectPattern, onIdent?: any): void; + enterArrayPattern(pattern: ArrayPattern, onIdent?: any): void; + enterRestElement(pattern: RestElement, onIdent?: any): void; + enterAssignmentPattern(pattern: AssignmentPattern, onIdent?: any): void; evaluateExpression(expression: Expression): BasicEvaluatedExpression; parseString(expression: Expression): string; parseCalculatedString(expression?: any): any; @@ -7765,18 +7990,18 @@ type NodeEstreeIndex = | Program | SwitchCase | CatchClause - | AssignmentPattern + | ObjectPattern | ArrayPattern | RestElement - | Property + | AssignmentPattern | SpreadElement + | Property | AssignmentProperty | ClassBody | ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier - | ObjectPattern | Super | TemplateElement; @@ -9269,6 +9494,13 @@ declare interface PathData { noChunkHash?: boolean; url?: string; } +type Pattern = + | Identifier + | MemberExpression + | ObjectPattern + | ArrayPattern + | RestElement + | AssignmentPattern; /** * Configuration object for web performance recommendations. From f003f1c13803ab916cf1d4f86b10bfe6145abc4c Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 22 May 2023 05:12:44 +0300 Subject: [PATCH 12/12] refactor: types more --- .../HarmonyDetectionParserPlugin.js | 10 +++ lib/dependencies/HarmonyModulesPlugin.js | 5 ++ lib/dependencies/RequireResolveDependency.js | 2 +- lib/javascript/JavascriptParser.js | 11 +++- lib/web/JsonpChunkLoadingRuntimeModule.js | 3 + .../ImportScriptsChunkLoadingRuntimeModule.js | 4 ++ types.d.ts | 61 +++++++++++++++++-- 7 files changed, 90 insertions(+), 6 deletions(-) diff --git a/lib/dependencies/HarmonyDetectionParserPlugin.js b/lib/dependencies/HarmonyDetectionParserPlugin.js index 2fd1435911d..58d4a6b8064 100644 --- a/lib/dependencies/HarmonyDetectionParserPlugin.js +++ b/lib/dependencies/HarmonyDetectionParserPlugin.js @@ -11,8 +11,12 @@ const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency const HarmonyExports = require("./HarmonyExports"); /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ +/** @typedef {import("./HarmonyModulesPlugin").HarmonyModulesPluginOptions} HarmonyModulesPluginOptions */ module.exports = class HarmonyDetectionParserPlugin { + /** + * @param {HarmonyModulesPluginOptions} options options + */ constructor(options) { const { topLevelAwait = false } = options || {}; this.topLevelAwait = topLevelAwait; @@ -71,12 +75,18 @@ module.exports = class HarmonyDetectionParserPlugin { module.buildMeta.async = true; }); + /** + * @returns {boolean | undefined} true if in harmony + */ const skipInHarmony = () => { if (HarmonyExports.isEnabled(parser.state)) { return true; } }; + /** + * @returns {null | undefined} null if in harmony + */ const nullInHarmony = () => { if (HarmonyExports.isEnabled(parser.state)) { return null; diff --git a/lib/dependencies/HarmonyModulesPlugin.js b/lib/dependencies/HarmonyModulesPlugin.js index 4b68a18cea9..a3bbd98de82 100644 --- a/lib/dependencies/HarmonyModulesPlugin.js +++ b/lib/dependencies/HarmonyModulesPlugin.js @@ -31,7 +31,12 @@ const HarmonyTopLevelThisParserPlugin = require("./HarmonyTopLevelThisParserPlug const PLUGIN_NAME = "HarmonyModulesPlugin"; +/** @typedef {{ topLevelAwait?: boolean }} HarmonyModulesPluginOptions */ + class HarmonyModulesPlugin { + /** + * @param {HarmonyModulesPluginOptions} options options + */ constructor(options) { this.options = options; } diff --git a/lib/dependencies/RequireResolveDependency.js b/lib/dependencies/RequireResolveDependency.js index b4f014b54c5..da0bd319b9d 100644 --- a/lib/dependencies/RequireResolveDependency.js +++ b/lib/dependencies/RequireResolveDependency.js @@ -19,7 +19,7 @@ class RequireResolveDependency extends ModuleDependency { /** * @param {string} request the request string * @param {Range} range location in source code - * @param {TODO} context context + * @param {string} [context] context */ constructor(request, range, context) { super(request); diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index 6c463cf0f2b..ad985694c4f 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -69,7 +69,9 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").ModuleDeclaration} ModuleDeclaration */ /** @typedef {import("estree").NewExpression} NewExpression */ /** @typedef {import("estree").SpreadElement} SpreadElement */ +/** @typedef {import("estree").FunctionExpression} FunctionExpression */ /** @typedef {import("estree").WhileStatement} WhileStatement */ +/** @typedef {import("estree").ArrowFunctionExpression} ArrowFunctionExpression */ /** @typedef {import("estree").ExpressionStatement} ExpressionStatement */ /** @typedef {import("estree").FunctionDeclaration} FunctionDeclaration */ /** @typedef {import("estree").DoWhileStatement} DoWhileStatement */ @@ -408,6 +410,7 @@ class JavascriptParser extends Parser { this.semicolons = undefined; /** @type {(Statement | ModuleDeclaration | Expression)[]} */ this.statementPath = undefined; + /** @type {Statement | ModuleDeclaration | Expression} */ this.prevStatement = undefined; /** @type {WeakMap>} */ this.destructuringAssignmentProperties = undefined; @@ -2812,6 +2815,9 @@ class JavascriptParser extends Parser { } } + /** + * @param {FunctionExpression} expression arrow function expression + */ walkFunctionExpression(expression) { const wasTopLevel = this.scope.topLevelScope; this.scope.topLevelScope = false; @@ -2819,7 +2825,7 @@ class JavascriptParser extends Parser { // Add function name in scope for recursive calls if (expression.id) { - scopeParams.push(expression.id.name); + scopeParams.push(expression.id); } this.inFunctionScope(true, scopeParams, () => { @@ -2839,6 +2845,9 @@ class JavascriptParser extends Parser { this.scope.topLevelScope = wasTopLevel; } + /** + * @param {ArrowFunctionExpression} expression arrow function expression + */ walkArrowFunctionExpression(expression) { const wasTopLevel = this.scope.topLevelScope; this.scope.topLevelScope = wasTopLevel ? "arrow" : false; diff --git a/lib/web/JsonpChunkLoadingRuntimeModule.js b/lib/web/JsonpChunkLoadingRuntimeModule.js index 9eaf9b35da2..fbcdab0ac86 100644 --- a/lib/web/JsonpChunkLoadingRuntimeModule.js +++ b/lib/web/JsonpChunkLoadingRuntimeModule.js @@ -46,6 +46,9 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule { return hooks; } + /** + * @param {Set} runtimeRequirements runtime requirements + */ constructor(runtimeRequirements) { super("jsonp chunk loading", RuntimeModule.STAGE_ATTACH); this._runtimeRequirements = runtimeRequirements; diff --git a/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js b/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js index b9947d6325f..7c6e0a26752 100644 --- a/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +++ b/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js @@ -18,6 +18,10 @@ const { getUndoPath } = require("../util/identifier"); /** @typedef {import("../Chunk")} Chunk */ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule { + /** + * @param {Set} runtimeRequirements runtime requirements + * @param {boolean} withCreateScriptUrl with createScriptUrl support + */ constructor(runtimeRequirements, withCreateScriptUrl) { super("importScripts chunk loading", RuntimeModule.STAGE_ATTACH); this.runtimeRequirements = runtimeRequirements; diff --git a/types.d.ts b/types.d.ts index 159b2a46edc..b1ee6ea0c86 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5373,7 +5373,60 @@ declare class JavascriptParser extends Parser { | ExportDefaultDeclaration | ExportAllDeclaration )[]; - prevStatement: any; + prevStatement: + | UnaryExpression + | ArrayExpression + | ArrowFunctionExpression + | AssignmentExpression + | AwaitExpression + | BinaryExpression + | SimpleCallExpression + | NewExpression + | ChainExpression + | ClassExpression + | ConditionalExpression + | FunctionExpression + | Identifier + | ImportExpression + | SimpleLiteral + | RegExpLiteral + | BigIntLiteral + | LogicalExpression + | MemberExpression + | MetaProperty + | ObjectExpression + | SequenceExpression + | TaggedTemplateExpression + | TemplateLiteral + | ThisExpression + | UpdateExpression + | YieldExpression + | FunctionDeclaration + | VariableDeclaration + | ClassDeclaration + | ExpressionStatement + | BlockStatement + | StaticBlock + | EmptyStatement + | DebuggerStatement + | WithStatement + | ReturnStatement + | LabeledStatement + | BreakStatement + | ContinueStatement + | IfStatement + | SwitchStatement + | ThrowStatement + | TryStatement + | WhileStatement + | DoWhileStatement + | ForStatement + | ForInStatement + | ForOfStatement + | ImportDeclaration + | ExportNamedDeclaration + | ExportDefaultDeclaration + | ExportAllDeclaration; destructuringAssignmentProperties: WeakMap>; currentTagData: any; destructuringAssignmentPropertiesFor( @@ -5674,8 +5727,8 @@ declare class JavascriptParser extends Parser { walkSpreadElement(expression: SpreadElement): void; walkObjectExpression(expression: ObjectExpression): void; walkProperty(prop: SpreadElement | Property): void; - walkFunctionExpression(expression?: any): void; - walkArrowFunctionExpression(expression?: any): void; + walkFunctionExpression(expression: FunctionExpression): void; + walkArrowFunctionExpression(expression: ArrowFunctionExpression): void; walkSequenceExpression(expression: SequenceExpression): void; walkUpdateExpression(expression: UpdateExpression): void; walkUnaryExpression(expression: UnaryExpression): void; @@ -6044,7 +6097,7 @@ declare interface JavascriptParserOptions { wrappedContextRegExp?: RegExp; } declare class JsonpChunkLoadingRuntimeModule extends RuntimeModule { - constructor(runtimeRequirements?: any); + constructor(runtimeRequirements: Set); static getCompilationHooks( compilation: Compilation ): JsonpCompilationPluginHooks;