Skip to content

Commit

Permalink
[RFC] Introduce infiniteWidth document
Browse files Browse the repository at this point in the history
For some reason I don't understand, breakParent doesn't have the intended effect. But having a document that always puts the remaining with to < 0 does.

I'm pretty sure that we should actually fix the issue with breakParent but wanted to show a solution that would work.

Fixes #625
Fixes #616
  • Loading branch information
vjeux committed Feb 23, 2017
1 parent c8c1875 commit 28ec75b
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/doc-builders.js
Expand Up @@ -68,6 +68,7 @@ function lineSuffix(contents) {
}

const lineSuffixBoundary = { type: "line-suffix-boundary" };
const infiniteWidth = { type: "infinite-width" };
const breakParent = { type: "break-parent" };
const line = { type: "line" };
const softline = { type: "line", soft: true };
Expand Down Expand Up @@ -104,5 +105,6 @@ module.exports = {
lineSuffixBoundary,
breakParent,
ifBreak,
infiniteWidth,
indent
};
6 changes: 6 additions & 0 deletions src/doc-printer.js
Expand Up @@ -28,6 +28,10 @@ function fits(next, restCommands, width) {
width -= doc.length;
} else {
switch (doc.type) {
case "infinite-width":
width = -1;

break;
case "concat":
for (var i = doc.parts.length - 1; i >= 0; i--) {
cmds.push([ind, mode, doc.parts[i]]);
Expand Down Expand Up @@ -101,6 +105,8 @@ function printDocToString(doc, width, newLine) {
pos += doc.length;
} else {
switch (doc.type) {
case "infinite-width":
break;
case "concat":
for (var i = doc.parts.length - 1; i >= 0; i--) {
cmds.push([ind, mode, doc.parts[i]]);
Expand Down
5 changes: 3 additions & 2 deletions src/printer.js
Expand Up @@ -19,6 +19,7 @@ var conditionalGroup = docBuilders.conditionalGroup;
var ifBreak = docBuilders.ifBreak;
var breakParent = docBuilders.breakParent;
var lineSuffixBoundary = docBuilders.lineSuffixBoundary;
var infiniteWidth = docBuilders.infiniteWidth;

var docUtils = require("./doc-utils");
var willBreak = docUtils.willBreak;
Expand Down Expand Up @@ -711,9 +712,9 @@ function genericPrintNoParens(path, options, print) {
parentIsUnionTypeAnnotation ? 2 : 0,
concat([options.bracketSpacing ? line : softline, rightBrace])
),
shouldBreak ? infiniteWidth : "",
path.call(print, "typeAnnotation")
]),
{ shouldBreak }
])
);
}

Expand Down
138 changes: 138 additions & 0 deletions tests/infinite_width/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,138 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`infinite.js 1`] = `
"function f() {
if (position)
return {name: pair};
else
return {name: pair.substring(0, position), value: pair.substring(position + 1)};
}
function f() {
if (position) return { name: pair };
else return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
function f() {
if (position)
return { name: pair };
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
function render() {
return (
<View>
<Image
onProgress={(e) => this.setState({progress: Math.round(100 * e.nativeEvent.loaded / e.nativeEvent.total)})}
/>
</View>
);
}
function render() {
return (
<View>
<Image
onProgress={e =>
this.setState({
progress: Math.round(
100 * e.nativeEvent.loaded / e.nativeEvent.total,
),
})}
/>
</View>
);
}
function render() {
return (
<View>
<Image
onProgress={e =>
this.setState({
progress: Math.round(
100 * e.nativeEvent.loaded / e.nativeEvent.total,
),
})}
/>
</View>
);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function f() {
if (position) return { name: pair };
else return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
function f() {
if (position) return { name: pair };
else return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
function f() {
if (position) return { name: pair };
else return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
function render() {
return (
<View>
<Image
onProgress={e =>
this.setState({
progress: Math.round(
100 * e.nativeEvent.loaded / e.nativeEvent.total
)
})}
/>
</View>
);
}
function render() {
return (
<View>
<Image
onProgress={e =>
this.setState({
progress: Math.round(
100 * e.nativeEvent.loaded / e.nativeEvent.total
)
})}
/>
</View>
);
}
function render() {
return (
<View>
<Image
onProgress={e =>
this.setState({
progress: Math.round(
100 * e.nativeEvent.loaded / e.nativeEvent.total
)
})}
/>
</View>
);
}
"
`;
64 changes: 64 additions & 0 deletions tests/infinite_width/infinite.js
@@ -0,0 +1,64 @@
function f() {
if (position)
return {name: pair};
else
return {name: pair.substring(0, position), value: pair.substring(position + 1)};
}

