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

Decorated async functions with retainLines is line-breaking after async #11870

Closed
andrewgies17 opened this issue Jul 22, 2020 · 1 comment · Fixed by #11947
Closed

Decorated async functions with retainLines is line-breaking after async #11870

andrewgies17 opened this issue Jul 22, 2020 · 1 comment · Fixed by #11947
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@andrewgies17
Copy link

Bug Report

Current behavior
Babel will generate invalid output from valid input when the following conditions hold:

  • An async function is decorated
  • Decorators are transformed using @babel/plugin-proposal-decorators in legacy (stage 1) mode.
  • @babel/preset-env is used targeting a version of Node that does not require regenerator-runtime (i.e. 12)
  • retainLines is set to true in the babel config

With this combination, the generated code will contain async and decorateMe() (in the below example), on two different lines. This does not make a valid async function declaration, and so node will complain about the presence of await in a non-async function.

Small Reproduction Repo: https://gitlab.com/andrew.gies.axiom/babel-repro

Input Code

const TestDecorator = (target, _property, descriptor) => {
  console.log("Decorated!");
};

class Test {
  @TestDecorator
  async decorateMe() {
    const text = await Promise.resolve('pinky swear');
    console.log(text);
  }
}

Expected behavior
This code will generate valid output that can be run by Node 12.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: .babelrc
{
  "presets": [
      ["@babel/env", {"targets": {"node": "12"}}]
    ],
  "retainLines": true,
  "plugins": [
    ["@babel/proposal-decorators", {"legacy": true}]
  ]
}

Environment

  System:
    OS: macOS Mojave 10.14.6
  Binaries:
    Node: 12.18.2 - ~/.nvm/versions/node/v12.18.2/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.6 - ~/.nvm/versions/node/v12.18.2/bin/npm
  npmPackages:
    @babel/cli: ^7.10.5 => 7.10.5 
    @babel/core: ^7.10.5 => 7.10.5 
    @babel/plugin-proposal-decorators: ^7.10.5 => 7.10.5 
    @babel/preset-env: ^7.10.4 => 7.10.4 
  • Babel version(s): 7.10.5 & main (2bf38fb)
  • Node/npm version: Node 12.18, npm 6.14
  • OS: macOS Mojave 10.14
  • Monorepo: no
  • How you are using Babel: cli

Possible Solution
A workaround is removing any of the four necessary conditions listed above, the easiest of which is to ditch retainLines. This is currently what I'm doing for my project - we don't actually need retainLines, it's a vestige of an older time.

Additional Context
I did some digging and found the following historical info related to this problem:

  • A related report (from Sep. 2018) can be found in #8650
  • #8650 was closed in favor of a related issue: #7275
  • #7275 was marked closed as a result of PR #8868 in Oct 2018
  • A very similar (but not quite the same) issue just surfaced about a week ago: #11830
  • #11830 was fixed by a recent PR: #11836
  • This PR hasn't yet landed in a release. However, I can still reproduce this issue on main (by cloning the babel
    repo and yarn linking the latest contents of main, including the merged PR #11836, into this test repo.)
@babel-bot
Copy link
Collaborator

Hey @andrewgies17! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Nov 10, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants