Skip to content

Commit

Permalink
Hide compiler module (denoland#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored and ry committed Oct 5, 2018
1 parent 6a64901 commit 6c42ded
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
7 changes: 3 additions & 4 deletions js/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
/// <amd-module name="compiler"/>
import * as ts from "typescript";
import { assetSourceCode } from "./assets";
// tslint:disable-next-line:no-circular-imports
import * as deno from "./deno";
import { globalEval } from "./global_eval";
import { libdeno } from "./libdeno";
Expand Down Expand Up @@ -648,7 +648,7 @@ export class DenoCompiler
this._log("resolveModuleNames()", { moduleNames, containingFile });
return moduleNames.map(name => {
let resolvedFileName;
if (name === "deno" || name === "compiler") {
if (name === "deno") {
// builtin modules are part of `globals.d.ts`
resolvedFileName = this._resolveModuleName("globals.d.ts", ASSETS);
} else if (name === "typescript") {
Expand Down Expand Up @@ -683,8 +683,7 @@ export class DenoCompiler
// tslint:disable-next-line:no-any
private static _builtins: { [mid: string]: any } = {
typescript: ts,
deno,
compiler: { DenoCompiler, ModuleMetaData }
deno
};

private static _instance: DenoCompiler | undefined;
Expand Down
30 changes: 15 additions & 15 deletions js/compiler_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEqual } from "./test_util.ts";
import * as compiler from "compiler";
import * as deno from "deno";
import * as ts from "typescript";

// We use a silly amount of `any` in these tests...
// tslint:disable:no-any

const { DenoCompiler } = compiler;
const { DenoCompiler } = (deno as any)._compiler;

interface ModuleInfo {
moduleName: string | null;
Expand Down Expand Up @@ -42,8 +42,8 @@ function mockModuleInfo(
}

// Some fixtures we will us in testing
const fooBarTsSource = `import * as compiler from "compiler";
console.log(compiler);
const fooBarTsSource = `import * as deno from "deno";
console.log(deno);
export const foo = "bar";
`;

Expand Down Expand Up @@ -81,13 +81,13 @@ const modBModuleInfo = mockModuleInfo(

// TODO(#23) Remove source map strings from fooBarTsOutput.
// tslint:disable:max-line-length
const fooBarTsOutput = `define(["require", "exports", "compiler"], function (require, exports, compiler) {
const fooBarTsOutput = `define(["require", "exports", "deno"], function (require, exports, deno) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
console.log(compiler);
console.log(deno);
exports.foo = "bar";
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiZmlsZTovLy9yb290L3Byb2plY3QvZm9vL2Jhci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7SUFDQSxPQUFPLENBQUMsR0FBRyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0lBQ1QsUUFBQSxHQUFHLEdBQUcsS0FBSyxDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0ICogYXMgY29tcGlsZXIgZnJvbSBcImNvbXBpbGVyXCI7XG5jb25zb2xlLmxvZyhjb21waWxlcik7XG5leHBvcnQgY29uc3QgZm9vID0gXCJiYXJcIjtcbiJdfQ==
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiZmlsZTovLy9yb290L3Byb2plY3QvZm9vL2Jhci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7SUFDQSxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDO0lBQ0wsUUFBQSxHQUFHLEdBQUcsS0FBSyxDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0ICogYXMgZGVubyBmcm9tIFwiZGVub1wiO1xuY29uc29sZS5sb2coZGVubyk7XG5leHBvcnQgY29uc3QgZm9vID0gXCJiYXJcIjtcbiJdfQ==
//# sourceURL=/root/project/foo/bar.ts`;

// TODO(#23) Remove source map strings from fooBazTsOutput.
Expand Down Expand Up @@ -159,7 +159,7 @@ let codeFetchStack: Array<{
}> = [];

let mockDepsStack: string[][] = [];
let mockFactoryStack: compiler.AmdFactory[] = [];
let mockFactoryStack: any[] = [];

function globalEvalMock(x: string): void {
globalEvalStack.push(x);
Expand Down Expand Up @@ -247,7 +247,7 @@ const serviceMock = {
);
}
};
const windowMock: { define?: compiler.AmdDefine } = {};
const windowMock: { define?: any } = {};
const mocks = {
_globalEval: globalEvalMock,
_log: logMock,
Expand Down Expand Up @@ -300,12 +300,12 @@ test(function compilerRun() {
// equal to `deno foo/bar.ts`
setup();
let factoryRun = false;
mockDepsStack.push(["require", "exports", "compiler"]);
mockFactoryStack.push((_require, _exports, _compiler) => {
mockDepsStack.push(["require", "exports", "deno"]);
mockFactoryStack.push((_require, _exports, _deno) => {
factoryRun = true;
assertEqual(typeof _require, "function");
assertEqual(typeof _exports, "object");
assert(_compiler === compiler);
assert(_deno === deno);
_exports.foo = "bar";
});
const moduleMetaData = compilerInstance.run("foo/bar.ts", "/root/project");
Expand Down Expand Up @@ -337,8 +337,8 @@ test(function compilerRunMultiModule() {
factoryStack.push("baz");
assertEqual(_bar.foo, "bar");
};
const barDeps = ["require", "exports", "compiler"];
const barFactory = (_require, _exports, _compiler) => {
const barDeps = ["require", "exports", "deno"];
const barFactory = (_require, _exports, _deno) => {
factoryStack.push("bar");
_exports.foo = "bar";
};
Expand Down Expand Up @@ -411,7 +411,7 @@ test(function compilerGetModuleDependencies() {
const bazFactory = () => {
throw new Error("Unexpected factory call");
};
const barDeps = ["require", "exports", "compiler"];
const barDeps = ["require", "exports", "deno"];
const barFactory = () => {
throw new Error("Unexpected factory call");
};
Expand Down
5 changes: 5 additions & 0 deletions js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ export { truncateSync, truncate } from "./truncate";
export { FileInfo } from "./file_info";
export { connect, dial, listen, Listener, Conn } from "./net";
export const args: string[] = [];

// Provide the compiler API in an obfuscated way
import * as compiler from "./compiler";
// @internal
export const _compiler = compiler;
1 change: 0 additions & 1 deletion js/tsconfig.declarations.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"files": [
"../node_modules/typescript/lib/lib.esnext.d.ts",
"./compiler.ts",
"./deno.ts",
"./globals.ts"
]
Expand Down

0 comments on commit 6c42ded

Please sign in to comment.