Skip to content

Commit

Permalink
Ensure the initializer of a destructuring declaration is always inclu…
Browse files Browse the repository at this point in the history
…ded if the id is included.
  • Loading branch information
lukastaegert committed Oct 12, 2022
1 parent 5c74cd9 commit e977509
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/ast/nodes/VariableDeclaration.ts
Expand Up @@ -15,8 +15,10 @@ import {
import type { InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH } from '../utils/PathTracker';
import type Variable from '../variables/Variable';
import ArrayPattern from './ArrayPattern';
import Identifier, { type IdentifierWithVariable } from './Identifier';
import * as NodeType from './NodeType';
import ObjectPattern from './ObjectPattern';
import type VariableDeclarator from './VariableDeclarator';
import type { InclusionOptions } from './shared/Expression';
import { type IncludeChildren, NodeBase } from './shared/Node';
Expand Down Expand Up @@ -62,8 +64,17 @@ export default class VariableDeclaration extends NodeBase {
for (const declarator of this.declarations) {
if (includeChildrenRecursively || declarator.shouldBeIncluded(context))
declarator.include(context, includeChildrenRecursively);
const { id, init } = declarator;
if (asSingleStatement) {
declarator.id.include(context, includeChildrenRecursively);
id.include(context, includeChildrenRecursively);
}
if (
init &&
id.included &&
!init.included &&
(id instanceof ObjectPattern || id instanceof ArrayPattern)
) {
init.include(context, includeChildrenRecursively);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/function/samples/for-loop-parameter/_config.js
@@ -0,0 +1,9 @@
const assert = require('assert');

module.exports = {
description: 'includes for-loop parameters',
exports({ checkObject, checkArray }) {
assert.strictEqual(checkObject({ foo: 1 }), 1, 'object');
assert.strictEqual(checkArray([2]), 2, 'array');
}
};
19 changes: 19 additions & 0 deletions test/function/samples/for-loop-parameter/main.js
@@ -0,0 +1,19 @@
export function checkObject(p) {
return getFromObjectInLoop(p);
}

export function checkArray(p) {
return getFromArrayInLoop(p);
}

function getFromObjectInLoop(path) {
for (let { foo } = path;;) {
return foo;
}
}

function getFromArrayInLoop(path) {
for (let [ foo ] = path;;) {
return foo;
}
}

0 comments on commit e977509

Please sign in to comment.