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

Updated inferred JS/TS project config #194847

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions extensions/typescript-language-features/src/tsServer/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class API {
public static readonly v320 = API.fromSimpleString('3.2.0');
public static readonly v333 = API.fromSimpleString('3.3.3');
public static readonly v340 = API.fromSimpleString('3.4.0');
public static readonly v345 = API.fromSimpleString('3.4.5');
public static readonly v350 = API.fromSimpleString('3.5.0');
public static readonly v370 = API.fromSimpleString('3.7.0');
public static readonly v380 = API.fromSimpleString('3.8.0');
Expand All @@ -32,8 +31,8 @@ export class API {
public static readonly v440 = API.fromSimpleString('4.4.0');
public static readonly v460 = API.fromSimpleString('4.6.0');
public static readonly v470 = API.fromSimpleString('4.7.0');
public static readonly v480 = API.fromSimpleString('4.8.0');
public static readonly v490 = API.fromSimpleString('4.9.0');
public static readonly v500 = API.fromSimpleString('5.0.0');
public static readonly v510 = API.fromSimpleString('5.1.0');
public static readonly v520 = API.fromSimpleString('5.2.0');

Expand Down
31 changes: 18 additions & 13 deletions extensions/typescript-language-features/src/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { TypeScriptServiceConfiguration } from './configuration/configuration';
import { API } from './tsServer/api';
import type * as Proto from './tsServer/protocol/protocol';
import { ITypeScriptServiceClient, ServerResponse } from './typescriptService';
import { nulToken } from './utils/cancellation';
import { TypeScriptServiceConfiguration } from './configuration/configuration';


export const enum ProjectType {
Expand All @@ -19,18 +20,21 @@ export function isImplicitProjectConfigFile(configFileName: string) {
return configFileName.startsWith('/dev/null/');
}

const defaultProjectConfig = Object.freeze<Proto.ExternalProjectCompilerOptions>({
module: 'ESNext' as Proto.ModuleKind,
moduleResolution: 'Node' as Proto.ModuleResolutionKind,
target: 'ES2020' as Proto.ScriptTarget,
jsx: 'react' as Proto.JsxEmit,
});

export function inferredProjectCompilerOptions(
version: API,
projectType: ProjectType,
serviceConfig: TypeScriptServiceConfiguration,
): Proto.ExternalProjectCompilerOptions {
const projectConfig = { ...defaultProjectConfig };
const projectConfig: Proto.ExternalProjectCompilerOptions = {
module: 'ESNext' as Proto.ModuleKind,
moduleResolution: (version.gte(API.v500) ? 'Bundler' : 'Node') as Proto.ModuleResolutionKind,
target: 'ES2022' as Proto.ScriptTarget,
jsx: 'react' as Proto.JsxEmit,
};

if (version.gte(API.v500)) {
projectConfig.allowImportingTsExtensions = true;
}

if (serviceConfig.implicitProjectConfiguration.checkJs) {
projectConfig.checkJs = true;
Expand All @@ -51,7 +55,6 @@ export function inferredProjectCompilerOptions(
projectConfig.strictFunctionTypes = true;
}


if (serviceConfig.implicitProjectConfiguration.module) {
projectConfig.module = serviceConfig.implicitProjectConfiguration.module as Proto.ModuleKind;
}
Expand All @@ -68,10 +71,11 @@ export function inferredProjectCompilerOptions(
}

function inferredProjectConfigSnippet(
version: API,
projectType: ProjectType,
config: TypeScriptServiceConfiguration
) {
const baseConfig = inferredProjectCompilerOptions(projectType, config);
const baseConfig = inferredProjectCompilerOptions(version, projectType, config);
const compilerOptions = Object.keys(baseConfig).map(key => `"${key}": ${JSON.stringify(baseConfig[key])}`);
return new vscode.SnippetString(`{
"compilerOptions": {
Expand All @@ -85,6 +89,7 @@ function inferredProjectConfigSnippet(
}

export async function openOrCreateConfig(
version: API,
projectType: ProjectType,
rootPath: vscode.Uri,
configuration: TypeScriptServiceConfiguration,
Expand All @@ -98,7 +103,7 @@ export async function openOrCreateConfig(
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(inferredProjectConfigSnippet(projectType, configuration));
await editor.insertSnippet(inferredProjectConfigSnippet(version, projectType, configuration));
}
return editor;
}
Expand Down Expand Up @@ -131,7 +136,7 @@ export async function openProjectConfigOrPromptToCreate(

switch (selected) {
case CreateConfigItem:
openOrCreateConfig(projectType, rootPath, client.configuration);
openOrCreateConfig(client.apiVersion, projectType, rootPath, client.configuration);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ export default class TypeScriptServiceClient extends Disposable implements IType

private getCompilerOptionsForInferredProjects(configuration: TypeScriptServiceConfiguration): Proto.ExternalProjectCompilerOptions {
return {
...inferredProjectCompilerOptions(ProjectType.TypeScript, configuration),
...inferredProjectCompilerOptions(this.apiVersion, ProjectType.TypeScript, configuration),
allowJs: true,
allowSyntheticDefaultImports: true,
allowNonTsExtensions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class IntellisenseStatus extends Disposable {
commandManager.register({
id: this.createOrOpenConfigCommandId,
execute: async (root: vscode.Uri, projectType: ProjectType) => {
await openOrCreateConfig(projectType, root, this._client.configuration);
await openOrCreateConfig(this._client.apiVersion, projectType, root, this._client.configuration);
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function onConfigureExcludesSelected(
const root = client.getWorkspaceRootForResource(vscode.Uri.file(configFileName));
if (root) {
openOrCreateConfig(
client.apiVersion,
/tsconfig\.?.*\.json/.test(configFileName) ? ProjectType.TypeScript : ProjectType.JavaScript,
root,
client.configuration);
Expand Down