Skip to content

Commit 22a384d

Browse files
committed
New --strictBindCallApply flag in compiler
1 parent df83784 commit 22a384d

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ namespace ts {
7777
const allowSyntheticDefaultImports = getAllowSyntheticDefaultImports(compilerOptions);
7878
const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks");
7979
const strictFunctionTypes = getStrictOptionValue(compilerOptions, "strictFunctionTypes");
80+
const strictBindCallApply = getStrictOptionValue(compilerOptions, "strictBindCallApply");
8081
const strictPropertyInitialization = getStrictOptionValue(compilerOptions, "strictPropertyInitialization");
8182
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");
8283
const noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");
@@ -463,6 +464,8 @@ namespace ts {
463464

464465
let globalObjectType: ObjectType;
465466
let globalFunctionType: ObjectType;
467+
let globalCallableFunctionType: ObjectType;
468+
let globalNewableFunctionType: ObjectType;
466469
let globalArrayType: GenericType;
467470
let globalReadonlyArrayType: GenericType;
468471
let globalStringType: ObjectType;
@@ -7366,8 +7369,12 @@ namespace ts {
73667369
if (symbol && symbolIsValue(symbol)) {
73677370
return symbol;
73687371
}
7369-
if (resolved === anyFunctionType || resolved.callSignatures.length || resolved.constructSignatures.length) {
7370-
const symbol = getPropertyOfObjectType(globalFunctionType, name);
7372+
const functionType = resolved === anyFunctionType ? globalFunctionType :
7373+
resolved.callSignatures.length ? globalCallableFunctionType :
7374+
resolved.constructSignatures.length ? globalNewableFunctionType :
7375+
undefined;
7376+
if (functionType) {
7377+
const symbol = getPropertyOfObjectType(functionType, name);
73717378
if (symbol) {
73727379
return symbol;
73737380
}
@@ -28527,6 +28534,8 @@ namespace ts {
2852728534
globalArrayType = getGlobalType("Array" as __String, /*arity*/ 1, /*reportErrors*/ true);
2852828535
globalObjectType = getGlobalType("Object" as __String, /*arity*/ 0, /*reportErrors*/ true);
2852928536
globalFunctionType = getGlobalType("Function" as __String, /*arity*/ 0, /*reportErrors*/ true);
28537+
globalCallableFunctionType = strictBindCallApply && getGlobalType("CallableFunction" as __String, /*arity*/ 0, /*reportErrors*/ true) || globalFunctionType;
28538+
globalNewableFunctionType = strictBindCallApply && getGlobalType("NewableFunction" as __String, /*arity*/ 0, /*reportErrors*/ true) || globalFunctionType;
2853028539
globalStringType = getGlobalType("String" as __String, /*arity*/ 0, /*reportErrors*/ true);
2853128540
globalNumberType = getGlobalType("Number" as __String, /*arity*/ 0, /*reportErrors*/ true);
2853228541
globalBooleanType = getGlobalType("Boolean" as __String, /*arity*/ 0, /*reportErrors*/ true);

src/compiler/commandLineParser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ namespace ts {
344344
category: Diagnostics.Strict_Type_Checking_Options,
345345
description: Diagnostics.Enable_strict_checking_of_function_types
346346
},
347+
{
348+
name: "strictBindCallApply",
349+
type: "boolean",
350+
strictFlag: true,
351+
showInSimplifiedHelpView: true,
352+
category: Diagnostics.Strict_Type_Checking_Options,
353+
description: Diagnostics.Enable_strict_bind_call_and_apply_methods_on_functions
354+
},
347355
{
348356
name: "strictPropertyInitialization",
349357
type: "boolean",

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,6 +3692,10 @@
36923692
"category": "Error",
36933693
"code": 6205
36943694
},
3695+
"Enable strict 'bind', 'call', and 'apply' methods on functions.": {
3696+
"category": "Message",
3697+
"code": 6206
3698+
},
36953699

36963700
"Projects to reference": {
36973701
"category": "Message",

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4402,6 +4402,7 @@ namespace ts {
44024402
sourceRoot?: string;
44034403
strict?: boolean;
44044404
strictFunctionTypes?: boolean; // Always combine with strict property
4405+
strictBindCallApply?: boolean; // Always combine with strict property
44054406
strictNullChecks?: boolean; // Always combine with strict property
44064407
strictPropertyInitialization?: boolean; // Always combine with strict property
44074408
stripInternal?: boolean;

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7074,7 +7074,7 @@ namespace ts {
70747074
return !!(compilerOptions.declaration || compilerOptions.composite);
70757075
}
70767076

7077-
export type StrictOptionName = "noImplicitAny" | "noImplicitThis" | "strictNullChecks" | "strictFunctionTypes" | "strictPropertyInitialization" | "alwaysStrict";
7077+
export type StrictOptionName = "noImplicitAny" | "noImplicitThis" | "strictNullChecks" | "strictFunctionTypes" | "strictBindCallApply" | "strictPropertyInitialization" | "alwaysStrict";
70787078

70797079
export function getStrictOptionValue(compilerOptions: CompilerOptions, flag: StrictOptionName): boolean {
70807080
return compilerOptions[flag] === undefined ? !!compilerOptions.strict : !!compilerOptions[flag];

0 commit comments

Comments
 (0)