The class decorators are not correctly positioned in the AST.
See original GitHub issueThe class decorators array is put before the Class Declaration
element instead of being put as the decorators
property’s value of the Class Declaration
element.
Here is an example. Given the following JavaScript code:
import {MyDecorator} from './decorators.js';
@MyDecorator
class MyViewModel {
}
the output AST is:
[
{
type: 'ImportDeclaration',
specifiers: [ [Object] ],
source: {
type: 'Literal',
value: './decorators.js',
start: 26,
end: 43,
range: [Array]
},
start: 0,
end: 44,
range: [ 0, 44 ]
},
[
{
type: 'Decorator',
expression: [Object],
start: 45,
end: 57,
range: [Array]
}
],
{
type: 'ClassDeclaration',
id: {
type: 'Identifier',
name: 'MyViewModel',
start: 64,
end: 75,
range: [Array]
},
superClass: null,
decorators: [],
body: { type: 'ClassBody', body: [], start: 76, end: 79, range: [Array] },
start: 58,
end: 79,
range: [ 58, 79 ]
}
]
As you can see the ClassDeclaration.decorators
property is an empty array. But just before the ClassDeclaration
object there is an array of decorators.
Removing the lines
case Token.Decorator:
return parseDecorators(parser, context) as ESTree.Decorator[];
from the parseModuleItem()
function seems to fix the problem.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Confusion about when decorators are called in TypeScript
Decorators are called when the class is declared—not when an object is instantiated. That's correct. As @H.B. has already said, ...
Read more >Primer on Python Decorators
The @property decorator is used to customize getters and setters for class attributes. Expand the box below for an example using these decorators....
Read more >TC39 Standards Track Decorators in Babel
Class decorators take an object which contains the descriptors of every single class element, making it possible to modify them before creating ...
Read more >Decorators in Python: A Complete Guide (with Examples)
Python decorators offer a way to extend the behavior of a function or method. Use decorators avoid repetition and improve the code quality....
Read more >Issue 34876: Python3.8 changes how decorators are traced
The stacked decorator behavior is not. ... The position of the AST node for decorated function and class was changed to the position...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Legacy decorator is very popular, has been used in many front-end frameworks. TypeScript’s decorator closely resembles legacy decorator. New decorator proposal is making noise for years, but I have not encountered any real usage.
Babel parser has moved from legacy to latest proposal, but provided two switches, one to turn on legacy decorator syntax, another only to turn on decoratorBeforeExport (as you can see, forcing decorator after export default is quite unpopular).
Since new decorator proposal is going nowhere and legacy decorator is still alive, I suggest we support decorator before and after export regardless. I don’t think we need a flag to turn decoratorBeforeExport on and off, because it only causes confusion to end users who are lost in this decorator spec mess.
If this sounds right, I can draft an implementation later next week.
I put the fix to a branch now.