From abcdfe2c8c6d295ce4cf26f886f21b0cf14cd6dc Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 26 Oct 2017 18:11:03 -0700 Subject: [PATCH] Set checkjs when generating jsconfig if javascript.implicitProjectConfig.checkJS is set Fixes #37011 --- extensions/typescript/src/typescriptMain.ts | 2 +- .../typescript/src/typescriptService.ts | 3 ++ .../typescript/src/typescriptServiceClient.ts | 40 ++++++++++--------- .../typescript/src/utils/projectStatus.ts | 14 +++++-- extensions/typescript/src/utils/tsconfig.ts | 22 ++++++++-- 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/extensions/typescript/src/typescriptMain.ts b/extensions/typescript/src/typescriptMain.ts index b7f99cb8024..839f4ad5fd0 100644 --- a/extensions/typescript/src/typescriptMain.ts +++ b/extensions/typescript/src/typescriptMain.ts @@ -570,7 +570,7 @@ class TypeScriptServiceClientHost implements ITypescriptServiceClientHost { switch (selected && selected.id) { case ProjectConfigAction.CreateConfig: - openOrCreateConfigFile(isTypeScriptProject, rootPath); + openOrCreateConfigFile(isTypeScriptProject, rootPath, this.client.configuration); return; case ProjectConfigAction.LearnMore: diff --git a/extensions/typescript/src/typescriptService.ts b/extensions/typescript/src/typescriptService.ts index bf367883cb5..ec72b5c899c 100644 --- a/extensions/typescript/src/typescriptService.ts +++ b/extensions/typescript/src/typescriptService.ts @@ -8,6 +8,7 @@ import { CancellationToken, Uri, Event } from 'vscode'; import * as Proto from './protocol'; import API from './utils/api'; import { TypeScriptServerPlugin } from './utils/plugins'; +import { TypeScriptServiceConfiguration } from './utils/configuration'; export interface ITypescriptServiceClientHost { syntaxDiagnosticsReceived(event: Proto.DiagnosticEvent): void; @@ -37,6 +38,8 @@ export interface ITypescriptServiceClient { plugins: TypeScriptServerPlugin[]; + configuration: TypeScriptServiceConfiguration; + execute(command: 'configure', args: Proto.ConfigureRequestArguments, token?: CancellationToken): Promise; execute(command: 'open', args: Proto.OpenRequestArgs, expectedResult: boolean, token?: CancellationToken): Promise; execute(command: 'close', args: Proto.FileRequestArgs, expectedResult: boolean, token?: CancellationToken): Promise; diff --git a/extensions/typescript/src/typescriptServiceClient.ts b/extensions/typescript/src/typescriptServiceClient.ts index a3528045852..6d596f81ac8 100644 --- a/extensions/typescript/src/typescriptServiceClient.ts +++ b/extensions/typescript/src/typescriptServiceClient.ts @@ -124,7 +124,7 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient private pathSeparator: string; private _onReady: { promise: Promise; resolve: () => void; reject: () => void; }; - private configuration: TypeScriptServiceConfiguration; + private _configuration: TypeScriptServiceConfiguration; private versionProvider: TypeScriptVersionProvider; private versionPicker: TypeScriptVersionPicker; @@ -173,28 +173,28 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient this.requestQueue = new RequestQueue(); this.callbacks = new CallbackMap(); - this.configuration = TypeScriptServiceConfiguration.loadFromWorkspace(); - this.versionProvider = new TypeScriptVersionProvider(this.configuration); + this._configuration = TypeScriptServiceConfiguration.loadFromWorkspace(); + this.versionProvider = new TypeScriptVersionProvider(this._configuration); this.versionPicker = new TypeScriptVersionPicker(this.versionProvider, this.workspaceState); this._apiVersion = API.defaultVersion; this.tracer = new Tracer(this.logger); workspace.onDidChangeConfiguration(() => { - const oldConfiguration = this.configuration; - this.configuration = TypeScriptServiceConfiguration.loadFromWorkspace(); + const oldConfiguration = this._configuration; + this._configuration = TypeScriptServiceConfiguration.loadFromWorkspace(); - this.versionProvider.updateConfiguration(this.configuration); + this.versionProvider.updateConfiguration(this._configuration); this.tracer.updateConfiguration(); if (this.servicePromise) { - if (this.configuration.checkJs !== oldConfiguration.checkJs - || this.configuration.experimentalDecorators !== oldConfiguration.experimentalDecorators + if (this._configuration.checkJs !== oldConfiguration.checkJs + || this._configuration.experimentalDecorators !== oldConfiguration.experimentalDecorators ) { - this.setCompilerOptionsForInferredProjects(this.configuration); + this.setCompilerOptionsForInferredProjects(this._configuration); } - if (!this.configuration.isEqualTo(oldConfiguration)) { + if (!this._configuration.isEqualTo(oldConfiguration)) { this.restartTsServer(); } } @@ -204,6 +204,10 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient this.startService(); } + public get configuration() { + return this._configuration; + } + public dispose() { if (this.servicePromise) { this.servicePromise.then(cp => { @@ -333,7 +337,7 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient args.push('--useSingleInferredProject'); } - if (this.configuration.disableAutomaticTypeAcquisition) { + if (this._configuration.disableAutomaticTypeAcquisition) { args.push('--disableAutomaticTypingAcquisition'); } } @@ -346,7 +350,7 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient } if (this.apiVersion.has222Features()) { - if (this.configuration.tsServerLogLevel !== TsServerLogLevel.Off) { + if (this._configuration.tsServerLogLevel !== TsServerLogLevel.Off) { try { const logDir = fs.mkdtempSync(path.join(os.tmpdir(), `vscode-tsserver-log-`)); this.tsServerLogFile = path.join(logDir, `tsserver.log`); @@ -356,7 +360,7 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient } if (this.tsServerLogFile) { - args.push('--logVerbosity', TsServerLogLevel.toString(this.configuration.tsServerLogLevel)); + args.push('--logVerbosity', TsServerLogLevel.toString(this._configuration.tsServerLogLevel)); args.push('--logFile', this.tsServerLogFile); } } @@ -372,13 +376,13 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient } if (this.apiVersion.has234Features()) { - if (this.configuration.npmLocation) { - args.push('--npmLocation', `"${this.configuration.npmLocation}"`); + if (this._configuration.npmLocation) { + args.push('--npmLocation', `"${this._configuration.npmLocation}"`); } } if (this.apiVersion.has260Features()) { - const tsLocale = getTsLocale(this.configuration); + const tsLocale = getTsLocale(this._configuration); if (tsLocale) { args.push('--locale', tsLocale); } @@ -472,7 +476,7 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient .then(() => false); } - if (this.configuration.tsServerLogLevel === TsServerLogLevel.Off) { + if (this._configuration.tsServerLogLevel === TsServerLogLevel.Off) { return window.showErrorMessage( localize( 'typescript.openTsServerLog.loggingNotEnabled', @@ -513,7 +517,7 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient hostInfo: 'vscode' }; this.execute('configure', configureOptions); - this.setCompilerOptionsForInferredProjects(this.configuration); + this.setCompilerOptionsForInferredProjects(this._configuration); if (resendModels) { this.host.populateService(); } diff --git a/extensions/typescript/src/utils/projectStatus.ts b/extensions/typescript/src/utils/projectStatus.ts index ef95074e01b..f46f509e674 100644 --- a/extensions/typescript/src/utils/projectStatus.ts +++ b/extensions/typescript/src/utils/projectStatus.ts @@ -152,7 +152,10 @@ function createLargeProjectMonitorFromTypeScript(item: ExcludeHintItem, client: }); } -function onConfigureExcludesSelected(client: ITypescriptServiceClient, configFileName: string) { +function onConfigureExcludesSelected( + client: ITypescriptServiceClient, + configFileName: string +) { if (!isImplicitProjectConfigFile(configFileName)) { vscode.workspace.openTextDocument(configFileName) .then(vscode.window.showTextDocument); @@ -161,12 +164,17 @@ function onConfigureExcludesSelected(client: ITypescriptServiceClient, configFil if (root) { openOrCreateConfigFile( configFileName.match(/tsconfig\.?.*\.json/) !== null, - root); + root, + client.configuration); } } } -export function create(client: ITypescriptServiceClient, isOpen: (path: string) => Promise, memento: vscode.Memento) { +export function create( + client: ITypescriptServiceClient, + isOpen: (path: string) => Promise, + memento: vscode.Memento +) { const toDispose: vscode.Disposable[] = []; const item = new ExcludeHintItem(client); diff --git a/extensions/typescript/src/utils/tsconfig.ts b/extensions/typescript/src/utils/tsconfig.ts index 0097140de03..725fc16133f 100644 --- a/extensions/typescript/src/utils/tsconfig.ts +++ b/extensions/typescript/src/utils/tsconfig.ts @@ -5,24 +5,38 @@ import * as vscode from 'vscode'; import * as path from 'path'; +import { TypeScriptServiceConfiguration } from './configuration'; export function isImplicitProjectConfigFile(configFileName: string) { return configFileName.indexOf('/dev/null/') === 0; } -const emptyConfig = new vscode.SnippetString(`{ +function getEmptyConfig( + isTypeScriptProject: boolean, + config: TypeScriptServiceConfiguration +) { + const compilerOptions = ['"target": "ES6"']; + if (!isTypeScriptProject && config.checkJs) { + compilerOptions.push('"checkJs": true'); + } + if (!isTypeScriptProject && config.experimentalDecorators) { + compilerOptions.push('"experimentalDecorators": true'); + } + return new vscode.SnippetString(`{ "compilerOptions": { - "target": "ES6"$0 + ${compilerOptions.join(',\n\t\t')}$0 }, "exclude": [ "node_modules", "**/node_modules/*" ] }`); +} export function openOrCreateConfigFile( isTypeScriptProject: boolean, - rootPath: string + rootPath: string, + config: TypeScriptServiceConfiguration ): Thenable { const configFile = vscode.Uri.file(path.join(rootPath, isTypeScriptProject ? 'tsconfig.json' : 'jsconfig.json')); const col = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined; @@ -33,7 +47,7 @@ export function openOrCreateConfigFile( const doc = await vscode.workspace.openTextDocument(configFile.with({ scheme: 'untitled' })); const editor = await vscode.window.showTextDocument(doc, col); if (editor.document.getText().length === 0) { - await editor.insertSnippet(emptyConfig); + await editor.insertSnippet(getEmptyConfig(isTypeScriptProject, config)); } return editor; });