Skip to content
This repository was archived by the owner on Mar 27, 2021. It is now read-only.

Commit 5e14d8d

Browse files
cweiss-stripemichelle
authored andcommitted
add prettier (#49)
1 parent a59491b commit 5e14d8d

File tree

13 files changed

+188
-147
lines changed

13 files changed

+188
-147
lines changed

.eslintrc.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
extends:
33
- stripe
44
- plugin:flowtype/recommended
5+
# overwrite any rules that may conflict with prettier
6+
- "prettier"
7+
- "prettier/flowtype"
8+
- "prettier/react"
59
parser: 'babel-eslint'
610
plugins:
711
- flowtype
@@ -13,7 +17,9 @@ rules:
1317
no-duplicate-imports: 0 # doesn't support flow imports.
1418
no-console: 1 # disable this while undergoing lots of development.
1519
func-style: 2
16-
arrow-parens: 2
20+
arrow-parens:
21+
- 2
22+
- 'as-needed'
1723
consistent-return: 2
1824
prefer-arrow-callback:
1925
- 2

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ script:
55
- npm run lint
66
- npm run flow
77
- npm run test
8+
- npm run prettier-list-different
89
cache: yarn

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ Build:
271271

272272
yarn run build
273273

274+
We use We use [prettier](https://github.com/prettier/prettier) for code formatting:
275+
276+
yarn run prettier
277+
274278
Checks:
275279

276280
yarn run lint

demo/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
injectStripe,
1717
} from '../src/index';
1818

19-
const handleChange = (change) => {
19+
const handleChange = change => {
2020
console.log('[change]', change);
2121
};
2222
const handleFocus = () => {
@@ -52,11 +52,11 @@ class _CardForm extends React.Component {
5252
props: {
5353
fontSize: string,
5454
stripe: StripeProps,
55-
}
56-
handleSubmit = (ev) => {
55+
};
56+
handleSubmit = ev => {
5757
ev.preventDefault();
58-
this.props.stripe.createToken().then((payload) => console.log(payload));
59-
}
58+
this.props.stripe.createToken().then(payload => console.log(payload));
59+
};
6060
render() {
6161
return (
6262
<form onSubmit={this.handleSubmit}>
@@ -81,11 +81,11 @@ class _SplitForm extends React.Component {
8181
props: {
8282
fontSize: string,
8383
stripe: StripeProps,
84-
}
85-
handleSubmit = (ev) => {
84+
};
85+
handleSubmit = ev => {
8686
ev.preventDefault();
87-
this.props.stripe.createToken().then((payload) => console.log(payload));
88-
}
87+
this.props.stripe.createToken().then(payload => console.log(payload));
88+
};
8989
render() {
9090
return (
9191
<form onSubmit={this.handleSubmit}>
@@ -145,14 +145,17 @@ class Checkout extends React.Component {
145145
window.addEventListener('resize', () => {
146146
if (window.innerWidth < 450 && this.state.elementFontSize !== '14px') {
147147
this.setState({elementFontSize: '14px'});
148-
} else if (window.innerWidth >= 450 && this.state.elementFontSize !== '18px') {
148+
} else if (
149+
window.innerWidth >= 450 &&
150+
this.state.elementFontSize !== '18px'
151+
) {
149152
this.setState({elementFontSize: '18px'});
150153
}
151154
});
152155
}
153156
state: {
154157
elementFontSize: string,
155-
}
158+
};
156159

157160
render() {
158161
const {elementFontSize} = this.state;

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"flow": "flow",
1616
"build": "yarn run lint && yarn run flow && yarn run build:commonjs && yarn run build:es && yarn run build:umd && yarn run build:umd:min",
1717
"clean": "rimraf lib dist es",
18+
"prettier": "prettier --single-quote --trailing-comma es5 --bracket-spacing false --parser flow src/**/*.js demo/**/*.js --write",
19+
"prettier-list-different": "prettier --single-quote --trailing-comma es5 --bracket-spacing false --parser flow src/**/*.js demo/**/*.js --list-different",
1820
"prepublish": "yarn run clean && yarn run build",
1921
"demo": "webpack-dev-server --content-base dist"
2022
},
@@ -47,12 +49,14 @@
4749
"cross-env": "^4.0.0",
4850
"enzyme": "^2.8.2",
4951
"eslint": "^3.19.0",
52+
"eslint-config-prettier": "^2.3.0",
5053
"eslint-config-stripe": "^1.0.10",
5154
"eslint-plugin-flowtype": "^2.32.1",
5255
"eslint-plugin-jest": "^19.0.1",
5356
"flow-bin": "^0.44.0",
5457
"html-webpack-plugin": "^2.28.0",
5558
"jest": "^19.0.2",
59+
"prettier": "^1.5.3",
5660
"react": "^15.5.4",
5761
"react-dom": "^15.5.4",
5862
"react-test-renderer": "^15.5.4",

src/components/Element.js

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,94 +14,96 @@ type Props = {
1414

1515
const noop = () => {};
1616

17-
const Element = (type: string, hocOptions: {sourceType?: string} = {}) => class extends React.Component {
18-
static propTypes = {
19-
elementRef: PropTypes.func,
20-
onChange: PropTypes.func,
21-
onBlur: PropTypes.func,
22-
onFocus: PropTypes.func,
23-
onReady: PropTypes.func,
24-
}
25-
static defaultProps = {
26-
elementRef: noop,
27-
onChange: noop,
28-
onBlur: noop,
29-
onFocus: noop,
30-
onReady: noop,
31-
}
17+
const Element = (type: string, hocOptions: {sourceType?: string} = {}) =>
18+
class extends React.Component {
19+
static propTypes = {
20+
elementRef: PropTypes.func,
21+
onChange: PropTypes.func,
22+
onBlur: PropTypes.func,
23+
onFocus: PropTypes.func,
24+
onReady: PropTypes.func,
25+
};
26+
static defaultProps = {
27+
elementRef: noop,
28+
onChange: noop,
29+
onBlur: noop,
30+
onFocus: noop,
31+
onReady: noop,
32+
};
3233

33-
static contextTypes = {
34-
elements: PropTypes.object.isRequired,
35-
registerElement: PropTypes.func.isRequired,
36-
unregisterElement: PropTypes.func.isRequired,
37-
}
34+
static contextTypes = {
35+
elements: PropTypes.object.isRequired,
36+
registerElement: PropTypes.func.isRequired,
37+
unregisterElement: PropTypes.func.isRequired,
38+
};
3839

39-
constructor(props: Props, context: ElementContext) {
40-
super(props, context);
40+
constructor(props: Props, context: ElementContext) {
41+
super(props, context);
4142

42-
const options = this._extractOptions(this.props);
43-
this._element = this.context.elements.create(type, options);
44-
this._setupEventListeners();
45-
this._options = options;
46-
}
43+
const options = this._extractOptions(this.props);
44+
this._element = this.context.elements.create(type, options);
45+
this._setupEventListeners();
46+
this._options = options;
47+
}
4748

48-
componentDidMount() {
49-
this._element.mount(this._ref);
50-
if (hocOptions.sourceType) {
51-
this.context.registerElement(hocOptions.sourceType, this._element);
49+
componentDidMount() {
50+
this._element.mount(this._ref);
51+
if (hocOptions.sourceType) {
52+
this.context.registerElement(hocOptions.sourceType, this._element);
53+
}
5254
}
53-
}
54-
componentWillReceiveProps(nextProps: Props) {
55-
const options = this._extractOptions(nextProps);
56-
if (Object.keys(options).length !== 0 && !shallowEqual(options, this._options)) {
57-
this._options = options;
58-
this._element.update(options);
55+
componentWillReceiveProps(nextProps: Props) {
56+
const options = this._extractOptions(nextProps);
57+
if (
58+
Object.keys(options).length !== 0 &&
59+
!shallowEqual(options, this._options)
60+
) {
61+
this._options = options;
62+
this._element.update(options);
63+
}
64+
}
65+
componentWillUnmount() {
66+
this._element.destroy();
67+
this.context.unregisterElement(this._element);
5968
}
60-
}
61-
componentWillUnmount() {
62-
this._element.destroy();
63-
this.context.unregisterElement(this._element);
64-
}
65-
props: Props
66-
context: ElementContext
67-
_element: ElementShape
68-
_ref: ?HTMLElement
69-
_options: Object
69+
props: Props;
70+
context: ElementContext;
71+
_element: ElementShape;
72+
_ref: ?HTMLElement;
73+
_options: Object;
7074

71-
_setupEventListeners() {
72-
this._element.on('ready', () => {
73-
this.props.elementRef(this._element);
74-
this.props.onReady();
75-
});
75+
_setupEventListeners() {
76+
this._element.on('ready', () => {
77+
this.props.elementRef(this._element);
78+
this.props.onReady();
79+
});
7680

77-
this._element.on('change', (change) => {
78-
this.props.onChange(change);
79-
});
81+
this._element.on('change', change => {
82+
this.props.onChange(change);
83+
});
8084

81-
this._element.on('blur', (...args) => this.props.onBlur(...args));
82-
this._element.on('focus', (...args) => this.props.onFocus(...args));
83-
}
85+
this._element.on('blur', (...args) => this.props.onBlur(...args));
86+
this._element.on('focus', (...args) => this.props.onFocus(...args));
87+
}
8488

85-
_extractOptions(props: Props): Object {
86-
const {
87-
elementRef,
88-
onChange,
89-
onFocus,
90-
onBlur,
91-
onReady,
92-
...options
93-
} = props;
94-
return options;
95-
}
89+
_extractOptions(props: Props): Object {
90+
const {
91+
elementRef,
92+
onChange,
93+
onFocus,
94+
onBlur,
95+
onReady,
96+
...options
97+
} = props;
98+
return options;
99+
}
96100

97-
handleRef = (ref: HTMLElement) => {
98-
this._ref = ref;
99-
}
100-
render() {
101-
return (
102-
<span ref={this.handleRef} />
103-
);
104-
}
105-
};
101+
handleRef = (ref: HTMLElement) => {
102+
this._ref = ref;
103+
};
104+
render() {
105+
return <span ref={this.handleRef} />;
106+
}
107+
};
106108

107109
export default Element;

src/components/Element.test.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ describe('Element', () => {
2727

2828
it('should call the right hooks for a source Element', () => {
2929
const SourceElement = Element('source', {sourceType: 'foobar'});
30-
const element = mount(
31-
<SourceElement onChange={jest.fn()} />,
32-
{context}
33-
);
30+
const element = mount(<SourceElement onChange={jest.fn()} />, {context});
3431

3532
expect(context.registerElement).toHaveBeenCalledTimes(1);
3633
expect(context.registerElement).toHaveBeenCalledWith('foobar', elementMock);
@@ -43,10 +40,7 @@ describe('Element', () => {
4340

4441
it('should call the right hooks for a non-source Element', () => {
4542
const SourceElement = Element('source');
46-
const element = mount(
47-
<SourceElement onChange={jest.fn()} />,
48-
{context}
49-
);
43+
const element = mount(<SourceElement onChange={jest.fn()} />, {context});
5044

5145
expect(context.registerElement).toHaveBeenCalledTimes(0);
5246

@@ -74,6 +68,8 @@ describe('Element', () => {
7468

7569
element.setProps({style: {base: {fontSize: '20px'}}, onChange: jest.fn()});
7670
expect(elementMock.update).toHaveBeenCalledTimes(1);
77-
expect(elementMock.update).toHaveBeenCalledWith({style: {base: {fontSize: '20px'}}});
71+
expect(elementMock.update).toHaveBeenCalledWith({
72+
style: {base: {fontSize: '20px'}},
73+
});
7874
});
7975
});

src/components/Elements.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ export default class Elements extends React.Component {
2222
elements: PropTypes.object.isRequired,
2323
registerElement: PropTypes.func.isRequired,
2424
unregisterElement: PropTypes.func.isRequired,
25-
registeredElements: PropTypes.arrayOf(PropTypes.shape({
26-
type: PropTypes.string.isRequired,
27-
element: PropTypes.object.isRequired,
28-
})).isRequired,
29-
}
25+
registeredElements: PropTypes.arrayOf(
26+
PropTypes.shape({
27+
type: PropTypes.string.isRequired,
28+
element: PropTypes.object.isRequired,
29+
})
30+
).isRequired,
31+
};
3032
static contextTypes = {
3133
stripe: PropTypes.object.isRequired,
32-
}
34+
};
3335

3436
constructor(props: Props, context: StripeContext) {
3537
super(props, context);
@@ -41,7 +43,7 @@ export default class Elements extends React.Component {
4143
registeredElements: [],
4244
};
4345
}
44-
state: FormContext
46+
state: FormContext;
4547

4648
getChildContext(): ElementsContext {
4749
return {
@@ -51,21 +53,23 @@ export default class Elements extends React.Component {
5153
registeredElements: this.state.registeredElements,
5254
};
5355
}
54-
props: Props
55-
context: StripeContext
56-
_elements: ElementsShape
56+
props: Props;
57+
context: StripeContext;
58+
_elements: ElementsShape;
5759

5860
handleRegisterElement = (type: string, element: Object) => {
59-
this.setState((prevState) => ({
61+
this.setState(prevState => ({
6062
registeredElements: [...prevState.registeredElements, {type, element}],
6163
}));
62-
}
64+
};
6365

6466
handleUnregisterElement = (el: Object) => {
65-
this.setState((prevState) => ({
66-
registeredElements: prevState.registeredElements.filter(({element}) => element !== el),
67+
this.setState(prevState => ({
68+
registeredElements: prevState.registeredElements.filter(
69+
({element}) => element !== el
70+
),
6771
}));
68-
}
72+
};
6973

7074
render() {
7175
return React.Children.only(this.props.children);

0 commit comments

Comments
 (0)