From c5006e025f9302a3ebd6f45818b3a3b32028a952 Mon Sep 17 00:00:00 2001 From: ivanwonder Date: Wed, 27 Nov 2019 03:11:28 +0000 Subject: [PATCH] fix(language-service): fix error of array-index out of bounds exception (#33928) PR Close #33928 --- packages/language-service/src/typescript_symbols.ts | 3 ++- packages/language-service/test/diagnostics_spec.ts | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/language-service/src/typescript_symbols.ts b/packages/language-service/src/typescript_symbols.ts index 2e4343206f..7928cfcf78 100644 --- a/packages/language-service/src/typescript_symbols.ts +++ b/packages/language-service/src/typescript_symbols.ts @@ -295,7 +295,8 @@ class TypeWrapper implements Symbol { if (nType) { // get the right tuple type by value, like 'var t: [number, string];' if (nType.isUnion()) { - return new TypeWrapper(nType.types[value], this.context); + // return undefined if array index out of bound. + return nType.types[value] && new TypeWrapper(nType.types[value], this.context); } return new TypeWrapper(nType, this.context); } diff --git a/packages/language-service/test/diagnostics_spec.ts b/packages/language-service/test/diagnostics_spec.ts index 0820891005..5a21eb43a2 100644 --- a/packages/language-service/test/diagnostics_spec.ts +++ b/packages/language-service/test/diagnostics_spec.ts @@ -159,6 +159,13 @@ describe('diagnostics', () => { .toBe(`Identifier 'badProperty' is not defined. 'Hero' does not contain such a member`); }); + it('should not produce errors if tuple array index out of bound', () => { + mockHost.override(TEST_TEMPLATE, ` + {{tupleArray[2].badProperty}}`); + const diags = ngLS.getDiagnostics(TEST_TEMPLATE); + expect(diags).toEqual([]); + }); + it('should not produce errors on function.bind()', () => { mockHost.override(TEST_TEMPLATE, `