2017-03-14 16:16:15 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 19:08:49 +00:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-03-14 16:16:15 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2017-03-14 16:16:15 +00:00
|
|
|
*/
|
|
|
|
|
|
2017-12-16 19:35:47 +00:00
|
|
|
import {SourceMap} from '../../index';
|
2021-09-25 15:26:04 +00:00
|
|
|
import {SourceMapConsumer} from 'source-map';
|
2017-03-14 16:16:15 +00:00
|
|
|
|
|
|
|
|
export interface SourceLocation {
|
2022-07-19 08:20:11 +00:00
|
|
|
line: number | null;
|
|
|
|
|
column: number | null;
|
|
|
|
|
source: string | null;
|
2017-03-14 16:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 08:20:11 +00:00
|
|
|
export async function originalPositionFor(
|
|
|
|
|
sourceMap: SourceMap,
|
|
|
|
|
genPosition: {line: number; column: number},
|
|
|
|
|
): Promise<SourceLocation> {
|
2021-09-25 15:26:04 +00:00
|
|
|
// Note: The `SourceMap` type from the compiler is different to `RawSourceMap`
|
|
|
|
|
// from the `source-map` package, but the method we rely on works as expected.
|
2022-07-19 08:20:11 +00:00
|
|
|
const smc = await new SourceMapConsumer(sourceMap as any);
|
2017-03-14 16:16:15 +00:00
|
|
|
// Note: We don't return the original object as it also contains a `name` property
|
|
|
|
|
// which is always null and we don't want to include that in our assertions...
|
|
|
|
|
const {line, column, source} = smc.originalPositionFor(genPosition);
|
|
|
|
|
return {line, column, source};
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 16:59:58 +00:00
|
|
|
export function extractSourceMap(source: string): SourceMap | null {
|
2017-03-14 16:16:15 +00:00
|
|
|
let idx = source.lastIndexOf('\n//#');
|
|
|
|
|
if (idx == -1) return null;
|
2019-02-08 22:10:20 +00:00
|
|
|
const smComment = source.slice(idx).split('\n', 2)[1].trim();
|
2017-03-14 16:16:15 +00:00
|
|
|
const smB64 = smComment.split('sourceMappingURL=data:application/json;base64,')[1];
|
2023-12-09 01:29:17 +00:00
|
|
|
return smB64 ? (JSON.parse(Buffer.from(smB64, 'base64').toString()) as SourceMap) : null;
|
2017-10-24 11:54:08 +00:00
|
|
|
}
|