mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 12:19:20 +00:00
Add js/ts.implicitProjectConfig.strict setting for implicit projects
- Add strict property to ImplicitProjectConfiguration class - Update inferredProjectCompilerOptions to handle strict setting - Add configuration setting to package.json with default true - Add localization string for the new setting - Create unit tests for the new functionality This enables strict mode by default in implicit JS/TS projects, aligning with TS 6.0. Co-authored-by: mjbvz <12821956+mjbvz@users.noreply.github.com>
This commit is contained in:
@@ -302,6 +302,12 @@
|
||||
"markdownDescription": "%configuration.implicitProjectConfig.strictFunctionTypes%",
|
||||
"scope": "window"
|
||||
},
|
||||
"js/ts.implicitProjectConfig.strict": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"markdownDescription": "%configuration.implicitProjectConfig.strict%",
|
||||
"scope": "window"
|
||||
},
|
||||
"typescript.tsc.autoDetect": {
|
||||
"type": "string",
|
||||
"default": "on",
|
||||
|
||||
@@ -86,6 +86,7 @@
|
||||
"configuration.implicitProjectConfig.experimentalDecorators": "Enable/disable `experimentalDecorators` in JavaScript files that are not part of a project. Existing `jsconfig.json` or `tsconfig.json` files override this setting.",
|
||||
"configuration.implicitProjectConfig.strictNullChecks": "Enable/disable [strict null checks](https://www.typescriptlang.org/tsconfig#strictNullChecks) in JavaScript and TypeScript files that are not part of a project. Existing `jsconfig.json` or `tsconfig.json` files override this setting.",
|
||||
"configuration.implicitProjectConfig.strictFunctionTypes": "Enable/disable [strict function types](https://www.typescriptlang.org/tsconfig#strictFunctionTypes) in JavaScript and TypeScript files that are not part of a project. Existing `jsconfig.json` or `tsconfig.json` files override this setting.",
|
||||
"configuration.implicitProjectConfig.strict": "Enable/disable [strict mode](https://www.typescriptlang.org/tsconfig#strict) in JavaScript and TypeScript files that are not part of a project. Existing `jsconfig.json` or `tsconfig.json` files override this setting.",
|
||||
"configuration.suggest.jsdoc.generateReturns": "Enable/disable generating `@returns` annotations for JSDoc templates.",
|
||||
"configuration.suggest.autoImports": "Enable/disable auto import suggestions.",
|
||||
"configuration.preferGoToSourceDefinition": "Makes `Go to Definition` avoid type declaration files when possible by triggering `Go to Source Definition` instead. This allows `Go to Source Definition` to be triggered with the mouse gesture.",
|
||||
|
||||
@@ -64,6 +64,7 @@ export class ImplicitProjectConfiguration {
|
||||
public readonly experimentalDecorators: boolean;
|
||||
public readonly strictNullChecks: boolean;
|
||||
public readonly strictFunctionTypes: boolean;
|
||||
public readonly strict: boolean;
|
||||
|
||||
constructor(configuration: vscode.WorkspaceConfiguration) {
|
||||
this.target = ImplicitProjectConfiguration.readTarget(configuration);
|
||||
@@ -72,6 +73,7 @@ export class ImplicitProjectConfiguration {
|
||||
this.experimentalDecorators = ImplicitProjectConfiguration.readExperimentalDecorators(configuration);
|
||||
this.strictNullChecks = ImplicitProjectConfiguration.readImplicitStrictNullChecks(configuration);
|
||||
this.strictFunctionTypes = ImplicitProjectConfiguration.readImplicitStrictFunctionTypes(configuration);
|
||||
this.strict = ImplicitProjectConfiguration.readImplicitStrict(configuration);
|
||||
}
|
||||
|
||||
public isEqualTo(other: ImplicitProjectConfiguration): boolean {
|
||||
@@ -101,6 +103,10 @@ export class ImplicitProjectConfiguration {
|
||||
private static readImplicitStrictFunctionTypes(configuration: vscode.WorkspaceConfiguration): boolean {
|
||||
return configuration.get<boolean>('js/ts.implicitProjectConfig.strictFunctionTypes', true);
|
||||
}
|
||||
|
||||
private static readImplicitStrict(configuration: vscode.WorkspaceConfiguration): boolean {
|
||||
return configuration.get<boolean>('js/ts.implicitProjectConfig.strict', true);
|
||||
}
|
||||
}
|
||||
|
||||
export interface TypeScriptServiceConfiguration {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as assert from 'assert';
|
||||
import 'mocha';
|
||||
import { ImplicitProjectConfiguration } from '../../configuration/configuration';
|
||||
|
||||
suite('ImplicitProjectConfiguration', () => {
|
||||
|
||||
test('should default strict to true', () => {
|
||||
const mockConfiguration = {
|
||||
get: <T>(key: string, defaultValue?: T): T => {
|
||||
// Return default values for all settings except when explicitly overridden
|
||||
if (key === 'js/ts.implicitProjectConfig.strict') {
|
||||
return (true as any) as T;
|
||||
}
|
||||
return defaultValue as T;
|
||||
}
|
||||
} as any;
|
||||
|
||||
const config = new ImplicitProjectConfiguration(mockConfiguration);
|
||||
assert.strictEqual(config.strict, true);
|
||||
});
|
||||
|
||||
test('should respect user setting for strict', () => {
|
||||
const mockConfiguration = {
|
||||
get: <T>(key: string, defaultValue?: T): T => {
|
||||
if (key === 'js/ts.implicitProjectConfig.strict') {
|
||||
return (false as any) as T;
|
||||
}
|
||||
return defaultValue as T;
|
||||
}
|
||||
} as any;
|
||||
|
||||
const config = new ImplicitProjectConfiguration(mockConfiguration);
|
||||
assert.strictEqual(config.strict, false);
|
||||
});
|
||||
|
||||
test('should include strict in equality comparison', () => {
|
||||
const mockConfigurationTrue = {
|
||||
get: <T>(key: string, defaultValue?: T): T => {
|
||||
if (key === 'js/ts.implicitProjectConfig.strict') {
|
||||
return (true as any) as T;
|
||||
}
|
||||
return defaultValue as T;
|
||||
}
|
||||
} as any;
|
||||
|
||||
const mockConfigurationFalse = {
|
||||
get: <T>(key: string, defaultValue?: T): T => {
|
||||
if (key === 'js/ts.implicitProjectConfig.strict') {
|
||||
return (false as any) as T;
|
||||
}
|
||||
return defaultValue as T;
|
||||
}
|
||||
} as any;
|
||||
|
||||
const configTrue1 = new ImplicitProjectConfiguration(mockConfigurationTrue);
|
||||
const configTrue2 = new ImplicitProjectConfiguration(mockConfigurationTrue);
|
||||
const configFalse = new ImplicitProjectConfiguration(mockConfigurationFalse);
|
||||
|
||||
assert.strictEqual(configTrue1.isEqualTo(configTrue2), true);
|
||||
assert.strictEqual(configTrue1.isEqualTo(configFalse), false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as assert from 'assert';
|
||||
import 'mocha';
|
||||
import { ImplicitProjectConfiguration } from '../../configuration/configuration';
|
||||
import { API } from '../../tsServer/api';
|
||||
import { inferredProjectCompilerOptions, ProjectType } from '../../tsconfig';
|
||||
|
||||
suite('inferredProjectCompilerOptions', () => {
|
||||
|
||||
function createMockServiceConfig(strictValue: boolean) {
|
||||
const mockConfiguration = {
|
||||
get: <T>(key: string, defaultValue?: T): T => {
|
||||
if (key === 'js/ts.implicitProjectConfig.strict') {
|
||||
return (strictValue as any) as T;
|
||||
}
|
||||
return defaultValue as T;
|
||||
}
|
||||
} as any;
|
||||
|
||||
return {
|
||||
implicitProjectConfiguration: new ImplicitProjectConfiguration(mockConfiguration)
|
||||
} as any;
|
||||
}
|
||||
|
||||
test('should include strict: true when setting is enabled', () => {
|
||||
const serviceConfig = createMockServiceConfig(true);
|
||||
const version = API.fromVersionString('4.0.0');
|
||||
|
||||
const options = inferredProjectCompilerOptions(version, ProjectType.TypeScript, serviceConfig);
|
||||
|
||||
assert.strictEqual(options.strict, true);
|
||||
});
|
||||
|
||||
test('should not include strict when setting is disabled', () => {
|
||||
const serviceConfig = createMockServiceConfig(false);
|
||||
const version = API.fromVersionString('4.0.0');
|
||||
|
||||
const options = inferredProjectCompilerOptions(version, ProjectType.TypeScript, serviceConfig);
|
||||
|
||||
assert.strictEqual(options.strict, undefined);
|
||||
});
|
||||
|
||||
test('should work for JavaScript projects', () => {
|
||||
const serviceConfig = createMockServiceConfig(true);
|
||||
const version = API.fromVersionString('4.0.0');
|
||||
|
||||
const options = inferredProjectCompilerOptions(version, ProjectType.JavaScript, serviceConfig);
|
||||
|
||||
assert.strictEqual(options.strict, true);
|
||||
});
|
||||
});
|
||||
@@ -55,6 +55,10 @@ export function inferredProjectCompilerOptions(
|
||||
projectConfig.strictFunctionTypes = true;
|
||||
}
|
||||
|
||||
if (serviceConfig.implicitProjectConfiguration.strict) {
|
||||
projectConfig.strict = true;
|
||||
}
|
||||
|
||||
if (serviceConfig.implicitProjectConfiguration.module) {
|
||||
projectConfig.module = serviceConfig.implicitProjectConfiguration.module as Proto.ModuleKind;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user