Open ChenZhouUC opened 9 months ago
As a workaround. If you really want to simply get this feature back as it was, you can take the old implementation (formatCommaPositions.ts) and run the formatted code through it. Something like:
import { format } from "sql-formatter";
import { formatCommaPositions } from "./formatCommaPositions";
const sql = format("SELECT foo, bar, baz FROM tbl", { language: "sqlite" });
console.log(formatCommaPositions(sql, "before", " "));
You should be aware that this whole implementation is one big hack and will easily break. e.g. it will break for BigQuery which allows a trailing comma in SELECT (though this style won't really make much sense in case of BigQuery anyway). And it will break when it encounters comments in unexpected places. And likely many more scenarios...
The implementation also conflicts with how the same feature is implemented in SQLFluff. Namely, SQLFluff expects leading commas to be indented like so (keeping the actual indentation of each line):
SELECT
foo
, bar
, baz
FROM
tbl
But this implementation unindents each line with a comma by two spaces:
SELECT
foo
, bar
, baz
FROM
tbl
Which is IMHO pretty awkward and also prevents it from working when TABs are used for indentation.
I currently don't have any plans to re-implement this feature. I'm open to pull requests if anybody is interested in creating this feature properly.
As a workaround. If you really want to simply get this feature back as it was, you can take the old implementation (formatCommaPositions.ts) and run the formatted code through it. Something like:
import { format } from "sql-formatter"; import { formatCommaPositions } from "./formatCommaPositions"; const sql = format("SELECT foo, bar, baz FROM tbl", { language: "sqlite" }); console.log(formatCommaPositions(sql, "before", " "));
Issues
You should be aware that this whole implementation is one big hack and will easily break. e.g. it will break for BigQuery which allows a trailing comma in SELECT (though this style won't really make much sense in case of BigQuery anyway). And it will break when it encounters comments in unexpected places. And likely many more scenarios...
The implementation also conflicts with how the same feature is implemented in SQLFluff. Namely, SQLFluff expects leading commas to be indented like so (keeping the actual indentation of each line):
SELECT foo , bar , baz FROM tbl
But this implementation unindents each line with a comma by two spaces:
SELECT foo , bar , baz FROM tbl
Which is IMHO pretty awkward and also prevents it from working when TABs are used for indentation.
Future
I currently don't have any plans to re-implement this feature. I'm open to pull requests if anybody is interested in creating this feature properly.
Thank you for the workaround and clear explanation. Looking forward to the future plans.
When using the previous version of sql formatter, I found comma position configuration very useful in debugging. I wondered if this feature could be brought back sometime.