Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to allow formatting files listed in .gitignore #917

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added runtime syntax configuration option `syntax` to help handle ambiguous syntax. By default, StyLua builds and runs with a parser to handle all Lua versions. However, the syntax of some Lua versions conflict with eachother: most notably, Lua 5.2+ goto label syntax `::label::` and Luau type assertion operator `::`. This option allows choosing what syntax to parse, to handle these conflicts. ([#407](https://github.com/JohnnyMorganz/StyLua/issues/407))
- Added configuration option `space_after_function_names` to specify whether to include a space between a function name and parentheses ([#839](https://github.com/JohnnyMorganz/StyLua/issues/839))
- Added flag `--allow-gitignore` to continue formatting files listed in a `.gitignore` file, instead of skipping over them ([#895](https://github.com/JohnnyMorganz/StyLua/issues/895))

### Changed

Expand Down
37 changes: 36 additions & 1 deletion src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ fn format(opt: opt::Opt) -> Result<i32> {
}

walker_builder
.standard_filters(true)
.standard_filters(!opt.allow_gitignore)
.hidden(!opt.allow_hidden)
.parents(true)
.add_custom_ignore_filename(".styluaignore");
Expand Down Expand Up @@ -808,4 +808,39 @@ mod tests {

cwd.close().unwrap();
}

#[test]
fn test_formatting_respects_gitignore() {
let cwd = construct_tree!({
".git/dummy.txt": "", // Need a .git folder for .gitignore lookup
".gitignore": "foo.lua",
"foo.lua": "local x = 1",
});

let mut cmd = create_stylua();
cmd.current_dir(cwd.path()).args(["."]).assert().success();

cwd.child("foo.lua").assert("local x = 1");

cwd.close().unwrap();
}

#[test]
fn test_formatting_still_formats_gitignore_files_if_requested() {
let cwd = construct_tree!({
".git/dummy.txt": "", // Need a .git folder for .gitignore lookup
".gitignore": "foo.lua",
"foo.lua": "local x = 1",
});

let mut cmd = create_stylua();
cmd.current_dir(cwd.path())
.args(["--allow-gitignore", "."])
.assert()
.success();

cwd.child("foo.lua").assert("local x = 1\n");

cwd.close().unwrap();
}
}
4 changes: 4 additions & 0 deletions src/cli/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ pub struct Opt {
#[structopt(short, long)]
pub allow_hidden: bool,

/// Whether to continue formatting files listed in .gitignore / .ignore
#[structopt(long)]
pub allow_gitignore: bool,

/// Disables the EditorConfig feature.
///
/// Has no effect if a stylua.toml configuration file is found.
Expand Down
Loading