Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
378 changes: 333 additions & 45 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ overflow-checks = false
panic = "abort"

[features]
wasm = ["anyhow", "serde_json", "dprint-core/wasm"]
wasm = ["serde_json", "dprint-core/wasm"]
tracing = ["dprint-core/tracing"]

[dependencies]
anyhow = { version = "1.0.62", optional = true }
dprint-core = { version = "0.65.0", features = ["formatting"] }
dprint-core = { version = "0.68.1", features = ["formatting"], default-features = false }
dprint-core-macros = "0.1.0"
monch = "0.6.0"
serde = { version = "1.0.144", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
thiserror = "2"

[dev-dependencies]
dprint-development = "0.9.5"
dprint-development = "0.11.0"
serde_json = { version = "1.0" }
5 changes: 4 additions & 1 deletion deployment/npm/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const getPath = require("./index").getPath;

const buffer = require("fs").readFileSync(getPath());
const formatter = createFromBuffer(buffer);
const result = formatter.formatText("file.dockerfile", "RUN /bin/bash");
const result = formatter.formatText({
filePath: "file.dockerfile",
fileText: "RUN /bin/bash",
});

assert.strictEqual(result, "RUN /bin/bash\n");
37 changes: 23 additions & 14 deletions deployment/npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deployment/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"test": "node index.test.js"
},
"devDependencies": {
"@dprint/formatter": "~0.1.4"
"@dprint/formatter": "~0.5.1"
}
}
93 changes: 47 additions & 46 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dprint_core::formatting::ir_helpers::SingleLineOptions;
use dprint_core::formatting::ir_helpers::gen_from_raw_string;
use dprint_core::formatting::*;
use dprint_core_macros::sc;

use super::context::Context;
use super::helpers::*;
Expand Down Expand Up @@ -66,11 +67,11 @@ fn gen_node<'a>(node: Node<'a>, context: &mut Context<'a>) -> PrintItems {
fn gen_arg_instruction<'a>(node: &'a ArgInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();

items.push_str("ARG ");
items.push_sc(sc!("ARG "));
items.extend(gen_node((&node.name).into(), context));

if let Some(value) = &node.value {
items.push_str("=");
items.push_sc(sc!("="));
items.extend(gen_node(value.into(), context));
}

Expand All @@ -79,7 +80,7 @@ fn gen_arg_instruction<'a>(node: &'a ArgInstruction, context: &mut Context<'a>)

