mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
Cleanup compiler and re-enable tests (#1512)
This commit is contained in:
parent
8039e2a55a
commit
de9c67a702
4 changed files with 124 additions and 299 deletions
|
@ -5,9 +5,6 @@ import { assetSourceCode } from "./assets";
|
||||||
import * as os from "./os";
|
import * as os from "./os";
|
||||||
import { assert, log, notImplemented } from "./util";
|
import { assert, log, notImplemented } from "./util";
|
||||||
|
|
||||||
// tslint:disable-next-line:no-circular-imports
|
|
||||||
// import * as deno from "./deno";
|
|
||||||
|
|
||||||
const EOL = "\n";
|
const EOL = "\n";
|
||||||
const ASSETS = "$asset$";
|
const ASSETS = "$asset$";
|
||||||
const LIB_RUNTIME = "lib.deno_runtime.d.ts";
|
const LIB_RUNTIME = "lib.deno_runtime.d.ts";
|
||||||
|
@ -38,7 +35,6 @@ type SourceMap = string;
|
||||||
|
|
||||||
/** Abstraction of the APIs required from the `os` module so they can be
|
/** Abstraction of the APIs required from the `os` module so they can be
|
||||||
* easily mocked.
|
* easily mocked.
|
||||||
* @internal
|
|
||||||
*/
|
*/
|
||||||
export interface Os {
|
export interface Os {
|
||||||
codeCache: typeof os.codeCache;
|
codeCache: typeof os.codeCache;
|
||||||
|
@ -48,7 +44,6 @@ export interface Os {
|
||||||
|
|
||||||
/** Abstraction of the APIs required from the `typescript` module so they can
|
/** Abstraction of the APIs required from the `typescript` module so they can
|
||||||
* be easily mocked.
|
* be easily mocked.
|
||||||
* @internal
|
|
||||||
*/
|
*/
|
||||||
export interface Ts {
|
export interface Ts {
|
||||||
createLanguageService: typeof ts.createLanguageService;
|
createLanguageService: typeof ts.createLanguageService;
|
||||||
|
@ -62,10 +57,6 @@ export interface Ts {
|
||||||
* the module, not the actual module instance.
|
* the module, not the actual module instance.
|
||||||
*/
|
*/
|
||||||
export class ModuleMetaData implements ts.IScriptSnapshot {
|
export class ModuleMetaData implements ts.IScriptSnapshot {
|
||||||
public deps?: ModuleFileName[];
|
|
||||||
public exports = {};
|
|
||||||
public gatheringDeps = false;
|
|
||||||
public hasRun = false;
|
|
||||||
public scriptVersion = "";
|
public scriptVersion = "";
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
@ -113,19 +104,19 @@ function getExtension(
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Generate output code for a provided JSON string along with its source. */
|
/** Generate output code for a provided JSON string along with its source. */
|
||||||
export function jsonAmdTemplate(
|
export function jsonEsmTemplate(
|
||||||
jsonString: string,
|
jsonString: string,
|
||||||
sourceFileName: string
|
sourceFileName: string
|
||||||
): OutputCode {
|
): OutputCode {
|
||||||
// tslint:disable-next-line:max-line-length
|
// tslint:disable-next-line:max-line-length
|
||||||
return `define([], function() { return JSON.parse(\`${jsonString}\`); });\n//# sourceURL=${sourceFileName}`;
|
return `const _json = JSON.parse(\`${jsonString}\`)\nexport default _json;\n//# sourceURL=${sourceFileName}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A singleton class that combines the TypeScript Language Service host API
|
/** A singleton class that combines the TypeScript Language Service host API
|
||||||
* with Deno specific APIs to provide an interface for compiling and running
|
* with Deno specific APIs to provide an interface for compiling and running
|
||||||
* TypeScript and JavaScript modules.
|
* TypeScript and JavaScript modules.
|
||||||
*/
|
*/
|
||||||
export class DenoCompiler
|
export class Compiler
|
||||||
implements ts.LanguageServiceHost, ts.FormatDiagnosticsHost {
|
implements ts.LanguageServiceHost, ts.FormatDiagnosticsHost {
|
||||||
// Modules are usually referenced by their ModuleSpecifier and ContainingFile,
|
// Modules are usually referenced by their ModuleSpecifier and ContainingFile,
|
||||||
// and keeping a map of the resolved module file name allows more efficient
|
// and keeping a map of the resolved module file name allows more efficient
|
||||||
|
@ -164,6 +155,7 @@ export class DenoCompiler
|
||||||
// A reference to `typescript` module so it can be monkey patched during
|
// A reference to `typescript` module so it can be monkey patched during
|
||||||
// testing
|
// testing
|
||||||
private _ts: Ts = ts;
|
private _ts: Ts = ts;
|
||||||
|
|
||||||
// Flags forcing recompilation of TS code
|
// Flags forcing recompilation of TS code
|
||||||
public recompile = false;
|
public recompile = false;
|
||||||
|
|
||||||
|
@ -175,7 +167,9 @@ export class DenoCompiler
|
||||||
* TypeScript compiler, but the TypeScript compiler shouldn't be asking about
|
* TypeScript compiler, but the TypeScript compiler shouldn't be asking about
|
||||||
* external modules that we haven't told it about yet.
|
* external modules that we haven't told it about yet.
|
||||||
*/
|
*/
|
||||||
getModuleMetaData(fileName: ModuleFileName): ModuleMetaData | undefined {
|
private _getModuleMetaData(
|
||||||
|
fileName: ModuleFileName
|
||||||
|
): ModuleMetaData | undefined {
|
||||||
return this._moduleMetaDataMap.has(fileName)
|
return this._moduleMetaDataMap.has(fileName)
|
||||||
? this._moduleMetaDataMap.get(fileName)
|
? this._moduleMetaDataMap.get(fileName)
|
||||||
: fileName.startsWith(ASSETS)
|
: fileName.startsWith(ASSETS)
|
||||||
|
@ -218,7 +212,7 @@ export class DenoCompiler
|
||||||
}
|
}
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
if (DenoCompiler._instance) {
|
if (Compiler._instance) {
|
||||||
throw new TypeError("Attempt to create an additional compiler.");
|
throw new TypeError("Attempt to create an additional compiler.");
|
||||||
}
|
}
|
||||||
this._service = this._ts.createLanguageService(this);
|
this._service = this._ts.createLanguageService(this);
|
||||||
|
@ -229,17 +223,22 @@ export class DenoCompiler
|
||||||
/** Retrieve the output of the TypeScript compiler for a given module and
|
/** Retrieve the output of the TypeScript compiler for a given module and
|
||||||
* cache the result. Re-compilation can be forced using '--recompile' flag.
|
* cache the result. Re-compilation can be forced using '--recompile' flag.
|
||||||
*/
|
*/
|
||||||
compile(moduleMetaData: ModuleMetaData): OutputCode {
|
compile(
|
||||||
|
moduleSpecifier: ModuleSpecifier,
|
||||||
|
containingFile: ContainingFile
|
||||||
|
): ModuleMetaData {
|
||||||
|
const moduleMetaData = this.resolveModule(moduleSpecifier, containingFile);
|
||||||
|
this._scriptFileNames = [moduleMetaData.fileName];
|
||||||
const recompile = !!this.recompile;
|
const recompile = !!this.recompile;
|
||||||
if (!recompile && moduleMetaData.outputCode) {
|
if (!recompile && moduleMetaData.outputCode) {
|
||||||
return moduleMetaData.outputCode;
|
return moduleMetaData;
|
||||||
}
|
}
|
||||||
const { fileName, sourceCode, mediaType, moduleId } = moduleMetaData;
|
const { fileName, sourceCode, mediaType, moduleId } = moduleMetaData;
|
||||||
console.warn("Compiling", moduleId);
|
console.warn("Compiling", moduleId);
|
||||||
// Instead of using TypeScript to transpile JSON modules, we will just do
|
// Instead of using TypeScript to transpile JSON modules, we will just do
|
||||||
// it directly.
|
// it directly.
|
||||||
if (mediaType === MediaType.Json) {
|
if (mediaType === MediaType.Json) {
|
||||||
moduleMetaData.outputCode = jsonAmdTemplate(sourceCode, fileName);
|
moduleMetaData.outputCode = jsonEsmTemplate(sourceCode, fileName);
|
||||||
} else {
|
} else {
|
||||||
const service = this._service;
|
const service = this._service;
|
||||||
assert(
|
assert(
|
||||||
|
@ -302,20 +301,7 @@ export class DenoCompiler
|
||||||
moduleMetaData.outputCode,
|
moduleMetaData.outputCode,
|
||||||
moduleMetaData.sourceMap
|
moduleMetaData.sourceMap
|
||||||
);
|
);
|
||||||
return moduleMetaData.outputCode;
|
return moduleMetaData;
|
||||||
}
|
|
||||||
|
|
||||||
/** Given a fileName, return what was generated by the compiler. */
|
|
||||||
getGeneratedSourceMap(fileName: string): string {
|
|
||||||
const moduleMetaData = this._moduleMetaDataMap.get(fileName);
|
|
||||||
return moduleMetaData ? moduleMetaData.sourceMap : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
getOutput(filename: ModuleFileName): OutputCode {
|
|
||||||
const moduleMetaData = this.getModuleMetaData(filename)!;
|
|
||||||
assert(moduleMetaData != null, `Module not loaded: "${filename}"`);
|
|
||||||
this._scriptFileNames = [moduleMetaData.fileName];
|
|
||||||
return this.compile(moduleMetaData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Given a `moduleSpecifier` and `containingFile`, resolve the module and
|
/** Given a `moduleSpecifier` and `containingFile`, resolve the module and
|
||||||
|
@ -365,7 +351,6 @@ export class DenoCompiler
|
||||||
}
|
}
|
||||||
assert(moduleId != null, "No module ID.");
|
assert(moduleId != null, "No module ID.");
|
||||||
assert(fileName != null, "No file name.");
|
assert(fileName != null, "No file name.");
|
||||||
assert(sourceCode ? sourceCode.length > 0 : false, "No source code.");
|
|
||||||
assert(
|
assert(
|
||||||
mediaType !== MediaType.Unknown,
|
mediaType !== MediaType.Unknown,
|
||||||
`Unknown media type for: "${moduleSpecifier}" from "${containingFile}".`
|
`Unknown media type for: "${moduleSpecifier}" from "${containingFile}".`
|
||||||
|
@ -394,33 +379,6 @@ export class DenoCompiler
|
||||||
return moduleMetaData;
|
return moduleMetaData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Load and run a module and all of its dependencies based on a module
|
|
||||||
* specifier and a containing file
|
|
||||||
*/
|
|
||||||
run(
|
|
||||||
moduleSpecifier: ModuleSpecifier,
|
|
||||||
containingFile: ContainingFile
|
|
||||||
): ModuleMetaData {
|
|
||||||
this._log("compiler.run", { moduleSpecifier, containingFile });
|
|
||||||
const moduleMetaData = this.resolveModule(moduleSpecifier, containingFile);
|
|
||||||
this._scriptFileNames = [moduleMetaData.fileName];
|
|
||||||
return moduleMetaData;
|
|
||||||
}
|
|
||||||
|
|
||||||
getSource(filename: ModuleFileName): SourceCode {
|
|
||||||
const moduleMetaData = this.getModuleMetaData(filename)!;
|
|
||||||
assert(moduleMetaData != null, `Module not loaded: "${filename}"`);
|
|
||||||
return moduleMetaData.sourceCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
getJavaScriptSource(filename: ModuleFileName): OutputCode {
|
|
||||||
let s = this.getOutput(filename);
|
|
||||||
if (!s) {
|
|
||||||
s = this.getSource(filename);
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TypeScript Language Service and Format Diagnostic Host API
|
// TypeScript Language Service and Format Diagnostic Host API
|
||||||
|
|
||||||
getCanonicalFileName(fileName: string): string {
|
getCanonicalFileName(fileName: string): string {
|
||||||
|
@ -446,7 +404,7 @@ export class DenoCompiler
|
||||||
|
|
||||||
getScriptKind(fileName: ModuleFileName): ts.ScriptKind {
|
getScriptKind(fileName: ModuleFileName): ts.ScriptKind {
|
||||||
this._log("getScriptKind()", fileName);
|
this._log("getScriptKind()", fileName);
|
||||||
const moduleMetaData = this.getModuleMetaData(fileName);
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
||||||
if (moduleMetaData) {
|
if (moduleMetaData) {
|
||||||
switch (moduleMetaData.mediaType) {
|
switch (moduleMetaData.mediaType) {
|
||||||
case MediaType.TypeScript:
|
case MediaType.TypeScript:
|
||||||
|
@ -465,13 +423,13 @@ export class DenoCompiler
|
||||||
|
|
||||||
getScriptVersion(fileName: ModuleFileName): string {
|
getScriptVersion(fileName: ModuleFileName): string {
|
||||||
this._log("getScriptVersion()", fileName);
|
this._log("getScriptVersion()", fileName);
|
||||||
const moduleMetaData = this.getModuleMetaData(fileName);
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
||||||
return (moduleMetaData && moduleMetaData.scriptVersion) || "";
|
return (moduleMetaData && moduleMetaData.scriptVersion) || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
getScriptSnapshot(fileName: ModuleFileName): ts.IScriptSnapshot | undefined {
|
getScriptSnapshot(fileName: ModuleFileName): ts.IScriptSnapshot | undefined {
|
||||||
this._log("getScriptSnapshot()", fileName);
|
this._log("getScriptSnapshot()", fileName);
|
||||||
return this.getModuleMetaData(fileName);
|
return this._getModuleMetaData(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentDirectory(): string {
|
getCurrentDirectory(): string {
|
||||||
|
@ -497,7 +455,7 @@ export class DenoCompiler
|
||||||
}
|
}
|
||||||
|
|
||||||
fileExists(fileName: string): boolean {
|
fileExists(fileName: string): boolean {
|
||||||
const moduleMetaData = this.getModuleMetaData(fileName);
|
const moduleMetaData = this._getModuleMetaData(fileName);
|
||||||
const exists = moduleMetaData != null;
|
const exists = moduleMetaData != null;
|
||||||
this._log("fileExists()", fileName, exists);
|
this._log("fileExists()", fileName, exists);
|
||||||
return exists;
|
return exists;
|
||||||
|
@ -537,12 +495,10 @@ export class DenoCompiler
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static _instance: DenoCompiler | undefined;
|
private static _instance: Compiler | undefined;
|
||||||
|
|
||||||
/** Returns the instance of `DenoCompiler` or creates a new instance. */
|
/** Returns the instance of `Compiler` or creates a new instance. */
|
||||||
static instance(): DenoCompiler {
|
static instance(): Compiler {
|
||||||
return (
|
return Compiler._instance || (Compiler._instance = new Compiler());
|
||||||
DenoCompiler._instance || (DenoCompiler._instance = new DenoCompiler())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
import { test, assert, assertEqual } from "./test_util.ts";
|
import { test, assert, assertEqual } from "./test_util.ts";
|
||||||
import * as deno from "deno";
|
import * as deno from "deno";
|
||||||
import * as ts from "typescript";
|
|
||||||
|
|
||||||
// We use a silly amount of `any` in these tests...
|
// We use a silly amount of `any` in these tests...
|
||||||
// tslint:disable:no-any
|
// tslint:disable:no-any
|
||||||
|
|
||||||
const { DenoCompiler, jsonAmdTemplate } = (deno as any)._compiler;
|
const { Compiler, jsonEsmTemplate } = (deno as any)._compiler;
|
||||||
|
|
||||||
interface ModuleInfo {
|
interface ModuleInfo {
|
||||||
moduleName: string | undefined;
|
moduleName: string | undefined;
|
||||||
|
@ -17,16 +16,28 @@ interface ModuleInfo {
|
||||||
sourceMap: string | undefined;
|
sourceMap: string | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const compilerInstance = DenoCompiler.instance();
|
// Since we can't/don't want to import all of TypeScript for this one enum, we
|
||||||
|
// we will replicate it from TypeScript. This does mean that if other script
|
||||||
|
// kinds are added in the future we would need to add them manually to the tests
|
||||||
|
enum ScriptKind {
|
||||||
|
Unknown = 0,
|
||||||
|
JS = 1,
|
||||||
|
JSX = 2,
|
||||||
|
TS = 3,
|
||||||
|
TSX = 4,
|
||||||
|
External = 5,
|
||||||
|
JSON = 6,
|
||||||
|
Deferred = 7
|
||||||
|
}
|
||||||
|
|
||||||
|
const compilerInstance = Compiler.instance();
|
||||||
|
|
||||||
// References to original items we are going to mock
|
// References to original items we are going to mock
|
||||||
const originals = {
|
const originals = {
|
||||||
_globalEval: (compilerInstance as any)._globalEval,
|
|
||||||
_log: (compilerInstance as any)._log,
|
_log: (compilerInstance as any)._log,
|
||||||
_os: (compilerInstance as any)._os,
|
_os: (compilerInstance as any)._os,
|
||||||
_ts: (compilerInstance as any)._ts,
|
_ts: (compilerInstance as any)._ts,
|
||||||
_service: (compilerInstance as any)._service,
|
_service: (compilerInstance as any)._service
|
||||||
_window: (compilerInstance as any)._window
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum MediaType {
|
enum MediaType {
|
||||||
|
@ -97,27 +108,13 @@ const modBModuleInfo = mockModuleInfo(
|
||||||
);
|
);
|
||||||
|
|
||||||
// tslint:disable:max-line-length
|
// tslint:disable:max-line-length
|
||||||
const fooBarTsOutput = `define(["require", "exports", "deno"], function (require, exports, deno) {
|
const fooBarTsOutput = `import * as deno from "deno";
|
||||||
"use strict";
|
console.log(deno);
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
export const foo = "bar";
|
||||||
console.log(deno);
|
|
||||||
exports.foo = "bar";
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=bar.js.map
|
//# sourceMappingURL=bar.js.map
|
||||||
//# sourceURL=/root/project/foo/bar.ts`;
|
//# sourceURL=/root/project/foo/bar.ts`;
|
||||||
|
|
||||||
const fooBarTsSourcemap = `{"version":3,"file":"bar.js","sourceRoot":"","sources":["file:///root/project/foo/bar.ts"],"names":[],"mappings":";;;IACA,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACL,QAAA,GAAG,GAAG,KAAK,CAAC"}`;
|
const fooBarTsSourcemap = `{"version":3,"file":"bar.js","sourceRoot":"","sources":["file:///root/project/foo/bar.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAClB,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,CAAC"}`;
|
||||||
|
|
||||||
const fooBazTsOutput = `define(["require", "exports", "./bar.ts"], function (require, exports, bar_ts_1) {
|
|
||||||
"use strict";
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
||||||
console.log(bar_ts_1.foo);
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=baz.js.map
|
|
||||||
//# sourceURL=/root/project/foo/baz.ts`;
|
|
||||||
|
|
||||||
// This is not a valid map, just mock data
|
|
||||||
const fooBazTsSourcemap = `{"version":3,"file":"baz.js","sourceRoot":"","sources":["file:///root/project/foo/baz.ts"],"names":[],"mappings":""}`;
|
|
||||||
|
|
||||||
const loadConfigSource = `import * as config from "./config.json";
|
const loadConfigSource = `import * as config from "./config.json";
|
||||||
console.log(config.foo.baz);
|
console.log(config.foo.baz);
|
||||||
|
@ -142,8 +139,8 @@ const moduleMap: {
|
||||||
"/root/project/foo/baz.ts",
|
"/root/project/foo/baz.ts",
|
||||||
MediaType.TypeScript,
|
MediaType.TypeScript,
|
||||||
fooBazTsSource,
|
fooBazTsSource,
|
||||||
fooBazTsOutput,
|
null,
|
||||||
fooBazTsSourcemap
|
null
|
||||||
),
|
),
|
||||||
"modA.ts": modAModuleInfo,
|
"modA.ts": modAModuleInfo,
|
||||||
"some.txt": mockModuleInfo(
|
"some.txt": mockModuleInfo(
|
||||||
|
@ -252,7 +249,6 @@ const emittedFiles = {
|
||||||
"/root/project/foo/qat.ts": "console.log('foo');"
|
"/root/project/foo/qat.ts": "console.log('foo');"
|
||||||
};
|
};
|
||||||
|
|
||||||
let globalEvalStack: string[] = [];
|
|
||||||
let getEmitOutputStack: string[] = [];
|
let getEmitOutputStack: string[] = [];
|
||||||
let logStack: any[][] = [];
|
let logStack: any[][] = [];
|
||||||
let codeCacheStack: Array<{
|
let codeCacheStack: Array<{
|
||||||
|
@ -269,12 +265,6 @@ let codeFetchStack: Array<{
|
||||||
let mockDepsStack: string[][] = [];
|
let mockDepsStack: string[][] = [];
|
||||||
let mockFactoryStack: any[] = [];
|
let mockFactoryStack: any[] = [];
|
||||||
|
|
||||||
function globalEvalMock(x: string): void {
|
|
||||||
globalEvalStack.push(x);
|
|
||||||
if (windowMock.define && mockDepsStack.length && mockFactoryStack.length) {
|
|
||||||
windowMock.define(mockDepsStack.pop(), mockFactoryStack.pop());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function logMock(...args: any[]): void {
|
function logMock(...args: any[]): void {
|
||||||
logStack.push(args);
|
logStack.push(args);
|
||||||
}
|
}
|
||||||
|
@ -315,62 +305,45 @@ const osMock = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const tsMock = {
|
const tsMock = {
|
||||||
createLanguageService(host: ts.LanguageServiceHost): ts.LanguageService {
|
createLanguageService() {
|
||||||
return {} as ts.LanguageService;
|
return {} as any;
|
||||||
},
|
},
|
||||||
formatDiagnosticsWithColorAndContext(
|
formatDiagnosticsWithColorAndContext(
|
||||||
diagnostics: ReadonlyArray<ts.Diagnostic>,
|
diagnostics: ReadonlyArray<any>,
|
||||||
_host: ts.FormatDiagnosticsHost
|
_host: any
|
||||||
): string {
|
): string {
|
||||||
return JSON.stringify(diagnostics.map(({ messageText }) => messageText));
|
return JSON.stringify(diagnostics.map(({ messageText }) => messageText));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getEmitOutputPassThrough = true;
|
|
||||||
|
|
||||||
const serviceMock = {
|
const serviceMock = {
|
||||||
getCompilerOptionsDiagnostics(): ts.Diagnostic[] {
|
getCompilerOptionsDiagnostics() {
|
||||||
return originals._service.getCompilerOptionsDiagnostics.call(
|
return originals._service.getCompilerOptionsDiagnostics.call(
|
||||||
originals._service
|
originals._service
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
getEmitOutput(fileName: string): ts.EmitOutput {
|
getEmitOutput(fileName: string) {
|
||||||
getEmitOutputStack.push(fileName);
|
getEmitOutputStack.push(fileName);
|
||||||
if (getEmitOutputPassThrough) {
|
return originals._service.getEmitOutput.call(originals._service, fileName);
|
||||||
return originals._service.getEmitOutput.call(
|
|
||||||
originals._service,
|
|
||||||
fileName
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (fileName in emittedFiles) {
|
|
||||||
return {
|
|
||||||
outputFiles: [{ text: emittedFiles[fileName] }] as any,
|
|
||||||
emitSkipped: false
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return { outputFiles: [], emitSkipped: false };
|
|
||||||
},
|
},
|
||||||
getSemanticDiagnostics(fileName: string): ts.Diagnostic[] {
|
getSemanticDiagnostics(fileName: string) {
|
||||||
return originals._service.getSemanticDiagnostics.call(
|
return originals._service.getSemanticDiagnostics.call(
|
||||||
originals._service,
|
originals._service,
|
||||||
fileName
|
fileName
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
getSyntacticDiagnostics(fileName: string): ts.Diagnostic[] {
|
getSyntacticDiagnostics(fileName: string) {
|
||||||
return originals._service.getSyntacticDiagnostics.call(
|
return originals._service.getSyntacticDiagnostics.call(
|
||||||
originals._service,
|
originals._service,
|
||||||
fileName
|
fileName
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const windowMock: { define?: any } = {};
|
|
||||||
const mocks = {
|
const mocks = {
|
||||||
_globalEval: globalEvalMock,
|
|
||||||
_log: logMock,
|
_log: logMock,
|
||||||
_os: osMock,
|
_os: osMock,
|
||||||
_ts: tsMock,
|
_ts: tsMock,
|
||||||
_service: serviceMock,
|
_service: serviceMock
|
||||||
_window: windowMock
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -394,7 +367,6 @@ function teardown() {
|
||||||
codeCacheStack = [];
|
codeCacheStack = [];
|
||||||
logStack = [];
|
logStack = [];
|
||||||
getEmitOutputStack = [];
|
getEmitOutputStack = [];
|
||||||
globalEvalStack = [];
|
|
||||||
|
|
||||||
assertEqual(mockDepsStack.length, 0);
|
assertEqual(mockDepsStack.length, 0);
|
||||||
assertEqual(mockFactoryStack.length, 0);
|
assertEqual(mockFactoryStack.length, 0);
|
||||||
|
@ -405,49 +377,35 @@ function teardown() {
|
||||||
Object.assign(compilerInstance, originals);
|
Object.assign(compilerInstance, originals);
|
||||||
}
|
}
|
||||||
|
|
||||||
test(function testJsonAmdTemplate() {
|
test(function testJsonEsmTemplate() {
|
||||||
let deps: string[];
|
const result = jsonEsmTemplate(
|
||||||
let factory: Function;
|
`{ "hello": "world", "foo": "bar" }`,
|
||||||
function define(d: string[], f: Function) {
|
"/foo.ts"
|
||||||
deps = d;
|
);
|
||||||
factory = f;
|
assertEqual(
|
||||||
}
|
result,
|
||||||
|
`const _json = JSON.parse(\`{ "hello": "world", "foo": "bar" }\`)\n` +
|
||||||
const code = jsonAmdTemplate(`{ "hello": "world", "foo": "bar" }`);
|
`export default _json;\n` +
|
||||||
const result = eval(code);
|
`//# sourceURL=/foo.ts`
|
||||||
assert(result == null);
|
);
|
||||||
assertEqual(deps && deps.length, 0);
|
|
||||||
assert(factory != null);
|
|
||||||
const factoryResult = factory();
|
|
||||||
assertEqual(factoryResult, { hello: "world", foo: "bar" });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function compilerInstance() {
|
test(function compilerInstance() {
|
||||||
assert(DenoCompiler != null);
|
assert(Compiler != null);
|
||||||
assert(DenoCompiler.instance() != null);
|
assert(Compiler.instance() != null);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Testing the internal APIs
|
// Testing the internal APIs
|
||||||
|
|
||||||
test(function compilerRun() {
|
test(function compilerCompile() {
|
||||||
// equal to `deno foo/bar.ts`
|
// equal to `deno foo/bar.ts`
|
||||||
setup();
|
setup();
|
||||||
let factoryRun = false;
|
const moduleMetaData = compilerInstance.compile(
|
||||||
mockDepsStack.push(["require", "exports", "deno"]);
|
"foo/bar.ts",
|
||||||
mockFactoryStack.push((_require, _exports, _deno) => {
|
"/root/project"
|
||||||
factoryRun = true;
|
);
|
||||||
assertEqual(typeof _require, "function");
|
|
||||||
assertEqual(typeof _exports, "object");
|
|
||||||
assert(_deno === deno);
|
|
||||||
_exports.foo = "bar";
|
|
||||||
});
|
|
||||||
const moduleMetaData = compilerInstance.run("foo/bar.ts", "/root/project");
|
|
||||||
assert(factoryRun);
|
|
||||||
assert(moduleMetaData.hasRun);
|
|
||||||
assertEqual(moduleMetaData.sourceCode, fooBarTsSource);
|
assertEqual(moduleMetaData.sourceCode, fooBarTsSource);
|
||||||
assertEqual(moduleMetaData.outputCode, fooBarTsOutput);
|
assertEqual(moduleMetaData.outputCode, fooBarTsOutput);
|
||||||
// assertEqual(JSON.stringify(moduleMetaData.sourceMap), fooBarTsSourcemap);
|
|
||||||
assertEqual(moduleMetaData.exports, { foo: "bar" });
|
|
||||||
|
|
||||||
assertEqual(
|
assertEqual(
|
||||||
codeFetchStack.length,
|
codeFetchStack.length,
|
||||||
|
@ -467,86 +425,20 @@ test(function compilerRun() {
|
||||||
teardown();
|
teardown();
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function compilerRunMultiModule() {
|
test(function compilerCompilerMultiModule() {
|
||||||
// equal to `deno foo/baz.ts`
|
// equal to `deno foo/baz.ts`
|
||||||
setup();
|
setup();
|
||||||
const factoryStack: string[] = [];
|
compilerInstance.compile("foo/baz.ts", "/root/project");
|
||||||
const bazDeps = ["require", "exports", "./bar.ts"];
|
assertEqual(codeFetchStack.length, 2, "Two modules fetched.");
|
||||||
const bazFactory = (_require, _exports, _bar) => {
|
assertEqual(codeCacheStack.length, 1, "Only one module compiled.");
|
||||||
factoryStack.push("baz");
|
|
||||||
assertEqual(_bar.foo, "bar");
|
|
||||||
};
|
|
||||||
const barDeps = ["require", "exports", "deno"];
|
|
||||||
const barFactory = (_require, _exports, _deno) => {
|
|
||||||
factoryStack.push("bar");
|
|
||||||
_exports.foo = "bar";
|
|
||||||
};
|
|
||||||
mockDepsStack.push(barDeps);
|
|
||||||
mockFactoryStack.push(barFactory);
|
|
||||||
mockDepsStack.push(bazDeps);
|
|
||||||
mockFactoryStack.push(bazFactory);
|
|
||||||
compilerInstance.run("foo/baz.ts", "/root/project");
|
|
||||||
assertEqual(factoryStack, ["bar", "baz"]);
|
|
||||||
|
|
||||||
assertEqual(
|
|
||||||
codeFetchStack.length,
|
|
||||||
2,
|
|
||||||
"Modules should have only been fetched once."
|
|
||||||
);
|
|
||||||
assertEqual(codeCacheStack.length, 0, "No code should have been cached.");
|
|
||||||
teardown();
|
|
||||||
});
|
|
||||||
|
|
||||||
test(function compilerRunCircularDependency() {
|
|
||||||
setup();
|
|
||||||
const factoryStack: string[] = [];
|
|
||||||
const modADeps = ["require", "exports", "./modB.ts"];
|
|
||||||
const modAFactory = (_require, _exports, _modB) => {
|
|
||||||
assertEqual(_modB.foo, "bar");
|
|
||||||
factoryStack.push("modA");
|
|
||||||
_exports.bar = "baz";
|
|
||||||
_modB.assertModA();
|
|
||||||
};
|
|
||||||
const modBDeps = ["require", "exports", "./modA.ts"];
|
|
||||||
const modBFactory = (_require, _exports, _modA) => {
|
|
||||||
assertEqual(_modA, {});
|
|
||||||
factoryStack.push("modB");
|
|
||||||
_exports.foo = "bar";
|
|
||||||
_exports.assertModA = () => {
|
|
||||||
assertEqual(_modA, {
|
|
||||||
bar: "baz"
|
|
||||||
});
|
|
||||||
};
|
|
||||||
};
|
|
||||||
mockDepsStack.push(modBDeps);
|
|
||||||
mockFactoryStack.push(modBFactory);
|
|
||||||
mockDepsStack.push(modADeps);
|
|
||||||
mockFactoryStack.push(modAFactory);
|
|
||||||
compilerInstance.run("modA.ts", "/root/project");
|
|
||||||
assertEqual(factoryStack, ["modB", "modA"]);
|
|
||||||
teardown();
|
teardown();
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function compilerLoadJsonModule() {
|
test(function compilerLoadJsonModule() {
|
||||||
setup();
|
setup();
|
||||||
const factoryStack: string[] = [];
|
compilerInstance.compile("loadConfig.ts", "/root/project");
|
||||||
const configJsonDeps: string[] = [];
|
assertEqual(codeFetchStack.length, 2, "Two modules fetched.");
|
||||||
const configJsonFactory = () => {
|
assertEqual(codeCacheStack.length, 1, "Only one module compiled.");
|
||||||
factoryStack.push("configJson");
|
|
||||||
return JSON.parse(configJsonSource);
|
|
||||||
};
|
|
||||||
const loadConfigDeps = ["require", "exports", "./config.json"];
|
|
||||||
const loadConfigFactory = (_require, _exports, _config) => {
|
|
||||||
factoryStack.push("loadConfig");
|
|
||||||
assertEqual(_config, JSON.parse(configJsonSource));
|
|
||||||
};
|
|
||||||
|
|
||||||
mockDepsStack.push(configJsonDeps);
|
|
||||||
mockFactoryStack.push(configJsonFactory);
|
|
||||||
mockDepsStack.push(loadConfigDeps);
|
|
||||||
mockFactoryStack.push(loadConfigFactory);
|
|
||||||
compilerInstance.run("loadConfig.ts", "/root/project");
|
|
||||||
assertEqual(factoryStack, ["configJson", "loadConfig"]);
|
|
||||||
teardown();
|
teardown();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -556,12 +448,10 @@ test(function compilerResolveModule() {
|
||||||
"foo/baz.ts",
|
"foo/baz.ts",
|
||||||
"/root/project"
|
"/root/project"
|
||||||
);
|
);
|
||||||
|
console.log(moduleMetaData);
|
||||||
assertEqual(moduleMetaData.sourceCode, fooBazTsSource);
|
assertEqual(moduleMetaData.sourceCode, fooBazTsSource);
|
||||||
assertEqual(moduleMetaData.outputCode, fooBazTsOutput);
|
assertEqual(moduleMetaData.outputCode, null);
|
||||||
assertEqual(JSON.stringify(moduleMetaData.sourceMap), fooBazTsSourcemap);
|
assertEqual(moduleMetaData.sourceMap, null);
|
||||||
assert(!moduleMetaData.hasRun);
|
|
||||||
assert(!moduleMetaData.deps);
|
|
||||||
assertEqual(moduleMetaData.exports, {});
|
|
||||||
assertEqual(moduleMetaData.scriptVersion, "1");
|
assertEqual(moduleMetaData.scriptVersion, "1");
|
||||||
|
|
||||||
assertEqual(codeFetchStack.length, 1, "Only initial module is resolved.");
|
assertEqual(codeFetchStack.length, 1, "Only initial module is resolved.");
|
||||||
|
@ -585,25 +475,28 @@ test(function compilerResolveModuleUnknownMediaType() {
|
||||||
teardown();
|
teardown();
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function compilerGetModuleDependencies() {
|
test(function compilerRecompileFlag() {
|
||||||
setup();
|
setup();
|
||||||
const bazDeps = ["require", "exports", "./bar.ts"];
|
compilerInstance.compile("foo/bar.ts", "/root/project");
|
||||||
const bazFactory = () => {
|
assertEqual(
|
||||||
throw new Error("Unexpected factory call");
|
getEmitOutputStack.length,
|
||||||
};
|
1,
|
||||||
const barDeps = ["require", "exports", "deno"];
|
"Expected only a single emitted file."
|
||||||
const barFactory = () => {
|
);
|
||||||
throw new Error("Unexpected factory call");
|
// running compiler against same file should use cached code
|
||||||
};
|
compilerInstance.compile("foo/bar.ts", "/root/project");
|
||||||
mockDepsStack.push(barDeps);
|
assertEqual(
|
||||||
mockFactoryStack.push(barFactory);
|
getEmitOutputStack.length,
|
||||||
mockDepsStack.push(bazDeps);
|
1,
|
||||||
mockFactoryStack.push(bazFactory);
|
"Expected only a single emitted file."
|
||||||
const deps = compilerInstance.getModuleDependencies(
|
);
|
||||||
"foo/baz.ts",
|
compilerInstance.recompile = true;
|
||||||
"/root/project"
|
compilerInstance.compile("foo/bar.ts", "/root/project");
|
||||||
|
assertEqual(getEmitOutputStack.length, 2, "Expected two emitted file.");
|
||||||
|
assert(
|
||||||
|
getEmitOutputStack[0] === getEmitOutputStack[1],
|
||||||
|
"Expected same file to be emitted twice."
|
||||||
);
|
);
|
||||||
assertEqual(deps, ["/root/project/foo/bar.ts", "/root/project/foo/baz.ts"]);
|
|
||||||
teardown();
|
teardown();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -634,38 +527,13 @@ test(function compilerGetNewLine() {
|
||||||
|
|
||||||
test(function compilerGetScriptFileNames() {
|
test(function compilerGetScriptFileNames() {
|
||||||
setup();
|
setup();
|
||||||
compilerInstance.run("foo/bar.ts", "/root/project");
|
compilerInstance.compile("foo/bar.ts", "/root/project");
|
||||||
const result = compilerInstance.getScriptFileNames();
|
const result = compilerInstance.getScriptFileNames();
|
||||||
assertEqual(result.length, 1, "Expected only a single filename.");
|
assertEqual(result.length, 1, "Expected only a single filename.");
|
||||||
assertEqual(result[0], "/root/project/foo/bar.ts");
|
assertEqual(result[0], "/root/project/foo/bar.ts");
|
||||||
teardown();
|
teardown();
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function compilerRecompileFlag() {
|
|
||||||
setup();
|
|
||||||
compilerInstance.run("foo/bar.ts", "/root/project");
|
|
||||||
assertEqual(
|
|
||||||
getEmitOutputStack.length,
|
|
||||||
1,
|
|
||||||
"Expected only a single emitted file."
|
|
||||||
);
|
|
||||||
// running compiler against same file should use cached code
|
|
||||||
compilerInstance.run("foo/bar.ts", "/root/project");
|
|
||||||
assertEqual(
|
|
||||||
getEmitOutputStack.length,
|
|
||||||
1,
|
|
||||||
"Expected only a single emitted file."
|
|
||||||
);
|
|
||||||
compilerInstance.recompile = true;
|
|
||||||
compilerInstance.run("foo/bar.ts", "/root/project");
|
|
||||||
assertEqual(getEmitOutputStack.length, 2, "Expected two emitted file.");
|
|
||||||
assert(
|
|
||||||
getEmitOutputStack[0] === getEmitOutputStack[1],
|
|
||||||
"Expected same file to be emitted twice."
|
|
||||||
);
|
|
||||||
teardown();
|
|
||||||
});
|
|
||||||
|
|
||||||
test(function compilerGetScriptKind() {
|
test(function compilerGetScriptKind() {
|
||||||
setup();
|
setup();
|
||||||
compilerInstance.resolveModule("foo.ts", "/moduleKinds");
|
compilerInstance.resolveModule("foo.ts", "/moduleKinds");
|
||||||
|
@ -675,34 +543,33 @@ test(function compilerGetScriptKind() {
|
||||||
compilerInstance.resolveModule("foo.txt", "/moduleKinds");
|
compilerInstance.resolveModule("foo.txt", "/moduleKinds");
|
||||||
assertEqual(
|
assertEqual(
|
||||||
compilerInstance.getScriptKind("/moduleKinds/foo.ts"),
|
compilerInstance.getScriptKind("/moduleKinds/foo.ts"),
|
||||||
ts.ScriptKind.TS
|
ScriptKind.TS
|
||||||
);
|
);
|
||||||
assertEqual(
|
assertEqual(
|
||||||
compilerInstance.getScriptKind("/moduleKinds/foo.d.ts"),
|
compilerInstance.getScriptKind("/moduleKinds/foo.d.ts"),
|
||||||
ts.ScriptKind.TS
|
ScriptKind.TS
|
||||||
);
|
);
|
||||||
assertEqual(
|
assertEqual(
|
||||||
compilerInstance.getScriptKind("/moduleKinds/foo.js"),
|
compilerInstance.getScriptKind("/moduleKinds/foo.js"),
|
||||||
ts.ScriptKind.JS
|
ScriptKind.JS
|
||||||
);
|
);
|
||||||
assertEqual(
|
assertEqual(
|
||||||
compilerInstance.getScriptKind("/moduleKinds/foo.json"),
|
compilerInstance.getScriptKind("/moduleKinds/foo.json"),
|
||||||
ts.ScriptKind.JSON
|
ScriptKind.JSON
|
||||||
);
|
);
|
||||||
assertEqual(
|
assertEqual(
|
||||||
compilerInstance.getScriptKind("/moduleKinds/foo.txt"),
|
compilerInstance.getScriptKind("/moduleKinds/foo.txt"),
|
||||||
ts.ScriptKind.JS
|
ScriptKind.JS
|
||||||
);
|
);
|
||||||
teardown();
|
teardown();
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function compilerGetScriptVersion() {
|
test(function compilerGetScriptVersion() {
|
||||||
setup();
|
setup();
|
||||||
const moduleMetaData = compilerInstance.resolveModule(
|
const moduleMetaData = compilerInstance.compile(
|
||||||
"foo/bar.ts",
|
"foo/bar.ts",
|
||||||
"/root/project"
|
"/root/project"
|
||||||
);
|
);
|
||||||
compilerInstance.compile(moduleMetaData);
|
|
||||||
assertEqual(
|
assertEqual(
|
||||||
compilerInstance.getScriptVersion(moduleMetaData.fileName),
|
compilerInstance.getScriptVersion(moduleMetaData.fileName),
|
||||||
"1",
|
"1",
|
||||||
|
|
18
js/main.ts
18
js/main.ts
|
@ -5,7 +5,7 @@ import * as flatbuffers from "./flatbuffers";
|
||||||
import * as msg from "gen/msg_generated";
|
import * as msg from "gen/msg_generated";
|
||||||
import { assert, log, setLogDebug } from "./util";
|
import { assert, log, setLogDebug } from "./util";
|
||||||
import * as os from "./os";
|
import * as os from "./os";
|
||||||
import { DenoCompiler } from "./compiler";
|
import { Compiler } from "./compiler";
|
||||||
import { libdeno } from "./libdeno";
|
import { libdeno } from "./libdeno";
|
||||||
import { args } from "./deno";
|
import { args } from "./deno";
|
||||||
import { sendSync, handleAsyncMsgFromRust } from "./dispatch";
|
import { sendSync, handleAsyncMsgFromRust } from "./dispatch";
|
||||||
|
@ -37,18 +37,19 @@ function sendStart(): void {
|
||||||
|
|
||||||
function compilerMain() {
|
function compilerMain() {
|
||||||
// workerMain should have already been called since a compiler is a worker.
|
// workerMain should have already been called since a compiler is a worker.
|
||||||
const compiler = DenoCompiler.instance();
|
const compiler = Compiler.instance();
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const decoder = new TextDecoder();
|
||||||
compiler.recompile = startResMsg.recompileFlag();
|
compiler.recompile = startResMsg.recompileFlag();
|
||||||
log(`recompile ${compiler.recompile}`);
|
log(`recompile ${compiler.recompile}`);
|
||||||
window.onmessage = (e: { data: Uint8Array }) => {
|
window.onmessage = ({ data }: { data: Uint8Array }) => {
|
||||||
const json = new TextDecoder().decode(e.data);
|
const json = decoder.decode(data);
|
||||||
const lookup = JSON.parse(json) as CompilerLookup;
|
const lookup = JSON.parse(json) as CompilerLookup;
|
||||||
|
|
||||||
const moduleMetaData = compiler.run(lookup.specifier, lookup.referrer);
|
const moduleMetaData = compiler.compile(lookup.specifier, lookup.referrer);
|
||||||
moduleMetaData.outputCode = compiler.compile(moduleMetaData);
|
|
||||||
|
|
||||||
const responseJson = JSON.stringify(moduleMetaData);
|
const responseJson = JSON.stringify(moduleMetaData);
|
||||||
const response = new TextEncoder().encode(responseJson);
|
const response = encoder.encode(responseJson);
|
||||||
postMessage(response);
|
postMessage(response);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -70,8 +71,9 @@ export default function denoMain() {
|
||||||
setLogDebug(startResMsg.debugFlag());
|
setLogDebug(startResMsg.debugFlag());
|
||||||
|
|
||||||
// handle `--types`
|
// handle `--types`
|
||||||
|
// TODO(kitsonk) move to Rust fetching from compiler
|
||||||
if (startResMsg.typesFlag()) {
|
if (startResMsg.typesFlag()) {
|
||||||
const compiler = DenoCompiler.instance();
|
const compiler = Compiler.instance();
|
||||||
const defaultLibFileName = compiler.getDefaultLibFileName();
|
const defaultLibFileName = compiler.getDefaultLibFileName();
|
||||||
const defaultLibModule = compiler.resolveModule(defaultLibFileName, "");
|
const defaultLibModule = compiler.resolveModule(defaultLibFileName, "");
|
||||||
console.log(defaultLibModule.sourceCode);
|
console.log(defaultLibModule.sourceCode);
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
import "./blob_test.ts";
|
import "./blob_test.ts";
|
||||||
import "./buffer_test.ts";
|
import "./buffer_test.ts";
|
||||||
import "./chmod_test.ts";
|
import "./chmod_test.ts";
|
||||||
// import "./compiler_test.ts";
|
import "./compiler_test.ts";
|
||||||
import "./console_test.ts";
|
import "./console_test.ts";
|
||||||
import "./copy_file_test.ts";
|
import "./copy_file_test.ts";
|
||||||
import "./dir_test.ts";
|
import "./dir_test.ts";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue