angular/packages/compiler/testing/src/output/source_map_util.ts
George Kalpakas 00c110b055 build: upgrade jasmine (and related typings) to latest version (#19904)
With these changes, the types are a little stricter now and also not
compatible with Protractor's jasmine-like syntax. So, we have to also
use `@types/jasminewd2` for e2e tests (but not for non-e2e tests).

I also had to "augment" `@types/jasminewd2`, because the latest
typings from [DefinitelyTyped][1] do not reflect the fact that the
`jasminewd2` version (v2.1.0) currently used by Protractor supports
passing a `done` callback to a spec.

[1]: 566e039485/types/jasminewd2/index.d.ts (L9-L15)

Fixes #23952
Closes #24733

PR Close #19904
2018-07-06 13:48:02 -07:00

40 lines
1.3 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {SourceMap} from '@angular/compiler';
const b64 = require('base64-js');
const SourceMapConsumer = require('source-map').SourceMapConsumer;
export interface SourceLocation {
line: number;
column: number;
source: string;
}
export function originalPositionFor(
sourceMap: SourceMap,
genPosition: {line: number | null, column: number | null}): SourceLocation {
const smc = new SourceMapConsumer(sourceMap);
// 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};
}
export function extractSourceMap(source: string): SourceMap|null {
let idx = source.lastIndexOf('\n//#');
if (idx == -1) return null;
const smComment = source.slice(idx).trim();
const smB64 = smComment.split('sourceMappingURL=data:application/json;base64,')[1];
return smB64 ? JSON.parse(decodeB64String(smB64)) : null;
}
function decodeB64String(s: string): string {
return b64.toByteArray(s).reduce((s: string, c: number) => s + String.fromCharCode(c), '');
}