From b543074861f71653eaef98d812708fe928398817 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 8 Jul 2016 15:13:32 -0700 Subject: [PATCH 1/6] Compile with --noImplicitThis 1. Add to various tsconfig.json 2. Add to Jakefile 3. Add annotations where needed. 4. Add workaround to shims.ts, which uses toplevel `this`. --- Jakefile.js | 2 +- src/compiler/core.ts | 6 +++--- src/compiler/program.ts | 2 +- src/compiler/sys.ts | 4 ++-- src/compiler/tsconfig.json | 1 + src/server/editorServices.ts | 2 +- src/server/tsconfig.json | 1 + src/services/shims.ts | 2 +- src/services/tsconfig.json | 1 + 9 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Jakefile.js b/Jakefile.js index 828f4b084d1..0dd589f90e3 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -284,7 +284,7 @@ var builtLocalCompiler = path.join(builtLocalDirectory, compilerFilename); function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, opts, callback) { file(outFile, prereqs, function() { var compilerPath = useBuiltCompiler ? builtLocalCompiler : LKGCompiler; - var options = "--noImplicitAny --noEmitOnError --types --pretty"; + var options = "--noImplicitAny --noImplicitThis --noEmitOnError --types --pretty"; opts = opts || {}; // Keep comments when specifically requested // or when in debug mode. diff --git a/src/compiler/core.ts b/src/compiler/core.ts index fe4731a1c91..cc6a9e6db9a 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1237,20 +1237,20 @@ namespace ts { getSignatureConstructor(): new (checker: TypeChecker) => Signature; } - function Symbol(flags: SymbolFlags, name: string) { + function Symbol(this: Symbol, flags: SymbolFlags, name: string) { this.flags = flags; this.name = name; this.declarations = undefined; } - function Type(checker: TypeChecker, flags: TypeFlags) { + function Type(this: Type, checker: TypeChecker, flags: TypeFlags) { this.flags = flags; } function Signature(checker: TypeChecker) { } - function Node(kind: SyntaxKind, pos: number, end: number) { + function Node(this: Node, kind: SyntaxKind, pos: number, end: number) { this.kind = kind; this.pos = pos; this.end = end; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 2e4492efa7b..31332a92b0a 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1398,7 +1398,7 @@ namespace ts { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = createTypeChecker(program, /*produceDiagnostics:*/ false)); } - function emit(sourceFile?: SourceFile, writeFileCallback?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult { + function emit(this: Program, sourceFile?: SourceFile, writeFileCallback?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult { return runWithCancellationToken(() => emitWorker(this, sourceFile, writeFileCallback, cancellationToken)); } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 338b6de1e63..5000be8a0ca 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -200,7 +200,7 @@ namespace ts { directoryExists(path: string) { return fso.FolderExists(path); }, - createDirectory(directoryName: string) { + createDirectory(this: System, directoryName: string) { if (!this.directoryExists(directoryName)) { fso.CreateFolder(directoryName); } @@ -500,7 +500,7 @@ namespace ts { }, fileExists, directoryExists, - createDirectory(directoryName: string) { + createDirectory(this: System, directoryName: string) { if (!this.directoryExists(directoryName)) { _fs.mkdirSync(directoryName); } diff --git a/src/compiler/tsconfig.json b/src/compiler/tsconfig.json index 76308c2cba4..5418ca1abe7 100644 --- a/src/compiler/tsconfig.json +++ b/src/compiler/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "noImplicitAny": true, + "noImplicitThis": true, "removeComments": true, "preserveConstEnums": true, "outFile": "../../built/local/tsc.js", diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 6a2beccc243..fc2131d0922 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2081,7 +2081,7 @@ namespace ts.server { const walkFns = { goSubtree: true, done: false, - leaf: function (relativeStart: number, relativeLength: number, ll: LineLeaf) { + leaf: function (this: ILineIndexWalker, relativeStart: number, relativeLength: number, ll: LineLeaf) { if (!f(ll, relativeStart, relativeLength)) { this.done = true; } diff --git a/src/server/tsconfig.json b/src/server/tsconfig.json index 0a8cfb89ab3..57c54b47ad8 100644 --- a/src/server/tsconfig.json +++ b/src/server/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "noImplicitAny": true, + "noImplicitThis": true, "removeComments": true, "preserveConstEnums": true, "out": "../../built/local/tsserver.js", diff --git a/src/services/shims.ts b/src/services/shims.ts index e8b9b59b3cd..e85bf537d30 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -16,7 +16,7 @@ /// /* @internal */ -let debugObjectHost = (this); +let debugObjectHost = new Function("return this")(); // We need to use 'null' to interface with the managed side. /* tslint:disable:no-null-keyword */ diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index 4bf6e87d7a6..26903bf28ec 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "noImplicitAny": true, + "noImplicitThis": true, "removeComments": false, "preserveConstEnums": true, "outFile": "../../built/local/typescriptServices.js", From cf15e825ee012cce09cff590510df7e0227b30d1 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 11 Jul 2016 09:30:58 -0700 Subject: [PATCH 2/6] Fix `this` in harness and improve gulpfile defaults --- Gulpfile.ts | 3 +++ src/harness/harness.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Gulpfile.ts b/Gulpfile.ts index 6f63fc23587..739fc0d3720 100644 --- a/Gulpfile.ts +++ b/Gulpfile.ts @@ -306,6 +306,9 @@ function needsUpdate(source: string | string[], dest: string | string[]): boolea function getCompilerSettings(base: tsc.Settings, useBuiltCompiler?: boolean): tsc.Settings { const copy: tsc.Settings = {}; + // TODO: Add --noImplicitThis --types --pretty when gulp-typescript adds support for them + copy.noImplicitAny = true; + copy.noEmitOnError = true; for (const key in base) { copy[key] = base[key]; } diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 1977d95492c..985d00bcf63 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -127,7 +127,7 @@ namespace Utils { export function memoize(f: T): T { const cache: { [idx: string]: any } = {}; - return (function() { + return (function(this: any) { const key = Array.prototype.join.call(arguments); const cachedResult = cache[key]; if (cachedResult) { From 6d21cf6434fcb7009433928878911d5fac0d56eb Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 11 Jul 2016 10:32:35 -0700 Subject: [PATCH 3/6] Add more default options to gulpfile --- Gulpfile.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Gulpfile.ts b/Gulpfile.ts index 739fc0d3720..f0757c89bba 100644 --- a/Gulpfile.ts +++ b/Gulpfile.ts @@ -11,8 +11,11 @@ import newer = require("gulp-newer"); import tsc = require("gulp-typescript"); declare module "gulp-typescript" { interface Settings { - stripInternal?: boolean; + pretty?: boolean; newLine?: string; + noImplicitThis?: boolean; + stripInternal?: boolean; + types?: string[]; } interface CompileStream extends NodeJS.ReadWriteStream {} // Either gulp or gulp-typescript has some odd typings which don't reflect reality, making this required } @@ -306,9 +309,11 @@ function needsUpdate(source: string | string[], dest: string | string[]): boolea function getCompilerSettings(base: tsc.Settings, useBuiltCompiler?: boolean): tsc.Settings { const copy: tsc.Settings = {}; - // TODO: Add --noImplicitThis --types --pretty when gulp-typescript adds support for them - copy.noImplicitAny = true; copy.noEmitOnError = true; + copy.noImplicitAny = true; + copy.noImplicitThis = true; + copy.pretty = true; + copy.types = []; for (const key in base) { copy[key] = base[key]; } From a3d9a855460eee7e902023a01c2271f6f2432743 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 11 Jul 2016 11:00:06 -0700 Subject: [PATCH 4/6] Add --pretty to tsconfigs --- src/compiler/tsconfig.json | 1 + src/server/tsconfig.json | 1 + src/services/tsconfig.json | 1 + 3 files changed, 3 insertions(+) diff --git a/src/compiler/tsconfig.json b/src/compiler/tsconfig.json index 5418ca1abe7..827a9b81c4d 100644 --- a/src/compiler/tsconfig.json +++ b/src/compiler/tsconfig.json @@ -4,6 +4,7 @@ "noImplicitThis": true, "removeComments": true, "preserveConstEnums": true, + "pretty": true, "outFile": "../../built/local/tsc.js", "sourceMap": true, "declaration": true, diff --git a/src/server/tsconfig.json b/src/server/tsconfig.json index 57c54b47ad8..df7dcdfe8ad 100644 --- a/src/server/tsconfig.json +++ b/src/server/tsconfig.json @@ -4,6 +4,7 @@ "noImplicitThis": true, "removeComments": true, "preserveConstEnums": true, + "pretty": true, "out": "../../built/local/tsserver.js", "sourceMap": true, "stripInternal": true diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index 26903bf28ec..86efd254937 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -4,6 +4,7 @@ "noImplicitThis": true, "removeComments": false, "preserveConstEnums": true, + "pretty": true, "outFile": "../../built/local/typescriptServices.js", "sourceMap": true, "stripInternal": true, From a138e6307fa6e9d60e7179f4aa5e49617fd5fd9c Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 11 Jul 2016 11:30:22 -0700 Subject: [PATCH 5/6] Avoid using `this` in object literals where possible --- src/compiler/sys.ts | 14 ++++++++------ src/server/editorServices.ts | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 5000be8a0ca..29ae2c60af1 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -182,7 +182,7 @@ namespace ts { return matchFiles(path, extensions, excludes, includes, /*useCaseSensitiveFileNames*/ false, shell.CurrentDirectory, getAccessibleFileSystemEntries); } - return { + const wscriptSystem: System = { args, newLine: "\r\n", useCaseSensitiveFileNames: false, @@ -200,8 +200,8 @@ namespace ts { directoryExists(path: string) { return fso.FolderExists(path); }, - createDirectory(this: System, directoryName: string) { - if (!this.directoryExists(directoryName)) { + createDirectory(directoryName: string) { + if (!wscriptSystem.directoryExists(directoryName)) { fso.CreateFolder(directoryName); } }, @@ -221,6 +221,7 @@ namespace ts { } } }; + return wscriptSystem; } function getNodeSystem(): System { @@ -439,7 +440,7 @@ namespace ts { return filter(_fs.readdirSync(path), p => fileSystemEntryExists(combinePaths(path, p), FileSystemEntryKind.Directory)); } - return { + const nodeSystem: System = { args: process.argv.slice(2), newLine: _os.EOL, useCaseSensitiveFileNames: useCaseSensitiveFileNames, @@ -500,8 +501,8 @@ namespace ts { }, fileExists, directoryExists, - createDirectory(this: System, directoryName: string) { - if (!this.directoryExists(directoryName)) { + createDirectory(directoryName: string) { + if (!nodeSystem.directoryExists(directoryName)) { _fs.mkdirSync(directoryName); } }, @@ -549,6 +550,7 @@ namespace ts { return _fs.realpathSync(path); } }; + return nodeSystem; } function getChakraSystem(): System { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index fc2131d0922..8854ea9f432 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2081,9 +2081,9 @@ namespace ts.server { const walkFns = { goSubtree: true, done: false, - leaf: function (this: ILineIndexWalker, relativeStart: number, relativeLength: number, ll: LineLeaf) { + leaf: function (relativeStart: number, relativeLength: number, ll: LineLeaf) { if (!f(ll, relativeStart, relativeLength)) { - this.done = true; + walkFns.done = true; } } }; From 6414a5721c833148d646b91b6f68ee0ea1c265aa Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 11 Jul 2016 13:49:36 -0700 Subject: [PATCH 6/6] Remove another use of `this`, in program.ts --- src/compiler/program.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 31332a92b0a..af0a13d640b 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1398,8 +1398,8 @@ namespace ts { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = createTypeChecker(program, /*produceDiagnostics:*/ false)); } - function emit(this: Program, sourceFile?: SourceFile, writeFileCallback?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult { - return runWithCancellationToken(() => emitWorker(this, sourceFile, writeFileCallback, cancellationToken)); + function emit(sourceFile?: SourceFile, writeFileCallback?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult { + return runWithCancellationToken(() => emitWorker(program, sourceFile, writeFileCallback, cancellationToken)); } function isEmitBlocked(emitFileName: string): boolean {