Skip to content

Commit

Permalink
Added type checks to avoid false positive results
Browse files Browse the repository at this point in the history
  • Loading branch information
navya9singh committed Jul 19, 2022
1 parent d0547f1 commit 9b1cbe0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Expand Up @@ -15589,8 +15589,8 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
return accessFlags & AccessFlags.IncludeUndefined ? getUnionType([indexInfo.type, undefinedType]) : indexInfo.type;
}
errorIfWritingToReadonlyIndex(indexInfo);
if (accessFlags & AccessFlags.IncludeUndefined && objectType.symbol.flags & (SymbolFlags.RegularEnum | SymbolFlags.ConstEnum)) {
if(indexType.flags & TypeFlags.EnumLiteral){
if (accessFlags & AccessFlags.IncludeUndefined && objectType.symbol && objectType.symbol.flags & (SymbolFlags.RegularEnum | SymbolFlags.ConstEnum)) {
if (indexType.symbol && indexType.flags & TypeFlags.EnumLiteral && getParentOfSymbol(indexType.symbol) === objectType.symbol) {
return indexInfo.type;
}
}
Expand Down
31 changes: 26 additions & 5 deletions tests/baselines/reference/noUncheckedIndexAccess.js
Expand Up @@ -15,9 +15,17 @@ enum Meat {
//Avoiding a false positive
const value = Meat[0]

const t = "testing"
const value2 = Meat[t]

const valueUndefined = "testing"
const value2 = Meat[valueUndefined]

enum A {
a, b, c
}
enum B {
x, y, z
}

const value3 = A[B.x];

//// [noUncheckedIndexAccess.js]
var Meat;
Expand All @@ -33,5 +41,18 @@ var union = Meat.Bacon;
var valueUnion = Meat[union];
//Avoiding a false positive
var value = Meat[0];
var t = "testing";
var value2 = Meat[t];
var valueUndefined = "testing";
var value2 = Meat[valueUndefined];
var A;
(function (A) {
A[A["a"] = 0] = "a";
A[A["b"] = 1] = "b";
A[A["c"] = 2] = "c";
})(A || (A = {}));
var B;
(function (B) {
B[B["x"] = 0] = "x";
B[B["y"] = 1] = "y";
B[B["z"] = 2] = "z";
})(B || (B = {}));
var value3 = A[B.x];
32 changes: 28 additions & 4 deletions tests/baselines/reference/noUncheckedIndexAccess.symbols
Expand Up @@ -50,11 +50,35 @@ enum Meat {
>value : Symbol(value, Decl(noUncheckedIndexAccess.ts, 14, 7))
>Meat : Symbol(Meat, Decl(noUncheckedIndexAccess.ts, 0, 0))

const t = "testing"
>t : Symbol(t, Decl(noUncheckedIndexAccess.ts, 16, 7))
const valueUndefined = "testing"
>valueUndefined : Symbol(valueUndefined, Decl(noUncheckedIndexAccess.ts, 16, 7))

const value2 = Meat[t]
const value2 = Meat[valueUndefined]
>value2 : Symbol(value2, Decl(noUncheckedIndexAccess.ts, 17, 7))
>Meat : Symbol(Meat, Decl(noUncheckedIndexAccess.ts, 0, 0))
>t : Symbol(t, Decl(noUncheckedIndexAccess.ts, 16, 7))
>valueUndefined : Symbol(valueUndefined, Decl(noUncheckedIndexAccess.ts, 16, 7))

enum A {
>A : Symbol(A, Decl(noUncheckedIndexAccess.ts, 17, 37))

a, b, c
>a : Symbol(A.a, Decl(noUncheckedIndexAccess.ts, 19, 10))
>b : Symbol(A.b, Decl(noUncheckedIndexAccess.ts, 20, 6))
>c : Symbol(A.c, Decl(noUncheckedIndexAccess.ts, 20, 9))
}
enum B {
>B : Symbol(B, Decl(noUncheckedIndexAccess.ts, 21, 3))

x, y, z
>x : Symbol(B.x, Decl(noUncheckedIndexAccess.ts, 22, 10))
>y : Symbol(B.y, Decl(noUncheckedIndexAccess.ts, 23, 6))
>z : Symbol(B.z, Decl(noUncheckedIndexAccess.ts, 23, 9))
}

const value3 = A[B.x];
>value3 : Symbol(value3, Decl(noUncheckedIndexAccess.ts, 26, 7))
>A : Symbol(A, Decl(noUncheckedIndexAccess.ts, 17, 37))
>B.x : Symbol(B.x, Decl(noUncheckedIndexAccess.ts, 22, 10))
>B : Symbol(B, Decl(noUncheckedIndexAccess.ts, 21, 3))
>x : Symbol(B.x, Decl(noUncheckedIndexAccess.ts, 22, 10))

35 changes: 30 additions & 5 deletions tests/baselines/reference/noUncheckedIndexAccess.types
Expand Up @@ -53,13 +53,38 @@ enum Meat {
>Meat : typeof Meat
>0 : 0

const t = "testing"
>t : "testing"
const valueUndefined = "testing"
>valueUndefined : "testing"
>"testing" : "testing"

const value2 = Meat[t]
const value2 = Meat[valueUndefined]
>value2 : error
>Meat[t] : error
>Meat[valueUndefined] : error
>Meat : typeof Meat
>t : "testing"
>valueUndefined : "testing"

enum A {
>A : A

a, b, c
>a : A.a
>b : A.b
>c : A.c
}
enum B {
>B : B

x, y, z
>x : B.x
>y : B.y
>z : B.z
}

const value3 = A[B.x];
>value3 : string | undefined
>A[B.x] : string | undefined
>A : typeof A
>B.x : B.x
>B : typeof B
>x : B.x

13 changes: 11 additions & 2 deletions tests/cases/compiler/noUncheckedIndexAccess.ts
Expand Up @@ -17,5 +17,14 @@ enum Meat {
//Avoiding a false positive
const value = Meat[0]

const t = "testing"
const value2 = Meat[t]
const valueUndefined = "testing"
const value2 = Meat[valueUndefined]

enum A {
a, b, c
}
enum B {
x, y, z
}

const value3 = A[B.x];

0 comments on commit 9b1cbe0

Please sign in to comment.