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

Input Code is not formatted the same as Output #9860

Open
moonmeister opened this Issue Apr 15, 2019 · 2 comments

Comments

Projects
None yet
3 participants
@moonmeister
Copy link

commented Apr 15, 2019

Report

Current Behavior
We're trying to use parse/generate to read a js config file, modify the config, then write back to the same file. To start I decided to ignore the modification step to verify I could read a config and write it back accurately. This is where we ran into an issue. Formatting and some comments get lost.

I honestly not sure if this is expected, a bug, or I'm missing a critical configuration option. Thanks for the help.

Input code:

module.exports = {
  siteMetadata: {
    title: `Gatsby Default Starter`,
    description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
    author: `@gatsbyjs`,
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
      },
    },
    /* this (optional) plugin enables Progressive Web App + Offline functionality */
    // To learn more, visit: https://gatsby.dev/offline
    // 'gatsby-plugin-offline'
  ],
}

What get's written to a file:

module.exports = {
  siteMetadata: {
    title: `Gatsby Default Starter`,
    description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
    author: `@gatsbyjs`
  },
  plugins: [`gatsby-plugin-react-helmet`, {
    resolve: `gatsby-source-filesystem`,
    options: {
      name: `images`,
      path: `${__dirname}/src/images`
    }
  }, `gatsby-transformer-sharp`, `gatsby-plugin-sharp`, {
    resolve: `gatsby-plugin-manifest`,
    options: {
      name: `gatsby-starter-default`,
      short_name: `starter`,
      start_url: `/`,
      background_color: `#663399`,
      theme_color: `#663399`,
      display: `minimal-ui`,
      icon: `src/images/gatsby-icon.png` // This path is relative to the root of the site.

    }
  }]
};
  1. Notice the {,[ brackets on the same line and not on a new line.
  2. Notice the final 3 lines of comments are missing.

Input Code

  • REPL or Repo link if applicable:
const fileLocation = path.join(rootPath, `gatsby-config.js`)
const file = fs.readFileSync(fileLocation).toString()
const currentConfigAST = parse(file)

const configString = generate(currentConfigAST, {
    comments: true,
    compact: false,
    concise: false,
  }).code

fs.writeFile(`gatsby-config-test.js`, configString, err => {
    if (err) throw new Error(`gatsby-config-test.js write error: ${err}`)
  })

Expected behavior/code
I would expect these two files to be identical. The brackets on a newline I could probably ignore, but missing comments is definitely not okay for our situation.

Environment

  • "@babel/code-frame": "^7.0.0",
    "@babel/core": "^7.4.3",
    "@babel/generator": "^7.4.0",
    "@babel/parser": "^7.4.3",
    "@babel/polyfill": "^7.0.0",
    "@babel/runtime": "^7.0.0",
    "@babel/traverse": "^7.4.3",
  • Node/npm version: node v10.15.3/npm v6.4.1
  • OS: Ubuntu Linux 18.10
  • Monorepo: lerna
  • How you are using Babel: import

Thanks ya'll.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 15, 2019

Hey @moonmeister! 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.

@andreydrozd

This comment has been minimized.

Copy link

commented Apr 17, 2019

I'm getting similar formatting issues on JSX, except I'm primarily getting newlines added in functions and in markup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.