From c003df53ab1aba724cdd6a7566302f72d0df97d7 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 23 Aug 2018 21:07:01 -0700 Subject: [PATCH] Integrate ScriptSnapshot into ModuleMetaData --- js/compiler.ts | 36 +++++++++++++++--------------------- js/compiler_test.ts | 7 +++++++ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/js/compiler.ts b/js/compiler.ts index fcbfc7c5ef..93cfd34c19 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -57,9 +57,8 @@ export interface Ts { * Named `ModuleMetaData` to clarify it is just a representation of meta data of * the module, not the actual module instance. */ -export class ModuleMetaData { +export class ModuleMetaData implements ts.IScriptSnapshot { public readonly exports = {}; - public scriptSnapshot?: ts.IScriptSnapshot; public scriptVersion = ""; constructor( @@ -71,6 +70,19 @@ export class ModuleMetaData { this.scriptVersion = "1"; } } + + public getText(start: number, end: number): string { + return this.sourceCode.substring(start, end); + } + + public getLength(): number { + return this.sourceCode.length; + } + + public getChangeRange(): undefined { + // Required `IScriptSnapshot` API, but not implemented/needed in deno + return undefined; + } } /** @@ -478,25 +490,7 @@ export class DenoCompiler implements ts.LanguageServiceHost { getScriptSnapshot(fileName: ModuleFileName): ts.IScriptSnapshot | undefined { this._log("getScriptSnapshot()", fileName); - const moduleMetaData = this._getModuleMetaData(fileName); - if (moduleMetaData) { - return ( - moduleMetaData.scriptSnapshot || - (moduleMetaData.scriptSnapshot = { - getText(start, end) { - return moduleMetaData.sourceCode.substring(start, end); - }, - getLength() { - return moduleMetaData.sourceCode.length; - }, - getChangeRange() { - return undefined; - } - }) - ); - } else { - return undefined; - } + return this._getModuleMetaData(fileName); } getCurrentDirectory(): string { diff --git a/js/compiler_test.ts b/js/compiler_test.ts index cc3cda4062..8960112de4 100644 --- a/js/compiler_test.ts +++ b/js/compiler_test.ts @@ -436,7 +436,14 @@ test(function compilerGetScriptSnapshot() { "Expected .getText() to equal 'import'" ); assertEqual(result.getChangeRange(result), undefined); + // This is and optional part of the `IScriptSnapshot` API which we don't + // define, os checking for the lack of this property. assert(!("dispose" in result)); + + assert( + result === moduleMetaData, + "result should strictly equal moduleMetaData" + ); }); test(function compilerGetCurrentDirectory() {