From 4692ecdb174f082c6ab8f51b23f080ca96cf130d Mon Sep 17 00:00:00 2001 From: Kyle Scully Date: Thu, 10 Oct 2024 20:34:43 -0700 Subject: [PATCH] feat(putout): add printer option (#24) --- .../java/org/openrewrite/codemods/Putout.java | 20 +++++++++ src/main/resources/config/putout.js | 30 +++++++++++++ .../org/openrewrite/codemods/PutoutTest.java | 45 +++++++++++++++++-- 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/config/putout.js diff --git a/src/main/java/org/openrewrite/codemods/Putout.java b/src/main/java/org/openrewrite/codemods/Putout.java index 5ba741d..377f4c2 100644 --- a/src/main/java/org/openrewrite/codemods/Putout.java +++ b/src/main/java/org/openrewrite/codemods/Putout.java @@ -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() { @@ -51,6 +52,20 @@ public String getDescription() { @Nullable Set 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 getNpmCommand(Accumulator acc, ExecutionContext ctx) { List commands = new ArrayList<>(); @@ -66,6 +81,11 @@ protected List 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; } diff --git a/src/main/resources/config/putout.js b/src/main/resources/config/putout.js new file mode 100644 index 0000000..6589280 --- /dev/null +++ b/src/main/resources/config/putout.js @@ -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); + }); + + diff --git a/src/test/java/org/openrewrite/codemods/PutoutTest.java b/src/test/java/org/openrewrite/codemods/PutoutTest.java index 20ca91f..489df77 100644 --- a/src/test/java/org/openrewrite/codemods/PutoutTest.java +++ b/src/test/java/org/openrewrite/codemods/PutoutTest.java @@ -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 """ @@ -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 """ @@ -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 """ @@ -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") + ) + ); + } } \ No newline at end of file