Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add support for --types #729

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ ts_sources = [
"js/v8_source_maps.ts",
"js/write_file.ts",

"js/tsconfig.declarations.json",
"tsconfig.json",

# Listing package.json and yarn.lock as sources ensures the bundle is rebuilt
Expand Down Expand Up @@ -245,26 +244,32 @@ executable("snapshot_creator") {
configs += [ ":deno_config" ]
}

# Generates type declarations for files that need to be included
# in the runtime bundle
run_node("gen_declarations") {
# Generates the core TypeScript type library for deno that will be
# included in the runtime bundle
run_node("deno_runtime_declaration") {
out_dir = target_gen_dir
sources = ts_sources
outputs = [
"$out_dir/types/globals.d.ts",
"$out_dir/lib/lib.deno_runtime.d.ts",
]
deps = [
":msg_ts",
]
args = [
"./node_modules/typescript/bin/tsc",
"-p",
rebase_path("js/tsconfig.declarations.json", root_build_dir),
"--baseUrl",
rebase_path("tools/ts_library_builder/main", root_build_dir),
"--basePath",
rebase_path(".", root_build_dir),
"--buildPath",
rebase_path(root_build_dir, root_build_dir),
"--outFile",
rebase_path("$out_dir/types/globals.js", root_build_dir),
rebase_path("$out_dir/lib/lib.deno_runtime.d.ts", root_build_dir),
"--silent",
]
if (is_debug) {
args += [
"--debug",
]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

run_node("bundle") {
Expand All @@ -275,7 +280,7 @@ run_node("bundle") {
out_dir + "main.js.map",
]
deps = [
":gen_declarations",
":deno_runtime_declaration",
":msg_ts",
]
args = [
Expand Down
6 changes: 2 additions & 4 deletions js/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// tslint:disable:max-line-length

// Generated default library
import globalsDts from "gen/types/globals.d.ts!string";
import libDts from "gen/lib/lib.deno_runtime.d.ts!string";

// Static libraries
import libEs2015Dts from "/third_party/node_modules/typescript/lib/lib.es2015.d.ts!string";
Expand Down Expand Up @@ -40,15 +40,14 @@ import libEsnextIntlDts from "/third_party/node_modules/typescript/lib/lib.esnex
import libEsnextSymbolDts from "/third_party/node_modules/typescript/lib/lib.esnext.symbol.d.ts!string";

// Static definitions
import flatbuffersDts from "/third_party/node_modules/@types/flatbuffers/index.d.ts!string";
import textEncodingDts from "/third_party/node_modules/@types/text-encoding/index.d.ts!string";
import typescriptDts from "/third_party/node_modules/typescript/lib/typescript.d.ts!string";
// tslint:enable:max-line-length

// @internal
export const assetSourceCode: { [key: string]: string } = {
// Generated library
"globals.d.ts": globalsDts,
"lib.deno_runtime.d.ts": libDts,

// Static libraries
"lib.es2015.collection.d.ts": libEs2015CollectionDts,
Expand Down Expand Up @@ -81,7 +80,6 @@ export const assetSourceCode: { [key: string]: string } = {
"lib.esnext.symbol.d.ts": libEsnextSymbolDts,

// Static definitions
"flatbuffers.d.ts": flatbuffersDts,
"text-encoding.d.ts": textEncodingDts,
"typescript.d.ts": typescriptDts
};
7 changes: 4 additions & 3 deletions js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as sourceMaps from "./v8_source_maps";

const EOL = "\n";
const ASSETS = "$asset$";
const LIB_RUNTIME = "lib.deno_runtime.d.ts";

// tslint:disable:no-any
type AmdCallback = (...args: any[]) => void;
Expand Down Expand Up @@ -619,7 +620,7 @@ export class DenoCompiler

getDefaultLibFileName(): string {
this._log("getDefaultLibFileName()");
const moduleSpecifier = "globals.d.ts";
const moduleSpecifier = LIB_RUNTIME;
const moduleMetaData = this.resolveModule(moduleSpecifier, ASSETS);
return moduleMetaData.fileName;
}
Expand Down Expand Up @@ -649,8 +650,8 @@ export class DenoCompiler
return moduleNames.map(name => {
let resolvedFileName;
if (name === "deno") {
// builtin modules are part of `globals.d.ts`
resolvedFileName = this._resolveModuleName("globals.d.ts", ASSETS);
// builtin modules are part of the runtime lib
resolvedFileName = this._resolveModuleName(LIB_RUNTIME, ASSETS);
} else if (name === "typescript") {
resolvedFileName = this._resolveModuleName("typescript.d.ts", ASSETS);
} else {
Expand Down
9 changes: 6 additions & 3 deletions js/compiler_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ test(function compilerGetCurrentDirectory() {

test(function compilerGetDefaultLibFileName() {
setup();
assertEqual(compilerInstance.getDefaultLibFileName(), "$asset$/globals.d.ts");
assertEqual(
compilerInstance.getDefaultLibFileName(),
"$asset$/lib.deno_runtime.d.ts"
);
teardown();
});

Expand All @@ -572,7 +575,7 @@ test(function compilerFileExists() {
"/root/project"
);
assert(compilerInstance.fileExists(moduleMetaData.fileName));
assert(compilerInstance.fileExists("$asset$/globals.d.ts"));
assert(compilerInstance.fileExists("$asset$/lib.deno_runtime.d.ts"));
assertEqual(
compilerInstance.fileExists("/root/project/unknown-module.ts"),
false
Expand All @@ -590,7 +593,7 @@ test(function compilerResolveModuleNames() {
const fixtures: Array<[string, boolean]> = [
["/root/project/foo/bar.ts", false],
["/root/project/foo/baz.ts", false],
["$asset$/globals.d.ts", true]
["$asset$/lib.deno_runtime.d.ts", true]
];
for (let i = 0; i < results.length; i++) {
const result = results[i];
Expand Down
61 changes: 14 additions & 47 deletions js/globals.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,22 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.

import { Console } from "./console";
import * as timers from "./timers";
import * as textEncoding from "./text_encoding";
import * as blob from "./blob";
import * as console from "./console";
import * as fetch_ from "./fetch";
import { libdeno } from "./libdeno";
import { globalEval } from "./global_eval";
import { DenoHeaders } from "./fetch";
import { DenoBlob } from "./blob";

declare global {
interface Window {
define: Readonly<unknown>;

clearTimeout: typeof clearTimeout;
clearInterval: typeof clearInterval;
setTimeout: typeof setTimeout;
setInterval: typeof setInterval;

console: typeof console;
window: typeof window;

fetch: typeof fetch;

TextEncoder: typeof TextEncoder;
TextDecoder: typeof TextDecoder;
atob: typeof atob;
btoa: typeof btoa;
import { libdeno } from "./libdeno";
import * as textEncoding from "./text_encoding";
import * as timers from "./timers";

Headers: typeof Headers;
Blob: typeof Blob;
}
// During the build process, augmentations to the variable `window` in this
// file are tracked and created as part of default library that is built into
// deno, we only need to declare the enough to compile deno.

const clearTimeout: typeof timers.clearTimer;
const clearInterval: typeof timers.clearTimer;
declare global {
const console: console.Console;
const setTimeout: typeof timers.setTimeout;
const setInterval: typeof timers.setInterval;

const console: Console;
const window: Window;

const fetch: typeof fetch_.fetch;

// tslint:disable:variable-name
// tslint:disable-next-line:variable-name
const TextEncoder: typeof textEncoding.TextEncoder;
const TextDecoder: typeof textEncoding.TextDecoder;
const atob: typeof textEncoding.atob;
const btoa: typeof textEncoding.btoa;
const Headers: typeof DenoHeaders;
const Blob: typeof DenoBlob;
// tslint:enable:variable-name
}

// A reference to the global object.
Expand All @@ -61,13 +28,13 @@ window.setInterval = timers.setInterval;
window.clearTimeout = timers.clearTimer;
window.clearInterval = timers.clearTimer;

window.console = new Console(libdeno.print);
window.console = new console.Console(libdeno.print);
window.TextEncoder = textEncoding.TextEncoder;
window.TextDecoder = textEncoding.TextDecoder;
window.atob = textEncoding.atob;
window.btoa = textEncoding.btoa;

window.fetch = fetch_.fetch;

window.Headers = DenoHeaders;
window.Blob = DenoBlob;
window.Headers = fetch_.DenoHeaders;
window.Blob = blob.DenoBlob;
12 changes: 10 additions & 2 deletions js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export default function denoMain() {

setLogDebug(startResMsg.debugFlag());

// handle `--types`
if (startResMsg.typesFlag()) {
const defaultLibFileName = compiler.getDefaultLibFileName();
const defaultLibModule = compiler.resolveModule(defaultLibFileName, "");
console.log(defaultLibModule.sourceCode);
os.exit(0);
}

const cwd = startResMsg.cwd();
log("cwd", cwd);

Expand All @@ -64,8 +72,8 @@ export default function denoMain() {
os.exit(1);
}

const printDeps = startResMsg.depsFlag();
if (printDeps) {
// handle `--deps`
if (startResMsg.depsFlag()) {
for (const dep of compiler.getModuleDependencies(inputFn, `${cwd}/`)) {
console.log(dep);
}
Expand Down
19 changes: 0 additions & 19 deletions js/tsconfig.declarations.json

This file was deleted.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"devDependencies": {
"@types/base64-js": "^1.2.5",
"@types/flatbuffers": "^1.9.0",
"@types/prettier": "^1.13.2",
"@types/source-map-support": "^0.4.1",
"@types/text-encoding": "0.0.33",
"base64-js": "^1.3.0",
Expand All @@ -20,6 +21,8 @@
"rollup-pluginutils": "^2.3.0",
"source-map-support": "^0.5.6",
"text-encoding": "0.6.4",
"ts-node": "^7.0.1",
"ts-simple-ast": "^16.0.3",
"tslint": "^5.10.0",
"tslint-eslint-rules": "^5.3.1",
"tslint-no-circular-imports": "^0.5.0",
Expand Down
10 changes: 1 addition & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ const tsconfigOverride = {
}
};

// this is a preamble for the `globals.d.ts` file to allow it to be the default
// lib for deno.
const libPreamble = `/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
`;

// this is a rollup plugin which will look for imports ending with `!string` and resolve
// them with a module that will inline the contents of the file as a string. Needed to
// support `js/assets.ts`.
Expand Down Expand Up @@ -70,9 +64,7 @@ function strings({ include, exclude } = {}) {
transform(code, id) {
if (filter(id)) {
return {
code: `export default ${JSON.stringify(
id.endsWith("globals.d.ts") ? libPreamble + code : code
)};`,
code: `export default ${JSON.stringify(code)};`,
map: { mappings: "" }
};
}
Expand Down
18 changes: 17 additions & 1 deletion src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct DenoFlags {
pub allow_net: bool,
pub allow_env: bool,
pub deps_flag: bool,
pub types_flag: bool,
}

pub fn process(flags: &DenoFlags) {
Expand Down Expand Up @@ -58,7 +59,8 @@ pub fn print_usage() {
-D or --log-debug Log debug output.
-h or --help Print this message.
--v8-options Print V8 command line options.
--deps Print module dependencies."
--deps Print module dependencies.
--types Print global environment types."
);
}

Expand All @@ -82,6 +84,7 @@ pub fn set_flags(args: Vec<String>) -> (DenoFlags, Vec<String>) {
"--allow-net" => flags.allow_net = true,
"--allow-env" => flags.allow_env = true,
"--deps" => flags.deps_flag = true,
"--types" => flags.types_flag = true,
"--" => break,
_ => unimplemented!(),
}
Expand Down Expand Up @@ -165,6 +168,19 @@ fn test_set_flags_4() {
);
}

#[test]
fn test_set_flags_5() {
let (flags, rest) = set_flags(svec!["deno", "--types"]);
assert_eq!(rest, svec!["deno"]);
assert_eq!(
flags,
DenoFlags {
types_flag: true,
..DenoFlags::default()
}
)
}

// Returns args passed to V8, followed by args passed to JS
fn v8_set_flags_preprocess(args: Vec<String>) -> (Vec<String>, Vec<String>) {
let mut rest = vec![];
Expand Down
1 change: 1 addition & 0 deletions src/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ table StartRes {
debug_flag: bool;
deps_flag: bool;
recompile_flag: bool;
types_flag: bool;
}

table CodeFetch {
Expand Down
1 change: 1 addition & 0 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ fn op_start(
argv: Some(argv_off),
debug_flag: state.flags.log_debug,
recompile_flag: state.flags.recompile,
types_flag: state.flags.types_flag,
..Default::default()
},
);
Expand Down
4 changes: 2 additions & 2 deletions tests/error_003_typescript.ts.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[WILDCARD][0m consol.log("hello world!");
  ~~~~~~

$asset$/globals.d.tsILDCARD]
[WILDCARD]const console: Console;
$asset$/lib.deno_runtime.d.tsILDCARD]
[WILDCARD]const console: console.Console;
[WILDCARD]~~~~~~~
[WILDCARD]'console' is declared here.

2 changes: 1 addition & 1 deletion third_party
Submodule third_party updated 1545 files
Loading