Ts-morph: Detect Array/Object Destructuring

Created on 7 Sep 2019  路  5Comments  路  Source: dsherret/ts-morph

Is it possible to add Array/Object Destructuring as a new feature?

Object Destructuring:

const {"some property": someProperty} = obj;
var {w, x, ...remaining} = {w: 1, x: 2, y: 3, z: 4};

Array Destructuring:

var [x, y, ...remaining] = [1, 2, 3, 4];
var [x, , ...remaining] = [1, 2, 3, 4];

It is hard to detect Destructuring from VariableDecleration.

question suggestion

Most helpful comment

What do you mean by "as a new feature"?

You should look for BindingElement, that means that descructing was used in code. "Name" and "property name" will give you what you're destructing (required) and how you've named it (optional). Presence of dotDotDotToken will tell if you if spread operator was used. OmittedExpression will be present in case you emit an element when destructing an array.

All 5 comments

What do you mean by "as a new feature"?

You should look for BindingElement, that means that descructing was used in code. "Name" and "property name" will give you what you're destructing (required) and how you've named it (optional). Presence of dotDotDotToken will tell if you if spread operator was used. OmittedExpression will be present in case you emit an element when destructing an array.

@lazarljubenovic

What do you mean by "as a new feature"?

Something like getDestructuringStatements() method.

Anyway, You explained so good Thanks.

Thanks for answering this @lazarljubenovic!

@HamedFathi These are still variable statement's whose only variable declaration's name is either an ObjectBindingPattern or ArrayBindingPattern. I believe those nodes are implemented right now, but let me know if you find any gaps!

AST Viewer Link

@dsherret

These are still variable statement

Yes, but Object literals are a variable statement too but you have documentation for those.

I think this is not bad to have a Destructuring section in the documentation.

Thanks

export class DestructuringExtractor {
    public extract(node: VariableStatement): DestructuringInfo[] | undefined {
        let result: DestructuringInfo[] = [];
        node.getDeclarations().forEach(declaration => {
            let elements: DestructuringElementInfo[] = [];
            let bindingElements = declaration.getDescendantsOfKind(SyntaxKind.BindingElement);
            if (bindingElements.length > 0) {
                let defaultValue = declaration.getInitializer() === undefined ? undefined : declaration.getInitializerOrThrow().getText();
                let isArrayDestructuring = declaration.getDescendantsOfKind(SyntaxKind.ArrayBindingPattern).length > 0;
                bindingElements.forEach(element => {
                    let name = element.getName();
                    let propertyName = element.getPropertyNameNode() === undefined
                        ? undefined : element.getPropertyNameNodeOrThrow().getText()
                    let isRest = element.getDotDotDotToken() !== undefined;
                    elements.push({
                        name: name,
                        propertyName: propertyName,
                        isRest: isRest
                    });
                });
                result.push({
                    isArrayDestructuring: isArrayDestructuring,
                    elements: elements,
                    defaultValue: defaultValue
                });
            }
        });
        return result.length === 0 ? undefined : result;
    }
}

@HamedFathi for sure! I've opened #700.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

allenhwkim picture allenhwkim  路  4Comments

HamedFathi picture HamedFathi  路  3Comments

rulatir picture rulatir  路  3Comments

HoldYourWaffle picture HoldYourWaffle  路  4Comments

GrandSchtroumpf picture GrandSchtroumpf  路  3Comments