Skip to content

Commit ffd1005

Browse files
authored
style: Add prettier (#156)
* Add prettier * Add prettier rule to eslint * Add pre-commit hook and use prettierrc
1 parent 2ffd0a4 commit ffd1005

29 files changed

+1057
-1380
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"parser": "babel-eslint",
3-
"extends": "airbnb"
3+
"extends": ["airbnb", "prettier"]
44
}

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"print-width": "120",
3+
"singleQuote": true,
4+
"trailingComma": "all"
5+
}

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"tag": true
1616
}
1717
},
18+
"husky": {
19+
"hooks": {
20+
"pre-commit": "pretty-quick --staged"
21+
}
22+
},
1823
"license": "MIT",
1924
"homepage": "https://github.com/formsy/formsy-react",
2025
"bugs": "https://github.com/formsy/formsy-react/issues",
@@ -31,6 +36,7 @@
3136
"build": "NODE_ENV=production webpack -p --config webpack.config.js && yarn run prepublish",
3237
"changelog": "standard-changelog",
3338
"deploy": "np --any-branch",
39+
"format": "prettier --write src/**/* tests/**/* tests/*",
3440
"lint": "eslint src/**/*.js",
3541
"prepublish": "rm -Rf ./lib && babel ./src/ -d ./lib/",
3642
"test": "babel-node testrunner",
@@ -49,12 +55,16 @@
4955
"babel-preset-stage-2": "^6.24.1",
5056
"eslint": "^4.14.0",
5157
"eslint-config-airbnb": "^16.1.0",
58+
"eslint-config-prettier": "^5.0.0",
5259
"eslint-plugin-import": "^2.7.0",
5360
"eslint-plugin-jsx-a11y": "^6.0.3",
5461
"eslint-plugin-react": "^7.5.1",
62+
"husky": "^2.4.1",
5563
"jsdom": "^11.5.1",
5664
"nodeunit": "^0.11.1",
5765
"np": "^5.0.2",
66+
"prettier": "^1.18.2",
67+
"pretty-quick": "^1.11.1",
5868
"react": "^16.2.0 || ^16.0.0",
5969
"react-addons-pure-render-mixin": "^15.6.0",
6070
"react-addons-test-utils": "^15.6.0",

src/Wrapper.js

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import utils from './utils';
44

55
/* eslint-disable react/default-props-match-prop-types */
66

7-
const convertValidationsToObject = (validations) => {
7+
const convertValidationsToObject = validations => {
88
if (typeof validations === 'string') {
99
return validations.split(/,(?![^{[]*[}\]])/g).reduce((validationsAccumulator, validation) => {
1010
let args = validation.split(':');
1111
const validateMethod = args.shift();
1212

13-
args = args.map((arg) => {
13+
args = args.map(arg => {
1414
try {
1515
return JSON.parse(arg);
1616
} catch (e) {
@@ -19,7 +19,9 @@ const convertValidationsToObject = (validations) => {
1919
});
2020

2121
if (args.length > 1) {
22-
throw new Error('Formsy does not support multiple args on string validations. Use object format of validations instead.');
22+
throw new Error(
23+
'Formsy does not support multiple args on string validations. Use object format of validations instead.',
24+
);
2325
}
2426

2527
// Avoid parameter reassignment
@@ -35,21 +37,14 @@ const convertValidationsToObject = (validations) => {
3537
const propTypes = {
3638
innerRef: PropTypes.func,
3739
name: PropTypes.string.isRequired,
38-
required: PropTypes.oneOfType([
39-
PropTypes.bool,
40-
PropTypes.object,
41-
PropTypes.string,
42-
]),
43-
validations: PropTypes.oneOfType([
44-
PropTypes.object,
45-
PropTypes.string,
46-
]),
40+
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.object, PropTypes.string]),
41+
validations: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
4742
value: PropTypes.any, // eslint-disable-line react/forbid-prop-types
4843
};
4944

5045
export { propTypes };
5146

52-
export default (Component) => {
47+
export default Component => {
5348
class WrappedComponent extends React.Component {
5449
constructor(props) {
5550
super(props);
@@ -86,12 +81,8 @@ export default (Component) => {
8681
}
8782

8883
shouldComponentUpdate(nextProps, nextState) {
89-
const isPropsChanged = Object
90-
.keys(this.props)
91-
.some(k => (this.props[k] !== nextProps[k]));
92-
const isStateChanged = Object
93-
.keys(this.state)
94-
.some(k => (this.state[k] !== nextState[k]));
84+
const isPropsChanged = Object.keys(this.props).some(k => this.props[k] !== nextProps[k]);
85+
const isStateChanged = Object.keys(this.state).some(k => this.state[k] !== nextState[k]);
9586

9687
return isPropsChanged || isStateChanged;
9788
}
@@ -104,8 +95,10 @@ export default (Component) => {
10495
}
10596

10697
// If validations or required is changed, run a new validation
107-
if (!utils.isSame(this.props.validations, prevProps.validations) ||
108-
!utils.isSame(this.props.required, prevProps.required)) {
98+
if (
99+
!utils.isSame(this.props.validations, prevProps.validations) ||
100+
!utils.isSame(this.props.required, prevProps.required)
101+
) {
109102
this.context.formsy.validate(this);
110103
}
111104
}
@@ -118,23 +111,23 @@ export default (Component) => {
118111
getErrorMessage = () => {
119112
const messages = this.getErrorMessages();
120113
return messages.length ? messages[0] : null;
121-
}
114+
};
122115

123116
getErrorMessages = () => {
124117
if (!this.isValid() || this.showRequired()) {
125118
return this.state.externalError || this.state.validationError || [];
126119
}
127120
return [];
128-
}
121+
};
129122

130123
getValue = () => this.state.value;
131124

132125
setValidations = (validations, required) => {
133126
// Add validations to the store itself as the props object can not be modified
134127
this.validations = convertValidationsToObject(validations) || {};
135-
this.requiredValidations = required === true ? { isDefaultRequiredValue: true } :
136-
convertValidationsToObject(required);
137-
}
128+
this.requiredValidations =
129+
required === true ? { isDefaultRequiredValue: true } : convertValidationsToObject(required);
130+
};
138131

139132
// By default, we validate after the value has been set.
140133
// A user can override this and pass a second parameter of `false` to skip validation.
@@ -144,14 +137,17 @@ export default (Component) => {
144137
value,
145138
});
146139
} else {
147-
this.setState({
148-
value,
149-
isPristine: false,
150-
}, () => {
151-
this.context.formsy.validate(this);
152-
});
140+
this.setState(
141+
{
142+
value,
143+
isPristine: false,
144+
},
145+
() => {
146+
this.context.formsy.validate(this);
147+
},
148+
);
153149
}
154-
}
150+
};
155151

156152
hasValue = () => this.state.value !== '';
157153

@@ -165,17 +161,19 @@ export default (Component) => {
165161

166162
isValid = () => this.state.isValid;
167163

168-
isValidValue = value =>
169-
this.context.formsy.isValidValue.call(null, this, value);
164+
isValidValue = value => this.context.formsy.isValidValue.call(null, this, value);
170165

171166
resetValue = () => {
172-
this.setState({
173-
value: this.state.pristineValue,
174-
isPristine: true,
175-
}, () => {
176-
this.context.formsy.validate(this);
177-
});
178-
}
167+
this.setState(
168+
{
169+
value: this.state.pristineValue,
170+
isPristine: true,
171+
},
172+
() => {
173+
this.context.formsy.validate(this);
174+
},
175+
);
176+
};
179177

180178
showError = () => !this.showRequired() && !this.isValid();
181179

@@ -211,11 +209,7 @@ export default (Component) => {
211209
}
212210

213211
function getDisplayName(component) {
214-
return (
215-
component.displayName ||
216-
component.name ||
217-
(typeof component === 'string' ? component : 'Component')
218-
);
212+
return component.displayName || component.name || (typeof component === 'string' ? component : 'Component');
219213
}
220214

221215
WrappedComponent.displayName = `Formsy(${getDisplayName(Component)})`;

0 commit comments

Comments
 (0)