Skip to content

Drop variable statements when the declaration list gets transformed to an empty list #59893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2777,7 +2777,7 @@ export function transformES2015(context: TransformationContext): (x: SourceFile
*
* @param node A VariableDeclarationList node.
*/
function visitVariableDeclarationList(node: VariableDeclarationList): VariableDeclarationList {
function visitVariableDeclarationList(node: VariableDeclarationList): VariableDeclarationList | undefined {
if (node.flags & NodeFlags.BlockScoped || node.transformFlags & TransformFlags.ContainsBindingPattern) {
if (node.flags & NodeFlags.BlockScoped) {
enableSubstitutionsForBlockScopedBindings();
Expand All @@ -2791,6 +2791,10 @@ export function transformES2015(context: TransformationContext): (x: SourceFile
isVariableDeclaration,
);

if (!declarations.length) {
return;
}

const declarationList = factory.createVariableDeclarationList(declarations);
setOriginalNode(declarationList, node);
setTextRange(declarationList, node);
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/visitorPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ export function visitEachChild<T extends Node>(node: T | undefined, visitor: Vis
return fn === undefined ? node : fn(node, visitor, context, nodesVisitor, nodeVisitor, tokenVisitor);
}

type VisitEachChildFunction<T extends Node> = (node: T, visitor: Visitor, context: TransformationContext, nodesVisitor: NodesVisitor, nodeVisitor: NodeVisitor, tokenVisitor: Visitor | undefined) => T;
type VisitEachChildFunction<T extends Node> = (node: T, visitor: Visitor, context: TransformationContext, nodesVisitor: NodesVisitor, nodeVisitor: NodeVisitor, tokenVisitor: Visitor | undefined) => T | undefined;

// A type that correlates a `SyntaxKind` to a `VisitEachChildFunction<T>`, for nodes in the `HasChildren` union.
// This looks something like:
Expand Down Expand Up @@ -1280,10 +1280,14 @@ const visitEachChildTable: VisitEachChildTable = {
},

[SyntaxKind.VariableStatement]: function visitEachChildOfVariableStatement(node, visitor, context, nodesVisitor, nodeVisitor, _tokenVisitor) {
const declarationList = nodeVisitor(node.declarationList, visitor, isVariableDeclarationList);
if (!declarationList) {
return;
}
return context.factory.updateVariableStatement(
node,
nodesVisitor(node.modifiers, visitor, isModifierLike),
Debug.checkDefined(nodeVisitor(node.declarationList, visitor, isVariableDeclarationList)),
declarationList,
);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ var Inner;
(function (Inner) {
var ;
let;
var ;
Comment on lines 42 to -44
Copy link
Contributor Author

@Andarist Andarist Sep 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All those inputs are incorrect in the first place. So I'm not quite bothered that they produce different kind of an output. For an invalid input it's OK to emit an invalid output. It just happened that fixing a case for a valid input changed this here - but I think that's fine too. In a similar vein, I think it's OK to drop invalid statements from the invalid input.

var A = /** @class */ (function () {
function A() {
}
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/downlevelLetConst1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
const

//// [downlevelLetConst1.js]
var ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spooky given this test is named in such a way that implies that it's testing exactly this transformation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see https://github.com/microsoft/TypeScript/pull/59893/files#r1749158145 , i think the same principle would apply here

this particular test case is testing const, the next one in line is testing const a and the next one is const a = 1. So they just exercise different kinds of inputs and now the question is if it's ok to drop the variable declaration that was invalid in the first place

35 changes: 35 additions & 0 deletions tests/baselines/reference/emptyArrayBindingPattern01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/conformance/es6/destructuring/emptyArrayBindingPattern01.ts] ////

//// [emptyArrayBindingPattern01.ts]
export const cilBlurLinear1: string[][] = [[]];
const [,] = cilBlurLinear1;

export const cilBlurLinear2: string[][] = [[]];
let [,] = cilBlurLinear2;

export const cilBlurLinear3: string[][] = [[]];
var [,] = cilBlurLinear3;

export const cilBlurLinear4: string[][] = [[]];
const [[]] = cilBlurLinear4;

export const cilBlurLinear5: string[][] = [[]];
let [[]] = cilBlurLinear5;

export const cilBlurLinear6: string[][] = [[]];
var [[]] = cilBlurLinear6;


//// [emptyArrayBindingPattern01.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cilBlurLinear6 = exports.cilBlurLinear5 = exports.cilBlurLinear4 = exports.cilBlurLinear3 = exports.cilBlurLinear2 = exports.cilBlurLinear1 = void 0;
exports.cilBlurLinear1 = [[]];
exports.cilBlurLinear2 = [[]];
exports.cilBlurLinear3 = [[]];
exports.cilBlurLinear4 = [[]];
var _a = exports.cilBlurLinear4[0];
exports.cilBlurLinear5 = [[]];
var _b = exports.cilBlurLinear5[0];
exports.cilBlurLinear6 = [[]];
var _c = exports.cilBlurLinear6[0];
39 changes: 39 additions & 0 deletions tests/baselines/reference/emptyArrayBindingPattern01.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [tests/cases/conformance/es6/destructuring/emptyArrayBindingPattern01.ts] ////

=== emptyArrayBindingPattern01.ts ===
export const cilBlurLinear1: string[][] = [[]];
>cilBlurLinear1 : Symbol(cilBlurLinear1, Decl(emptyArrayBindingPattern01.ts, 0, 12))

const [,] = cilBlurLinear1;
>cilBlurLinear1 : Symbol(cilBlurLinear1, Decl(emptyArrayBindingPattern01.ts, 0, 12))

export const cilBlurLinear2: string[][] = [[]];
>cilBlurLinear2 : Symbol(cilBlurLinear2, Decl(emptyArrayBindingPattern01.ts, 3, 12))

let [,] = cilBlurLinear2;
>cilBlurLinear2 : Symbol(cilBlurLinear2, Decl(emptyArrayBindingPattern01.ts, 3, 12))

export const cilBlurLinear3: string[][] = [[]];
>cilBlurLinear3 : Symbol(cilBlurLinear3, Decl(emptyArrayBindingPattern01.ts, 6, 12))

var [,] = cilBlurLinear3;
>cilBlurLinear3 : Symbol(cilBlurLinear3, Decl(emptyArrayBindingPattern01.ts, 6, 12))

export const cilBlurLinear4: string[][] = [[]];
>cilBlurLinear4 : Symbol(cilBlurLinear4, Decl(emptyArrayBindingPattern01.ts, 9, 12))

const [[]] = cilBlurLinear4;
>cilBlurLinear4 : Symbol(cilBlurLinear4, Decl(emptyArrayBindingPattern01.ts, 9, 12))

export const cilBlurLinear5: string[][] = [[]];
>cilBlurLinear5 : Symbol(cilBlurLinear5, Decl(emptyArrayBindingPattern01.ts, 12, 12))

let [[]] = cilBlurLinear5;
>cilBlurLinear5 : Symbol(cilBlurLinear5, Decl(emptyArrayBindingPattern01.ts, 12, 12))

export const cilBlurLinear6: string[][] = [[]];
>cilBlurLinear6 : Symbol(cilBlurLinear6, Decl(emptyArrayBindingPattern01.ts, 15, 12))

var [[]] = cilBlurLinear6;
>cilBlurLinear6 : Symbol(cilBlurLinear6, Decl(emptyArrayBindingPattern01.ts, 15, 12))

81 changes: 81 additions & 0 deletions tests/baselines/reference/emptyArrayBindingPattern01.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//// [tests/cases/conformance/es6/destructuring/emptyArrayBindingPattern01.ts] ////

=== emptyArrayBindingPattern01.ts ===
export const cilBlurLinear1: string[][] = [[]];
>cilBlurLinear1 : string[][]
> : ^^^^^^^^^^
>[[]] : undefined[][]
> : ^^^^^^^^^^^^^
>[] : undefined[]
> : ^^^^^^^^^^^

const [,] = cilBlurLinear1;
> : undefined
> : ^^^^^^^^^
>cilBlurLinear1 : string[][]
> : ^^^^^^^^^^

export const cilBlurLinear2: string[][] = [[]];
>cilBlurLinear2 : string[][]
> : ^^^^^^^^^^
>[[]] : undefined[][]
> : ^^^^^^^^^^^^^
>[] : undefined[]
> : ^^^^^^^^^^^

let [,] = cilBlurLinear2;
> : undefined
> : ^^^^^^^^^
>cilBlurLinear2 : string[][]
> : ^^^^^^^^^^

export const cilBlurLinear3: string[][] = [[]];
>cilBlurLinear3 : string[][]
> : ^^^^^^^^^^
>[[]] : undefined[][]
> : ^^^^^^^^^^^^^
>[] : undefined[]
> : ^^^^^^^^^^^

var [,] = cilBlurLinear3;
> : undefined
> : ^^^^^^^^^
>cilBlurLinear3 : string[][]
> : ^^^^^^^^^^

export const cilBlurLinear4: string[][] = [[]];
>cilBlurLinear4 : string[][]
> : ^^^^^^^^^^
>[[]] : undefined[][]
> : ^^^^^^^^^^^^^
>[] : undefined[]
> : ^^^^^^^^^^^

const [[]] = cilBlurLinear4;
>cilBlurLinear4 : string[][]
> : ^^^^^^^^^^

export const cilBlurLinear5: string[][] = [[]];
>cilBlurLinear5 : string[][]
> : ^^^^^^^^^^
>[[]] : undefined[][]
> : ^^^^^^^^^^^^^
>[] : undefined[]
> : ^^^^^^^^^^^

let [[]] = cilBlurLinear5;
>cilBlurLinear5 : string[][]
> : ^^^^^^^^^^

export const cilBlurLinear6: string[][] = [[]];
>cilBlurLinear6 : string[][]
> : ^^^^^^^^^^
>[[]] : undefined[][]
> : ^^^^^^^^^^^^^
>[] : undefined[]
> : ^^^^^^^^^^^

var [[]] = cilBlurLinear6;
>cilBlurLinear6 : string[][]
> : ^^^^^^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ define(["require", "exports"], function (require, exports) {
exports.h1 = exports.g1 = exports.f1 = exports.e1 = exports.d1 = exports.c1 = exports.b1 = exports.a1 = exports.f = exports.e = exports.d = exports.c = exports.b = exports.a = void 0;
var ;
let;
var ;
var A = /** @class */ (function () {
function A() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.h1 = exports.g1 = exports.f1 = exports.e1 = exports.d1 = exports.c1 = exports.b1 = exports.a1 = exports.f = exports.e = exports.d = exports.c = exports.b = exports.a = void 0;
var ;
let;
var ;
var A = /** @class */ (function () {
function A() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export let h1: D = new D;
exports.h1 = exports.g1 = exports.f1 = exports.e1 = exports.d1 = exports.c1 = exports.b1 = exports.a1 = exports.f = exports.e = exports.d = exports.c = exports.b = exports.a = void 0;
var ;
let;
var ;
var A = /** @class */ (function () {
function A() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @module: commonjs
// @target: es5

export const cilBlurLinear1: string[][] = [[]];
const [,] = cilBlurLinear1;

export const cilBlurLinear2: string[][] = [[]];
let [,] = cilBlurLinear2;

export const cilBlurLinear3: string[][] = [[]];
var [,] = cilBlurLinear3;

export const cilBlurLinear4: string[][] = [[]];
const [[]] = cilBlurLinear4;

export const cilBlurLinear5: string[][] = [[]];
let [[]] = cilBlurLinear5;

export const cilBlurLinear6: string[][] = [[]];
var [[]] = cilBlurLinear6;