question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Input Code is not formatted the same as Output

See original GitHub issue

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

Thanks ya’ll.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, May 1, 2019

Yeah, comments shouldn’t disappear

1reaction
nicolo-ribaudocommented, Apr 30, 2019

Matching the original formatting is not a goal of Babel. I suggest using recast with Babel if you want to do so.

You can find an example at https://github.com/nicolo-ribaudo/legacy-decorators-migration-utility/blob/master/packages/wrap-legacy-decorators/src/index.js#L105

Read more comments on GitHub >

github_iconTop Results From Across the Web

Formatted and Unformatted Input/Output functions in C with ...
Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user. These types of I/O...
Read more >
Basic Input, Output, and String Formatting in Python
In this step-by-step Python tutorial, you'll learn how to take user input from the keyboard with the built-in function input(), how to display...
Read more >
OFDM, The input CODE does not have the same format as the ...
The error shown is Warning: The input CODE does not have the same format as the output of ENCODE. Check your computation procedures...
Read more >
How to keep the same format of the input file in the output file?
The problem is that infile brings me words, not whitespaces, etc. So, I am using a tab to separate the words from each...
Read more >
Basics of Formatted Input/Output in C
Basics of Formatted Input/Output in C. Concepts. I/O is essentially done one character (or byte) at a time; stream -- a sequence of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found