Skip to content

Commit

Permalink
Merge pull request #170 from cleandart/120-master
Browse files Browse the repository at this point in the history
Reformat with line length of 120
  • Loading branch information
greglittlefield-wf authored Mar 18, 2019
2 parents f94c0b2 + 9af6605 commit 2f80f01
Show file tree
Hide file tree
Showing 29 changed files with 487 additions and 1,044 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
dart: stable
script:
- dartanalyzer .
- dartfmt -n --set-exit-if-changed .
- dartfmt --line-length=120 --dry-run --set-exit-if-changed .
- pub run dependency_validator -i build_runner,build_test,build_web_compilers
- pub run test -p chrome
- pub run build_runner test -- -p chrome
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ void main() {
## Contributing
Format using
```bash
dartfmt -l 120 -w .
```
While we'd like to adhere to the recommended line length of 80, it's too too short for much of the code
repo written before a formatter was use, causing excessive wrapping and code that's hard to read.
So, we us a line length of 120 instead.
### Running Tests
#### Dart 2: dart2js
Expand Down
79 changes: 37 additions & 42 deletions example/geocodes/geocodes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class _GeocodesResultItem extends react.Component {
/// shortly.
///
/// This is the only correct way to create a [Component]. Do not use the constructor!
var geocodesResultItem =
react.registerComponent(() => new _GeocodesResultItem());
var geocodesResultItem = react.registerComponent(() => new _GeocodesResultItem());

/// In this component we'll build an HTML `<table>` element full of the `<tr>` elements generated by
/// [_GeocodesResultItem]
Expand Down Expand Up @@ -80,23 +79,23 @@ class _GeocodesResultList extends react.Component {
]),
]),
react.tbody(
{'key': 'tbody'},
// The second argument contains the body of the component (as you have already seen).
//
// It can be a String, a Component or an Iterable.
props['data'].map((item) => geocodesResultItem({
'key': item['formatted_address'],
'lat': item['geometry']['location']['lat'],
'lng': item['geometry']['location']['lng'],
'formatted': item['formatted_address']
})))
{'key': 'tbody'},
// The second argument contains the body of the component (as you have already seen).
//
// It can be a String, a Component or an Iterable.
props['data'].map((item) => geocodesResultItem({
'key': item['formatted_address'],
'lat': item['geometry']['location']['lat'],
'lng': item['geometry']['location']['lng'],
'formatted': item['formatted_address']
})),
)
])
]);
}
}

var geocodesResultList =
react.registerComponent(() => new _GeocodesResultList());
var geocodesResultList = react.registerComponent(() => new _GeocodesResultList());

/// In this [Component] we'll build a search form.
///
Expand Down Expand Up @@ -184,15 +183,14 @@ class _GeocodesHistoryItem extends react.Component {
react.button({
'key': 'button',
'className': 'btn btn-sm btn-default',
'onClick': reload
'onClick': reload,
}, 'Reload'),
' (${props['status']}) ${props['query']}'
]);
}
}

var geocodesHistoryItem =
react.registerComponent(() => new _GeocodesHistoryItem());
var geocodesHistoryItem = react.registerComponent(() => new _GeocodesHistoryItem());

/// Renders the "history list"
///
Expand All @@ -203,21 +201,21 @@ class _GeocodesHistoryList extends react.Component {
return react.div({}, [
react.h3({'key': '1'}, 'History:'),
react.ul(
{
'key': 'list',
},
new List.from(props['data'].keys.map((key) => geocodesHistoryItem({
'key': key,
'query': props['data'][key]['query'],
'status': props['data'][key]['status'],
'reloader': props['reloader']
}))).reversed)
{
'key': 'list',
},
new List.from(props['data'].keys.map((key) => geocodesHistoryItem({
'key': key,
'query': props['data'][key]['query'],
'status': props['data'][key]['status'],
'reloader': props['reloader'],
}))).reversed,
)
]);
}
}

var geocodesHistoryList =
react.registerComponent(() => new _GeocodesHistoryList());
var geocodesHistoryList = react.registerComponent(() => new _GeocodesHistoryList());

