Skip to content

Commit e38cbf9

Browse files
committed
feat(ui-codemods): read consumer config for prettier if it exists when applying codemods
TEST PLAN: - Codemods should work as before, but the code output should be formatted according to the consumer prettier config Change-Id: I88a500ed9d88ca3e4f6d4ad8714bc9cff681bdad Reviewed-on: https://gerrit.instructure.com/c/instructure-ui/+/237054 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Pam Hiett <phiett@instructure.com> Product-Review: Pam Hiett <phiett@instructure.com> QA-Review: Steve Jensen <sejensen@instructure.com> Visual-Regression-Test: Steve Jensen <sejensen@instructure.com>
1 parent 3abd6ca commit e38cbf9

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true,
4+
"trailingComma": "none"
5+
}

packages/ui-codemods/lib/updateImports.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ module.exports = function (file, api, options) {
4848
}
4949

5050
const root = j(file.source)
51+
5152
let hasModifications = false
5253

5354
hasModifications = replaceDeprecatedImports(j, root, config, api) || hasModifications
5455

5556
return hasModifications
56-
? formatSource(root.toSource())
57+
? formatSource(root.toSource(), file.path)
5758
: null
5859
}

packages/ui-codemods/lib/updatePropNames.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ module.exports = function (file, api, options) {
4444
hasModifications = replaceDeprecatedProps(j, root, config) || hasModifications
4545

4646
return hasModifications
47-
? formatSource(root.toSource())
47+
? formatSource(root.toSource(), file.path)
4848
: null
4949
}

packages/ui-codemods/lib/utils/formatSource.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,24 @@
2424

2525
const prettier = require('prettier')
2626

27-
module.exports = function formatSource (source) {
28-
return prettier.format(source, {
27+
module.exports = function formatSource (source, sourcePath) {
28+
let options = null
29+
30+
try {
31+
options = prettier.resolveConfig.sync(sourcePath)
32+
33+
if (options) {
34+
// Set the parser argument if the consumer did not set one to avoid a console warning
35+
options = {
36+
...options,
37+
parser: options.parser || 'babel'
38+
}
39+
}
40+
} catch (err) {
41+
// Will revert to the deafult prettier options if a config cannot be parsed
42+
}
43+
44+
return prettier.format(source, options || {
2945
parser: 'babel',
3046
semi: false,
3147
singleQuote: true,

0 commit comments

Comments
 (0)