Input Code is not formatted the same as Output
See original GitHub issueReport
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.
}
}]
};
- Notice the
{,[
brackets on the same line and not on a new line. - 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.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top 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 >
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 Free
Top 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
Yeah, comments shouldn’t disappear
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