-
-
Notifications
You must be signed in to change notification settings - Fork 627
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
feat: Add prettier for formatting #79
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,99 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`transform should respect recast options 1`] = ` | ||
" | ||
module.exports = { | ||
devtool: 'eval', | ||
entry: [ | ||
'./src/index' | ||
], | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: 'index.js' | ||
}, | ||
module: { | ||
rules: [{ | ||
test: /.js$/, | ||
use: \\"babel\\", | ||
include: path.join(__dirname, 'src') | ||
}] | ||
}, | ||
resolve: { | ||
modules: ['node_modules', path.resolve('/src')], | ||
}, | ||
plugins: [ | ||
new webpack.optimize.UglifyJsPlugin({ | ||
sourceMap: true, | ||
}), | ||
new webpack.optimize.LoaderOptionsPlugin({ | ||
\\"debug\\": true, | ||
\\"minimize\\": true, | ||
}) | ||
], | ||
debug: true | ||
"module.exports = { | ||
devtool: 'eval', | ||
entry: ['./src/index'], | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: 'index.js' | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /.js$/, | ||
use: 'babel', | ||
include: path.join(__dirname, 'src') | ||
} | ||
] | ||
}, | ||
resolve: { | ||
modules: ['node_modules', path.resolve('/src')] | ||
}, | ||
plugins: [ | ||
new webpack.optimize.UglifyJsPlugin({ | ||
sourceMap: true | ||
}), | ||
new webpack.optimize.LoaderOptionsPlugin({ | ||
debug: true, | ||
minimize: true | ||
}) | ||
], | ||
debug: true | ||
}; | ||
" | ||
`; | ||
|
||
exports[`transform should transform only using specified transformations 1`] = ` | ||
" | ||
module.exports = { | ||
devtool: 'eval', | ||
entry: [ | ||
'./src/index' | ||
], | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: 'index.js' | ||
}, | ||
module: { | ||
rules: [{ | ||
test: /.js$/, | ||
use: ['babel'], | ||
include: path.join(__dirname, 'src') | ||
}] | ||
}, | ||
resolve: { | ||
root: path.resolve('/src'), | ||
modules: ['node_modules'] | ||
}, | ||
plugins: [ | ||
new webpack.optimize.UglifyJsPlugin(), | ||
new webpack.optimize.OccurrenceOrderPlugin() | ||
], | ||
debug: true | ||
"module.exports = { | ||
devtool: 'eval', | ||
entry: ['./src/index'], | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: 'index.js' | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /.js$/, | ||
use: ['babel'], | ||
include: path.join(__dirname, 'src') | ||
} | ||
] | ||
}, | ||
resolve: { | ||
root: path.resolve('/src'), | ||
modules: ['node_modules'] | ||
}, | ||
plugins: [ | ||
new webpack.optimize.UglifyJsPlugin(), | ||
new webpack.optimize.OccurrenceOrderPlugin() | ||
], | ||
debug: true | ||
}; | ||
" | ||
`; | ||
|
||
exports[`transform should transform using all transformations 1`] = ` | ||
" | ||
module.exports = { | ||
devtool: 'eval', | ||
entry: [ | ||
'./src/index' | ||
], | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: 'index.js' | ||
}, | ||
module: { | ||
rules: [{ | ||
test: /.js$/, | ||
use: 'babel', | ||
include: path.join(__dirname, 'src') | ||
}] | ||
}, | ||
resolve: { | ||
modules: ['node_modules', path.resolve('/src')] | ||
}, | ||
plugins: [ | ||
new webpack.optimize.UglifyJsPlugin({ | ||
sourceMap: true | ||
}), | ||
new webpack.optimize.LoaderOptionsPlugin({ | ||
'debug': true, | ||
'minimize': true | ||
}) | ||
], | ||
debug: true | ||
"module.exports = { | ||
devtool: 'eval', | ||
entry: ['./src/index'], | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: 'index.js' | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /.js$/, | ||
use: 'babel', | ||
include: path.join(__dirname, 'src') | ||
} | ||
] | ||
}, | ||
resolve: { | ||
modules: ['node_modules', path.resolve('/src')] | ||
}, | ||
plugins: [ | ||
new webpack.optimize.UglifyJsPlugin({ | ||
sourceMap: true | ||
}), | ||
new webpack.optimize.LoaderOptionsPlugin({ | ||
debug: true, | ||
minimize: true | ||
}) | ||
], | ||
debug: true | ||
}; | ||
" | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const jscodeshift = require('jscodeshift'); | ||
|
||
const prettier = require('prettier'); | ||
const loadersTransform = require('./loaders/loaders'); | ||
const resolveTransform = require('./resolve/resolve'); | ||
const removeJsonLoaderTransform = require('./removeJsonLoader/removeJsonLoader'); | ||
|
@@ -38,7 +38,8 @@ function transform(source, transforms, options) { | |
}, options); | ||
transforms = transforms || Object.keys(transformations).map(k => transformations[k]); | ||
transforms.forEach(f => f(jscodeshift, ast)); | ||
return ast.toSource(recastOptions); | ||
const astStr = ast.toSource(recastOptions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 Yes DAMP (Descriptive and Meaningful Phrases) ftw. |
||
return prettier.format(astStr, {singleQuote: true, tabWidth: 2}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Users should be able to pass prettier options. Also, some options like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets drop recast options. Recast pretty print is not reliable. I'll add prettier options instead |
||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,8 @@ module.exports = { | |
`; | ||
|
||
describe('transform', () => { | ||
it('should not transform if no transformations defined', () => { | ||
// WIll not be equal unless input is also from prettier | ||
xit('should not transform if no transformations defined', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, that's pretty much an issue to me. We only should apply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we applly prettier after showing diff to user? I'm not sure how to check if a transformation happened.. can AST's be compared? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it can be done by checking the return value. Check out jscodeshift docs since I'm on mobile now |
||
const output = transform(input, []); | ||
expect(output).toEqual(input); | ||
}); | ||
|
@@ -46,6 +47,7 @@ describe('transform', () => { | |
expect(output).toMatchSnapshot(); | ||
}); | ||
|
||
//TODO - What will we depend on? Recast or prettier options? | ||
it('should respect recast options', () => { | ||
const output = transform(input, undefined, { | ||
quote: 'double', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will make git diff impossible and it will require an extra step from users to remove the old config and rename the new. IMO it defeats the purpose of using this tool. Why would someone like to keep the old config?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right. But can we keep this until testing is done?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@okonet I could see a benefit of being able to have the old one left around especially while still a [WIP]. There were cases on initial trials of migrate where I had wished I specified outputConfigName beacuse I had lost the original, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the least we should store it somewhere, but only as a temp trial for now, or if user specifies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay but in this case I'd suggest renaming the original config to something with
webpack.config.orig.js
in order to be able to runwebpack
without specifying the path to the new one in case of the default name. Either way, I guess for most people the next operation after transforming would be to try run the build and see if everything works. With a different name it's not possible.