Add JS-CATCH-FALLBACK-01 rule and update npm packages

Add PHP-ALIAS-01 rule: prohibit field aliasing in serialization

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-12-23 07:36:18 +00:00
parent 3cc590186a
commit 3ce82a924a
1256 changed files with 6491 additions and 3989 deletions

View File

@@ -5,11 +5,14 @@
"use strict";
const { CSS_TYPE, JAVASCRIPT_TYPE } = require("../ModuleSourceTypeConstants");
const WebpackError = require("../WebpackError");
const { cssExportConvention } = require("../util/conventions");
const createHash = require("../util/createHash");
const { makePathsRelative } = require("../util/identifier");
const makeSerializable = require("../util/makeSerializable");
const memoize = require("../util/memoize");
const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
const CssIcssImportDependency = require("./CssIcssImportDependency");
const NullDependency = require("./NullDependency");
@@ -20,6 +23,7 @@ const getCssParser = memoize(() => require("../css/CssParser"));
/** @typedef {import("../../declarations/WebpackOptions").CssGeneratorLocalIdentName} CssGeneratorLocalIdentName */
/** @typedef {import("../CssModule")} CssModule */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
@@ -28,6 +32,7 @@ const getCssParser = memoize(() => require("../css/CssParser"));
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("../util/Hash")} Hash */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("../css/CssParser").Range} Range */
@@ -43,7 +48,7 @@ const getLocalIdent = (local, module, chunkGraph, runtimeTemplate) => {
const generator = /** @type {CssGenerator} */ (module.generator);
const localIdentName =
/** @type {CssGeneratorLocalIdentName} */
(generator.localIdentName);
(generator.options.localIdentName);
const relativeResourcePath = makePathsRelative(
/** @type {string} */
(module.context),
@@ -51,38 +56,92 @@ const getLocalIdent = (local, module, chunkGraph, runtimeTemplate) => {
(module.getResource()),
runtimeTemplate.compilation.compiler.root
);
const { hashFunction, hashDigest, hashDigestLength, hashSalt, uniqueName } =
runtimeTemplate.outputOptions;
const hash = createHash(hashFunction);
const { uniqueName } = runtimeTemplate.outputOptions;
if (hashSalt) {
hash.update(hashSalt);
}
let localIdentHash = "";
hash.update(relativeResourcePath);
if (/\[(fullhash|hash)\]/.test(localIdentName)) {
const hashSalt = generator.options.localIdentHashSalt;
const hashDigest =
/** @type {string} */
(generator.options.localIdentHashDigest);
const hashDigestLength = generator.options.localIdentHashDigestLength;
const { hashFunction } = runtimeTemplate.outputOptions;
if (!/\[local\]/.test(localIdentName)) {
const hash = createHash(hashFunction);
if (hashSalt) {
hash.update(hashSalt);
}
if (uniqueName) {
hash.update(uniqueName);
}
hash.update(relativeResourcePath);
hash.update(local);
localIdentHash = hash.digest(hashDigest).slice(0, hashDigestLength);
}
const localIdentHash = hash.digest(hashDigest).slice(0, hashDigestLength);
let contentHash = "";
return runtimeTemplate.compilation
.getPath(localIdentName, {
filename: relativeResourcePath,
hash: localIdentHash,
contentHash: localIdentHash,
chunkGraph,
module
})
.replace(/\[local\]/g, local)
.replace(/\[uniqueName\]/g, /** @type {string} */ (uniqueName))
.replace(/^((-?[0-9])|--)/, "_$1");
if (/\[contenthash\]/.test(localIdentName)) {
const hash = createHash(runtimeTemplate.outputOptions.hashFunction);
const source = module.originalSource();
if (source) {
hash.update(source.buffer());
}
if (module.error) {
hash.update(module.error.toString());
}
const fullContentHash = hash.digest(
runtimeTemplate.outputOptions.hashDigest
);
contentHash = nonNumericOnlyHash(
fullContentHash,
runtimeTemplate.outputOptions.hashDigestLength
);
}
let localIdent = runtimeTemplate.compilation.getPath(localIdentName, {
prepareId: (id) => {
if (typeof id !== "string") return id;
return id
.replace(/^([.-]|[^a-zA-Z0-9_-])+/, "")
.replace(/[^a-zA-Z0-9_-]+/g, "_");
},
filename: relativeResourcePath,
hash: localIdentHash,
contentHash,
chunkGraph,
module
});
if (/\[local\]/.test(localIdentName)) {
localIdent = localIdent.replace(/\[local\]/g, local);
}
if (/\[uniqueName\]/.test(localIdentName)) {
localIdent = localIdent.replace(
/\[uniqueName\]/g,
/** @type {string} */ (uniqueName)
);
}
// Protect the first character from unsupported values
return localIdent.replace(/^((-?[0-9])|--)/, "_$1");
};
// 0 - replace, 1 - append, 2 - once
/** @typedef {0 | 1 | 2} ExportMode */
// 0 - none, 1 - name, 1 - value
/** @typedef {0 | 1 | 2} InterpolationMode */
// 0 - replace, 1 - replace, 2 - append, 2 - once
/** @typedef {0 | 1 | 2 | 3 | 4} ExportMode */
// 0 - normal, 1 - custom css variable, 2 - grid custom ident
/** @typedef {0 | 1 | 2} ExportType */
class CssIcssExportDependency extends NullDependency {
/**
@@ -90,20 +149,27 @@ class CssIcssExportDependency extends NullDependency {
*
* :export { LOCAL_NAME: EXPORT_NAME }
* @param {string} name export name
* @param {string} value export value or true when we need interpolate name as a value
* @param {string=} reexport reexport name
* @param {string | [string, string, boolean]} value export value or true when we need interpolate name as a value
* @param {Range=} range range
* @param {boolean=} interpolate true when value need to be interpolated, otherwise false
* @param {ExportMode=} exportMode export mode
* @param {ExportType=} exportType export type
*/
constructor(name, value, reexport, range) {
constructor(
name,
value,
range,
interpolate = false,
exportMode = CssIcssExportDependency.EXPORT_MODE.REPLACE,
exportType = CssIcssExportDependency.EXPORT_TYPE.NORMAL
) {
super();
this.name = name;
this.value = value;
this.reexport = reexport;
this.range = range;
/** @type {undefined | InterpolationMode} */
this.interpolationMode = undefined;
/** @type {ExportMode} */
this.exportMode = CssIcssExportDependency.EXPORT_MODE.REPLACE;
this.interpolate = interpolate;
this.exportMode = exportMode;
this.exportType = exportType;
this._hashUpdate = undefined;
}
@@ -124,21 +190,52 @@ class CssIcssExportDependency extends NullDependency {
return this._conventionNames;
}
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
* @returns {ReferencedExports} referenced exports
*/
getReferencedExports(moduleGraph, runtime) {
if (
this.exportMode === CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE
) {
return [
{
name: [this.name],
canMangle: true
}
];
}
return super.getReferencedExports(moduleGraph, runtime);
}
/**
* Returns the exported names
* @param {ModuleGraph} moduleGraph module graph
* @returns {ExportsSpec | undefined} export names
*/
getExports(moduleGraph) {
if (
this.exportMode === CssIcssExportDependency.EXPORT_MODE.NONE ||
this.exportMode === CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE
) {
return;
}
const module = /** @type {CssModule} */ (moduleGraph.getParentModule(this));
const generator = /** @type {CssGenerator} */ (module.generator);
const names = this.getExportsConventionNames(
this.name,
/** @type {CssGeneratorExportsConvention} */
(generator.convention)
(generator.options.exportsConvention)
);
return {
exports: names.map((name) => ({
exports: [
...names,
...(Array.isArray(this.value) ? [this.value[1]] : [])
].map((name) => ({
name,
canMangle: true
})),
@@ -146,6 +243,34 @@ class CssIcssExportDependency extends NullDependency {
};
}
/**
* Returns warnings
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[] | null | undefined} warnings
*/
getWarnings(moduleGraph) {
if (
this.exportMode === CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE &&
!Array.isArray(this.value)
) {
const module = moduleGraph.getParentModule(this);
if (
module &&
!moduleGraph.getExportsInfo(module).isExportProvided(this.value)
) {
const error = new WebpackError(
`Self-referencing name "${this.value}" not found`
);
error.module = module;
return [error];
}
}
return null;
}
/**
* Update the hash
* @param {Hash} hash hash to be updated
@@ -161,9 +286,9 @@ class CssIcssExportDependency extends NullDependency {
const names = this.getExportsConventionNames(
this.name,
/** @type {CssGeneratorExportsConvention} */
(generator.convention)
(generator.options.exportsConvention)
);
this._hashUpdate = `exportsConvention|${JSON.stringify(names)}|localIdentName|${JSON.stringify(generator.localIdentName)}`;
this._hashUpdate = `exportsConvention|${JSON.stringify(names)}|localIdentName|${JSON.stringify(generator.options.localIdentName)}`;
}
hash.update(this._hashUpdate);
}
@@ -175,10 +300,10 @@ class CssIcssExportDependency extends NullDependency {
const { write } = context;
write(this.name);
write(this.value);
write(this.reexport);
write(this.range);
write(this.interpolationMode);
write(this.interpolate);
write(this.exportMode);
write(this.exportType);
super.serialize(context);
}
@@ -189,10 +314,10 @@ class CssIcssExportDependency extends NullDependency {
const { read } = context;
this.name = read();
this.value = read();
this.reexport = read();
this.range = read();
this.interpolationMode = read();
this.interpolate = read();
this.exportMode = read();
this.exportType = read();
super.deserialize(context);
}
}
@@ -222,17 +347,21 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
nestedDep instanceof CssIcssExportDependency &&
symbol === nestedDep.name
) {
if (nestedDep.reexport) {
return this.findReference(nestedDep.reexport, {
if (Array.isArray(nestedDep.value)) {
return this.findReference(nestedDep.value[1], {
...templateContext,
module
});
}
return CssIcssExportDependency.Template.getIdentifier(nestedDep, {
...templateContext,
module
});
return CssIcssExportDependency.Template.getIdentifier(
nestedDep.value,
nestedDep,
{
...templateContext,
module
}
);
}
}
}
@@ -240,45 +369,30 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
}
/**
* @param {string} value value to identifier
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {DependencyTemplateContext} templateContext the context object
* @returns {string} identifier
*/
static getIdentifier(dependency, templateContext) {
static getIdentifier(value, dependency, templateContext) {
const dep = /** @type {CssIcssExportDependency} */ (dependency);
if (
dep.interpolationMode ===
CssIcssExportDependency.INTERPOLATION_MODE.NAME ||
dep.interpolationMode === CssIcssExportDependency.INTERPOLATION_MODE.VALUE
) {
const { module: m, moduleGraph, runtime } = templateContext;
if (dep.interpolate) {
const { module: m } = templateContext;
const module = /** @type {CssModule} */ (m);
const generator = /** @type {CssGenerator} */ (module.generator);
const names = dep.getExportsConventionNames(
dep.interpolationMode ===
CssIcssExportDependency.INTERPOLATION_MODE.NAME
? dep.name
: dep.value,
const local = cssExportConvention(
value,
/** @type {CssGeneratorExportsConvention} */
(generator.convention)
);
const usedNames =
/** @type {string[]} */
(
names
.map((name) =>
moduleGraph.getExportInfo(module, name).getUsedName(name, runtime)
)
.filter(Boolean)
);
const local = usedNames.length === 0 ? names[0] : usedNames[0];
(generator.options.exportsConvention)
)[0];
const prefix =
/** @type {CssIcssExportDependency & { prefix: string }} */
(dependency).prefix;
dep.exportType === CssIcssExportDependency.EXPORT_TYPE.CUSTOM_VARIABLE
? "--"
: "";
return (
(prefix || "") +
prefix +
getCssParser().escapeIdentifier(
getLocalIdent(
local,
@@ -302,7 +416,7 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
*/
apply(dependency, source, templateContext) {
const dep = /** @type {CssIcssExportDependency} */ (dependency);
if (!dep.range && templateContext.type !== "javascript") return;
if (!dep.range && templateContext.type !== JAVASCRIPT_TYPE) return;
const { cssData } = templateContext;
const { module: m, moduleGraph, runtime } = templateContext;
const module = /** @type {CssModule} */ (m);
@@ -310,7 +424,7 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
const names = dep.getExportsConventionNames(
dep.name,
/** @type {CssGeneratorExportsConvention} */
(generator.convention)
(generator.options.exportsConvention)
);
const usedNames =
/** @type {string[]} */
@@ -323,42 +437,63 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
);
const allNames = new Set([...usedNames, ...names]);
const isReference = Array.isArray(dep.value);
/** @type {string} */
let value;
if (dep.reexport) {
if (isReference && dep.value[2] === true) {
const resolved = CssIcssExportDependencyTemplate.findReference(
dep.reexport,
dep.value[1],
templateContext
);
if (resolved) {
dep.value = resolved;
}
// Fallback to the original name if not found
value = resolved || dep.value[0];
} else {
value = isReference ? dep.value[1] : /** @type {string} */ (dep.value);
}
if (typeof dep.interpolationMode !== "undefined") {
if (dep.interpolate) {
value = CssIcssExportDependencyTemplate.getIdentifier(
value,
dep,
templateContext
);
} else {
value = dep.value;
}
if (templateContext.type === "javascript") {
if (
dep.exportType ===
CssIcssExportDependency.EXPORT_TYPE.GRID_CUSTOM_IDENTIFIER
) {
value += `-${dep.name}`;
}
if (
templateContext.type === JAVASCRIPT_TYPE &&
dep.exportMode !== CssIcssExportDependency.EXPORT_MODE.NONE
) {
for (const used of allNames) {
if (dep.exportMode === 2) {
if (dep.exportMode === CssIcssExportDependency.EXPORT_MODE.ONCE) {
const newValue = getCssParser().unescapeIdentifier(value);
if (isReference) {
cssData.exports.set(dep.value[1], newValue);
}
if (cssData.exports.has(used)) return;
cssData.exports.set(
used,
`${getCssParser().unescapeIdentifier(value)}`
);
cssData.exports.set(used, newValue);
} else {
const originalValue =
dep.exportMode === 0 ? undefined : cssData.exports.get(used);
const newValue = getCssParser().unescapeIdentifier(value);
dep.exportMode === CssIcssExportDependency.EXPORT_MODE.REPLACE
? undefined
: cssData.exports.get(used);
const newValue =
dep.exportMode ===
CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE
? cssData.exports.get(
isReference ? dep.value[0] : /** @type {string} */ (dep.value)
) || value
: getCssParser().unescapeIdentifier(value);
cssData.exports.set(
used,
@@ -368,26 +503,29 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
}
} else if (
dep.range &&
templateContext.type === "css" &&
dep.exportMode !== 1
templateContext.type === CSS_TYPE &&
dep.exportMode !== CssIcssExportDependency.EXPORT_MODE.APPEND &&
dep.exportMode !== CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE
) {
source.replace(dep.range[0], dep.range[1] - 1, value);
}
}
};
/** @type {Record<"REPLACE" | "APPEND" | "ONCE", ExportMode>} */
/** @type {Record<"NONE" | "REPLACE" | "APPEND" | "ONCE" | "SELF_REFERENCE", ExportMode>} */
CssIcssExportDependency.EXPORT_MODE = {
REPLACE: 0,
APPEND: 1,
ONCE: 2
NONE: 0,
REPLACE: 1,
APPEND: 2,
ONCE: 3,
SELF_REFERENCE: 4
};
/** @type {Record<"NONE" | "NAME" | "VALUE", InterpolationMode>} */
CssIcssExportDependency.INTERPOLATION_MODE = {
NONE: 0,
NAME: 1,
VALUE: 2
/** @type {Record<"NORMAL" | "CUSTOM_VARIABLE" | "GRID_CUSTOM_IDENTIFIER", ExportType>} */
CssIcssExportDependency.EXPORT_TYPE = {
NORMAL: 0,
CUSTOM_VARIABLE: 1,
GRID_CUSTOM_IDENTIFIER: 2
};
makeSerializable(