Skip to content

Commit d60c640

Browse files
committed
Add utility function to check for strict option flags
- Correctelly check for noImplicitAny in checker - Correctelly check for noImplicitAny in installTypesForPackage refactor
1 parent ceba507 commit d60c640

File tree

8 files changed

+17
-14
lines changed

8 files changed

+17
-14
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ namespace ts {
192192
return bindSourceFile;
193193

194194
function bindInStrictMode(file: SourceFile, opts: CompilerOptions): boolean {
195-
if ((opts.alwaysStrict === undefined ? opts.strict : opts.alwaysStrict) && !file.isDeclarationFile) {
195+
if (getStrictOptionValue(opts, "alwaysStrict") && !file.isDeclarationFile) {
196196
// bind in strict mode source files with alwaysStrict option
197197
return true;
198198
}

src/compiler/checker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ namespace ts {
6565
const modulekind = getEmitModuleKind(compilerOptions);
6666
const noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters;
6767
const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System;
68-
const strictNullChecks = compilerOptions.strictNullChecks === undefined ? compilerOptions.strict : compilerOptions.strictNullChecks;
69-
const strictFunctionTypes = compilerOptions.strictFunctionTypes === undefined ? compilerOptions.strict : compilerOptions.strictFunctionTypes;
70-
const noImplicitAny = compilerOptions.noImplicitAny === undefined ? compilerOptions.strict : compilerOptions.noImplicitAny;
71-
const noImplicitThis = compilerOptions.noImplicitThis === undefined ? compilerOptions.strict : compilerOptions.noImplicitThis;
68+
const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks");
69+
const strictFunctionTypes = getStrictOptionValue(compilerOptions, "strictFunctionTypes");
70+
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");
71+
const noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");
7272

7373
const emitResolver = createResolver();
7474
const nodeBuilder = createNodeBuilder();
@@ -6875,7 +6875,7 @@ namespace ts {
68756875
const numTypeArguments = length(node.typeArguments);
68766876
const minTypeArgumentCount = getMinTypeArgumentCount(typeParameters);
68776877
const isJs = isInJavaScriptFile(node);
6878-
const isJsImplicitAny = !compilerOptions.noImplicitAny && isJs;
6878+
const isJsImplicitAny = !noImplicitAny && isJs;
68796879
if (!isJsImplicitAny && (numTypeArguments < minTypeArgumentCount || numTypeArguments > typeParameters.length)) {
68806880
const missingAugmentsTag = isJs && node.parent.kind !== SyntaxKind.JSDocAugmentsTag;
68816881
const diag = minTypeArgumentCount === typeParameters.length

src/compiler/core.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,10 @@ namespace ts {
17101710
return moduleResolution;
17111711
}
17121712

1713+
export function getStrictOptionValue(compilerOptions: CompilerOptions, flag: "noImplicitAny" | "noImplicitThis" | "strictNullChecks" | "strictFunctionTypes" | "alwaysStrict"): boolean {
1714+
return compilerOptions[flag] === undefined ? compilerOptions.strict : compilerOptions[flag];
1715+
}
1716+
17131717
export function hasZeroOrOneAsteriskCharacter(str: string): boolean {
17141718
let seenAsterisk = false;
17151719
for (let i = 0; i < str.length; i++) {

src/compiler/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,7 +2127,7 @@ namespace ts {
21272127
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "lib", "noLib");
21282128
}
21292129

2130-
if (options.noImplicitUseStrict && (options.alwaysStrict === undefined ? options.strict : options.alwaysStrict)) {
2130+
if (options.noImplicitUseStrict && getStrictOptionValue(options, "alwaysStrict")) {
21312131
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "noImplicitUseStrict", "alwaysStrict");
21322132
}
21332133

@@ -2356,7 +2356,7 @@ namespace ts {
23562356
return options.jsx ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set;
23572357
}
23582358
function needAllowJs() {
2359-
return options.allowJs || !options.noImplicitAny ? undefined : Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type;
2359+
return options.allowJs || !getStrictOptionValue(options, "noImplicitAny") ? undefined : Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type;
23602360
}
23612361
}
23622362

src/compiler/transformers/module/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace ts {
9191
startLexicalEnvironment();
9292

9393
const statements: Statement[] = [];
94-
const ensureUseStrict = compilerOptions.alwaysStrict || (!compilerOptions.noImplicitUseStrict && isExternalModule(currentSourceFile));
94+
const ensureUseStrict = getStrictOptionValue(compilerOptions, "alwaysStrict") || (!compilerOptions.noImplicitUseStrict && isExternalModule(currentSourceFile));
9595
const statementOffset = addPrologue(statements, node.statements, ensureUseStrict, sourceElementVisitor);
9696

9797
if (shouldEmitUnderscoreUnderscoreESModule()) {

src/compiler/transformers/module/system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ namespace ts {
225225
startLexicalEnvironment();
226226

227227
// Add any prologue directives.
228-
const ensureUseStrict = compilerOptions.alwaysStrict || (!compilerOptions.noImplicitUseStrict && isExternalModule(currentSourceFile));
228+
const ensureUseStrict = getStrictOptionValue(compilerOptions, "alwaysStrict") || (!compilerOptions.noImplicitUseStrict && isExternalModule(currentSourceFile));
229229
const statementOffset = addPrologue(statements, node.statements, ensureUseStrict, sourceElementVisitor);
230230

231231
// var __moduleName = context_1 && context_1.id;

src/compiler/transformers/ts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace ts {
4545

4646
const resolver = context.getEmitResolver();
4747
const compilerOptions = context.getCompilerOptions();
48-
const strictNullChecks = typeof compilerOptions.strictNullChecks === "undefined" ? compilerOptions.strict : compilerOptions.strictNullChecks;
48+
const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks");
4949
const languageVersion = getEmitScriptTarget(compilerOptions);
5050
const moduleKind = getEmitModuleKind(compilerOptions);
5151

@@ -521,7 +521,7 @@ namespace ts {
521521
}
522522

523523
function visitSourceFile(node: SourceFile) {
524-
const alwaysStrict = (compilerOptions.alwaysStrict === undefined ? compilerOptions.strict : compilerOptions.alwaysStrict) &&
524+
const alwaysStrict = getStrictOptionValue(compilerOptions, "alwaysStrict") &&
525525
!(isExternalModule(node) && moduleKind >= ModuleKind.ES2015);
526526
return updateSourceFileNode(
527527
node,

src/services/refactors/installTypesForPackage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ namespace ts.refactor.installTypesForPackage {
1212
registerRefactor(installTypesForPackage);
1313

1414
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
15-
const options = context.program.getCompilerOptions();
16-
if (options.noImplicitAny || options.strict) {
15+
if (getStrictOptionValue(context.program.getCompilerOptions(), "noImplicitAny")) {
1716
// Then it will be available via `fixCannotFindModule`.
1817
return undefined;
1918
}

0 commit comments

Comments
 (0)