Skip to content

Commit

Permalink
Upgrade to TypeScript 3.4.1 (denoland#2027)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored and 王松 committed Apr 2, 2019
1 parent 4058ab8 commit 0d07681
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 86 deletions.
10 changes: 10 additions & 0 deletions js/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ import libEs2017SharedmemoryDts from "/third_party/node_modules/typescript/lib/l
import libEs2017StringDts from "/third_party/node_modules/typescript/lib/lib.es2017.string.d.ts!string";
import libEs2017TypedarraysDts from "/third_party/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts!string";
import libEs2018Dts from "/third_party/node_modules/typescript/lib/lib.es2018.d.ts!string";
import libEs2018AsyncIterableDts from "/third_party/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts!string";
import libEs2018IntlDts from "/third_party/node_modules/typescript/lib/lib.es2018.intl.d.ts!string";
import libEs2018PromiseDts from "/third_party/node_modules/typescript/lib/lib.es2018.promise.d.ts!string";
import libEs2018RegexpDts from "/third_party/node_modules/typescript/lib/lib.es2018.regexp.d.ts!string";
import libEs2019Dts from "/third_party/node_modules/typescript/lib/lib.es2019.d.ts!string";
import libEs2019ArrayDts from "/third_party/node_modules/typescript/lib/lib.es2019.array.d.ts!string";
import libEs2019StringDts from "/third_party/node_modules/typescript/lib/lib.es2019.string.d.ts!string";
import libEs2019SymbolDts from "/third_party/node_modules/typescript/lib/lib.es2019.symbol.d.ts!string";
import libEs5Dts from "/third_party/node_modules/typescript/lib/lib.es5.d.ts!string";
import libEsnextArrayDts from "/third_party/node_modules/typescript/lib/lib.esnext.array.d.ts!string";
import libEsnextAsynciterablesDts from "/third_party/node_modules/typescript/lib/lib.esnext.asynciterable.d.ts!string";
Expand Down Expand Up @@ -64,9 +69,14 @@ export const assetSourceCode: { [key: string]: string } = {
"lib.es2017.string.d.ts": libEs2017StringDts,
"lib.es2017.typedarrays.d.ts": libEs2017TypedarraysDts,
"lib.es2018.d.ts": libEs2018Dts,
"lib.es2018.asynciterable.d.ts": libEs2018AsyncIterableDts,
"lib.es2018.intl.d.ts": libEs2018IntlDts,
"lib.es2018.promise.d.ts": libEs2018PromiseDts,
"lib.es2018.regexp.d.ts": libEs2018RegexpDts,
"lib.es2019.d.ts": libEs2019Dts,
"lib.es2019.array.d.ts": libEs2019ArrayDts,
"lib.es2019.string.d.ts": libEs2019StringDts,
"lib.es2019.symbol.d.ts": libEs2019SymbolDts,
"lib.es5.d.ts": libEs5Dts,
"lib.esnext.d.ts": libEsnextDts,
"lib.esnext.array.d.ts": libEsnextArrayDts,
Expand Down
1 change: 1 addition & 0 deletions js/globals_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test(function windowWindowExists() {
});

test(function globalThisEqualsWindow() {
// @ts-ignore (TypeScript thinks globalThis and window don't match)
assert(globalThis === window);
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"rollup-pluginutils": "2.4.1",
"ts-morph": "1.3.0",
"ts-node": "8.0.2",
"typescript": "3.3.3333"
"typescript": "3.4.1"
}
}
2 changes: 1 addition & 1 deletion third_party
Submodule third_party updated 29 files
+2 −2 node_modules/.yarn-integrity
+0 −334 node_modules/typescript/.failed-tests
+658 −214 node_modules/typescript/lib/lib.dom.d.ts
+7 −0 node_modules/typescript/lib/lib.dom.iterable.d.ts
+1 −1 node_modules/typescript/lib/lib.es2015.collection.d.ts
+1 −1 node_modules/typescript/lib/lib.es2015.iterable.d.ts
+2 −2 node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts
+44 −0 node_modules/typescript/lib/lib.es2018.asynciterable.d.ts
+1 −0 node_modules/typescript/lib/lib.es2018.d.ts
+223 −0 node_modules/typescript/lib/lib.es2019.array.d.ts
+24 −0 node_modules/typescript/lib/lib.es2019.d.ts
+25 −0 node_modules/typescript/lib/lib.es2019.full.d.ts
+33 −0 node_modules/typescript/lib/lib.es2019.string.d.ts
+26 −0 node_modules/typescript/lib/lib.es2019.symbol.d.ts
+39 −39 node_modules/typescript/lib/lib.es5.d.ts
+4 −4 node_modules/typescript/lib/lib.esnext.bigint.d.ts
+1 −4 node_modules/typescript/lib/lib.esnext.d.ts
+158 −24 node_modules/typescript/lib/lib.webworker.d.ts
+25 −0 node_modules/typescript/lib/protocol.d.ts
+3,755 −1,988 node_modules/typescript/lib/tsc.js
+6,462 −3,105 node_modules/typescript/lib/tsserver.js
+153 −69 node_modules/typescript/lib/tsserverlibrary.d.ts
+5,450 −2,946 node_modules/typescript/lib/tsserverlibrary.js
+123 −59 node_modules/typescript/lib/typescript.d.ts
+5,271 −2,813 node_modules/typescript/lib/typescript.js
+122 −58 node_modules/typescript/lib/typescriptServices.d.ts
+5,270 −2,813 node_modules/typescript/lib/typescriptServices.js
+4,314 −2,477 node_modules/typescript/lib/typingsInstaller.js
+13 −20 node_modules/typescript/package.json
156 changes: 86 additions & 70 deletions tools/ts_library_builder/ast_util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { relative } from "path";
import { basename, dirname, join, relative } from "path";
import { readFileSync } from "fs";
import { EOL } from "os";
import {
Expand All @@ -12,6 +12,7 @@ import {
SourceFile,
StatementedNode,
ts,
TypeAliasDeclaration,
TypeGuards,
VariableStatement,
VariableDeclarationKind
Expand All @@ -20,7 +21,7 @@ import {
let silent = false;

/** Logs a message to the console. */
export function log(message: any = "", ...args: any[]) {
export function log(message: any = "", ...args: any[]): void {
if (!silent) {
console.log(message, ...args);
}
Expand Down Expand Up @@ -64,7 +65,7 @@ export function addTypeAlias(
type: string,
hasDeclareKeyword = false,
jsdocs?: JSDoc[]
) {
): TypeAliasDeclaration {
return node.addTypeAlias({
name,
type,
Expand Down Expand Up @@ -112,8 +113,30 @@ export function appendSourceFile(
targetSourceFile.addStatements(`\n${sourceFile.print()}`);
}

/** Used when formatting diagnostics */
const formatDiagnosticHost: ts.FormatDiagnosticsHost = {
getCurrentDirectory() {
return process.cwd();
},
getCanonicalFileName(path: string) {
return path;
},
getNewLine() {
return EOL;
}
};

/** Log diagnostics to the console with colour. */
export function logDiagnostics(diagnostics: ts.Diagnostic[]): void {
if (diagnostics.length) {
console.log(
ts.formatDiagnosticsWithColorAndContext(diagnostics, formatDiagnosticHost)
);
}
}

/** Check diagnostics, and if any exist, exit the process */
export function checkDiagnostics(project: Project, onlyFor?: string[]) {
export function checkDiagnostics(project: Project, onlyFor?: string[]): void {
const program = project.getProgram();
const diagnostics = [
...program.getGlobalDiagnostics(),
Expand Down Expand Up @@ -154,6 +177,32 @@ export interface FlattenNamespaceOptions {
sourceFile: SourceFile;
}

/** Returns a string which indicates the source file as the source */
export function getSourceComment(
sourceFile: SourceFile,
rootPath: string
): string {
return `\n// @url ${relative(rootPath, sourceFile.getFilePath())}\n\n`;
}

/** Return a set of fully qualified symbol names for the files exports */
function getExportedSymbols(sourceFile: SourceFile): Set<string> {
const exportedSymbols = new Set<string>();
const exportDeclarations = sourceFile.getExportDeclarations();
for (const exportDeclaration of exportDeclarations) {
const exportSpecifiers = exportDeclaration.getNamedExports();
for (const exportSpecifier of exportSpecifiers) {
const aliasedSymbol = exportSpecifier
.getSymbolOrThrow()
.getAliasedSymbol();
if (aliasedSymbol) {
exportedSymbols.add(aliasedSymbol.getFullyQualifiedName());
}
}
}
return exportedSymbols;
}

/** Take a namespace and flatten all exports. */
export function flattenNamespace({
customSources,
Expand All @@ -167,15 +216,16 @@ export function flattenNamespace({

function flattenDeclarations(
declaration: ImportDeclaration | ExportDeclaration
) {
): void {
const declarationSourceFile = declaration.getModuleSpecifierSourceFile();
if (declarationSourceFile) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
processSourceFile(declarationSourceFile);
declaration.remove();
}
}

function rectifyNodes(currentSourceFile: SourceFile) {
function rectifyNodes(currentSourceFile: SourceFile): void {
currentSourceFile.forEachChild(node => {
if (TypeGuards.isAmbientableNode(node)) {
node.setHasDeclareKeyword(false);
Expand All @@ -192,7 +242,9 @@ export function flattenNamespace({
});
}

function processSourceFile(currentSourceFile: SourceFile) {
function processSourceFile(
currentSourceFile: SourceFile
): string | undefined {
if (sourceFiles.has(currentSourceFile)) {
return;
}
Expand Down Expand Up @@ -237,45 +289,6 @@ export function flattenNamespace({
);
}

/** Used when formatting diagnostics */
const formatDiagnosticHost: ts.FormatDiagnosticsHost = {
getCurrentDirectory() {
return process.cwd();
},
getCanonicalFileName(path: string) {
return path;
},
getNewLine() {
return EOL;
}
};

/** Return a set of fully qualified symbol names for the files exports */
function getExportedSymbols(sourceFile: SourceFile): Set<string> {
const exportedSymbols = new Set<string>();
const exportDeclarations = sourceFile.getExportDeclarations();
for (const exportDeclaration of exportDeclarations) {
const exportSpecifiers = exportDeclaration.getNamedExports();
for (const exportSpecifier of exportSpecifiers) {
const aliasedSymbol = exportSpecifier
.getSymbolOrThrow()
.getAliasedSymbol();
if (aliasedSymbol) {
exportedSymbols.add(aliasedSymbol.getFullyQualifiedName());
}
}
}
return exportedSymbols;
}

/** Returns a string which indicates the source file as the source */
export function getSourceComment(
sourceFile: SourceFile,
rootPath: string
): string {
return `\n// @url ${relative(rootPath, sourceFile.getFilePath())}\n\n`;
}

interface InlineFilesOptions {
basePath: string;
debug?: boolean;
Expand All @@ -289,7 +302,7 @@ export function inlineFiles({
debug,
inline,
targetSourceFile
}: InlineFilesOptions) {
}: InlineFilesOptions): void {
for (const filename of inline) {
const text = readFileSync(filename, {
encoding: "utf8"
Expand All @@ -302,11 +315,34 @@ export function inlineFiles({
}
}

/** Load a set of files into a file system host. */
export function loadFiles(
project: Project,
filePaths: string[],
rebase?: string
): void {
const fileSystem = project.getFileSystem();
for (const filePath of filePaths) {
const fileText = readFileSync(filePath, {
encoding: "utf8"
});
fileSystem.writeFileSync(
rebase ? join(rebase, basename(filePath)) : filePath,
fileText
);
}
}

/**
* Load and write to a virtual file system all the default libs needed to
* resolve types on project.
*/
export function loadDtsFiles(project: Project) {
export function loadDtsFiles(
project: Project,
compilerOptions: ts.CompilerOptions
): void {
const libSourcePath = dirname(ts.getDefaultLibFilePath(compilerOptions));
// TODO (@kitsonk) Add missing libs when ts-morph supports TypeScript 3.4
loadFiles(
project,
[
Expand All @@ -331,37 +367,17 @@ export function loadDtsFiles(project: Project) {
"lib.es2018.d.ts",
"lib.es2018.intl.d.ts",
"lib.es2018.promise.d.ts",
"lib.es2018.regexp.d.ts",
"lib.es5.d.ts",
"lib.esnext.d.ts",
"lib.esnext.array.d.ts",
"lib.esnext.asynciterable.d.ts",
"lib.esnext.intl.d.ts",
"lib.esnext.symbol.d.ts"
].map(fileName => `node_modules/typescript/lib/${fileName}`)
].map(fileName => join(libSourcePath, fileName)),
"node_modules/typescript/lib/"
);
}

/** Load a set of files into a file system host. */
export function loadFiles(project: Project, filePaths: string[]) {
const fileSystem = project.getFileSystem();
for (const filePath of filePaths) {
const fileText = readFileSync(filePath, {
encoding: "utf8"
});
fileSystem.writeFileSync(filePath, fileText);
}
}

/** Log diagnostics to the console with colour. */
export function logDiagnostics(diagnostics: ts.Diagnostic[]): void {
if (diagnostics.length) {
console.log(
ts.formatDiagnosticsWithColorAndContext(diagnostics, formatDiagnosticHost)
);
}
}

export interface NamespaceSourceFileOptions {
debug?: boolean;
namespace?: string;
Expand Down
18 changes: 10 additions & 8 deletions tools/ts_library_builder/build_library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,19 +439,21 @@ export function main({

// the outputProject will contain the final library file we are looking to
// build
const outputProjectCompilerOptions: ts.CompilerOptions = {
baseUrl: buildPath,
lib: ["esnext"],
moduleResolution: ModuleResolutionKind.NodeJs,
strict: true,
target: ScriptTarget.ESNext
};

const outputProject = new Project({
compilerOptions: {
baseUrl: buildPath,
lib: ["esnext"],
moduleResolution: ModuleResolutionKind.NodeJs,
strict: true,
target: ScriptTarget.ESNext
},
compilerOptions: outputProjectCompilerOptions,
useVirtualFileSystem: true
});

// There are files we need to load into memory, so that the project "compiles"
loadDtsFiles(outputProject);
loadDtsFiles(outputProject, outputProjectCompilerOptions);

// libDts is the final output file we are looking to build and we are not
// actually creating it, only in memory at this stage.
Expand Down
13 changes: 7 additions & 6 deletions tools/ts_library_builder/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { inlineFiles, loadDtsFiles } from "./ast_util";
const { ModuleKind, ModuleResolutionKind, ScriptTarget } = ts;

/** setups and returns the fixtures for testing */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function setupFixtures() {
const basePath = process.cwd();
const buildPath = `${basePath}/tools/ts_library_builder/testdata`;
Expand All @@ -35,15 +36,15 @@ function setupFixtures() {
compilerOptions: {},
useVirtualFileSystem: true
});
loadDtsFiles(declarationProject);
loadDtsFiles(declarationProject, {});
for (const { filePath, text } of inputProject.emitToMemory().getFiles()) {
declarationProject.createSourceFile(filePath, text);
}
const outputProject = new Project({
compilerOptions: {},
useVirtualFileSystem: true
});
loadDtsFiles(outputProject);
loadDtsFiles(outputProject, {});
const outputSourceFile = outputProject.createSourceFile(outputFile);
const debug = true;

Expand All @@ -59,7 +60,7 @@ function setupFixtures() {
};
}

function buildLibraryFlatten() {
function buildLibraryFlatten(): void {
const {
basePath,
buildPath,
Expand Down Expand Up @@ -135,7 +136,7 @@ function buildLibraryFlatten() {
assert.equal(variableDeclarationsNs.length, 1);
}

function buildLibraryMerge() {
function buildLibraryMerge(): void {
const {
basePath,
buildPath,
Expand Down Expand Up @@ -198,7 +199,7 @@ function buildLibraryMerge() {
assert.equal(interfaceProperties![1].type, "number");
}

function testInlineFiles() {
function testInlineFiles(): void {
const {
basePath,
buildPath,
Expand All @@ -220,7 +221,7 @@ function testInlineFiles() {

// TODO author unit tests for `ast_util.ts`

function main() {
function main(): void {
console.log("ts_library_builder buildLibraryFlatten");
buildLibraryFlatten();
console.log("ts_library_builder buildLibraryMerge");
Expand Down

0 comments on commit 0d07681

Please sign in to comment.