Skip to content

feat: handle DEP0195: Instantiating node:http classes without new #174

@AugustinMauroy

Description

@AugustinMauroy

Description

This codemod should add new keyword when instantiating http classes without it. It's useful to migrate code that calls class constructors directly instead of using proper JavaScript class instantiation.

It should add new keyword when instantiating http classes without it. It should handle all http classes like OutgoingMessage, IncomingMessage, ServerResponse, and ClientRequest. It should handle both CommonJS and ESM imports. It should preserve any constructor arguments and assignments.

Examples

Case 1

Before:

const http = require("node:http");
const message = http.OutgoingMessage();

After:

const http = require("node:http");
const message = new http.OutgoingMessage();

Case 2

Before:

const http = require("node:http");
const response = http.ServerResponse(socket);

After:

const http = require("node:http");
const response = new http.ServerResponse(socket);

Case 3

Before:

import { IncomingMessage, ClientRequest } from "node:http";
const incoming = IncomingMessage(socket);
const request = ClientRequest(options);

After:

import { IncomingMessage, ClientRequest } from "node:http";
const incoming = new IncomingMessage(socket);
const request = new ClientRequest(options);

Case 4

Before:

const http = require("node:http");
function createMessage(socket) {
  return http.IncomingMessage(socket);
}

After:

const http = require("node:http");
function createMessage(socket) {
  return new http.IncomingMessage(socket);
}

REFS

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    🏗 In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions