Skip to content

Commit f7242f3

Browse files
authored
Merge pull request #14486 from Microsoft/strictChecks
New --strict master option
2 parents c968eed + de120b3 commit f7242f3

File tree

15 files changed

+48
-35
lines changed

15 files changed

+48
-35
lines changed

src/compiler/binder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ namespace ts {
182182
return bindSourceFile;
183183

184184
function bindInStrictMode(file: SourceFile, opts: CompilerOptions): boolean {
185-
if (opts.alwaysStrict && !isDeclarationFile(file)) {
185+
if ((opts.alwaysStrict === undefined ? opts.strict : opts.alwaysStrict) && !isDeclarationFile(file)) {
186186
// bind in strict mode source files with alwaysStrict option
187187
return true;
188188
}

src/compiler/checker.ts

+21-19
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ namespace ts {
5656
const modulekind = getEmitModuleKind(compilerOptions);
5757
const noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters;
5858
const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System;
59-
const strictNullChecks = compilerOptions.strictNullChecks;
59+
const strictNullChecks = compilerOptions.strictNullChecks === undefined ? compilerOptions.strict : compilerOptions.strictNullChecks;
60+
const noImplicitAny = compilerOptions.noImplicitAny === undefined ? compilerOptions.strict : compilerOptions.noImplicitAny;
61+
const noImplicitThis = compilerOptions.noImplicitThis === undefined ? compilerOptions.strict : compilerOptions.noImplicitThis;
6062

6163
const emitResolver = createResolver();
6264

@@ -1571,7 +1573,7 @@ namespace ts {
15711573
error(errorNode, diag, moduleReference, resolvedModule.resolvedFileName);
15721574
return undefined;
15731575
}
1574-
else if (compilerOptions.noImplicitAny && moduleNotFoundError) {
1576+
else if (noImplicitAny && moduleNotFoundError) {
15751577
error(errorNode,
15761578
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
15771579
moduleReference,
@@ -3414,7 +3416,7 @@ namespace ts {
34143416
return addOptionality(declaredType, /*optional*/ declaration.questionToken && includeOptionality);
34153417
}
34163418

3417-
if ((compilerOptions.noImplicitAny || declaration.flags & NodeFlags.JavaScriptFile) &&
3419+
if ((noImplicitAny || declaration.flags & NodeFlags.JavaScriptFile) &&
34183420
declaration.kind === SyntaxKind.VariableDeclaration && !isBindingPattern(declaration.name) &&
34193421
!(getCombinedModifierFlags(declaration) & ModifierFlags.Export) && !isInAmbientContext(declaration)) {
34203422
// If --noImplicitAny is on or the declaration is in a Javascript file,
@@ -3516,7 +3518,7 @@ namespace ts {
35163518
if (isBindingPattern(element.name)) {
35173519
return getTypeFromBindingPattern(<BindingPattern>element.name, includePatternInType, reportErrors);
35183520
}
3519-
if (reportErrors && compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) {
3521+
if (reportErrors && noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) {
35203522
reportImplicitAnyError(element, anyType);
35213523
}
35223524
return anyType;
@@ -3614,7 +3616,7 @@ namespace ts {
36143616
type = declaration.dotDotDotToken ? anyArrayType : anyType;
36153617

36163618
// Report implicit any errors unless this is a private property within an ambient declaration
3617-
if (reportErrors && compilerOptions.noImplicitAny) {
3619+
if (reportErrors && noImplicitAny) {
36183620
if (!declarationBelongsToPrivateAmbientMember(declaration)) {
36193621
reportImplicitAnyError(declaration, type);
36203622
}
@@ -3733,7 +3735,7 @@ namespace ts {
37333735
}
37343736
// Otherwise, fall back to 'any'.
37353737
else {
3736-
if (compilerOptions.noImplicitAny) {
3738+
if (noImplicitAny) {
37373739
if (setter) {
37383740
error(setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol));
37393741
}
@@ -3748,7 +3750,7 @@ namespace ts {
37483750
}
37493751
if (!popTypeResolution()) {
37503752
type = anyType;
3751-
if (compilerOptions.noImplicitAny) {
3753+
if (noImplicitAny) {
37523754
const getter = <AccessorDeclaration>getDeclarationOfKind(symbol, SyntaxKind.GetAccessor);
37533755
error(getter, Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, symbolToString(symbol));
37543756
}
@@ -3831,7 +3833,7 @@ namespace ts {
38313833
return unknownType;
38323834
}
38333835
// Otherwise variable has initializer that circularly references the variable itself
3834-
if (compilerOptions.noImplicitAny) {
3836+
if (noImplicitAny) {
38353837
error(symbol.valueDeclaration, Diagnostics._0_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer,
38363838
symbolToString(symbol));
38373839
}
@@ -5592,7 +5594,7 @@ namespace ts {
55925594
}
55935595
if (!popTypeResolution()) {
55945596
type = anyType;
5595-
if (compilerOptions.noImplicitAny) {
5597+
if (noImplicitAny) {
55965598
const declaration = <Declaration>signature.declaration;
55975599
if (declaration.name) {
55985600
error(declaration.name, Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, declarationNameToString(declaration.name));
@@ -6552,7 +6554,7 @@ namespace ts {
65526554
return indexInfo.type;
65536555
}
65546556
if (accessExpression && !isConstEnumObjectType(objectType)) {
6555-
if (compilerOptions.noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors) {
6557+
if (noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors) {
65566558
if (getIndexTypeOfType(objectType, IndexKind.Number)) {
65576559
error(accessExpression.argumentExpression, Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number);
65586560
}
@@ -9146,7 +9148,7 @@ namespace ts {
91469148
}
91479149

91489150
function reportErrorsFromWidening(declaration: Declaration, type: Type) {
9149-
if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & TypeFlags.ContainsWideningType) {
9151+
if (produceDiagnostics && noImplicitAny && type.flags & TypeFlags.ContainsWideningType) {
91509152
// Report implicit any error within type if possible, otherwise report error on declaration
91519153
if (!reportWideningErrorsInType(type)) {
91529154
reportImplicitAnyError(declaration, type);
@@ -11056,7 +11058,7 @@ namespace ts {
1105611058
// control flow based type does include undefined.
1105711059
if (type === autoType || type === autoArrayType) {
1105811060
if (flowType === autoType || flowType === autoArrayType) {
11059-
if (compilerOptions.noImplicitAny) {
11061+
if (noImplicitAny) {
1106011062
error(declaration.name, Diagnostics.Variable_0_implicitly_has_type_1_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol), typeToString(flowType));
1106111063
error(node, Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(flowType));
1106211064
}
@@ -11326,7 +11328,7 @@ namespace ts {
1132611328
}
1132711329
}
1132811330

11329-
if (compilerOptions.noImplicitThis) {
11331+
if (noImplicitThis) {
1133011332
// With noImplicitThis, functions may not reference 'this' if it has type 'any'
1133111333
error(node, Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
1133211334
}
@@ -12599,7 +12601,7 @@ namespace ts {
1259912601
return links.resolvedSymbol = unknownSymbol;
1260012602
}
1260112603
else {
12602-
if (compilerOptions.noImplicitAny) {
12604+
if (noImplicitAny) {
1260312605
error(node, Diagnostics.JSX_element_implicitly_has_type_any_because_no_interface_JSX_0_exists, JsxNames.IntrinsicElements);
1260412606
}
1260512607
return links.resolvedSymbol = unknownSymbol;
@@ -12987,7 +12989,7 @@ namespace ts {
1298712989
}
1298812990

1298912991
if (jsxElementType === undefined) {
12990-
if (compilerOptions.noImplicitAny) {
12992+
if (noImplicitAny) {
1299112993
error(errorNode, Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
1299212994
}
1299312995
}
@@ -14810,7 +14812,7 @@ namespace ts {
1481014812
if (funcSymbol && funcSymbol.members && funcSymbol.flags & SymbolFlags.Function) {
1481114813
return getInferredClassType(funcSymbol);
1481214814
}
14813-
else if (compilerOptions.noImplicitAny) {
14815+
else if (noImplicitAny) {
1481414816
error(node, Diagnostics.new_expression_whose_target_lacks_a_construct_signature_implicitly_has_an_any_type);
1481514817
}
1481614818
return anyType;
@@ -15070,7 +15072,7 @@ namespace ts {
1507015072
const iterableIteratorAny = functionFlags & FunctionFlags.Async
1507115073
? createAsyncIterableIteratorType(anyType) // AsyncGenerator function
1507215074
: createIterableIteratorType(anyType); // Generator function
15073-
if (compilerOptions.noImplicitAny) {
15075+
if (noImplicitAny) {
1507415076
error(func.asteriskToken,
1507515077
Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny));
1507615078
}
@@ -16619,7 +16621,7 @@ namespace ts {
1661916621

1662016622
if (produceDiagnostics) {
1662116623
checkCollisionWithArgumentsInGeneratedCode(node);
16622-
if (compilerOptions.noImplicitAny && !node.type) {
16624+
if (noImplicitAny && !node.type) {
1662316625
switch (node.kind) {
1662416626
case SyntaxKind.ConstructSignature:
1662516627
error(node, Diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type);
@@ -17921,7 +17923,7 @@ namespace ts {
1792117923
if (produceDiagnostics && !node.type) {
1792217924
// Report an implicit any error if there is no body, no explicit return type, and node is not a private method
1792317925
// in an ambient context
17924-
if (compilerOptions.noImplicitAny && nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) {
17926+
if (noImplicitAny && nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) {
1792517927
reportImplicitAnyError(node, anyType);
1792617928
}
1792717929

src/compiler/commandLineParser.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ namespace ts {
467467
type: "boolean",
468468
description: Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file
469469
},
470+
{
471+
name: "strict",
472+
type: "boolean",
473+
description: Diagnostics.Enable_all_strict_type_checks
474+
},
470475
{
471476
// A list of plugins to load in the language service
472477
name: "plugins",
@@ -520,7 +525,7 @@ namespace ts {
520525
export const defaultInitCompilerOptions: CompilerOptions = {
521526
module: ModuleKind.CommonJS,
522527
target: ScriptTarget.ES5,
523-
noImplicitAny: false,
528+
strict: true,
524529
sourceMap: false,
525530
};
526531

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3045,6 +3045,10 @@
30453045
"category": "Message",
30463046
"code": 6149
30473047
},
3048+
"Enable all strict type checks.": {
3049+
"category": "Message",
3050+
"code": 6150
3051+
},
30483052
"Variable '{0}' implicitly has an '{1}' type.": {
30493053
"category": "Error",
30503054
"code": 7005

src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ namespace ts {
16301630
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "lib", "noLib"));
16311631
}
16321632

1633-
if (options.noImplicitUseStrict && options.alwaysStrict) {
1633+
if (options.noImplicitUseStrict && (options.alwaysStrict === undefined ? options.strict : options.alwaysStrict)) {
16341634
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "noImplicitUseStrict", "alwaysStrict"));
16351635
}
16361636

src/compiler/transformers/ts.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ namespace ts {
472472
}
473473

474474
function visitSourceFile(node: SourceFile) {
475-
const alwaysStrict = compilerOptions.alwaysStrict && !(isExternalModule(node) && moduleKind === ModuleKind.ES2015);
475+
const alwaysStrict = (compilerOptions.alwaysStrict === undefined ? compilerOptions.strict : compilerOptions.alwaysStrict) &&
476+
!(isExternalModule(node) && moduleKind === ModuleKind.ES2015);
476477
return updateSourceFileNode(
477478
node,
478479
visitLexicalEnvironment(node.statements, sourceElementVisitor, context, /*start*/ 0, alwaysStrict));

src/compiler/types.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -3309,7 +3309,7 @@
33093309
allowSyntheticDefaultImports?: boolean;
33103310
allowUnreachableCode?: boolean;
33113311
allowUnusedLabels?: boolean;
3312-
alwaysStrict?: boolean;
3312+
alwaysStrict?: boolean; // Always combine with strict property
33133313
baseUrl?: string;
33143314
charset?: string;
33153315
/* @internal */ configFilePath?: string;
@@ -3345,9 +3345,9 @@
33453345
noEmitOnError?: boolean;
33463346
noErrorTruncation?: boolean;
33473347
noFallthroughCasesInSwitch?: boolean;
3348-
noImplicitAny?: boolean;
3348+
noImplicitAny?: boolean; // Always combine with strict property
33493349
noImplicitReturns?: boolean;
3350-
noImplicitThis?: boolean;
3350+
noImplicitThis?: boolean; // Always combine with strict property
33513351
noUnusedLocals?: boolean;
33523352
noUnusedParameters?: boolean;
33533353
noImplicitUseStrict?: boolean;
@@ -3370,7 +3370,8 @@
33703370
skipDefaultLibCheck?: boolean;
33713371
sourceMap?: boolean;
33723372
sourceRoot?: string;
3373-
strictNullChecks?: boolean;
3373+
strict?: boolean;
3374+
strictNullChecks?: boolean; // Always combine with strict property
33743375
/* @internal */ stripInternal?: boolean;
33753376
suppressExcessPropertyErrors?: boolean;
33763377
suppressImplicitAnyIndexErrors?: boolean;

tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5-
"noImplicitAny": false,
5+
"strict": true,
66
"sourceMap": false
77
}
88
}

tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5-
"noImplicitAny": false,
5+
"strict": true,
66
"sourceMap": false,
77
"noUnusedLocals": true
88
}

tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5-
"noImplicitAny": false,
5+
"strict": true,
66
"sourceMap": false,
77
"jsx": "react"
88
}

tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5-
"noImplicitAny": false,
5+
"strict": true,
66
"sourceMap": false
77
},
88
"files": [

tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5-
"noImplicitAny": false,
5+
"strict": true,
66
"sourceMap": false,
77
"lib": [
88
"es5",

tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5-
"noImplicitAny": false,
5+
"strict": true,
66
"sourceMap": false
77
}
88
}

tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5-
"noImplicitAny": false,
5+
"strict": true,
66
"sourceMap": false,
77
"lib": [
88
"es5",

tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5-
"noImplicitAny": false,
5+
"strict": true,
66
"sourceMap": false,
77
"types": [
88
"jquery",

0 commit comments

Comments
 (0)