summaryrefslogtreecommitdiff
path: root/cli/js/compiler
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2020-03-29 04:03:49 +1100
committerGitHub <noreply@github.com>2020-03-28 13:03:49 -0400
commitbced52505f32d6cca4f944bb610a8a26767908a8 (patch)
treeda49a5df4b7bd6f8306248069228cd6bd0db1303 /cli/js/compiler
parent1397b8e0e7c85762e19d88fde103342bfa563360 (diff)
Update to Prettier 2 and use ES Private Fields (#4498)
Diffstat (limited to 'cli/js/compiler')
-rw-r--r--cli/js/compiler/api.ts10
-rw-r--r--cli/js/compiler/bootstrap.ts4
-rw-r--r--cli/js/compiler/bundler.ts8
-rw-r--r--cli/js/compiler/host.ts90
-rw-r--r--cli/js/compiler/imports.ts4
-rw-r--r--cli/js/compiler/sourcefile.ts32
-rw-r--r--cli/js/compiler/type_directives.ts4
-rw-r--r--cli/js/compiler/util.ts6
8 files changed, 77 insertions, 81 deletions
diff --git a/cli/js/compiler/api.ts b/cli/js/compiler/api.ts
index 409ad94db..b7c57b528 100644
--- a/cli/js/compiler/api.ts
+++ b/cli/js/compiler/api.ts
@@ -157,7 +157,7 @@ export async function transpileOnly(
util.log("Deno.transpileOnly", { sources: Object.keys(sources), options });
const payload = {
sources,
- options: JSON.stringify(options)
+ options: JSON.stringify(options),
};
const result = await runtimeCompilerOps.transpile(payload);
return JSON.parse(result);
@@ -172,12 +172,12 @@ export async function compile(
rootName: sources ? rootName : checkRelative(rootName),
sources,
options: JSON.stringify(options),
- bundle: false
+ bundle: false,
};
util.log("Deno.compile", {
rootName: payload.rootName,
sources: !!sources,
- options
+ options,
});
const result = await runtimeCompilerOps.compile(payload);
return JSON.parse(result);
@@ -192,12 +192,12 @@ export async function bundle(
rootName: sources ? rootName : checkRelative(rootName),
sources,
options: JSON.stringify(options),
- bundle: true
+ bundle: true,
};
util.log("Deno.bundle", {
rootName: payload.rootName,
sources: !!sources,
- options
+ options,
});
const result = await runtimeCompilerOps.compile(payload);
return JSON.parse(result);
diff --git a/cli/js/compiler/bootstrap.ts b/cli/js/compiler/bootstrap.ts
index 978ddbaf8..63de783cf 100644
--- a/cli/js/compiler/bootstrap.ts
+++ b/cli/js/compiler/bootstrap.ts
@@ -9,7 +9,7 @@ import { getAsset } from "./util.ts";
// load all type definitions and snapshot them.
const host = new Host({
target: CompilerHostTarget.Main,
- writeFile(): void {}
+ writeFile(): void {},
});
const options = host.getCompilationSettings();
@@ -34,7 +34,7 @@ host.getSourceFile(
export const TS_SNAPSHOT_PROGRAM = ts.createProgram({
rootNames: [`${ASSETS}/bootstrap.ts`],
options,
- host
+ host,
});
export const SYSTEM_LOADER = getAsset("system_loader.js");
diff --git a/cli/js/compiler/bundler.ts b/cli/js/compiler/bundler.ts
index 8e35befc8..c9578fe20 100644
--- a/cli/js/compiler/bundler.ts
+++ b/cli/js/compiler/bundler.ts
@@ -14,7 +14,7 @@ function normalizeUrl(rootName: string): string {
path,
false,
"/",
- code => code === CHAR_FORWARD_SLASH
+ (code) => code === CHAR_FORWARD_SLASH
)}`;
} else {
return rootName;
@@ -29,7 +29,7 @@ export function buildBundle(
// when outputting to AMD and a single outfile, TypeScript makes up the module
// specifiers which are used to define the modules, and doesn't expose them
// publicly, so we have to try to replicate
- const sources = sourceFiles.map(sf => sf.fileName);
+ const sources = sourceFiles.map((sf) => sf.fileName);
const sharedPath = commonPath(sources);
rootName = normalizeUrl(rootName)
.replace(sharedPath, "")
@@ -79,7 +79,7 @@ export function setRootExports(program: ts.Program, rootModule: string): void {
// that when there isn't. There appears to be no clean way of figuring that
// out, so inspecting SymbolFlags that might be present that are type only
.filter(
- sym =>
+ (sym) =>
sym.flags & ts.SymbolFlags.Class ||
!(
sym.flags & ts.SymbolFlags.Interface ||
@@ -94,5 +94,5 @@ export function setRootExports(program: ts.Program, rootModule: string): void {
sym.flags & ts.SymbolFlags.TypeAliasExcludes
)
)
- .map(sym => sym.getName());
+ .map((sym) => sym.getName());
}
diff --git a/cli/js/compiler/host.ts b/cli/js/compiler/host.ts
index 457388bd9..70e712ffe 100644
--- a/cli/js/compiler/host.ts
+++ b/cli/js/compiler/host.ts
@@ -9,7 +9,7 @@ import * as util from "../util.ts";
export enum CompilerHostTarget {
Main = "main",
Runtime = "runtime",
- Worker = "worker"
+ Worker = "worker",
}
export interface CompilerHostOptions {
@@ -32,7 +32,7 @@ export const defaultBundlerOptions: ts.CompilerOptions = {
outDir: undefined,
outFile: `${OUT_DIR}/bundle.js`,
// disabled until we have effective way to modify source maps
- sourceMap: false
+ sourceMap: false,
};
export const defaultCompileOptions: ts.CompilerOptions = {
@@ -47,11 +47,11 @@ export const defaultCompileOptions: ts.CompilerOptions = {
sourceMap: true,
strict: true,
stripComments: true,
- target: ts.ScriptTarget.ESNext
+ target: ts.ScriptTarget.ESNext,
};
export const defaultRuntimeCompileOptions: ts.CompilerOptions = {
- outDir: undefined
+ outDir: undefined,
};
export const defaultTranspileOptions: ts.CompilerOptions = {
@@ -59,7 +59,7 @@ export const defaultTranspileOptions: ts.CompilerOptions = {
module: ts.ModuleKind.ESNext,
sourceMap: true,
scriptComments: true,
- target: ts.ScriptTarget.ESNext
+ target: ts.ScriptTarget.ESNext,
};
const ignoredCompilerOptions: readonly string[] = [
@@ -117,43 +117,41 @@ const ignoredCompilerOptions: readonly string[] = [
"types",
"typeRoots",
"version",
- "watch"
+ "watch",
];
-export class Host implements ts.CompilerHost {
- private readonly _options = defaultCompileOptions;
-
- private _target: CompilerHostTarget;
-
- private _writeFile: WriteFileCallback;
-
- private _getAsset(filename: string): SourceFile {
- const lastSegment = filename.split("/").pop()!;
- const url = ts.libMap.has(lastSegment)
- ? ts.libMap.get(lastSegment)!
- : lastSegment;
- const sourceFile = SourceFile.get(url);
- if (sourceFile) {
- return sourceFile;
- }
- const name = url.includes(".") ? url : `${url}.d.ts`;
- const sourceCode = getAsset(name);
- return new SourceFile({
- url,
- filename: `${ASSETS}/${name}`,
- mediaType: MediaType.TypeScript,
- sourceCode
- });
+function getAssetInternal(filename: string): SourceFile {
+ const lastSegment = filename.split("/").pop()!;
+ const url = ts.libMap.has(lastSegment)
+ ? ts.libMap.get(lastSegment)!
+ : lastSegment;
+ const sourceFile = SourceFile.get(url);
+ if (sourceFile) {
+ return sourceFile;
}
+ const name = url.includes(".") ? url : `${url}.d.ts`;
+ const sourceCode = getAsset(name);
+ return new SourceFile({
+ url,
+ filename: `${ASSETS}/${name}`,
+ mediaType: MediaType.TypeScript,
+ sourceCode,
+ });
+}
+
+export class Host implements ts.CompilerHost {
+ readonly #options = defaultCompileOptions;
+ #target: CompilerHostTarget;
+ #writeFile: WriteFileCallback;
/* Deno specific APIs */
constructor({ bundle = false, target, writeFile }: CompilerHostOptions) {
- this._target = target;
- this._writeFile = writeFile;
+ this.#target = target;
+ this.#writeFile = writeFile;
if (bundle) {
// options we need to change when we are generating a bundle
- Object.assign(this._options, defaultBundlerOptions);
+ Object.assign(this.#options, defaultBundlerOptions);
}
}
@@ -175,22 +173,22 @@ export class Host implements ts.CompilerHost {
for (const key of Object.keys(options)) {
if (
ignoredCompilerOptions.includes(key) &&
- (!(key in this._options) || options[key] !== this._options[key])
+ (!(key in this.#options) || options[key] !== this.#options[key])
) {
ignoredOptions.push(key);
delete options[key];
}
}
- Object.assign(this._options, options);
+ Object.assign(this.#options, options);
return {
ignoredOptions: ignoredOptions.length ? ignoredOptions : undefined,
- diagnostics: errors.length ? errors : undefined
+ diagnostics: errors.length ? errors : undefined,
};
}
mergeOptions(...options: ts.CompilerOptions[]): ts.CompilerOptions {
- Object.assign(this._options, ...options);
- return Object.assign({}, this._options);
+ Object.assign(this.#options, ...options);
+ return Object.assign({}, this.#options);
}
/* TypeScript CompilerHost APIs */
@@ -205,7 +203,7 @@ export class Host implements ts.CompilerHost {
getCompilationSettings(): ts.CompilerOptions {
util.log("compiler::host.getCompilationSettings()");
- return this._options;
+ return this.#options;
}
getCurrentDirectory(): string {
@@ -214,7 +212,7 @@ export class Host implements ts.CompilerHost {
getDefaultLibFileName(_options: ts.CompilerOptions): string {
util.log("compiler::host.getDefaultLibFileName()");
- switch (this._target) {
+ switch (this.#target) {
case CompilerHostTarget.Main:
case CompilerHostTarget.Runtime:
return `${ASSETS}/lib.deno.window.d.ts`;
@@ -237,7 +235,7 @@ export class Host implements ts.CompilerHost {
try {
assert(!shouldCreateNewSourceFile);
const sourceFile = fileName.startsWith(ASSETS)
- ? this._getAsset(fileName)
+ ? getAssetInternal(fileName)
: SourceFile.get(fileName);
assert(sourceFile != null);
if (!sourceFile.tsSourceFile) {
@@ -278,12 +276,12 @@ export class Host implements ts.CompilerHost {
): Array<ts.ResolvedModuleFull | undefined> {
util.log("compiler::host.resolveModuleNames", {
moduleNames,
- containingFile
+ containingFile,
});
- return moduleNames.map(specifier => {
+ return moduleNames.map((specifier) => {
const url = SourceFile.getUrl(specifier, containingFile);
const sourceFile = specifier.startsWith(ASSETS)
- ? this._getAsset(specifier)
+ ? getAssetInternal(specifier)
: url
? SourceFile.get(url)
: undefined;
@@ -293,7 +291,7 @@ export class Host implements ts.CompilerHost {
return {
resolvedFileName: sourceFile.url,
isExternalLibraryImport: specifier.startsWith(ASSETS),
- extension: sourceFile.extension
+ extension: sourceFile.extension,
};
});
}
@@ -310,6 +308,6 @@ export class Host implements ts.CompilerHost {
sourceFiles?: readonly ts.SourceFile[]
): void {
util.log("compiler::host.writeFile", fileName);
- this._writeFile(fileName, data, sourceFiles);
+ this.#writeFile(fileName, data, sourceFiles);
}
}
diff --git a/cli/js/compiler/imports.ts b/cli/js/compiler/imports.ts
index a3246c32f..6b48ec945 100644
--- a/cli/js/compiler/imports.ts
+++ b/cli/js/compiler/imports.ts
@@ -34,7 +34,7 @@ function resolvePath(...pathSegments: string[]): string {
resolvedPath,
!resolvedAbsolute,
"/",
- code => code === CHAR_FORWARD_SLASH
+ (code) => code === CHAR_FORWARD_SLASH
);
if (resolvedAbsolute) {
@@ -120,7 +120,7 @@ export function processLocalImports(
url: moduleName,
filename: moduleName,
sourceCode: sources[moduleName],
- mediaType: getMediaType(moduleName)
+ mediaType: getMediaType(moduleName),
});
sourceFile.cache(specifiers[i][0], referrer);
if (!sourceFile.processed) {
diff --git a/cli/js/compiler/sourcefile.ts b/cli/js/compiler/sourcefile.ts
index e400acbf5..a55de080b 100644
--- a/cli/js/compiler/sourcefile.ts
+++ b/cli/js/compiler/sourcefile.ts
@@ -12,7 +12,7 @@ export enum MediaType {
TSX = 3,
Json = 4,
Wasm = 5,
- Unknown = 6
+ Unknown = 6,
}
export interface SourceFileJson {
@@ -50,6 +50,11 @@ function getExtension(fileName: string, mediaType: MediaType): ts.Extension {
}
}
+/** A global cache of module source files that have been loaded. */
+const moduleCache: Map<string, SourceFile> = new Map();
+/** A map of maps which cache source files for quicker modules resolution. */
+const specifierCache: Map<string, Map<string, SourceFile>> = new Map();
+
export class SourceFile {
extension!: ts.Extension;
filename!: string;
@@ -63,20 +68,20 @@ export class SourceFile {
url!: string;
constructor(json: SourceFileJson) {
- if (SourceFile._moduleCache.has(json.url)) {
+ if (moduleCache.has(json.url)) {
throw new TypeError("SourceFile already exists");
}
Object.assign(this, json);
this.extension = getExtension(this.url, this.mediaType);
- SourceFile._moduleCache.set(this.url, this);
+ moduleCache.set(this.url, this);
}
cache(moduleSpecifier: string, containingFile?: string): void {
containingFile = containingFile || "";
- let innerCache = SourceFile._specifierCache.get(containingFile);
+ let innerCache = specifierCache.get(containingFile);
if (!innerCache) {
innerCache = new Map();
- SourceFile._specifierCache.set(containingFile, innerCache);
+ specifierCache.set(containingFile, innerCache);
}
innerCache.set(moduleSpecifier, this);
}
@@ -112,14 +117,14 @@ export class SourceFile {
importedFiles,
referencedFiles,
libReferenceDirectives,
- typeReferenceDirectives
+ typeReferenceDirectives,
} = preProcessedFileInfo;
const typeDirectives = parseTypeDirectives(this.sourceCode);
if (typeDirectives) {
for (const importedFile of importedFiles) {
files.push([
importedFile.fileName,
- getMappedModuleName(importedFile, typeDirectives)
+ getMappedModuleName(importedFile, typeDirectives),
]);
}
} else if (
@@ -145,18 +150,11 @@ export class SourceFile {
return files;
}
- private static _moduleCache: Map<string, SourceFile> = new Map();
-
- private static _specifierCache: Map<
- string,
- Map<string, SourceFile>
- > = new Map();
-
static getUrl(
moduleSpecifier: string,
containingFile: string
): string | undefined {
- const containingCache = this._specifierCache.get(containingFile);
+ const containingCache = specifierCache.get(containingFile);
if (containingCache) {
const sourceFile = containingCache.get(moduleSpecifier);
return sourceFile && sourceFile.url;
@@ -165,10 +163,10 @@ export class SourceFile {
}
static get(url: string): SourceFile | undefined {
- return this._moduleCache.get(url);
+ return moduleCache.get(url);
}
static has(url: string): boolean {
- return this._moduleCache.has(url);
+ return moduleCache.has(url);
}
}
diff --git a/cli/js/compiler/type_directives.ts b/cli/js/compiler/type_directives.ts
index 299532f98..5f33f4b33 100644
--- a/cli/js/compiler/type_directives.ts
+++ b/cli/js/compiler/type_directives.ts
@@ -39,7 +39,7 @@ export function parseTypeDirectives(
directives.push({
fileName,
pos,
- end: pos + matchString.length
+ end: pos + matchString.length,
});
}
if (!directives.length) {
@@ -60,7 +60,7 @@ export function parseTypeDirectives(
const target: FileReference = {
fileName: targetFileName,
pos: targetPos,
- end: targetPos + targetFileName.length
+ end: targetPos + targetFileName.length,
};
results.set(target, fileName);
}
diff --git a/cli/js/compiler/util.ts b/cli/js/compiler/util.ts
index 8acc83a2d..170dff30f 100644
--- a/cli/js/compiler/util.ts
+++ b/cli/js/compiler/util.ts
@@ -33,7 +33,7 @@ export interface WriteFileState {
export enum CompilerRequestType {
Compile = 0,
RuntimeCompile = 1,
- RuntimeTranspile = 2
+ RuntimeTranspile = 2,
}
export const OUT_DIR = "$deno$";
@@ -255,7 +255,7 @@ export function convertCompilerOptions(
}
return {
options: out as ts.CompilerOptions,
- files: files.length ? files : undefined
+ files: files.length ? files : undefined,
};
}
@@ -287,7 +287,7 @@ export const ignoredDiagnostics = [
// TS7016: Could not find a declaration file for module '...'. '...'
// implicitly has an 'any' type. This is due to `allowJs` being off by
// default but importing of a JavaScript module.
- 7016
+ 7016,
];
export function processConfigureResponse(