Skip to content

Commit

Permalink
feat(putout): add printer option (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
zieka authored Oct 11, 2024
1 parent 265c7ff commit 4692ecd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/main/java/org/openrewrite/codemods/Putout.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class Putout extends NodeBasedRecipe {

private static final String PUTOUT_DIR = Putout.class.getName() + ".PUTOUT_DIR";
private static final String REPO_DIRECTORY = ".";

@Override
public String getDisplayName() {
Expand All @@ -51,6 +52,20 @@ public String getDescription() {
@Nullable
Set<String> rules;

@Option(displayName = "Printer",
description = "By default Putout uses its own [putout] printer for formatting code. You can choose an alternative printer.",
valid = {"putout", "recast", "babel"},
required = false)
@Nullable
String printer;

@Override
public Accumulator getInitialValue(ExecutionContext ctx) {
Path path = RecipeResources.from(getClass()).extractResources("config", "", ctx);
ctx.putMessage(PUTOUT_DIR, path);
return super.getInitialValue(ctx);
}

@Override
protected List<String> getNpmCommand(Accumulator acc, ExecutionContext ctx) {
List<String> commands = new ArrayList<>();
Expand All @@ -66,6 +81,11 @@ protected List<String> getNpmCommand(Accumulator acc, ExecutionContext ctx) {
}

commands.add(executable + " ${repoDir}" + " --disable no-html-link-for-pages || true");

if (printer != null) {
commands.add("node " + ctx.getMessage(PUTOUT_DIR).toString() + "/putout.js " + printer);
}

commands.add(executable + " ${repoDir}" + " --fix || true");
return commands;
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/resources/config/putout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs');
const path = require('path');

const configPath = path.join(process.cwd(), '.putout.json');
const printer = process.argv[2];

(async function main() {

if (printer && fs.existsSync(configPath)) {
console.log('Updating printer in .putout.json');
try {
// Read existing .putout.json into a JavaScript object
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));

// Add or update the printer field
config.printer = printer;

// Write the updated configuration back to the file
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
console.log('Printer updated successfully!');
} catch (err) {
console.error('Error updating .putout.json:', err);
}
}
})().catch((error) => {
process.exitCode = 1;
console.error(error);
});


45 changes: 42 additions & 3 deletions src/test/java/org/openrewrite/codemods/PutoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PutoutTest implements RewriteTest {
@Test
void noRules() {
rewriteRun(
spec -> spec.recipe(new Putout(null)),
spec -> spec.recipe(new Putout(null, null)),
text(
//language=js
"""
Expand All @@ -48,7 +48,7 @@ export function used() {}
@Test
void withRules() {
rewriteRun(
spec -> spec.recipe(new Putout(Set.of("conditions/merge-if-statements"))),
spec -> spec.recipe(new Putout(Set.of("conditions/merge-if-statements"), null)),
text(
//language=js
"""
Expand All @@ -75,7 +75,7 @@ export function used() {}
@Test
void jsx() {
rewriteRun(
spec -> spec.recipe(new Putout(null)),
spec -> spec.recipe(new Putout(null, null)),
text(
//language=js
"""
Expand Down Expand Up @@ -105,4 +105,43 @@ export function Test() {
)
);
}

/**
* recast should keep format close to original while putout would format it
* have all props on a new line, leave trailing commas, etc.
*/
@Test
void printer() {
rewriteRun(
spec -> spec.recipe(new Putout(null, "recast")),
text(
//language=js
"""
export const square = (x) => {
return x * x
}
export const sum = (a, b) => { return a + b };
export const obj = {
key: 'value',
method: function() {
return something;
},
sum, square
};
""",
"""
export const square = x => x ** 2
export const sum = (a, b) => a + b;
export const obj = {
key: 'value',
method: function() {
return something;
},
sum, square
};
""",
spec -> spec.path("src/Foo.js")
)
);
}
}

0 comments on commit 4692ecd

Please sign in to comment.