From b8f08e5e7e839924552a9c4ef598bbbc39603a56 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Sun, 28 Aug 2022 07:13:53 +0800 Subject: [PATCH] lib: codify findSourceMap return value when not found Return `undefined` when no source map is found for the given filename on `findSourceMap`. PR-URL: https://github.com/nodejs/node/pull/44397 Fixes: https://github.com/nodejs/node/issues/44391 Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel --- doc/api/module.md | 3 ++- lib/internal/source_map/prepare_stack_trace.js | 2 +- lib/internal/source_map/source_map_cache.js | 2 +- test/parallel/test-source-map-api.js | 13 +++++++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/doc/api/module.md b/doc/api/module.md index 16938ffc317e81..31faaa93a88b5e 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -160,7 +160,8 @@ added: --> * `path` {string} -* Returns: {module.SourceMap} +* Returns: {module.SourceMap|undefined} Returns `module.SourceMap` if a source + map is found, `undefined` otherwise. `path` is the resolved path for the file for which a corresponding source map should be fetched. diff --git a/lib/internal/source_map/prepare_stack_trace.js b/lib/internal/source_map/prepare_stack_trace.js index 7f184545cd8843..2052651765265a 100644 --- a/lib/internal/source_map/prepare_stack_trace.js +++ b/lib/internal/source_map/prepare_stack_trace.js @@ -195,7 +195,7 @@ function getOriginalSource(payload, originalSourcePath) { function getSourceMapErrorSource(fileName, lineNumber, columnNumber) { const sm = findSourceMap(fileName); - if (sm === null) { + if (sm === undefined) { return; } const { diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index ba79540d837849..60852a4f439037 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -297,7 +297,7 @@ function findSourceMap(sourceURL) { if (sourceMap && sourceMap.data) { return new SourceMap(sourceMap.data); } - return null; + return undefined; } module.exports = { diff --git a/test/parallel/test-source-map-api.js b/test/parallel/test-source-map-api.js index f66a9af94784c7..03c892419be94c 100644 --- a/test/parallel/test-source-map-api.js +++ b/test/parallel/test-source-map-api.js @@ -21,6 +21,19 @@ const { readFileSync } = require('fs'); ); } +// `findSourceMap()` should return undefined when no source map is found. +{ + const files = [ + __filename, + '', + 'invalid-file', + ]; + for (const file of files) { + const sourceMap = findSourceMap(file); + assert.strictEqual(sourceMap, undefined); + } +} + // findSourceMap() can lookup source-maps based on URIs, in the // non-exceptional case. {