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 13, 2017
1 parent 6632b95 commit 3cebadd
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/doc-builders.js
Expand Up @@ -71,6 +71,7 @@ function lineSuffix(contents) {
return { type: "line-suffix", contents };
}

const infiniteWidth = { type: "infinite-width" };
const breakParent = { type: "break-parent" };
const line = { type: "line" };
const softline = { type: "line", soft: true };
Expand Down Expand Up @@ -106,5 +107,6 @@ module.exports = {
lineSuffix,
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 @@ -18,6 +18,7 @@ var indent = docBuilders.indent;
var conditionalGroup = docBuilders.conditionalGroup;
var ifBreak = docBuilders.ifBreak;
var breakParent = docBuilders.breakParent;
var infiniteWidth = docBuilders.infiniteWidth;

var docUtils = require("./doc-utils");
var willBreak = docUtils.willBreak;
Expand Down Expand Up @@ -650,10 +651,10 @@ function genericPrintNoParens(path, options, print) {
),
ifBreak(canHaveTrailingComma && options.trailingComma ? "," : ""),
options.bracketSpacing ? line : softline,
shouldBreak ? infiniteWidth : "",
rightBrace,
path.call(print, "typeAnnotation")
]),
{ shouldBreak }
])
);
}

Expand Down
136 changes: 136 additions & 0 deletions tests/infinite_width/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,136 @@
exports[`test 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);
44 changes: 24 additions & 20 deletions tests/method-chain/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -37,18 +37,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 @@ -137,14 +139,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 3cebadd

Please sign in to comment.