fix: Replace deprecated url.parse() and add lint rule to ban it (#14644)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-18 21:38:46 -05:00
1 parent 072c7c537f
commit a95a970fcb
3 files changed
+83 -5

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
/**
* Local oxlint plugin with rules for Node.js built-in modules.
*/
const urlModules = new Set(["url", "node:url"]);
/** Returns true if the member expression accesses the given property name. */
function isProperty(node, name) {
if (node.type !== "MemberExpression") {
return false;
}
if (!node.computed) {
return node.property.type === "Identifier" && node.property.name === name;
}
return node.property.type === "Literal" && node.property.value === name;
}
/** Returns the name of an import specifier's imported binding. */
function importedName(specifier) {
return specifier.imported.type === "Identifier"
? specifier.imported.name
: specifier.imported.value;
}
const plugin = {
meta: {
name: "node-local",
},
rules: {
"no-url-parse": {
meta: {
type: "problem",
docs: {
description:
"Disallow the legacy `url.parse()`, which is deprecated (DEP0169).",
},
messages: {
forbidden:
"`url.parse()` is deprecated and not standardized. Use the WHATWG `URL` API instead.",
},
},
create(context) {
const moduleNames = new Set();
return {
ImportDeclaration(node) {
if (!urlModules.has(node.source.value)) {
return;
}
for (const specifier of node.specifiers) {
if (specifier.type === "ImportSpecifier") {
if (importedName(specifier) === "parse") {
context.report({ node: specifier, messageId: "forbidden" });
}
continue;
}
moduleNames.add(specifier.local.name);
}
},
MemberExpression(node) {
if (
node.object.type === "Identifier" &&
moduleNames.has(node.object.name) &&
isProperty(node, "parse")
) {
context.report({ node, messageId: "forbidden" });
}
},
};
},
},
},
};
export default plugin;
+3 -1
View File
@@ -17,7 +17,8 @@
"./.oxlint/editor.mjs",
"./.oxlint/koa.mjs",
"./.oxlint/react-router.mjs",
"./.oxlint/classes.mjs"
"./.oxlint/classes.mjs",
"./.oxlint/node.mjs"
],
"plugins": ["eslint", "typescript", "unicorn", "oxc", "vitest"],
"rules": {
@@ -41,6 +42,7 @@
"koa/no-request-origin": "error",
"react-router/no-deferred-use-location": "error",
"classes/no-private-identifier": "error",
"node-local/no-url-parse": "error",
"for-direction": "error",
"no-async-promise-executor": "error",
"no-case-declarations": "error",
+2 -4
View File
@@ -1,7 +1,6 @@
import type { IncomingMessage } from "node:http";
import type http from "node:http";
import type { Duplex } from "node:stream";
import url from "node:url";
import { Redis } from "@hocuspocus/extension-redis";
import { Throttle } from "@hocuspocus/extension-throttle";
import { Server } from "@hocuspocus/server";
@@ -78,9 +77,8 @@ export default function init(
function (req: IncomingMessage, socket: Duplex, head: Buffer) {
if (req.url?.startsWith(path)) {
// parse document id and close connection if not present in request
const documentId = url
.parse(req.url)
.pathname?.replace(path, "")
const documentId = new URL(req.url, "http://localhost").pathname
.replace(path, "")
.split("/")
.pop();