Skip to content

Commit

Permalink
Refine handling of null sources in various places
Browse files Browse the repository at this point in the history
When a source is null, it won't be included in the file map and checks that use
a file won't be run. In the case that a sourcesContent is provided, it would
potentially be better to treat that as an anonymous file but we currently don't.

Also check for the string "null" in some cases, as the underlying source maps
library can return this instead of a null.
  • Loading branch information
takikawa committed May 24, 2024
1 parent 6384c33 commit 6680960
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/util/collectSourceFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import type { SourceMap } from "./sourceMap.js";
export function collectSourceFiles(sourceMap: SourceMap, originalFolderPath: string) {
const filesMap = new Map<string, TestingFile>();

sourceMap.sources.forEach((file: string, index: number) => {
filesMap.set(file, TestingFile.forTextFile(path.join(originalFolderPath, file), sourceMap.sourcesContent ? sourceMap.sourcesContent[index] : null))
sourceMap.sources.forEach((file: string | null, index: number) => {
// When the source is null, it might make sense to map to an anonymous source
// if a sourceContent is present, but for now just don't add it to the set.
if (file !== null)
filesMap.set(file, TestingFile.forTextFile(path.join(originalFolderPath, file), sourceMap.sourcesContent ? sourceMap.sourcesContent[index] : null))
});

return filesMap;
Expand Down
8 changes: 6 additions & 2 deletions src/validators/SourceFilesValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export class SourceFilesValidator extends Validator {
function check(sources : any) {
sources.forEach((sourceFileName: string | null, index: number) => {
const fullPath = sourceFileName === null ? null : path.join(context.originalFolderPath, sourceFileName);
if ((!fullPath || !fs.existsSync(fullPath)) && sourcesContent[index] == undefined) {
errors.push(new Error(`Source file not found: ${sourceFileName} ${sourcesContent[index]}`));
// If the path is null, we won't use the source in subsequent passes so
// it can be ignored. Otherwise ensure the source makes sense.
if (fullPath !== null) {
if ((!fullPath || !fs.existsSync(fullPath)) && sourcesContent[index] == undefined) {
errors.push(new Error(`Source file not found: ${sourceFileName} ${sourcesContent[index]}`));
}
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/validators/SourceMapMappingsValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class SourceMapMappingsValidator extends Validator {
await SourceMapConsumer.with(sourceMap, null, (consumer) => {
consumer.eachMapping((mapping) => {
// Is this a valid situation following the spec?
if (mapping.source === null) return
if (mapping.source === null || mapping.source === "null") return
const originalFile = originalFiles.get(mapping.source);

if (originalFile === undefined) {
Expand Down

0 comments on commit 6680960

Please sign in to comment.