Skip to content

Transforming class member with decorators throws #17384

Closed
@shlomiassaf

Description

@shlomiassaf

TypeScript Version: 2.4.0

Code

Can't use something that runs from "tsc" since it does not support transformers

In a PropertyDeclaration visitor

return ts.updateProperty(
          node,
          node.decorators[0], // assume we have 2 or more decorators...
          node.modifiers,
          node.name,
          node.type,
          node.initializer
        );

Expected behavior:
Should compile.

Actual behavior:
Exception is thrown TypeError: Cannot read property 'kind' of undefined

The exception is throw from here

    export function nodeCanBeDecorated(node: Node): boolean {
        switch (node.kind) {
            case SyntaxKind.ClassDeclaration:
                // classes are valid targets
                return true;

            case SyntaxKind.PropertyDeclaration:
                // property declarations are valid if their parent is a class declaration.
                return node.parent.kind === SyntaxKind.ClassDeclaration;

            case SyntaxKind.GetAccessor:
            case SyntaxKind.SetAccessor:
            case SyntaxKind.MethodDeclaration:
                // if this method has a body and its parent is a class declaration, this is a valid target.
                return (<FunctionLikeDeclaration>node).body !== undefined
                    && node.parent.kind === SyntaxKind.ClassDeclaration;

            case SyntaxKind.Parameter:
                // if the parameter's parent has a body and its grandparent is a class declaration, this is a valid target;
                return (<FunctionLikeDeclaration>node.parent).body !== undefined
                    && (node.parent.kind === SyntaxKind.Constructor
                        || node.parent.kind === SyntaxKind.MethodDeclaration
                        || node.parent.kind === SyntaxKind.SetAccessor)
                    && node.parent.parent.kind === SyntaxKind.ClassDeclaration;
        }

        return false;
    }

Trying to reach node.parent.kind while node.parent is undefined, which is common to all "synthesized" nodes, i.e. transforming nodes.

This is true to all other members... Parameter, MethodDeclaration, GetAccessor and SetAccessor

Metadata

Metadata

Assignees

Labels

APIRelates to the public API for TypeScriptBugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions