Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting decorated class error #4585

Closed
Awk34 opened this issue Sep 28, 2016 · 8 comments
Closed

Exporting decorated class error #4585

Awk34 opened this issue Sep 28, 2016 · 8 comments
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator

Comments

@Awk34
Copy link

Awk34 commented Sep 28, 2016

Input Code

@Decorator()
export class MyClass {}

Babel Configuration (.bablerc, package.json, cli command)

{
    "plugins": [
        "syntax-decorators"
    ]
}

Expected Behavior

@Decorator()
export class MyClass {}

Current Behavior

export @Decorator()
class MyClass {}

Possible Solution

What I'm currently doing to circumvent this:

export let MyClass = @Decorator()
class MyClass {}

¯\_(ツ)_/¯

Context

I've got a project where I'm trying to parse ES6 code with Flow types, and just take the types out. This minimal case reproduces the same issue, though.

Your Environment

software version
Babel babel-cli@6.14.0
node 6.5.0
npm 3.10.3
Operating System Win 10 x64
@Awk34
Copy link
Author

Awk34 commented Sep 28, 2016

This might be an issue for Babylon instead

@hzoo
Copy link
Member

hzoo commented Sep 28, 2016

Is there a reason why you aren't using the transform? You can use https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy to transform decorators and https://babeljs.io/docs/plugins/transform-flow-strip-types/ to remove flow types

@loganfsmyth
Copy link
Member

This should be a pretty self-contained code generator fix. We'll need the export declaration step to test what kind of declaration it has to potentially render decorators, and the opposite logic to skip them when rendering the class declaration itself.

@Awk34
Copy link
Author

Awk34 commented Sep 28, 2016

@hzoo it goes alongside #4388 that I reported. I have a Yeoman generator that I have parse JS files, and then strip flow types, but leave the remainder of the code mostly unchanged.

@Awk34
Copy link
Author

Awk34 commented Dec 14, 2016

Can you point me to where this might be fixed in the code?

@hzoo
Copy link
Member

hzoo commented Dec 14, 2016

Yeah babel-generator. each node type has a function that prints it out.

Example with decorator:

export function Decorator(node: Object) {
this.token("@");
this.print(node.expression, node);
this.newline();
}

Probably need to fix something in babel/modules.js like at

function ExportDeclaration(node: Object) {
if (node.declaration) {
let declar = node.declaration;
this.print(declar, node);
if (!t.isStatement(declar)) this.semicolon();
} else {
if (node.exportKind === "type") {
this.word("type");
this.space();
}
let specifiers = node.specifiers.slice(0);
// print "special" specifiers first
let hasSpecial = false;
while (true) {
let first = specifiers[0];
if (t.isExportDefaultSpecifier(first) || t.isExportNamespaceSpecifier(first)) {
hasSpecial = true;
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.token(",");
this.space();
}
} else {
break;
}
}
if (specifiers.length || (!specifiers.length && !hasSpecial)) {
this.token("{");
if (specifiers.length) {
this.space();
this.printList(specifiers, node);
this.space();
}
this.token("}");
}
if (node.source) {
this.space();
this.word("from");
this.space();
this.print(node.source, node);
}
this.semicolon();
}
}

Can use http://astexplorer.net/#/pzoRIHiZyd to view the AST

@devongovett
Copy link
Contributor

Fixed by #5020.

@hzoo
Copy link
Member

hzoo commented Aug 17, 2017

This isn't allowed in Stage 2 decorators anymore so closing

https://github.com/babel/babylon/pull/587/files#diff-cd89c8d2f00940bd0d50120e23c326edR1

3.4. export keyword in between of class body and decorators

@hzoo hzoo closed this as completed Aug 17, 2017
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 4, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants