Skip to content

fix(pglt_statement_splitter): properly split for windows #216

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

Merged
merged 16 commits into from
Mar 10, 2025
6 changes: 3 additions & 3 deletions .github/workflows/publish.dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
return;
}

if (releases.length < 100) {
if (releases.length < 100) {
exhausted = true;
} else if (page >= 10) {
throw new Error("We iterated over 10 pages. Does the script work?");
Expand All @@ -55,8 +55,8 @@ jobs:
- name: Abort
if: steps.validate-release.outputs.has-release != 'true'
run: |
{
echo "Tag ${{ github.event.inputs.release_tag }} not found."
{
echo "Tag ${{ github.event.inputs.release-tag }} not found."
exit 1
}

Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/publish.reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:
is-prerelease:
type: string
required: true
default: "false"

jobs:
publish:
Expand Down Expand Up @@ -52,17 +51,17 @@ jobs:
- name: Publish npm packages as nightly
if: inputs.is-prerelease == 'true'
run: |
for package in packages/@pglt/*; do
npm publish $package --tag nightly --access public --provenance
for package in packages/@pglt/*; do
npm publish "$package" --tag nightly --access public --provenance
done
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} #

- name: Publish npm packages as latest
if: inputs.is-prerelease != 'true'
run: |
for package in packages/@pglt/*; do
npm publish $package --tag latest --access public --provenance
for package in packages/@pglt/*; do
npm publish "$package" --tag latest --access public --provenance
done
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
14 changes: 12 additions & 2 deletions crates/pglt_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,18 @@ pub static WHITESPACE_TOKENS: &[SyntaxKind] = &[
SyntaxKind::SqlComment,
];

static PATTERN_LEXER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?P<whitespace> +)|(?P<newline>\r?\n+)|(?P<tab>\t+)").unwrap());
static PATTERN_LEXER: LazyLock<Regex> = LazyLock::new(|| {
#[cfg(windows)]
{
// On Windows, treat \r\n as a single newline token
Regex::new(r"(?P<whitespace> +)|(?P<newline>\r\n|\n+)|(?P<tab>\t+)").unwrap()
}
#[cfg(not(windows))]
{
// On other platforms, just check for \n
Regex::new(r"(?P<whitespace> +)|(?P<newline>\n+)|(?P<tab>\t+)").unwrap()
}
});

fn whitespace_tokens(input: &str) -> VecDeque<Token> {
let mut tokens = VecDeque::new();
Expand Down
6 changes: 6 additions & 0 deletions crates/pglt_statement_splitter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ mod tests {
]);
}

#[test]
fn single_newlines() {
Tester::from("select 1\nfrom contact\n\nselect 3")
.expect_statements(vec!["select 1\nfrom contact", "select 3"]);
}

#[test]
fn alter_column() {
Tester::from("alter table users alter column email drop not null;")
Expand Down
11 changes: 11 additions & 0 deletions crates/pglt_statement_splitter/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ impl Parser {
}
}

#[cfg(windows)]
/// Returns true if the token is relevant for the paring process
///
/// On windows, a newline is represented by `\r\n` which is two characters.
fn is_irrelevant_token(t: &Token) -> bool {
WHITESPACE_TOKENS.contains(&t.kind)
&& (t.kind != SyntaxKind::Newline || t.text == "\r\n" || t.text.chars().count() == 1)
}

#[cfg(not(windows))]
/// Returns true if the token is relevant for the paring process
fn is_irrelevant_token(t: &Token) -> bool {
WHITESPACE_TOKENS.contains(&t.kind)
&& (t.kind != SyntaxKind::Newline || t.text.chars().count() == 1)
Expand Down
13 changes: 13 additions & 0 deletions crates/pglt_statement_splitter/tests/data/with_comments__4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- test
select id, name, test1231234123, unknown from co;

-- in between two statements

select 14433313331333 -- after a statement

alter table --within a statement
test drop column id;

select lower('test');
--after a statement