Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions tools/sourcemap-tools/src/SourceProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Err, Ok, R, ResultPromise } from './models/Result';
export interface ProcessResult {
readonly debugId: string;
readonly source: string;
readonly sourceMap: RawSourceMapWithDebugId;
readonly sourceMap?: RawSourceMapWithDebugId;
}

export interface ProcessResultWithPaths extends ProcessResult {
Expand Down Expand Up @@ -82,18 +82,47 @@ export class SourceProcessor {
);
}

/**
* Adds required snippets and comments to source
* @param source Source content.
* @param debugId Debug ID. If not provided, one will be generated from `source`.
* @param force Force adding changes.
* @returns Used debug ID, new source and new sourcemap.
*/
public async processSource(source: string, debugId?: string, force?: boolean) {
return await this.processSourceAndAvailableSourceMap(source, undefined, debugId, force);
}

/**
* Adds required snippets and comments to source, and modifies sourcemap to include debug ID.
* @param source Source content.
* @param sourceMap Sourcemap object or JSON.
* @param debugId Debug ID. If not provided, one will be generated from `source`.
* @param force Force adding changes.
* @returns Used debug ID, new source and new sourcemap.
*/
public async processSourceAndSourceMap(
source: string,
sourceMap: RawSourceMap,
debugId?: string,
force?: boolean,
): Promise<ProcessResult> {
return await this.processSourceAndAvailableSourceMap(source, sourceMap, debugId, force);
}

/**
* Adds required snippets and comments to source, and modifies sourcemap to include debug ID if available.
* @param source Source content.
* @param sourceMap Sourcemap object or JSON.
* @param debugId Debug ID. If not provided, one will be generated from `source`.
* @param force Force adding changes.
* @returns Used debug ID, new source and new sourcemap.
*/
private async processSourceAndAvailableSourceMap(
source: string,
sourceMap?: RawSourceMap,
debugId?: string,
force?: boolean,
): Promise<ProcessResult> {
const sourceDebugId = this.getSourceDebugId(source);
if (!debugId) {
Expand All @@ -116,21 +145,26 @@ export class SourceProcessor {
? shebang + sourceSnippet + '\n' + source.substring(shebang.length)
: sourceSnippet + '\n' + source;

// We need to offset the source map by amount of lines that we're inserting to the source code
// Sourcemaps map code like this:
// original code X:Y => generated code A:B
// So if we add any code to generated code, mappings after that code will become invalid
// We need to offset the mapping lines by sourceSnippetNewlineCount:
// original code X:Y => generated code (A + sourceSnippetNewlineCount):B
const sourceSnippetNewlineCount = sourceSnippet.match(/\n/g)?.length ?? 0;
offsetSourceMap = await this.offsetSourceMap(sourceMap, sourceSnippetNewlineCount + 1);
if (sourceMap) {
// We need to offset the source map by amount of lines that we're inserting to the source code
// Sourcemaps map code like this:
// original code X:Y => generated code A:B
// So if we add any code to generated code, mappings after that code will become invalid
// We need to offset the mapping lines by sourceSnippetNewlineCount:
// original code X:Y => generated code (A + sourceSnippetNewlineCount):B
const sourceSnippetNewlineCount = sourceSnippet.match(/\n/g)?.length ?? 0;
offsetSourceMap = await this.offsetSourceMap(sourceMap, sourceSnippetNewlineCount + 1);
}
}

if (force || !sourceDebugId || !this._debugIdGenerator.hasCommentSnippet(source, debugId)) {
const sourceComment = this._debugIdGenerator.generateSourceComment(debugId);
newSource = appendBeforeWhitespaces(newSource, '\n' + sourceComment);
}

if (!sourceMap) {
return { debugId, source: newSource };
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why it doesn't show an error here? ProcessResult expects sourceMap to be set.

Can you add a new interface called ProcessResultWithoutSourcemap (this will reduce refactoring in other packages) and annotate this function like this:

    public async processSourceAndSourceMap(
        source: string,
        sourceMap: RawSourceMap,
        debugId?: string,
        force?: boolean,
    ): Promise<ProcessResult>;
    public async processSourceAndSourceMap(
        source: string,
        sourceMap?: undefined,
        debugId?: string,
        force?: boolean,
    ): Promise<ProcessResultWithoutSourcemap>;
    public async processSourceAndSourceMap(
        source: string,
        sourceMap?: RawSourceMap,
        debugId?: string,
        force?: boolean,
    ): Promise<ProcessResult> {

Also, change the return type on processSource to return Promise<ProcessResultWithoutSourcemap>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it DOES return an error!
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But not on build... strange.

const newSourceMap = this._debugIdGenerator.addSourceMapDebugId(offsetSourceMap ?? sourceMap, debugId);
return { debugId, source: newSource, sourceMap: newSourceMap };
}
Expand Down
Loading