function f() {
if (position) return { name: pair };
else return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}

function f() {
if (position)
return { name: pair };
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}

function render() {
return (
<View>
<Image
onProgress={(e) => this.setState({progress: Math.round(100 * e.nativeEvent.loaded / e.nativeEvent.total)})}
/>
</View>
);
}

function render() {
return (
<View>
<Image
onProgress={e =>
this.setState({
progress: Math.round(
100 * e.nativeEvent.loaded / e.nativeEvent.total,
),
})}
/>
</View>
);
}

function render() {
return (
<View>
<Image
onProgress={e =>
this.setState({
progress: Math.round(
100 * e.nativeEvent.loaded / e.nativeEvent.total,
),
})}
/>
</View>
);
}
1 change: 1 addition & 0 deletions tests/infinite_width/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(__dirname);
71 changes: 39 additions & 32 deletions tests/method-chain/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -39,18 +39,20 @@ it('should group messages with same created time', () => {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export default store => {
return callApi(endpoint, schema).then(
response => next(
actionWith({
response,
type: successType
})
),
error => next(
actionWith({
type: failureType,
error: error.message || \\"Something bad happened\\"
})
)
response =>
next(
actionWith({
response,
type: successType
})
),
error =>
next(
actionWith({
type: failureType,
error: error.message || \\"Something bad happened\\"
})
)
);
};
Expand Down Expand Up @@ -171,18 +173,21 @@ function f() {
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export default function theFunction(action$, store) {
return action$.ofType(THE_ACTION).switchMap(action => Observable.webSocket({
url: THE_URL,
more: stuff(),
evenMore: stuff({
value1: true,
value2: false,
value3: false
})
})
.filter(data => theFilter(data))
.map(({ theType, ...data }) => theMap(theType, data))
.retryWhen(errors => errors));
return action$
.ofType(THE_ACTION)
.switchMap(action =>
Observable.webSocket({
url: THE_URL,
more: stuff(),
evenMore: stuff({
value1: true,
value2: false,
value3: false
})
})
.filter(data => theFilter(data))
.map(({ theType, ...data }) => theMap(theType, data))
.retryWhen(errors => errors));
}
function f() {
Expand Down Expand Up @@ -227,14 +232,16 @@ if (testConfig.ENABLE_ONLINE_TESTS === \\"true\\") {
describe(\\"POST /users/me/pet\\", function() {
it(\\"saves pet\\", function() {
function assert(pet) {
expect(pet).to.have.property(\\"OwnerAddress\\").that.deep.equals({
AddressLine1: \\"Alexanderstrasse\\",
AddressLine2: \\"\\",
PostalCode: \\"10999\\",
Region: \\"Berlin\\",
City: \\"Berlin\\",
Country: \\"DE\\"
});
expect(pet).to.have
.property(\\"OwnerAddress\\")
.that.deep.equals({
AddressLine1: \\"Alexanderstrasse\\",
AddressLine2: \\"\\",
PostalCode: \\"10999\\",
Region: \\"Berlin\\",
City: \\"Berlin\\",
Country: \\"DE\\"
});
}
});
});
Expand Down

0 comments on commit 28ec75b

Please sign in to comment.