diff --git a/CHANGELOG.md b/CHANGELOG.md index b1a8a30b..b3d16ecd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `--output-format=json` now outputs all (error) messages in JSON format ([#453](https://github.com/JohnnyMorganz/StyLua/issues/453)) - Added WASM build support. Stylua is available on npm for consumption in Node.js or a browser (using a bundler) - https://www.npmjs.com/package/@johnnymorganz/stylua +- Ignore comments will now be respected before fields inside tables ([#448](https://github.com/JohnnyMorganz/StyLua/issues/448)) ### Fixed - [**Luau**] Fixed spacing lost before a comment within a type generic ([#446](https://github.com/JohnnyMorganz/StyLua/issues/446)) diff --git a/README.md b/README.md index d650d10d..54154001 100644 --- a/README.md +++ b/README.md @@ -108,9 +108,10 @@ AST, flagging any syntax errors or possible code semantics changes. This flag ma where not every file can be examined for spurious formatting. ### Ignoring parts of a file -If there is a specific statement within your file which you wish to skip formatting on, you can precede it with `-- stylua: ignore`, -and it will be skipped over during formatting. This may be useful when there is a specific formatting style you wish to preserve for -a statement. For example: + +To skip formatting a particular part of a file, you can add `-- stylua: ignore` before it. +This may be useful if there is a particular style you want to preseve for readability, e.g.: + ```lua -- stylua: ignore local matrix = { @@ -119,7 +120,9 @@ local matrix = { { 0, 0, 0 }, } ``` -You can also disable formatting over a block of code by using `-- stylua: ignore start` / `-- stylua: ignore end` respectively. + +Formatting can also be skipped over a block of code using `-- stylua: ignore start` and `-- stylua: ignore end`: + ```lua local foo = true -- stylua: ignore start @@ -128,8 +131,8 @@ local baz = 0 -- stylua: ignore end local foobar = false ``` -Note: this comment must be preceding a statement (same as `-- stylua: ignore`), and cannot cross block scope boundaries -(i.e. if formatting is disabled, and we exit a block, formatting is automatically re-enabled). + +Note that ignoring cannot cross scope boundaries - once a block is exited, formatting will be re-enabled. ### Formatting Ranges If you only want to format a specific range within a file, you can pass the `--range-start ` and/or `--range-end ` arguments, diff --git a/src/context.rs b/src/context.rs index 8605072d..6e73ee4a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -106,33 +106,22 @@ impl Context { } if let Some(range) = self.range { - let mut in_range = true; - - if let Some(start_bound) = range.start { - if let Some(node_start) = node.start_position() { - if node_start.bytes() < start_bound { - in_range = false; - } + match (range.start, node.start_position()) { + (Some(start_bound), Some(node_start)) if node_start.bytes() < start_bound => { + return FormatNode::NotInRange } - } + _ => (), + }; - if let Some(end_bound) = range.end { - if let Some(node_end) = node.end_position() { - if node_end.bytes() > end_bound { - in_range = false; - } + match (range.end, node.end_position()) { + (Some(end_bound), Some(node_end)) if node_end.bytes() > end_bound => { + return FormatNode::NotInRange } + _ => (), } - - if in_range { - FormatNode::Normal - } else { - FormatNode::NotInRange - } - } else { - // No range provided, therefore always in formatting range - FormatNode::Normal } + + FormatNode::Normal } pub fn should_omit_string_parens(&self) -> bool { diff --git a/src/formatters/table.rs b/src/formatters/table.rs index 03e5c2d5..ed43f31e 100644 --- a/src/formatters/table.rs +++ b/src/formatters/table.rs @@ -1,5 +1,5 @@ use crate::{ - context::{create_indent_trivia, create_newline_trivia, Context}, + context::{create_indent_trivia, create_newline_trivia, Context, FormatNode}, fmt_symbol, formatters::{ expression::{format_expression, hang_expression, is_brackets_string}, @@ -110,6 +110,12 @@ fn format_field( table_type: TableType, shape: Shape, ) -> (Field, Vec) { + match dbg!(ctx.should_format_node(field)) { + FormatNode::Skip => return (field.to_owned(), Vec::new()), + FormatNode::NotInRange => unreachable!("called format_field on a field not in range"), + _ => (), + } + let leading_trivia = match table_type { TableType::MultiLine => FormatTriviaType::Append(vec![create_indent_trivia(ctx, shape)]), _ => FormatTriviaType::NoChange, diff --git a/tests/inputs-ignore/ignore-table-field.lua b/tests/inputs-ignore/ignore-table-field.lua new file mode 100644 index 00000000..c1e23b55 --- /dev/null +++ b/tests/inputs-ignore/ignore-table-field.lua @@ -0,0 +1,20 @@ +local foo = { + -- stylua: ignore + x = 2, + y = 3, + z = " he " , +} + +local bar = { + x = 2, + -- stylua: ignore + y = 3, + z = " he " , +} + +local baz = { + x = 2, + y = 3, + -- stylua: ignore + z = " he " , +} diff --git a/tests/inputs-ignore/multiline-block-ignore-1.lua b/tests/inputs-ignore/multiline-block-ignore-1.lua new file mode 100644 index 00000000..53204fee --- /dev/null +++ b/tests/inputs-ignore/multiline-block-ignore-1.lua @@ -0,0 +1,5 @@ +local foo = bar +-- stylua: ignore start +local bar = baz +-- stylua: ignore end +local bar = baz diff --git a/tests/inputs-ignore/multiline-block-ignore-2.lua b/tests/inputs-ignore/multiline-block-ignore-2.lua new file mode 100644 index 00000000..e62c4b0b --- /dev/null +++ b/tests/inputs-ignore/multiline-block-ignore-2.lua @@ -0,0 +1,6 @@ +local foo = bar +-- stylua: ignore start +local bar = baz +local bar = baz +-- stylua: ignore end +local bar = baz diff --git a/tests/inputs-ignore/multiline-block-ignore-multiple-comments-leading-trivia.lua b/tests/inputs-ignore/multiline-block-ignore-multiple-comments-leading-trivia.lua new file mode 100644 index 00000000..c8035c8d --- /dev/null +++ b/tests/inputs-ignore/multiline-block-ignore-multiple-comments-leading-trivia.lua @@ -0,0 +1,17 @@ +--stylua: ignore start +local a = 1 +--stylua: ignore end + +--stylua: ignore start +local b = 2 +--stylua: ignore end + +--stylua: ignore start +local c = 3 +--stylua: ignore end + +-- Some very large comment + +--stylua: ignore start +local d = 4 +--stylua: ignore end diff --git a/tests/inputs-ignore/multiline-block-ignore-no-ending.lua b/tests/inputs-ignore/multiline-block-ignore-no-ending.lua new file mode 100644 index 00000000..de8fc98a --- /dev/null +++ b/tests/inputs-ignore/multiline-block-ignore-no-ending.lua @@ -0,0 +1,5 @@ +local foo = bar +-- stylua: ignore start +local bar = baz +local bar = baz +local bar = baz diff --git a/tests/inputs-ignore/multiline-block-ignore-no-starting.lua b/tests/inputs-ignore/multiline-block-ignore-no-starting.lua new file mode 100644 index 00000000..f699243e --- /dev/null +++ b/tests/inputs-ignore/multiline-block-ignore-no-starting.lua @@ -0,0 +1,5 @@ +local foo = bar +local bar = baz +local bar = baz +-- stylua: ignore end +local bar = baz diff --git a/tests/inputs-ignore/multiline-block-ignore-scope-no-ending.lua b/tests/inputs-ignore/multiline-block-ignore-scope-no-ending.lua new file mode 100644 index 00000000..73dcea27 --- /dev/null +++ b/tests/inputs-ignore/multiline-block-ignore-scope-no-ending.lua @@ -0,0 +1,7 @@ +local foo = bar +do + -- stylua: ignore start + local bar = baz + local bar = baz +end +local bar = baz diff --git a/tests/inputs-ignore/multiline-block-ignore-scope.lua b/tests/inputs-ignore/multiline-block-ignore-scope.lua new file mode 100644 index 00000000..d9abd091 --- /dev/null +++ b/tests/inputs-ignore/multiline-block-ignore-scope.lua @@ -0,0 +1,8 @@ +local foo = bar +do + -- stylua: ignore start + local bar = baz + -- stylua: ignore end + local bar = baz +end +local bar = baz diff --git a/tests/inputs-ignore/singleline-ignore-1.lua b/tests/inputs-ignore/singleline-ignore-1.lua new file mode 100644 index 00000000..184059ff --- /dev/null +++ b/tests/inputs-ignore/singleline-ignore-1.lua @@ -0,0 +1,3 @@ +local foo = bar +-- stylua: ignore +local bar = baz diff --git a/tests/inputs-ignore/singleline-ignore-2.lua b/tests/inputs-ignore/singleline-ignore-2.lua new file mode 100644 index 00000000..2aad1740 --- /dev/null +++ b/tests/inputs-ignore/singleline-ignore-2.lua @@ -0,0 +1,4 @@ +local foo = bar +-- stylua: ignore +local bar = baz +local bar = baz diff --git a/tests/inputs-ignore/singleline-ignore-last-stmt.lua b/tests/inputs-ignore/singleline-ignore-last-stmt.lua new file mode 100644 index 00000000..8a292fdf --- /dev/null +++ b/tests/inputs-ignore/singleline-ignore-last-stmt.lua @@ -0,0 +1,2 @@ +-- stylua: ignore +return "hi" diff --git a/tests/inputs-ignore/singleline-ignore-stmt-block.lua b/tests/inputs-ignore/singleline-ignore-stmt-block.lua new file mode 100644 index 00000000..d2646f25 --- /dev/null +++ b/tests/inputs-ignore/singleline-ignore-stmt-block.lua @@ -0,0 +1,5 @@ +local x = 1 +-- stylua: ignore +function foo () + return x + 1 +end diff --git a/tests/snapshots/tests__ignores@ignore-table-field.lua.snap b/tests/snapshots/tests__ignores@ignore-table-field.lua.snap new file mode 100644 index 00000000..2d0ed420 --- /dev/null +++ b/tests/snapshots/tests__ignores@ignore-table-field.lua.snap @@ -0,0 +1,27 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = { + -- stylua: ignore + x = 2, + y = 3, + z = " he ", +} + +local bar = { + x = 2, + -- stylua: ignore + y = 3, + z = " he ", +} + +local baz = { + x = 2, + y = 3, + -- stylua: ignore + z = " he " , +} + diff --git a/tests/snapshots/tests__ignores@multiline-block-ignore-1.lua.snap b/tests/snapshots/tests__ignores@multiline-block-ignore-1.lua.snap new file mode 100644 index 00000000..0faf7939 --- /dev/null +++ b/tests/snapshots/tests__ignores@multiline-block-ignore-1.lua.snap @@ -0,0 +1,12 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = bar +-- stylua: ignore start +local bar = baz +-- stylua: ignore end +local bar = baz + diff --git a/tests/snapshots/tests__ignores@multiline-block-ignore-2.lua.snap b/tests/snapshots/tests__ignores@multiline-block-ignore-2.lua.snap new file mode 100644 index 00000000..4e5fbc54 --- /dev/null +++ b/tests/snapshots/tests__ignores@multiline-block-ignore-2.lua.snap @@ -0,0 +1,13 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = bar +-- stylua: ignore start +local bar = baz +local bar = baz +-- stylua: ignore end +local bar = baz + diff --git a/tests/snapshots/tests__ignores@multiline-block-ignore-multiple-comments-leading-trivia.lua.snap b/tests/snapshots/tests__ignores@multiline-block-ignore-multiple-comments-leading-trivia.lua.snap new file mode 100644 index 00000000..8ad8a537 --- /dev/null +++ b/tests/snapshots/tests__ignores@multiline-block-ignore-multiple-comments-leading-trivia.lua.snap @@ -0,0 +1,24 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +--stylua: ignore start +local a = 1 +--stylua: ignore end + +--stylua: ignore start +local b = 2 +--stylua: ignore end + +--stylua: ignore start +local c = 3 +--stylua: ignore end + +-- Some very large comment + +--stylua: ignore start +local d = 4 +--stylua: ignore end + diff --git a/tests/snapshots/tests__ignores@multiline-block-ignore-no-ending.lua.snap b/tests/snapshots/tests__ignores@multiline-block-ignore-no-ending.lua.snap new file mode 100644 index 00000000..746b6bb6 --- /dev/null +++ b/tests/snapshots/tests__ignores@multiline-block-ignore-no-ending.lua.snap @@ -0,0 +1,12 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = bar +-- stylua: ignore start +local bar = baz +local bar = baz +local bar = baz + diff --git a/tests/snapshots/tests__ignores@multiline-block-ignore-no-starting.lua.snap b/tests/snapshots/tests__ignores@multiline-block-ignore-no-starting.lua.snap new file mode 100644 index 00000000..d8171f86 --- /dev/null +++ b/tests/snapshots/tests__ignores@multiline-block-ignore-no-starting.lua.snap @@ -0,0 +1,12 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = bar +local bar = baz +local bar = baz +-- stylua: ignore end +local bar = baz + diff --git a/tests/snapshots/tests__ignores@multiline-block-ignore-scope-no-ending.lua.snap b/tests/snapshots/tests__ignores@multiline-block-ignore-scope-no-ending.lua.snap new file mode 100644 index 00000000..2d10327e --- /dev/null +++ b/tests/snapshots/tests__ignores@multiline-block-ignore-scope-no-ending.lua.snap @@ -0,0 +1,14 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = bar +do + -- stylua: ignore start + local bar = baz + local bar = baz +end +local bar = baz + diff --git a/tests/snapshots/tests__ignores@multiline-block-ignore-scope.lua.snap b/tests/snapshots/tests__ignores@multiline-block-ignore-scope.lua.snap new file mode 100644 index 00000000..71f78809 --- /dev/null +++ b/tests/snapshots/tests__ignores@multiline-block-ignore-scope.lua.snap @@ -0,0 +1,15 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = bar +do + -- stylua: ignore start + local bar = baz + -- stylua: ignore end + local bar = baz +end +local bar = baz + diff --git a/tests/snapshots/tests__ignores@singleline-ignore-1.lua.snap b/tests/snapshots/tests__ignores@singleline-ignore-1.lua.snap new file mode 100644 index 00000000..140f32b3 --- /dev/null +++ b/tests/snapshots/tests__ignores@singleline-ignore-1.lua.snap @@ -0,0 +1,10 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = bar +-- stylua: ignore +local bar = baz + diff --git a/tests/snapshots/tests__ignores@singleline-ignore-2.lua.snap b/tests/snapshots/tests__ignores@singleline-ignore-2.lua.snap new file mode 100644 index 00000000..a9f1aa8f --- /dev/null +++ b/tests/snapshots/tests__ignores@singleline-ignore-2.lua.snap @@ -0,0 +1,11 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local foo = bar +-- stylua: ignore +local bar = baz +local bar = baz + diff --git a/tests/snapshots/tests__ignores@singleline-ignore-last-stmt.lua.snap b/tests/snapshots/tests__ignores@singleline-ignore-last-stmt.lua.snap new file mode 100644 index 00000000..9c1b7896 --- /dev/null +++ b/tests/snapshots/tests__ignores@singleline-ignore-last-stmt.lua.snap @@ -0,0 +1,9 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +-- stylua: ignore +return "hi" + diff --git a/tests/snapshots/tests__ignores@singleline-ignore-stmt-block.lua.snap b/tests/snapshots/tests__ignores@singleline-ignore-stmt-block.lua.snap new file mode 100644 index 00000000..1fbad2b6 --- /dev/null +++ b/tests/snapshots/tests__ignores@singleline-ignore-stmt-block.lua.snap @@ -0,0 +1,12 @@ +--- +source: tests/tests.rs +assertion_line: 56 +expression: format(&contents) + +--- +local x = 1 +-- stylua: ignore +function foo () + return x + 1 +end + diff --git a/tests/test_ignore.rs b/tests/test_ignore.rs deleted file mode 100644 index f61bbcc7..00000000 --- a/tests/test_ignore.rs +++ /dev/null @@ -1,258 +0,0 @@ -use stylua_lib::{format_code, Config, OutputVerification}; - -fn format(input: &str) -> String { - format_code(input, Config::default(), None, OutputVerification::None).unwrap() -} - -#[test] -fn test_singleline_ignore() { - insta::assert_snapshot!( - format( - r###" -local foo = bar --- stylua: ignore -local bar = baz - "### - ), - @r###" - local foo = bar - -- stylua: ignore - local bar = baz - "### - ); -} - -#[test] -fn test_singleline_ignore_2() { - insta::assert_snapshot!( - format( - r###" -local foo = bar --- stylua: ignore -local bar = baz -local bar = baz - "### - ), - @r###" - local foo = bar - -- stylua: ignore - local bar = baz - local bar = baz - "### - ); -} - -#[test] -fn test_singleline_ignore_last_stmt() { - insta::assert_snapshot!( - format( - r###"-- stylua: ignore -return "hi" - "### - ), - @r###" - -- stylua: ignore - return "hi" - "### - ); -} - -#[test] -fn test_singleline_ignore_stmt_block() { - insta::assert_snapshot!( - r###"local x = 1 --- stylua: ignore -function foo () - return x + 1 -end"###, @r###" - local x = 1 - -- stylua: ignore - function foo () - return x + 1 - end - "### - ) -} - -#[test] -fn test_multiline_block_ignore() { - insta::assert_snapshot!( - format( - r###" -local foo = bar --- stylua: ignore start -local bar = baz --- stylua: ignore end -local bar = baz -"### - ), - @r###" - local foo = bar - -- stylua: ignore start - local bar = baz - -- stylua: ignore end - local bar = baz - "###); -} - -#[test] -fn test_multiline_block_ignore_2() { - insta::assert_snapshot!( - format( - r###" -local foo = bar --- stylua: ignore start -local bar = baz -local bar = baz --- stylua: ignore end -local bar = baz -"### - ), - @r###" - local foo = bar - -- stylua: ignore start - local bar = baz - local bar = baz - -- stylua: ignore end - local bar = baz - "###); -} - -#[test] -fn test_multiline_block_ignore_no_ending() { - insta::assert_snapshot!( - format( - r###" -local foo = bar --- stylua: ignore start -local bar = baz -local bar = baz -local bar = baz -"### - ), - @r###" - local foo = bar - -- stylua: ignore start - local bar = baz - local bar = baz - local bar = baz - "###); -} - -#[test] -fn test_multiline_block_ignore_no_starting() { - insta::assert_snapshot!( - format( - r###" -local foo = bar -local bar = baz -local bar = baz --- stylua: ignore end -local bar = baz -"### - ), - @r###" - local foo = bar - local bar = baz - local bar = baz - -- stylua: ignore end - local bar = baz - "###); -} - -#[test] -fn test_multiline_block_ignore_block_scope() { - insta::assert_snapshot!( - format( - r###" -local foo = bar -do - -- stylua: ignore start - local bar = baz - -- stylua: ignore end - local bar = baz -end -local bar = baz -"### - ), - @r###" - local foo = bar - do - -- stylua: ignore start - local bar = baz - -- stylua: ignore end - local bar = baz - end - local bar = baz - "###); -} - -#[test] -fn test_multiline_block_ignore_block_scope_no_ending() { - insta::assert_snapshot!( - format( - r###" -local foo = bar -do - -- stylua: ignore start - local bar = baz - local bar = baz -end -local bar = baz -"### - ), - @r###" - local foo = bar - do - -- stylua: ignore start - local bar = baz - local bar = baz - end - local bar = baz - "###); -} - -#[test] -fn test_multiline_block_ignore_multiple_comments_in_leading_trivia() { - insta::assert_snapshot!( - format( - r###"--stylua: ignore start -local a = 1 ---stylua: ignore end - ---stylua: ignore start -local b = 2 ---stylua: ignore end - ---stylua: ignore start -local c = 3 ---stylua: ignore end - --- Some very large comment - ---stylua: ignore start -local d = 4 ---stylua: ignore end -"### - ), - @r###" - --stylua: ignore start - local a = 1 - --stylua: ignore end - - --stylua: ignore start - local b = 2 - --stylua: ignore end - - --stylua: ignore start - local c = 3 - --stylua: ignore end - - -- Some very large comment - - --stylua: ignore start - local d = 4 - --stylua: ignore end - "### - ) -} diff --git a/tests/test_ranges.rs b/tests/test_ranges.rs index 1b8b7fa4..d3812b6f 100644 --- a/tests/test_ranges.rs +++ b/tests/test_ranges.rs @@ -1,6 +1,6 @@ use stylua_lib::{format_code, Config, OutputVerification, Range}; -fn format(input: &str, range: Range) -> String { +fn format_range(input: &str, range: Range) -> String { format_code( input, Config::default(), @@ -10,15 +10,26 @@ fn format(input: &str, range: Range) -> String { .unwrap() } +fn format(input: &str) -> String { + let start_point = input.find("||"); + let end_point = input.rfind("||"); + + format_code( + &input.replace("||", ""), + Config::default(), + Some(Range::from_values(start_point, end_point)), + OutputVerification::None, + ) + .unwrap() +} + #[test] fn test_default() { insta::assert_snapshot!( format( - r###" -local foo = bar + r###"||local foo = bar|| local bar = baz "###, - Range::from_values(Some(0), Some(30)) ), @r###" local foo = bar @@ -32,10 +43,9 @@ local bar = baz fn test_ignore_last_stmt() { insta::assert_snapshot!( format( - r###"function foo() + r###"||f||unction foo() return bar -end"###, - Range::from_values(Some(0), Some(1)) +end"### ), @r###" function foo() @@ -48,17 +58,16 @@ end"###, fn test_dont_modify_eof() { insta::assert_snapshot!( format( - r###" -local foo = bar + r###"||local foo = bar|| local bar = baz - "###, - Range::from_values(Some(0), Some(30)) + "### ), @r###" + local foo = bar local bar = baz @@ -73,8 +82,7 @@ local bar = baz fn test_incomplete_range() { insta::assert_snapshot!( format( - r###"local fooo = bar"###, - Range::from_values(Some(0), Some(5)) + r###"||local|| fooo = bar"### ), @r###" @@ -87,8 +95,7 @@ fn test_incomplete_range() { fn test_large_example() { insta::assert_snapshot!( format( - r###" -if string.sub(msg, 1, 8) == "setgrav/" then + r###"||if string.sub(msg, 1, 8) == "setgrav/" then danumber = nil for i = 9, 100 do if string.sub(msg, i, i) == "/" then danumber = i break end end if danumber == nil then return end local player = findplayer(string.sub(msg, 9, danumber - 1), speaker) if player == 0 then return end for i = 1, #player do if player[i].Character ~= nil then @@ -101,7 +108,7 @@ bf.Parent = torso end local c2 = player[i].Character:GetChildren() for i = 1, #c2 do if c2[i].className == "Part" then torso.BF.force = torso.BF.force + Vector3.new(0, c2[i]:getMass() * -string.sub(msg, danumber + 1), 0) -end end end end end end +end end end end end end|| if string.sub(msg, 1, 5) == "trip/" then local player = findplayer(string.sub(msg, 6), speaker) if player ~= 0 then for i = 1, #player do @@ -109,10 +116,10 @@ if player[i].Character ~= nil then local torso = player[i].Character:FindFirstChild("Torso") if torso ~= nil then torso.CFrame = CFrame.new(torso.Position.x, torso.Position.y, torso.Position.z, 0, 0, 1, 0, -1, 0, 1, 0, 0) --math.random(),math.random(),math.random(),math.random(),math.random(),math.random(),math.random(),math.random(),math.random()) -- i like the people being upside down better. end end end end end - "###, - Range::from_values(Some(0), Some(847)) + "### ), @r###" + if string.sub(msg, 1, 8) == "setgrav/" then danumber = nil for i = 9, 100 do @@ -168,10 +175,9 @@ fn test_nested_range() { insta::assert_snapshot!( format( r###"local my_function = function() - local nested_statement = "foobar" + ||local nested_statement = "foobar"|| end -"###, - Range::from_values(Some(33), Some(76)) +"### ), @r###" @@ -187,11 +193,10 @@ fn test_nested_range_local_function() { format( r###"local function test() call "hello" - call { x = y} - local z = 1 + 3 - (2 / 3) + ||call { x = y} + local z = 1 + 3 - (2 / 3)|| end -"###, - Range::from_values(Some(33), Some(116)) +"### ), @r###" local function test() @@ -207,10 +212,9 @@ fn test_nested_range_while() { insta::assert_snapshot!( format( r###"while true do - local z = 2 + ||local z = 2|| end -"###, - Range::from_values(Some(21), Some(47)) +"### ), @r###" while true do @@ -223,11 +227,10 @@ fn test_nested_range_while() { fn test_nested_range_repeat() { insta::assert_snapshot!( format( - r###"repeat - local z = 2 + r###"repeat|| + local z = 2|| until true -"###, - Range::from_values(Some(6), Some(32)) +"### ), @r###" repeat @@ -241,10 +244,9 @@ fn test_nested_range_do() { insta::assert_snapshot!( format( r###"do - local z = 2 + ||local z = 2|| end -"###, - Range::from_values(Some(2), Some(32)) +"### ), @r###" do @@ -258,10 +260,9 @@ fn test_nested_range_generic_for() { insta::assert_snapshot!( format( r###"for i, v in pairs(x) do - local z = 2 + ||local z = 2|| end -"###, - Range::from_values(Some(30), Some(56)) +"### ), @r###" for i, v in pairs(x) do @@ -277,10 +278,9 @@ fn test_nested_range_else_if() { r###"if x and y then local p = q elseif c - d > 2 then - local z = 2 + ||local z = 2|| end -"###, - Range::from_values(Some(69), Some(95)) +"### ), @r###" if x and y then @@ -296,10 +296,9 @@ fn test_nested_range_function_call() { insta::assert_snapshot!( format( r###"call (function () - local z = 5 + ||local z = 5|| end) -"###, - Range::from_values(Some(22), Some(58)) +"### ), @r###" call (function () @@ -313,10 +312,9 @@ fn test_nested_range_function_call_table() { insta::assert_snapshot!( format( r###"call { x = function() - local z = 2 + ||local z = 2|| end} -"###, - Range::from_values(Some(28), Some(61)) +"### ), @r###" call { x = function() @@ -331,11 +329,10 @@ fn test_nested_range_table_1() { format( r###"local z = { function() - local z = 5 + ||local z = 5|| end } -"###, - Range::from_values(Some(50), Some(121)) +"### ), @r###" local z = { @@ -352,12 +349,11 @@ fn test_nested_range_table_2() { format( r###"local z = { [(function() - return random_func () + ||return random_func ()|| end)()] = true } -"###, - Range::from_values(Some(41), Some(121)) +"### ), @r###" local z = { @@ -374,10 +370,9 @@ fn test_nested_range_binop() { insta::assert_snapshot!( format( r###"local z =( 1 + (function() - local p = q + ||local p = q|| end)()) -"###, - Range::from_values(Some(40), Some(75)) +"### ), @r###" local z =( 1 + (function() @@ -385,3 +380,34 @@ fn test_nested_range_binop() { end)()) "###); } + +#[test] +fn test_no_range_start() { + insta::assert_snapshot!( + format_range( + r###"local z = 2 +local e = 5 +"###, + Range::from_values(None, Some(20)) + ), + @r###" + local z = 2 + local e = 5 + "###); +} + +#[test] +fn test_no_range_end() { + insta::assert_snapshot!( + format_range( + r###"local z = 2 +local e = 5 +"###, + Range::from_values(Some(20), None) + ), + @r###" + + local z = 2 + local e = 5 + "###); +} diff --git a/tests/tests.rs b/tests/tests.rs index 7f2baf96..1d951953 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -48,3 +48,11 @@ fn test_lua52() { insta::assert_snapshot!(format(&contents)); }) } + +#[test] +fn test_ignores() { + insta::glob!("inputs-ignore/*.lua", |path| { + let contents = std::fs::read_to_string(path).unwrap(); + insta::assert_snapshot!(format(&contents)); + }) +}