Commit 77c58e04 authored by Julien Puydt's avatar Julien Puydt

Update upstream source from tag 'upstream/1.4.1'

Update to upstream version '1.4.1'
with Debian dir cdddfc25c64c330c550c2b3f55346588f341063e
parents 3b8ba2c3 a71f17a4
# sourcemap-codec changelog
## 1.4.1
* GO FASTER ([#71](https://github.com/Rich-Harris/sourcemap-codec/pull/71))
## 1.4.0
* Add TypeScript declarations ([#70](https://github.com/Rich-Harris/sourcemap-codec/pull/70))
......
{
"name": "sourcemap-codec",
"version": "1.4.0",
"version": "1.4.1",
"description": "Encode/decode sourcemap mappings",
"main": "dist/sourcemap-codec.umd.js",
"module": "dist/sourcemap-codec.es.js",
......@@ -30,9 +30,7 @@
"url": "https://github.com/Rich-Harris/sourcemap-codec/issues"
},
"homepage": "https://github.com/Rich-Harris/sourcemap-codec",
"dependencies": {
"vlq": "^1.0.0"
},
"dependencies": {},
"devDependencies": {
"codecov.io": "^0.1.6",
"console-group": "^0.3.3",
......
import { decode as decodeVlq, encode as encodeVlq } from 'vlq';
export type SourceMapSegment = [number] | [number, number, number, number] | [number, number, number, number, number];
export type SourceMapSegment =
| [number]
| [number, number, number, number]
| [number, number, number, number, number];
export type SourceMapLine = SourceMapSegment[];
export type SourceMapMappings = SourceMapLine[];
function decodeSegments ( encodedSegments: string[] ): number[][] {
let i = encodedSegments.length;
const segments = new Array<number[]>( i );
const charToInteger: { [charCode: number]: number } = {};
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
while ( i-- ) segments[i] = decodeVlq( encodedSegments[i] );
return segments;
for (let i = 0; i < chars.length; i++) {
charToInteger[chars.charCodeAt(i)] = i;
}
export function decode ( mappings: string ): SourceMapMappings {
let sourceFileIndex = 0; // second field
let sourceCodeLine = 0; // third field
let sourceCodeColumn = 0; // fourth field
let nameIndex = 0; // fifth field
const lines = mappings.split( ';' );
const numLines = lines.length;
const decoded = new Array<SourceMapLine>( numLines );
let i: number;
let j: number;
let line: string;
let generatedCodeColumn: number;
let decodedLine: SourceMapLine;
let segments: number[][];
let segment: number[];
let result: SourceMapSegment;
for ( i = 0; i < numLines; i += 1 ) {
line = lines[i];
export function decode(mappings: string): SourceMapMappings {
let generatedCodeColumn = 0; // first field
let sourceFileIndex = 0; // second field
let sourceCodeLine = 0; // third field
let sourceCodeColumn = 0; // fourth field
let nameIndex = 0; // fifth field
const decoded: SourceMapMappings = [];
let line: SourceMapLine = [];
let segment: number[] = [];
for (let i = 0, j = 0, shift = 0, value = 0; i < mappings.length; i++) {
const c = mappings.charCodeAt(i);
if (c === 44) { // ","
if (segment.length) line.push(<SourceMapSegment>segment);
segment = [];
j = 0;
} else if (c === 59) { // ";"
if (segment.length) line.push(<SourceMapSegment>segment);
segment = [];
j = 0;
decoded.push(line);
line = [];
generatedCodeColumn = 0;
} else {
let integer = charToInteger[c];
if (integer === undefined) {
throw new Error('Invalid character (' + String.fromCharCode(c) + ')');
}
generatedCodeColumn = 0; // first field - reset each time
decodedLine = [];
const hasContinuationBit = integer & 32;
segments = decodeSegments( line.split( ',' ) );
integer &= 31;
value += integer << shift;
for ( j = 0; j < segments.length; j += 1 ) {
segment = segments[j];
if (hasContinuationBit) {
shift += 5;
} else {
const shouldNegate = value & 1;
value >>= 1;
if ( !segment.length ) {
break;
}
const num = shouldNegate ? -value : value;
generatedCodeColumn += segment[0];
if (j == 0) {
generatedCodeColumn += num;
segment.push(generatedCodeColumn);
result = [ generatedCodeColumn ];
decodedLine.push( result );
} else if (j === 1) {
sourceFileIndex += num;
segment.push(sourceFileIndex);
if ( segment.length === 1 ) {
// only one field!
continue;
}
} else if (j === 2) {
sourceCodeLine += num;
segment.push(sourceCodeLine);
sourceFileIndex += segment[1];
sourceCodeLine += segment[2];
sourceCodeColumn += segment[3];
} else if (j === 3) {
sourceCodeColumn += num;
segment.push(sourceCodeColumn);
result.push( sourceFileIndex, sourceCodeLine, sourceCodeColumn );
} else if (j === 4) {
nameIndex += num;
segment.push(nameIndex);
}
if ( segment.length === 5 ) {
nameIndex += segment[4];
result.push( nameIndex );
j++;
value = shift = 0; // reset
}
}
decoded[i] = decodedLine;
}
if (segment.length) line.push(<SourceMapSegment>segment);
decoded.push(line);
return decoded;
}
export function encode ( decoded: SourceMapMappings ): string {
const offsets = {
generatedCodeColumn: 0,
sourceFileIndex: 0, // second field
sourceCodeLine: 0, // third field
sourceCodeColumn: 0, // fourth field
nameIndex: 0 // fifth field
};
return decoded.map( line => {
offsets.generatedCodeColumn = 0; // first field - reset each time
return line.map( encodeSegment ).join( ',' );
}).join( ';' );
function encodeSegment ( segment: SourceMapSegment ): string {
if ( !segment.length ) {
return '';
}
export function encode(decoded: SourceMapMappings): string {
let sourceFileIndex = 0; // second field
let sourceCodeLine = 0; // third field
let sourceCodeColumn = 0; // fourth field
let nameIndex = 0; // fifth field
let mappings = '';
const result = new Array<number>( segment.length );
for (let i = 0; i < decoded.length; i++) {
const line = decoded[i];
if (i > 0) mappings += ';';
if (line.length === 0) continue;
result[0] = segment[0] - offsets.generatedCodeColumn;
offsets.generatedCodeColumn = segment[0];
let generatedCodeColumn = 0; // first field
if ( segment.length === 1 ) {
// only one field!
return encodeVlq( result );
}
const lineMappings: string[] = [];
for (const segment of line) {
let segmentMappings = encodeInteger(segment[0] - generatedCodeColumn);
generatedCodeColumn = segment[0];
result[1] = segment[1] - offsets.sourceFileIndex;
result[2] = segment[2] - offsets.sourceCodeLine;
result[3] = segment[3] - offsets.sourceCodeColumn;
if (segment.length > 1) {
segmentMappings +=
encodeInteger(segment[1] - sourceFileIndex) +
encodeInteger(segment[2] - sourceCodeLine) +
encodeInteger(segment[3] - sourceCodeColumn);
offsets.sourceFileIndex = segment[1];
offsets.sourceCodeLine = segment[2];
offsets.sourceCodeColumn = segment[3];
sourceFileIndex = segment[1];
sourceCodeLine = segment[2];
sourceCodeColumn = segment[3];
}
if (segment.length === 5) {
segmentMappings += encodeInteger(segment[4] - nameIndex);
nameIndex = segment[4];
}
if ( segment.length === 5 ) {
result[4] = segment[4] - offsets.nameIndex;
offsets.nameIndex = segment[4];
lineMappings.push(segmentMappings);
}
return encodeVlq( result );
mappings += lineMappings.join(',');
}
return mappings;
}
function encodeInteger(num: number): string {
var result = '';
num = num < 0 ? (-num << 1) | 1 : num << 1;
do {
var clamped = num & 31;
num >>= 5;
if (num > 0) {
clamped |= 32;
}
result += chars[clamped];
} while (num > 0);
return result;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment