Skip to content

Backslash causing issues #16

@OutGame1

Description

@OutGame1

Hello, I encountered a problem with backslashes. I don't know if this is the expected output, but when using a backslash in a string, it somehow consider that the string is not closed.

Expected behavior

import minify from "pg-minify";
const backslash = '\\';
console.log(minify(`SELECT * FROM USER WHERE NAME = '${backslash}'`, {compress: true}));
> "SELECT * FROM USER WHERE NAME='\'"

Actual behavior

SQLParsingError: Error parsing SQL at {line:1,col:33}: Unclosed text block.
    at throwError (...\node_modules\pg-minify\lib\parser.js:186:15)
    at minify (...\node_modules\pg-minify\lib\parser.js:124:17)
    at <anonymous> (...\src\index.ts:214:13)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12) {
  error: 'Unclosed text block.',
  code: 1,
  position: { line: 1, column: 33 }
}

When I replace all the backslashes with a token, then replaces them back, it works :

import minify from "pg-minify";
const bsToken = "__BS__";
const backslash = '\\';
const sql = `SELECT * FROM USER WHERE NAME = '${backslash}'`;
const replaced = sql.replaceAll(backslash, bsToken);
const minified = minify(replaced, {compress: true});
console.log(minified.replaceAll(bsToken, backslash));
> "SELECT * FROM USER WHERE NAME='\'"

Environment

  • Version of pg-minify: 1.7.0
  • OS type (Linux/Windows/Mac): Windows
  • Version of Node.js: 20.10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions