Skip to content

What is the expected return of the readMap arguments when the source map doesn't exist? #82

Description

@ericmorand-sonarsource

Methods fromMapFileComment and fromMapFileSource take a readMap function as second argument, that is expected to return either a string or a Promise of a string. But it is not clear how this method is supposed to behave when the passed file doesn't exist?

Should it throw? Or return null?

Activity

  1. prantlf commented on Nov 9, 2025

    @prantlf
    Contributor

    It'll depend on what you want. Usually you'd want to throw an error:

    function readMap(fileName) {
      if (file does not exist) {
        throw an error
      }
      read the file content and return it
    }

    Of you could fall back to empty or other source map:

    function readMap(fileName) {
      if (file does not exist) {
        return an empty or other source map
      }
      read the file content and return it
    }

    For example, if you just call the built-in Node.js function, you'll get an error thrown with the message from Node.js by default:

    import { readFile } from 'node:fs/promises'
    
    function readMap(fileName) {
      return readFile(fileName, 'utf8')
    }

    If you return null, you'll be sorry :-) It'll crash somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions