Skip to content

feat: handle DEP0187: Passing invalid argument types to fs.existsSync #178

@AugustinMauroy

Description

@AugustinMauroy

Description

This codemod should validate and convert invalid argument types to fs.existsSync(). It's useful to migrate code that passes invalid argument types which now causes deprecation warnings or errors.

It should validate that fs.existsSync() receives string, Buffer, or URL arguments only. It should convert invalid argument types to valid ones where possible. It should handle both CommonJS and ESM imports. It should add type checks or conversions to ensure argument validity.

Examples

Case 1

Before:

const fs = require("node:fs");

const exists = fs.existsSync(123);

After:

const fs = require("node:fs");

const exists = fs.existsSync(String(123));

Case 2

Before:

const fs = require("node:fs");

function checkFile(path) {
  return fs.existsSync(path);
}

After:

const fs = require("node:fs");

function checkFile(path) {
  if (typeof path !== 'string' && !Buffer.isBuffer(path) && !(path instanceof URL)) {
    path = String(path);
  }
  return fs.existsSync(path);
}

Case 3

Before:

const fs = require("node:fs");

const fileExists = fs.existsSync(null);

After:

const fs = require("node:fs");

const fileExists = fs.existsSync(String(null || ''));

Case 4

Before:

import { existsSync } from "node:fs";

const exists = existsSync({ path: '/some/file' });

After:

import { existsSync } from "node:fs";

const exists = existsSync(String({ path: '/some/file' }));

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