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

feat(tsconfig): module target #468

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/typescriptServiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export default class TypeScriptServiceClient extends Disposable implements IType
configuration: TypeScriptServiceConfiguration
): Proto.ExternalProjectCompilerOptions {
return {
...inferredProjectCompilerOptions(ProjectType.TypeScript, configuration),
...inferredProjectCompilerOptions(this.apiVersion, ProjectType.TypeScript, configuration),
allowJs: true,
allowSyntheticDefaultImports: true,
allowNonTsExtensions: true
Expand Down
2 changes: 2 additions & 0 deletions src/server/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export default class API {
public static readonly v460 = API.fromSimpleString('4.6.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 v540 = API.fromSimpleString('5.4.0');

public static fromVersionString(versionString: string): API {
let version = semver.valid(versionString)
Expand Down
30 changes: 18 additions & 12 deletions src/server/utils/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type * as Proto from '../protocol'
import { CancellationToken, snippetManager, window, workspace, Uri, MessageItem } from 'coc.nvim'
import { ITypeScriptServiceClient, ServerResponse } from '../typescriptService'
import { TypeScriptServiceConfiguration } from './configuration'
import API from './api'

export const enum ProjectType {
TypeScript,
Expand All @@ -18,18 +19,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: (version.gte(API.v540) ? 'Preserve' : 'ESNext') as Proto.ModuleKind,
moduleResolution: (version.gte(API.v540) ? '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 Down Expand Up @@ -67,10 +71,11 @@ export function inferredProjectCompilerOptions(
}

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

export async function openOrCreateConfig(
version: API,
projectType: ProjectType,
rootPath: string,
configuration: TypeScriptServiceConfiguration,
Expand All @@ -95,7 +101,7 @@ export async function openOrCreateConfig(
let text = doc.textDocument.getText()
if (text.length === 0) {
await workspace.nvim.command('startinsert')
await snippetManager.insertSnippet(inferredProjectConfigSnippet(projectType, configuration))
await snippetManager.insertSnippet(inferredProjectConfigSnippet(version, projectType, configuration))
}
} catch {
}
Expand Down Expand Up @@ -127,7 +133,7 @@ export async function openProjectConfigOrPromptToCreate(

switch (selected) {
case CreateConfigItem:
openOrCreateConfig(projectType, rootPath, client.configuration)
openOrCreateConfig(client.apiVersion, projectType, rootPath, client.configuration)
return
}
}
Expand All @@ -145,7 +151,7 @@ export async function openProjectConfigForFile(

const file = client.toPath(resource.toString())
// TSServer errors when 'projectInfo' is invoked on a non js/ts file
if (!file || !await client.toPath(resource.toString())) {
if (!file || !client.toPath(resource.toString())) {
window.showWarningMessage('Could not determine TypeScript or JavaScript project. Unsupported file type')
return
}
Expand Down