From a82d1f8580c8fb2a98ac3b215a2ed79be36a93ff Mon Sep 17 00:00:00 2001 From: Wenlu Wang <805037171@163.com> Date: Tue, 9 Jan 2018 05:13:23 +0800 Subject: [PATCH] ignore Initial check if left of DestructuringAsignment (#20906) --- src/compiler/checker.ts | 3 ++- ...structuringAssignmentWithStrictNullChecks.js | 17 +++++++++++++++++ ...turingAssignmentWithStrictNullChecks.symbols | 7 +++++++ ...ucturingAssignmentWithStrictNullChecks.types | 11 +++++++++++ ...structuringAssignmentWithStrictNullChecks.ts | 3 +++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.js create mode 100644 tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.symbols create mode 100644 tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.types create mode 100644 tests/cases/compiler/destructuringAssignmentWithStrictNullChecks.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b04d39bb2b1..b1f2df5ca6a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13333,6 +13333,7 @@ namespace ts { const declarationContainer = getControlFlowContainer(declaration); let flowContainer = getControlFlowContainer(node); const isOuterVariable = flowContainer !== declarationContainer; + const isSpreadDestructuringAsignmentTarget = node.parent && node.parent.parent && isSpreadAssignment(node.parent) && isDestructuringAssignmentTarget(node.parent.parent); // When the control flow originates in a function expression or arrow function and we are referencing // a const variable or parameter from an outer function, we extend the origin of the control flow // analysis to include the immediately enclosing function. @@ -13344,7 +13345,7 @@ namespace ts { // We only look for uninitialized variables in strict null checking mode, and only when we can analyze // the entire control flow graph from the variable's declaration (i.e. when the flow container and // declaration container are the same). - const assumeInitialized = isParameter || isAlias || isOuterVariable || + const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAsignmentTarget || type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) || node.parent.kind === SyntaxKind.NonNullExpression || diff --git a/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.js b/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.js new file mode 100644 index 00000000000..52bac6e2391 --- /dev/null +++ b/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.js @@ -0,0 +1,17 @@ +//// [destructuringAssignmentWithStrictNullChecks.ts] +let bar: {}; +({ ...bar } = {}); + + +//// [destructuringAssignmentWithStrictNullChecks.js] +var __rest = (this && this.__rest) || function (s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) + t[p[i]] = s[p[i]]; + return t; +}; +var bar; +(bar = __rest({}, [])); diff --git a/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.symbols b/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.symbols new file mode 100644 index 00000000000..b241c29c9ae --- /dev/null +++ b/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/destructuringAssignmentWithStrictNullChecks.ts === +let bar: {}; +>bar : Symbol(bar, Decl(destructuringAssignmentWithStrictNullChecks.ts, 0, 3)) + +({ ...bar } = {}); +>bar : Symbol(bar, Decl(destructuringAssignmentWithStrictNullChecks.ts, 0, 3)) + diff --git a/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.types b/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.types new file mode 100644 index 00000000000..375ddf82a59 --- /dev/null +++ b/tests/baselines/reference/destructuringAssignmentWithStrictNullChecks.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/destructuringAssignmentWithStrictNullChecks.ts === +let bar: {}; +>bar : {} + +({ ...bar } = {}); +>({ ...bar } = {}) : {} +>{ ...bar } = {} : {} +>{ ...bar } : {} +>bar : {} +>{} : {} + diff --git a/tests/cases/compiler/destructuringAssignmentWithStrictNullChecks.ts b/tests/cases/compiler/destructuringAssignmentWithStrictNullChecks.ts new file mode 100644 index 00000000000..b3565a38e10 --- /dev/null +++ b/tests/cases/compiler/destructuringAssignmentWithStrictNullChecks.ts @@ -0,0 +1,3 @@ +// @strictNullChecks: true +let bar: {}; +({ ...bar } = {});