From 2e0001cb224010728bca91e6cfe529163bbf3393 Mon Sep 17 00:00:00 2001 From: Ian Cook Date: Fri, 6 Sep 2019 10:45:13 -0400 Subject: [PATCH] Fix bug in squishing whitespace around comments (#14) --- R/squish_sql.R | 2 ++ tests/testthat/test-squish_sql.R | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/R/squish_sql.R b/R/squish_sql.R index 4e96bbe..e9e38a0 100644 --- a/R/squish_sql.R +++ b/R/squish_sql.R @@ -90,12 +90,14 @@ squish_sql <- function(x) { in_line_comment <- TRUE next; } + in_ws <- FALSE seek(rc_in, -1L, "current") } else if (identical(char, "/")) { if (identical(readChar(rc_in, 1L), "*")) { in_block_comment <- TRUE next; } + in_ws <- FALSE seek(rc_in, -1L, "current") } else if (isTRUE(is_whitespace_character(char))) { # this is a whitespace character diff --git a/tests/testthat/test-squish_sql.R b/tests/testthat/test-squish_sql.R index 16e2d3b..ececcb6 100644 --- a/tests/testthat/test-squish_sql.R +++ b/tests/testthat/test-squish_sql.R @@ -14,14 +14,14 @@ test_that("squish_sql() removes tabs and newlines from an expression", { test_that("squish_sql() removes line comments from a query", { expect_equal( - squish_sql("\nSELECT x - y--x\n\tFROM y--y' ' SELECT\r\n\tWHERE z\n\t WHERE w --???w\n"), - " SELECT x FROM y WHERE z WHERE w " + squish_sql("\nSELECT x - y --x \n\t FROM y--y' ' SELECT\r\n\tWHERE z--q\n\t WHERE w --???w\n"), + " SELECT x - y FROM y WHERE z WHERE w " ) }) test_that("squish_sql() removes block comments from a query", { expect_equal( - squish_sql("\nSELECT /*SELECT ' ' */ x\n\tFROM y\r\n\tWHERE z\n\t WHERE w\n"), + squish_sql("\nSELECT /*SELECT ' ' */ x\n\tFROM/*)*/ y\r\n\tWHERE /*abc(((*/z\n\t WHERE w\n"), " SELECT x FROM y WHERE z WHERE w " ) }) @@ -29,14 +29,14 @@ test_that("squish_sql() removes block comments from a query", { test_that("squish_sql() removes line comments from an expression", { expect_equal( squish_sql("\nx / 2 BETWEEN --a\ny AND\r\n\tz\n -- b"), - " x BETWEEN y AND z " + " x / 2 BETWEEN y AND z " ) }) test_that("squish_sql() removes block comments from an expression", { expect_equal( squish_sql("\nx*3 BETWEEN /*a AND x * 2 / 3 b*/ \ny AND /*' ' ! \"*/\r\n\tz\n"), - " x BETWEEN y AND z " + " x*3 BETWEEN y AND z " ) })