/// The root [Component] of our application.
///
Expand Down Expand Up @@ -248,21 +246,19 @@ class _GeocodesApp extends react.Component {
var last_id = 0;

/// Sends the [addressQuery] to the API and processes the result
newQuery(String addressQuery) {
newQuery(String addressQuery) async {
// Once the query is being sent, it appears in the history and is given an id.
var id = addQueryToHistory(addressQuery);

// Prepare the URL
addressQuery = Uri.encodeQueryComponent(addressQuery);
var path =
'https://maps.googleapis.com/maps/api/geocode/json?address=$addressQuery';

// Send the request
HttpRequest.getString(path)
.then((String value) =>
// Delay the answer 2 more seconds, for test purposes
new Future.delayed(new Duration(seconds: 2), () => value))
.then((String raw) {
var path = 'https://maps.googleapis.com/maps/api/geocode/json?address=$addressQuery';

try {
// Send the request
var raw = await HttpRequest.getString(path);
// Delay the answer 2 more seconds, for test purposes
await new Future.delayed(new Duration(seconds: 2));
// Is this the answer to the last request?
if (id == last_id) {
// If yes, query was `OK` and `shown_addresses` are replaced
Expand All @@ -280,19 +276,18 @@ class _GeocodesApp extends react.Component {
// change one item. Therefore we mutate it and then replace it by itself.
//
// Have a look at `vacuum_persistent` package to achieve immutability of state.
setState(
{'shown_addresses': data['results'], 'history': state['history']});
setState({'shown_addresses': data['results'], 'history': state['history']});
} else {
// Otherwise, query was `canceled`
state['history'][id]['status'] = 'canceled';

setState({'history': state['history']});
}
}).catchError((Error error) {
} catch (error) {
state['history'][id]['status'] = 'error';

setState({'history': state['history']});
});
}
}

/// Add a new [addressQuery] to the `state.history` Map with its status set to 'pending', then return its `id`.
Expand Down
24 changes: 6 additions & 18 deletions example/test/call_and_nosuchmethod_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ void main() {
react.div({}, [
react.div({'key': 'noChildren'}),
react.div({'key': 'emptyList'}, []),
react.div({'key': 'singleItemInList'},
[react.div({})]), // This should produce a key warning
react.div({
'key': 'twoItemsInList'
}, [
react.div({}),
react.div({})
]), // This should produce a key warning
react.div({'key': 'singleItemInList'}, [react.div({})]), // This should produce a key warning
react.div({'key': 'twoItemsInList'}, [react.div({}), react.div({})]), // This should produce a key warning
react.div({'key': 'oneVariadicChild'}, react.div({})),

// These tests of variadic children won't pass in the ddc until https://github.com/dart-lang/sdk/issues/29904
Expand All @@ -36,16 +30,10 @@ void main() {

customComponent({'key': 'noChildren2'}),
customComponent({'key': 'emptyList2'}, []),
customComponent({'key': 'singleItemInList2'},
[customComponent({})]), // This should produce a key warning
customComponent({
'key': 'twoItemsInList2'
}, [
customComponent({}),
customComponent({})
]), // This should produce a key warning
customComponent(
{'key': 'oneVariadicChild2'}, customComponent({'key': '1'})),
customComponent({'key': 'singleItemInList2'}, [customComponent({})]), // This should produce a key warning
customComponent({'key': 'twoItemsInList2'},
[customComponent({}), customComponent({})]), // This should produce a key warning
customComponent({'key': 'oneVariadicChild2'}, customComponent({'key': '1'})),

// These tests of variadic children won't pass in the ddc until https://github.com/dart-lang/sdk/issues/29904
// is resolved.
Expand Down
3 changes: 1 addition & 2 deletions example/test/context_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ void main() {
contextConsumerComponent({
'key': 'consumerComponent'
}, [
grandchildContextConsumerComponent(
{'key': 'consumerGrandchildComponent'})
grandchildContextConsumerComponent({'key': 'consumerGrandchildComponent'})
]),
]),
querySelector('#content'));
Expand Down
19 changes: 5 additions & 14 deletions example/test/get_dom_node_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,9 @@ class SimpleComponent extends react.Component {
componentWillUnmount() => print("unmount");

componentDidMount() {
customAssert(
"ref to span return span ",
(react_dom.findDOMNode(ref("refToSpan")) as SpanElement).text ==
"Test");
customAssert(
"findDOMNode works on this", react_dom.findDOMNode(this) != null);
customAssert("random ref resolves to null",
react_dom.findDOMNode(ref("someRandomRef")) == null);
customAssert("ref to span return span ", (react_dom.findDOMNode(ref("refToSpan")) as SpanElement).text == "Test");
customAssert("findDOMNode works on this", react_dom.findDOMNode(this) != null);
customAssert("random ref resolves to null", react_dom.findDOMNode(ref("someRandomRef")) == null);
}

var counter = 0;
Expand All @@ -56,18 +51,14 @@ class SimpleComponent extends react.Component {
react.button({
'key': 'button1',
'className': 'btn btn-primary',
'onClick': (_) => (react_dom.findDOMNode(this) as HtmlElement)
.children
.first
.text = (++counter).toString()
'onClick': (_) => (react_dom.findDOMNode(this) as HtmlElement).children.first.text = (++counter).toString()
}, 'Increase counter'),
react.br({'key': 'br'}),
ChildComponent({'key': 'child', "ref": "refToElement"}),
react.button({
'key': 'button2',
'className': 'btn btn-primary',
'onClick': (_) => window.alert(
(this.ref('refToElement') as _ChildComponent).counter.toString())
'onClick': (_) => window.alert((this.ref('refToElement') as _ChildComponent).counter.toString())
}, 'Show value of child element'),
]);
}
Expand Down
3 changes: 1 addition & 2 deletions example/test/order_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class _List extends react.Component {
}

render() {
return react.ul({'onClick': (e) => remove()},
items.map((text) => item({'text': text, 'key': text})));
return react.ul({'onClick': (e) => remove()}, items.map((text) => item({'text': text, 'key': text})));
}
}

Expand Down
20 changes: 9 additions & 11 deletions example/test/react_test_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ class _CheckBoxComponent extends react.Component {
react.label({
'htmlFor': 'doTheDishes',
'key': 'label',
'className': 'form-check-label ' +
(this.state['checked'] ? 'striked' : 'not-striked')
'className': 'form-check-label ' + (this.state['checked'] ? 'striked' : 'not-striked')
}, 'do the dishes'),
]);
}
Expand All @@ -79,8 +78,7 @@ class _ClockComponent extends react.Component {
Map getDefaultProps() => {'refreshRate': 1000};

void componentWillMount() {
timer = new Timer.periodic(
new Duration(milliseconds: this.props["refreshRate"]), this.tick);
timer = new Timer.periodic(new Duration(milliseconds: this.props["refreshRate"]), this.tick);
}

void componentWillUnmount() {
Expand Down Expand Up @@ -123,13 +121,15 @@ class _ListComponent extends react.Component {
}

void componentWillUpdate(nextProps, nextState) {
if (nextState["items"].length > state["items"].length)
if (nextState["items"].length > state["items"].length) {
print("Adding " + nextState["items"].last.toString());
}
}

void componentDidUpdate(prevProps, prevState) {
if (prevState["items"].length > state["items"].length)
if (prevState["items"].length > state["items"].length) {
print("Removed " + prevState["items"].first.toString());
}
}

int iterator = 3;
Expand Down Expand Up @@ -185,7 +185,7 @@ class _ContextComponent extends react.Component {
react.button({
'key': 'button',
'className': 'btn btn-primary',
'onClick': _onButtonClick
'onClick': _onButtonClick,
}, 'Redraw'),
react.br({'key': 'break1'}),
'ContextComponent.getChildContext(): ',
Expand Down Expand Up @@ -220,8 +220,7 @@ class _ContextConsumerComponent extends react.Component {
}
}

var contextConsumerComponent =
react.registerComponent(() => new _ContextConsumerComponent());
var contextConsumerComponent = react.registerComponent(() => new _ContextConsumerComponent());

class _GrandchildContextConsumerComponent extends react.Component {
@override
Expand All @@ -237,5 +236,4 @@ class _GrandchildContextConsumerComponent extends react.Component {
}
}

var grandchildContextConsumerComponent =
react.registerComponent(() => new _GrandchildContextConsumerComponent());
var grandchildContextConsumerComponent = react.registerComponent(() => new _GrandchildContextConsumerComponent());
2 changes: 1 addition & 1 deletion example/test/ref_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class _ParentComponent extends react.Component {
}, [
ChildComponent({
'key': 'callback-child',
"ref": (instance) => _childCallbackRef = instance
"ref": (instance) => _childCallbackRef = instance,
}),
'\u00a0',
react.button({
Expand Down
8 changes: 2 additions & 6 deletions example/test/unmount_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ void main() {
setClientConfiguration();
var mountedNode = querySelector('#content');

querySelector('#mount')
.onClick
.listen((_) => react_dom.render(simpleComponent({}), mountedNode));
querySelector('#mount').onClick.listen((_) => react_dom.render(simpleComponent({}), mountedNode));

querySelector('#unmount')
.onClick
.listen((_) => react_dom.unmountComponentAtNode(mountedNode));
querySelector('#unmount').onClick.listen((_) => react_dom.unmountComponentAtNode(mountedNode));
}
Loading

0 comments on commit 2f80f01

Please sign in to comment.