Skip to content
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

fix: empty destructuring with var should be converted to const #351

Open
wants to merge 1 commit into
base: master
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
24 changes: 23 additions & 1 deletion src/transform/let.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ export default function(ast, loggerInstance) {
variableMarker.markModified(decl.id.name);
}
});


// Empty destructuring should be converted to const.
//
// var [] = foo;
// var {} = foo;
//
// Mix of empty destructuring and other variable declarations will be handled
// by other parts of the transform.
if (node.kind === 'var' && node.declarations.every(isEmptyDestructuring)) {
node.kind = 'const';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Modifying the node.kind field here is definitely the easy way to fix this, but it doesn't align with how the rest of this transform is organized. Until now, there have been two distinct phases:

  • first gather all the data of all the var-declarations to be modified.
  • then go and convert the vars to let/const.

This change breaks this pattern. Now some var-declarations get modified during this first loop of gathering data, which breaks expectations of somebody reading this code in the future.

I'd rather leave these var-s unchanged than do it this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At first, I was trying to do it in the transformation phase, but nothing about empty restructuring will be collected in the "gathering data" stage. It's empty. 😢 That's why I ended up putting it here.

Copy link
Collaborator

@nene nene Oct 1, 2023

Choose a reason for hiding this comment

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

Yeah, it would need to involve some more reorganization. Currently this conversion loop goes through all variables, and takes the var-declarations of them, but as there are no variables here such an empty declaration will never be reached.

It's definitely not an easy change to make. There's a currently a heavily beaked-in assumption that all variable declarations actually declare at least a single variable.

One possible middle-ground solution would be to separately gather these empty declaration nodes and then separately loop through them in the modification-phase. This way at least the modification and gathering phases will be kept separate, avoiding confusion in that area.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. I will try to separate them.

}
}
else if (node.type === 'AssignmentExpression') {
destructuring.extractVariableNames(node.left).forEach(name => {
Expand Down Expand Up @@ -90,6 +102,16 @@ function isAnyWhileStatement(node) {
node.type === 'DoWhileStatement';
}

function isEmptyDestructuring(node) {
if (node.id.type === 'ObjectPattern') {
return node.id.properties.length === 0;
}
if (node.id.type === 'ArrayPattern') {
return node.id.elements.length === 0;
}
return false;
}

// Program node works almost like a function:
// it hoists all variables which can be transformed to block-scoped let/const.
// It just doesn't have name and parameters.
Expand Down Expand Up @@ -190,7 +212,7 @@ function transformVarsToLetOrConst() {

// let and const declarations aren't allowed in all the same places where
// var declarations are allowed. Notably, only var-declaration can occur
// directlt in if-statement (and other similar statements) body:
// directly in if-statement (and other similar statements) body:
//
// if (true) var x = 10;
//
Expand Down
25 changes: 25 additions & 0 deletions test/transform/letTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ describe('Let/const', () => {
'for (let i=0, len=arr.length; i<len; i++) {}'
);
});

it('should split to let & const when mixed with empty destructuring', () => {
expectTransform(
'var [] = [1, 2], {} = {foo: 1, bar: 2}, x = 1, y = 2;\n' +
'y = 3;'
).toReturn(
'const [] = [1, 2];\n' +
'const {} = {foo: 1, bar: 2};\n' +
'const x = 1;\n' +
'let y = 2;\n' +
'y = 3;'
);
});
});

describe('with variable declaration in restrictive parent', () => {
Expand Down Expand Up @@ -278,6 +291,18 @@ describe('Let/const', () => {
'console.log(x);'
);
});

it('should use const when no variables are destructured', () => {
expectTransform(
'var [] = [1, 2];\n' +
'var {} = {foo: 1, bar: 2};\n' +
'var [] = [1, 2], {} = {foo: 1, bar: 2};'
).toReturn(
'const [] = [1, 2];\n' +
'const {} = {foo: 1, bar: 2};\n' +
'const [] = [1, 2], {} = {foo: 1, bar: 2};'
);
});
});

describe('with nested function', () => {
Expand Down