Repository navigation
Transforming class member with decorators throws #17384
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: APIRelates to the public API for TypeScriptRelates to the public API for TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: APIRelates to the public API for TypeScriptRelates to the public API for TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Activity
shlomiassaf commentedon Jul 25, 2017
I think this PR should fix the problem...
Testing it now.
fix members decorator check with transformers
tbosch commentedon Aug 3, 2017
This bug also affects Angular / Tsickle as we are now using transformers. Our workaround (see transformer_util.ts):
parentfor synthetic nodests.updateSourceFileNodebut instead doing this:/cc Martin Probst (@mprobst) Evan Martin (@evmar) Alex Eagle (@alexeagle)
filipesilva commentedon Sep 15, 2017
Tobias Bosch (@tbosch) the related issue #17551 is fixed in 2.5.0 via #18051. Perhaps the workaround in Angular can go away when typescript is detected to be that version of greater.
This issue should be closed since I think #18051 fixes both.
shlomiassaf commentedon Nov 6, 2017
Filipe Silva (@filipesilva) from what I see #18051 did not solve the issue, or at least did not solve it completely.
See angular/angular#20216
cc Ron Buckton (@rbuckton)
attach parent in setOriginalNode
niveo commentedon Nov 28, 2017
typestack/class-transformer#108
Revin Roman (@rmrevin)
niveo commentedon Dec 15, 2017
angular/angular-cli#8434
George Mavritsakis (@gmavritsakis)
I had the same issue with:
Angular CLI 1.6.1
Typescript 2.4.2
Angular 5.0.2
Found a solution by changing typescript.js for now.
Replace all the function
function nodeCanBeDecorated(node)
with the following code
Which comes from here, if you compile typescript:
shlomiassaf/TypeScript@7017fa2
kirillgroshkov commentedon Jan 13, 2018
Waiting for the fix... It affects our project to upgrade to Angular5..
kirillgroshkov commentedon Jan 13, 2018
I want to share my workaround that I put together after reading other advices here and there. It's patching an
ngscript from@angular/cli@1.6.4(current version at the moment of writing). Because of that it prevents updating@angular/cli, so be prepared.ngsomewhere in your repo. You can grab patched version from here:https://github.com/kirillgroshkov/angular-cli/blob/18f14d71bc5d73dba488f8fc1d08fd46d1d885f1/packages/%40angular/cli/bin/ng
Here's what changed, compared to the original file: https://github.com/kirillgroshkov/angular-cli/commit/18f14d71bc5d73dba488f8fc1d08fd46d1d885f1
postinstallscript in yourpackage.jsonthat will run every time after you donpm install(oryarn install) and copy/overwritengfrom your location tonode_modules/@angular/cli/bin/ng.togbyte commentedon Jan 31, 2018
I have the same issue. Is this a problem with Angular, Typescript, or Angular CLI?
weswigham commentedon Feb 1, 2018
This is fixed in our just-released 2.7 build; so you'd have to wait for a new version of the angular CLI that makes use of it, or manually patch your local outdated copy like those above did (not recommended).
filipesilva commentedon Feb 1, 2018
Wesley Wigham (@weswigham) awesome, thanks for the update!