From 5e49b57fea08e913b222ec7a30dd24f7706173af Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Thu, 26 May 2016 13:00:38 -0700 Subject: [PATCH 1/2] Fix #8834: exclude paramters and variables from flag checks --- src/compiler/checker.ts | 6 ++++++ .../optionalParamterAndVariableDeclaration.js | 15 +++++++++++++++ ...ptionalParamterAndVariableDeclaration.symbols | 13 +++++++++++++ .../optionalParamterAndVariableDeclaration.types | 16 ++++++++++++++++ ...nalParamterAndVariableDeclaration2.errors.txt | 13 +++++++++++++ .../optionalParamterAndVariableDeclaration2.js | 16 ++++++++++++++++ .../optionalParamterAndVariableDeclaration.ts | 5 +++++ .../optionalParamterAndVariableDeclaration2.ts | 7 +++++++ 8 files changed, 91 insertions(+) create mode 100644 tests/baselines/reference/optionalParamterAndVariableDeclaration.js create mode 100644 tests/baselines/reference/optionalParamterAndVariableDeclaration.symbols create mode 100644 tests/baselines/reference/optionalParamterAndVariableDeclaration.types create mode 100644 tests/baselines/reference/optionalParamterAndVariableDeclaration2.errors.txt create mode 100644 tests/baselines/reference/optionalParamterAndVariableDeclaration2.js create mode 100644 tests/cases/compiler/optionalParamterAndVariableDeclaration.ts create mode 100644 tests/cases/compiler/optionalParamterAndVariableDeclaration2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5a7c6fa29e..db996d2613 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14527,6 +14527,12 @@ namespace ts { } function areDeclarationFlagsIdentical(left: Declaration, right: Declaration) { + if ((left.kind === SyntaxKind.Parameter && right.kind === SyntaxKind.VariableDeclaration) || + (left.kind === SyntaxKind.VariableDeclaration && right.kind === SyntaxKind.Parameter)) { + // Diffrences in optionality between paramters and variables are allowed. + return true; + } + if (hasQuestionToken(left) !== hasQuestionToken(right)) { return false; } diff --git a/tests/baselines/reference/optionalParamterAndVariableDeclaration.js b/tests/baselines/reference/optionalParamterAndVariableDeclaration.js new file mode 100644 index 0000000000..ae672b15d5 --- /dev/null +++ b/tests/baselines/reference/optionalParamterAndVariableDeclaration.js @@ -0,0 +1,15 @@ +//// [optionalParamterAndVariableDeclaration.ts] +class C { + constructor(options?: number) { + var options = (options || 0); + } +} + + +//// [optionalParamterAndVariableDeclaration.js] +var C = (function () { + function C(options) { + var options = (options || 0); + } + return C; +}()); diff --git a/tests/baselines/reference/optionalParamterAndVariableDeclaration.symbols b/tests/baselines/reference/optionalParamterAndVariableDeclaration.symbols new file mode 100644 index 0000000000..0a761182e0 --- /dev/null +++ b/tests/baselines/reference/optionalParamterAndVariableDeclaration.symbols @@ -0,0 +1,13 @@ +=== tests/cases/compiler/optionalParamterAndVariableDeclaration.ts === +class C { +>C : Symbol(C, Decl(optionalParamterAndVariableDeclaration.ts, 0, 0)) + + constructor(options?: number) { +>options : Symbol(options, Decl(optionalParamterAndVariableDeclaration.ts, 1, 16), Decl(optionalParamterAndVariableDeclaration.ts, 2, 11)) + + var options = (options || 0); +>options : Symbol(options, Decl(optionalParamterAndVariableDeclaration.ts, 1, 16), Decl(optionalParamterAndVariableDeclaration.ts, 2, 11)) +>options : Symbol(options, Decl(optionalParamterAndVariableDeclaration.ts, 1, 16), Decl(optionalParamterAndVariableDeclaration.ts, 2, 11)) + } +} + diff --git a/tests/baselines/reference/optionalParamterAndVariableDeclaration.types b/tests/baselines/reference/optionalParamterAndVariableDeclaration.types new file mode 100644 index 0000000000..36c74bf00b --- /dev/null +++ b/tests/baselines/reference/optionalParamterAndVariableDeclaration.types @@ -0,0 +1,16 @@ +=== tests/cases/compiler/optionalParamterAndVariableDeclaration.ts === +class C { +>C : C + + constructor(options?: number) { +>options : number + + var options = (options || 0); +>options : number +>(options || 0) : number +>options || 0 : number +>options : number +>0 : number + } +} + diff --git a/tests/baselines/reference/optionalParamterAndVariableDeclaration2.errors.txt b/tests/baselines/reference/optionalParamterAndVariableDeclaration2.errors.txt new file mode 100644 index 0000000000..0d30932753 --- /dev/null +++ b/tests/baselines/reference/optionalParamterAndVariableDeclaration2.errors.txt @@ -0,0 +1,13 @@ +tests/cases/compiler/optionalParamterAndVariableDeclaration2.ts(4,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'options' must be of type 'number | undefined', but here has type 'number'. + + +==== tests/cases/compiler/optionalParamterAndVariableDeclaration2.ts (1 errors) ==== + + class C { + constructor(options?: number) { + var options = (options || 0); + ~~~~~~~ +!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'options' must be of type 'number | undefined', but here has type 'number'. + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/optionalParamterAndVariableDeclaration2.js b/tests/baselines/reference/optionalParamterAndVariableDeclaration2.js new file mode 100644 index 0000000000..a4ffc9f382 --- /dev/null +++ b/tests/baselines/reference/optionalParamterAndVariableDeclaration2.js @@ -0,0 +1,16 @@ +//// [optionalParamterAndVariableDeclaration2.ts] + +class C { + constructor(options?: number) { + var options = (options || 0); + } +} + + +//// [optionalParamterAndVariableDeclaration2.js] +var C = (function () { + function C(options) { + var options = (options || 0); + } + return C; +}()); diff --git a/tests/cases/compiler/optionalParamterAndVariableDeclaration.ts b/tests/cases/compiler/optionalParamterAndVariableDeclaration.ts new file mode 100644 index 0000000000..d9c1d407dc --- /dev/null +++ b/tests/cases/compiler/optionalParamterAndVariableDeclaration.ts @@ -0,0 +1,5 @@ +class C { + constructor(options?: number) { + var options = (options || 0); + } +} diff --git a/tests/cases/compiler/optionalParamterAndVariableDeclaration2.ts b/tests/cases/compiler/optionalParamterAndVariableDeclaration2.ts new file mode 100644 index 0000000000..7bdb0ab89c --- /dev/null +++ b/tests/cases/compiler/optionalParamterAndVariableDeclaration2.ts @@ -0,0 +1,7 @@ +// @strictNullChecks: true + +class C { + constructor(options?: number) { + var options = (options || 0); + } +} From 04d6e9167f031e045958010f7705d7377c77597a Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Thu, 26 May 2016 14:11:06 -0700 Subject: [PATCH 2/2] Fix typos --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index db996d2613..2e701bd56b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14529,7 +14529,7 @@ namespace ts { function areDeclarationFlagsIdentical(left: Declaration, right: Declaration) { if ((left.kind === SyntaxKind.Parameter && right.kind === SyntaxKind.VariableDeclaration) || (left.kind === SyntaxKind.VariableDeclaration && right.kind === SyntaxKind.Parameter)) { - // Diffrences in optionality between paramters and variables are allowed. + // Differences in optionality between parameters and variables are allowed. return true; }