fn gen_cmd_instruction<'a>(node: &'a CmdInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.push_str("CMD ");
items.push_sc(sc!("CMD "));
items.extend(match &node.expr {
ShellOrExecExpr::Exec(node) => gen_node(node.into(), context),
ShellOrExecExpr::Shell(node) => gen_node(node.into(), context),
Expand All @@ -89,14 +90,14 @@ fn gen_cmd_instruction<'a>(node: &'a CmdInstruction, context: &mut Context<'a>)

fn gen_copy_instruction<'a>(node: &'a CopyInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
let prefix_str = "COPY ";
items.push_str(prefix_str);
let prefix = sc!("COPY ");
items.push_sc(prefix);

match &node.args {
CopyArgs::Exec(array) => {
for flag in &node.flags {
items.extend(gen_node(flag.into(), context));
items.push_str(" ");
items.push_sc(sc!(" "));
}
items.extend(gen_node(array.into(), context));
}
Expand All @@ -111,12 +112,12 @@ fn gen_copy_instruction<'a>(node: &'a CopyInstruction, context: &mut Context<'a>

if nodes.iter().any(|node| node.is_comment()) {
// preserve comments by breaking onto multiple lines, aligned with the arguments
items.extend(gen_multi_line_items(nodes, prefix_str.chars().count() as u32, context));
items.extend(gen_multi_line_items(nodes, prefix.text.chars().count() as u32, context));
} else {
// keep everything on a single line
for (i, node) in nodes.into_iter().enumerate() {
if i > 0 {
items.push_str(" ");
items.push_sc(sc!(" "));
}
items.extend(gen_node(node, context));
}
Expand All @@ -128,7 +129,7 @@ fn gen_copy_instruction<'a>(node: &'a CopyInstruction, context: &mut Context<'a>

fn gen_entrypoint_instruction<'a>(node: &'a EntrypointInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.push_str("ENTRYPOINT ");
items.push_sc(sc!("ENTRYPOINT "));
items.extend(match &node.expr {
ShellOrExecExpr::Exec(node) => gen_node(node.into(), context),
ShellOrExecExpr::Shell(node) => gen_node(node.into(), context),
Expand All @@ -139,58 +140,58 @@ fn gen_entrypoint_instruction<'a>(node: &'a EntrypointInstruction, context: &mut
fn gen_env_instruction<'a>(node: &'a EnvInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
let nodes = context.gen_nodes_with_comments(node.span.start, node.span.end, false, node.vars.iter().map(|i| i.into()));
let prefix_str = "ENV ";
items.push_str(prefix_str);
items.extend(gen_multi_line_items(nodes, prefix_str.chars().count() as u32, context));
let prefix = sc!("ENV ");
items.push_sc(prefix);
items.extend(gen_multi_line_items(nodes, prefix.text.chars().count() as u32, context));
items
}

fn gen_env_var<'a>(node: &'a EnvVar, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.extend(gen_node((&node.key).into(), context));
items.push_str("=");
items.push_sc(sc!("="));
items.extend(gen_node((&node.value).into(), context));
items
}

fn gen_from_instruction<'a>(node: &'a FromInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.push_str("FROM ");
items.push_sc(sc!("FROM "));
for flag in &node.flags {
items.extend(gen_node(flag.into(), context));
items.push_str(" ");
items.push_sc(sc!(" "));
}
items.extend(gen_node((&node.image).into(), context));
if let Some(alias) = &node.alias {
items.push_str(" AS ");
items.push_sc(sc!(" AS "));
items.extend(gen_node(alias.into(), context));
}
items
}

fn gen_from_flag<'a>(node: &'a FromFlag, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.push_str("--");
items.push_sc(sc!("--"));
items.extend(gen_node((&node.name).into(), context));
items.push_str("=");
items.push_sc(sc!("="));
items.extend(gen_node((&node.value).into(), context));
items
}

fn gen_label_instruction<'a>(node: &'a LabelInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
let prefix_str = "LABEL ";
items.push_str(prefix_str);
let prefix = sc!("LABEL ");
items.push_sc(prefix);
// route through gen_nodes_with_comments so comments between labels are kept
let nodes = context.gen_nodes_with_comments(node.span.start, node.span.end, false, node.labels.iter().map(|l| l.into()));
items.extend(gen_multi_line_items(nodes, prefix_str.chars().count() as u32, context));
items.extend(gen_multi_line_items(nodes, prefix.text.chars().count() as u32, context));
items
}

fn gen_label<'a>(node: &'a Label, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.extend(gen_node((&node.name).into(), context));
items.push_str("=");
items.push_sc(sc!("="));
items.extend(gen_node((&node.value).into(), context));
items
}
Expand Down Expand Up @@ -219,7 +220,7 @@ fn gen_multi_line_items<'a>(nodes: Vec<Node<'a>>, indent_width: u32, context: &m
if i < count - 1 && !is_comment {
node_items.push_condition(conditions::if_true("endLineText", is_multiline.create_resolver(), {
let mut items = PrintItems::new();
items.push_str(space_continuation);
items.push_sc(space_continuation);
items
}));
}
Expand Down Expand Up @@ -260,15 +261,15 @@ fn gen_multi_line_items<'a>(nodes: Vec<Node<'a>>, indent_width: u32, context: &m
fn gen_misc_instruction<'a>(node: &'a MiscInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.extend(gen_node((&node.instruction).into(), context));
items.push_str(" ");
items.push_sc(sc!(" "));
items.extend(gen_node((&node.arguments).into(), context));
items
}

fn gen_run_instruction<'a>(node: &'a RunInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();

