From ba2e4dc866f8bc9912b05ff60d0edf60f4337646 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 6 Jul 2018 17:29:42 -0700 Subject: [PATCH] PR Feedback --- src/compiler/emitter.ts | 10 ++-------- src/compiler/sourcemap.ts | 9 +++++---- src/compiler/sourcemapDecoder.ts | 16 +++------------- src/compiler/types.ts | 6 +++--- src/services/sourcemaps.ts | 12 +++--------- 5 files changed, 16 insertions(+), 37 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 29a1077815a..87b425f6ed4 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -461,7 +461,7 @@ namespace ts { emitSyntheticTripleSlashReferencesIfNeeded(bundle); for (const prepend of bundle.prepends) { - writeSignificantLine(); + writeLine(); print(EmitHint.Unspecified, prepend, /*sourceFile*/ undefined); } @@ -2613,7 +2613,7 @@ namespace ts { // generated line. Until we can rework the source map support in the test // harness, ensure each source file is on a new line, even when using a // whitespace-removing writer. - writeSignificantLine(); + writeLine(); const statements = node.statements; if (emitBodyWithDetachedComments) { // Emit detached comment if there are no prologue directives or if the first node is synthesized. @@ -3111,12 +3111,6 @@ namespace ts { writer.writeProperty(s); } - function writeSignificantLine() { - // writer.flush(); - // if (!writer.isAtStartOfLine()) writer.rawWrite(newLine); - writeLine(); - } - function writeLine() { writer.writeLine(); } diff --git a/src/compiler/sourcemap.ts b/src/compiler/sourcemap.ts index db474f6049a..88d09ae6915 100644 --- a/src/compiler/sourcemap.ts +++ b/src/compiler/sourcemap.ts @@ -655,12 +655,12 @@ namespace ts { && typeof x.file === "string" && typeof x.mappings === "string" && isArray(x.sources) && every(x.sources, isString) - && (x.sourceRoot === undefined || typeof x.sourceRoot === "string") - && (x.sourcesContent === undefined || isArray(x.sourcesContent) && every(x.sourcesContent, isStringOrNull)) - && (x.names === undefined || isArray(x.names) && every(x.names, isString)); + && (x.sourceRoot === undefined || x.sourceRoot === null || typeof x.sourceRoot === "string") + && (x.sourcesContent === undefined || x.sourcesContent === null || isArray(x.sourcesContent) && every(x.sourcesContent, isStringOrNull)) + && (x.names === undefined || x.names === null || isArray(x.names) && every(x.names, isString)); } - function tryParseRawSourceMap(text: string) { + export function tryParseRawSourceMap(text: string) { try { const parsed = JSON.parse(text); if (isRawSourceMap(parsed)) { @@ -673,6 +673,7 @@ namespace ts { return undefined; } + const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function base64FormatEncode(inValue: number) { diff --git a/src/compiler/sourcemapDecoder.ts b/src/compiler/sourcemapDecoder.ts index 4248ac21829..5dae6dbd3b7 100644 --- a/src/compiler/sourcemapDecoder.ts +++ b/src/compiler/sourcemapDecoder.ts @@ -30,16 +30,6 @@ namespace ts { /* @internal */ namespace ts.sourcemaps { - export interface SourceMapData { - version?: number; - file?: string; - sourceRoot?: string; - sources: string[]; - sourcesContent?: (string | null)[]; - names?: string[]; - mappings: string; - } - export interface SourceMappableLocation { fileName: string; position: number; @@ -59,7 +49,7 @@ namespace ts.sourcemaps { log(text: string): void; } - export function decode(host: SourceMapDecodeHost, mapPath: string, map: SourceMapData, program?: Program, fallbackCache = createSourceFileLikeCache(host)): SourceMapper { + export function decode(host: SourceMapDecodeHost, mapPath: string, map: RawSourceMap, program?: Program, fallbackCache = createSourceFileLikeCache(host)): SourceMapper { const currentDirectory = getDirectoryPath(mapPath); const sourceRoot = map.sourceRoot || currentDirectory; let decodedMappings: ProcessedSourceMapPosition[]; @@ -156,7 +146,7 @@ namespace ts.sourcemaps { } /*@internal*/ - export function decodeMappings(map: SourceMapData): MappingsDecoder { + export function decodeMappings(map: RawSourceMap): MappingsDecoder { const state: DecoderState = { encodedText: map.mappings, currentNameIndex: undefined, @@ -190,7 +180,7 @@ namespace ts.sourcemaps { }; } - function calculateDecodedMappings(map: SourceMapData, processPosition: (position: RawSourceMapPosition) => T, host?: { log?(s: string): void }): T[] { + function calculateDecodedMappings(map: RawSourceMap, processPosition: (position: RawSourceMapPosition) => T, host?: { log?(s: string): void }): T[] { const decoder = decodeMappings(map); const positions = arrayFrom(decoder, processPosition); if (decoder.error) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 95dada4d2ef..32a64ee97fd 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5293,11 +5293,11 @@ namespace ts { export interface RawSourceMap { version: 3; file: string; - sourceRoot?: string; + sourceRoot?: string | null; sources: string[]; - sourcesContent?: (string | null)[]; + sourcesContent?: (string | null)[] | null; mappings: string; - names?: string[]; + names?: string[] | null; } /** diff --git a/src/services/sourcemaps.ts b/src/services/sourcemaps.ts index f617d879893..be825564a77 100644 --- a/src/services/sourcemaps.ts +++ b/src/services/sourcemaps.ts @@ -41,14 +41,8 @@ namespace ts { } function convertDocumentToSourceMapper(file: { sourceMapper?: sourcemaps.SourceMapper }, contents: string, mapFileName: string) { - let maps: sourcemaps.SourceMapData | undefined; - try { - maps = JSON.parse(contents); - } - catch { - // swallow error - } - if (!maps || !maps.sources || !maps.file || !maps.mappings) { + const map = tryParseRawSourceMap(contents); + if (!map || !map.sources || !map.file || !map.mappings) { // obviously invalid map return file.sourceMapper = sourcemaps.identitySourceMapper; } @@ -57,7 +51,7 @@ namespace ts { fileExists: s => host.fileExists!(s), // TODO: GH#18217 getCanonicalFileName, log, - }, mapFileName, maps, getProgram(), sourcemappedFileCache); + }, mapFileName, map, getProgram(), sourcemappedFileCache); } function getSourceMapper(fileName: string, file: SourceFileLike): sourcemaps.SourceMapper {