Skip to content

Commit 7bb321a

Browse files
ValentinHCompuIves
authored andcommitted
Handle .prettierrc file at root (codesandbox#419)
* Handle .prettierrc root file * Parse .prettierrc on prettify * Add a info message about .prettierrc support in Preferences. * Update Prettier options documentation link
1 parent a2b9e32 commit 7bb321a

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

packages/app/src/app/containers/Preferences/CodeFormatting/Prettier.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ class Prettier extends React.PureComponent {
4444
return (
4545
<Container>
4646
<PreferenceContainer>
47+
<Description>
48+
This configuration can be overridden by a{' '}
49+
<a
50+
href="https://prettier.io/docs/en/configuration.html"
51+
target="_blank"
52+
rel="noopener noreferrer"
53+
>
54+
.prettierrc
55+
</a>{' '}
56+
JSON file at the root of the sandbox.
57+
</Description>
58+
<Rule />
59+
4760
<PaddedPreference
4861
title="Print width"
4962
type="number"

packages/app/src/app/containers/Preferences/CodeFormatting/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default () => (
1010
<Title>
1111
Prettier Settings{' '}
1212
<a
13-
href="https://github.com/prettier/prettier#options"
13+
href="https://prettier.io/docs/en/options.html"
1414
target="_blank"
1515
rel="noopener noreferrer"
1616
>

packages/app/src/app/store/entities/sandboxes/modules/actions.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// @flow
22

3-
import { singleModuleSelector } from './selectors';
3+
import { singleModuleSelector, modulesFromSandboxSelector } from './selectors';
44
import { preferencesSelector } from '../../../preferences/selectors';
5+
import { sandboxesSelector } from '../selectors';
6+
import notificationActions from '../../../notifications/actions';
57

68
import prettify from './utils/prettify';
79

@@ -33,9 +35,43 @@ const setCode = (id: string, code: string, isNotSynced: boolean = true) => (
3335
dispatch(setModuleSynced(id, !isNotSynced));
3436
};
3537

38+
const getPrettierConfigForModule = (
39+
id: string,
40+
dispatch: Function,
41+
state: Object
42+
) => {
43+
const sandboxes = sandboxesSelector(state);
44+
const sandboxOfModule = Object.values(sandboxes).find(s =>
45+
s.modules.includes(id)
46+
);
47+
if (!sandboxOfModule) return null;
48+
49+
const modules = modulesFromSandboxSelector(state, {
50+
sandbox: sandboxOfModule,
51+
});
52+
const prettierRcModule = modules.find(
53+
m => m.directoryShortid == null && m.title === '.prettierrc'
54+
);
55+
if (!prettierRcModule) return null;
56+
57+
// if we have found a config file, we try to parse it
58+
try {
59+
return JSON.parse(prettierRcModule.code);
60+
} catch (e) {
61+
dispatch(
62+
notificationActions.addNotification(
63+
'Your .prettierrc is not a valid JSON file',
64+
'error'
65+
)
66+
);
67+
}
68+
return null;
69+
};
70+
3671
const prettifyModule = (id: string) => async (dispatch, getState: Function) => {
3772
const module = singleModuleSelector(getState(), { id });
3873
const preferences = preferencesSelector(getState());
74+
const prettierRcConfig = getPrettierConfigForModule(id, dispatch, getState());
3975

4076
dispatch({
4177
type: PRETTIFY_MODULE,
@@ -46,7 +82,7 @@ const prettifyModule = (id: string) => async (dispatch, getState: Function) => {
4682
const newCode = await prettify(
4783
module.title,
4884
module.code,
49-
preferences.prettierConfig
85+
prettierRcConfig || preferences.prettierConfig
5086
);
5187

5288
dispatch(setCode(id, newCode));

0 commit comments

Comments
 (0)