Skip to content

Commit

Permalink
feat(31388): allow variables starting with an underscore in array des…
Browse files Browse the repository at this point in the history
…tructuring
  • Loading branch information
a-tarasyuk committed Nov 3, 2020
1 parent 9871b5f commit 406f130
Show file tree
Hide file tree
Showing 23 changed files with 326 additions and 144 deletions.
5 changes: 1 addition & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33363,10 +33363,7 @@ namespace ts {

function isValidUnusedLocalDeclaration(declaration: Declaration): boolean {
if (isBindingElement(declaration) && isIdentifierThatStartsWithUnderscore(declaration.name)) {
return !!findAncestor(declaration.parent, ancestor =>
isArrayBindingPattern(ancestor) || isVariableDeclaration(ancestor) || isVariableDeclarationList(ancestor) ? false :
isForOfStatement(ancestor) ? true : "quit"
);
return isArrayBindingPattern(declaration.parent);
}

return isAmbientModule(declaration) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [unusedVariablesWithUnderscoreInDestructuringArray1.ts]
function test() {
const [_a1, b1] = [1, 2];
console.log(b1);

const [a2, _b2] = [1, 2];
console.log(a2);

const [_a3, _b3] = [1, 2];
}


//// [unusedVariablesWithUnderscoreInDestructuringArray1.js]
function test() {
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
console.log(b1);
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
console.log(a2);
var _c = [1, 2], _a3 = _c[0], _b3 = _c[1];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray1.ts ===
function test() {
>test : Symbol(test, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 0, 0))

const [_a1, b1] = [1, 2];
>_a1 : Symbol(_a1, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 1, 11))
>b1 : Symbol(b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 1, 15))

console.log(b1);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>b1 : Symbol(b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 1, 15))

const [a2, _b2] = [1, 2];
>a2 : Symbol(a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 4, 11))
>_b2 : Symbol(_b2, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 4, 14))

console.log(a2);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>a2 : Symbol(a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 4, 11))

const [_a3, _b3] = [1, 2];
>_a3 : Symbol(_a3, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 7, 11))
>_b3 : Symbol(_b3, Decl(unusedVariablesWithUnderscoreInDestructuringArray1.ts, 7, 15))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray1.ts ===
function test() {
>test : () => void

const [_a1, b1] = [1, 2];
>_a1 : number
>b1 : number
>[1, 2] : [number, number]
>1 : 1
>2 : 2

console.log(b1);
>console.log(b1) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>b1 : number

const [a2, _b2] = [1, 2];
>a2 : number
>_b2 : number
>[1, 2] : [number, number]
>1 : 1
>2 : 2

console.log(a2);
>console.log(a2) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>a2 : number

const [_a3, _b3] = [1, 2];
>_a3 : number
>_b3 : number
>[1, 2] : [number, number]
>1 : 1
>2 : 2
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts(2,17): error TS6133: 'b1' is declared but its value is never read.
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts(3,12): error TS6133: 'a2' is declared but its value is never read.
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts(4,12): error TS6133: 'a3' is declared but its value is never read.
tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts(4,16): error TS6133: 'b3' is declared but its value is never read.


==== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts (4 errors) ====
function f() {
const [_a1, b1] = [1, 2];
~~
!!! error TS6133: 'b1' is declared but its value is never read.
const [a2, _b2] = [1, 2];
~~
!!! error TS6133: 'a2' is declared but its value is never read.
const [a3, b3] = [1, 2];
~~
!!! error TS6133: 'a3' is declared but its value is never read.
~~
!!! error TS6133: 'b3' is declared but its value is never read.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [unusedVariablesWithUnderscoreInDestructuringArray2.ts]
function f() {
const [_a1, b1] = [1, 2];
const [a2, _b2] = [1, 2];
const [a3, b3] = [1, 2];
}


//// [unusedVariablesWithUnderscoreInDestructuringArray2.js]
function f() {
var _a = [1, 2], _a1 = _a[0], b1 = _a[1];
var _b = [1, 2], a2 = _b[0], _b2 = _b[1];
var _c = [1, 2], a3 = _c[0], b3 = _c[1];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts ===
function f() {
>f : Symbol(f, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 0, 0))

const [_a1, b1] = [1, 2];
>_a1 : Symbol(_a1, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 1, 11))
>b1 : Symbol(b1, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 1, 15))

const [a2, _b2] = [1, 2];
>a2 : Symbol(a2, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 2, 11))
>_b2 : Symbol(_b2, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 2, 14))

const [a3, b3] = [1, 2];
>a3 : Symbol(a3, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 3, 11))
>b3 : Symbol(b3, Decl(unusedVariablesWithUnderscoreInDestructuringArray2.ts, 3, 14))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/unusedVariablesWithUnderscoreInDestructuringArray2.ts ===
function f() {
>f : () => void

const [_a1, b1] = [1, 2];
>_a1 : number
>b1 : number
>[1, 2] : [number, number]
>1 : 1
>2 : 2

const [a2, _b2] = [1, 2];
>a2 : number
>_b2 : number
>[1, 2] : [number, number]
>1 : 1
>2 : 2

const [a3, b3] = [1, 2];
>a3 : number
>b3 : number
>[1, 2] : [number, number]
>1 : 1
>2 : 2
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
//// [unusedVariablesWithUnderscoreInForOfLoop1.ts]
function f() {
for (const [_a, b] of [['key', 1]]) {}
for (const [_a, b] of [['key', 1]]) {
console.log(b);
}

for (const [a, _b] of [['key', 1]]) {}
for (const [a, _b] of [['key', 1]]) {
console.log(a);
}

for (const [a, b] of [['key', 1]]) {}
for (const [_a, _b] of [['key', 1]]) {}
}


//// [unusedVariablesWithUnderscoreInForOfLoop1.js]
function f() {
for (var _i = 0, _c = [['key', 1]]; _i < _c.length; _i++) {
var _d = _c[_i], _a = _d[0], b = _d[1];
console.log(b);
}
for (var _e = 0, _f = [['key', 1]]; _e < _f.length; _e++) {
var _g = _f[_e], a = _g[0], _b = _g[1];
console.log(a);
}
for (var _h = 0, _j = [['key', 1]]; _h < _j.length; _h++) {
var _k = _j[_h], a = _k[0], b = _k[1];
var _k = _j[_h], _a = _k[0], _b = _k[1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@
function f() {
>f : Symbol(f, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 0, 0))

for (const [_a, b] of [['key', 1]]) {}
for (const [_a, b] of [['key', 1]]) {
>_a : Symbol(_a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 1, 16))
>b : Symbol(b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 1, 19))

for (const [a, _b] of [['key', 1]]) {}
>a : Symbol(a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 3, 16))
>_b : Symbol(_b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 3, 18))
console.log(b);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>b : Symbol(b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 1, 19))
}

for (const [a, _b] of [['key', 1]]) {
>a : Symbol(a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 5, 16))
>_b : Symbol(_b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 5, 18))

for (const [a, b] of [['key', 1]]) {}
console.log(a);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 5, 16))
>b : Symbol(b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 5, 18))
}

for (const [_a, _b] of [['key', 1]]) {}
>_a : Symbol(_a, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 9, 16))
>_b : Symbol(_b, Decl(unusedVariablesWithUnderscoreInForOfLoop1.ts, 9, 19))
}

Loading

0 comments on commit 406f130

Please sign in to comment.