items.push_str("RUN ");
items.push_sc(sc!("RUN "));
items.extend(match &node.expr {
ShellOrExecExpr::Exec(node) => gen_node(node.into(), context),
ShellOrExecExpr::Shell(node) => gen_node(node.into(), context),
Expand All @@ -279,7 +280,7 @@ fn gen_run_instruction<'a>(node: &'a RunInstruction, context: &mut Context<'a>)

fn gen_shell_instruction<'a>(node: &'a ShellInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.push_str("SHELL ");
items.push_sc(sc!("SHELL "));
items.extend(match &node.expr {
ShellOrExecExpr::Exec(node) => gen_node(node.into(), context),
ShellOrExecExpr::Shell(node) => gen_node(node.into(), context),
Expand All @@ -289,24 +290,24 @@ fn gen_shell_instruction<'a>(node: &'a ShellInstruction, context: &mut Context<'

fn gen_onbuild_instruction<'a>(node: &'a OnbuildInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.push_str("ONBUILD ");
items.push_sc(sc!("ONBUILD "));
items.extend(gen_node((&*node.instruction).into(), context));
items
}

fn gen_healthcheck_instruction<'a>(node: &'a HealthcheckInstruction, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.push_str("HEALTHCHECK ");
items.push_sc(sc!("HEALTHCHECK "));
for flag in &node.flags {
items.push_str("--");
items.push_sc(sc!("--"));
items.extend(gen_node((&flag.name).into(), context));
items.push_str("=");
items.push_sc(sc!("="));
items.extend(gen_node((&flag.value).into(), context));
items.push_str(" ");
items.push_sc(sc!(" "));
}
match &node.cmd {
Some(instruction) => items.extend(gen_node((&**instruction).into(), context)),
None => items.push_str("NONE"),
None => items.push_sc(sc!("NONE")),
}
items
}
Expand All @@ -323,25 +324,25 @@ fn gen_heredoc_instruction<'a>(node: &'a HeredocInstruction, context: &mut Conte

fn gen_string_array<'a>(node: &'a StringArray, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
items.push_str("[");
items.push_sc(sc!("["));
for (i, element) in node.elements.iter().enumerate() {
items.extend(gen_node(element.into(), context));
if i < node.elements.len() - 1 {
items.push_str(", ");
items.push_sc(sc!(", "));
}
}
items.push_str("]");
items.push_sc(sc!("]"));
items
}

/// The line-continuation marker for the file's escape character.
fn continuation(escape: char) -> &'static str {
if escape == '`' { "`" } else { "\\" }
fn continuation(escape: char) -> &'static StringContainer {
if escape == '`' { sc!("`") } else { sc!("\\") }
}

/// The line-continuation marker preceded by a separating space.
fn space_continuation(escape: char) -> &'static str {
if escape == '`' { " `" } else { " \\" }
fn space_continuation(escape: char) -> &'static StringContainer {
if escape == '`' { sc!(" `") } else { sc!(" \\") }
}

fn gen_breakable_string<'a>(node: &'a BreakableString, context: &mut Context<'a>) -> PrintItems {
Expand All @@ -363,13 +364,13 @@ fn gen_breakable_string<'a>(node: &'a BreakableString, context: &mut Context<'a>
let space_continuation = space_continuation(context.escape());

if use_quotes {
items.push_str("\"");
items.push_sc(sc!("\""));
}
// when the breakable starts with a comment (e.g. `RUN \` followed by a
// comment line), emit the line continuation so the comment stays attached to
// the instruction instead of being dropped or turning the rest into a comment
if matches!(node.components.first(), Some(BreakableStringComponent::Comment(_))) {
items.push_str(continuation);
items.push_sc(continuation);
items.push_signal(Signal::NewLine);
}
for (i, component) in node.components.iter().enumerate() {
Expand All @@ -389,16 +390,16 @@ fn gen_breakable_string<'a>(node: &'a BreakableString, context: &mut Context<'a>
// part of the (kept) string content, so don't add a separator space
let ends_in_quote = context.collapse_shell_ws && context.shell_quote.is_some();
if !use_quotes && !ends_in_quote && text.content.ends_with(" ") {
items.push_str(space_continuation);
items.push_sc(space_continuation);
} else {
items.push_str(continuation);
items.push_sc(continuation);
}
}
items.push_signal(Signal::NewLine);
}
}
if use_quotes {
items.push_str("\"");
items.push_sc(sc!("\""));
}

context.gen_string_content = previous_gen_string_content;
Expand Down Expand Up @@ -537,9 +538,9 @@ fn collapse_shell_whitespace(text: &str, drop_leading: bool, quote: &mut Option<
fn gen_copy_flag<'a>(node: &'a CopyFlag, context: &mut Context<'a>) -> PrintItems {
// ex: --from=foo
let mut items = PrintItems::new();
items.push_str("--");
items.push_sc(sc!("--"));
items.extend(gen_node((&node.name).into(), context));
items.push_str("=");
items.push_sc(sc!("="));
items.extend(gen_node((&node.value).into(), context));
items
}
Expand Down
Loading