Hi @Sec-ant , hope you're well! ๐
(very similar to #34, maybe the same issue)
sql-formatter
crashes with prettier-plugin-embed
, embedded SQL in JavaScript/TypeScript template string and prettier-plugin-sql
, when a cast is used:
a.sql
a.js
function up(sql) {
await sql`SELECT '[]'::jsonb;`;
}
$ pnpm prettier a.sql --write
a.sql 58ms
$ pnpm prettier a.js --write
a.js
Error: Parse error: Unexpected "::jsonb;" at line 1 column 12
at TokenizerEngine.createParseError (file:///Users/k/p/project/node_modules/sql-formatter/lib/lexer/TokenizerEngine.js:53:12)
at TokenizerEngine.tokenize (file:///Users/k/p/project/node_modules/sql-formatter/lib/lexer/TokenizerEngine.js:35:22)
at Tokenizer.tokenize (file:///Users/k/p/project/node_modules/sql-formatter/lib/lexer/Tokenizer.js:16:47)
at LexerAdapter.tokenize (file:///Users/k/p/project/node_modules/sql-formatter/lib/parser/createParser.js:16:76)
at LexerAdapter.reset (file:///Users/k/p/project/node_modules/sql-formatter/lib/parser/LexerAdapter.js:17:24)
at Parser.feed (/Users/k/p/project/node_modules/nearley/lib/nearley.js:281:15)
at Object.parse (file:///Users/k/p/project/node_modules/sql-formatter/lib/parser/createParser.js:26:18)
at Formatter.parse (file:///Users/k/p/project/node_modules/sql-formatter/lib/formatter/Formatter.js:32:49)
at Formatter.format (file:///Users/k/p/project/node_modules/sql-formatter/lib/formatter/Formatter.js:25:22)
at formatDialect (file:///Users/k/p/project/node_modules/sql-formatter/lib/sqlFormatter.js:78:57)
a.js 40ms (unchanged)
Other casts that also fail:
SELECT '[]'::jsonb->0;
SELECT ARRAY['5', '6']::int[];
Tildes
This also crashes similarly on the ~
(tilde) operator:
SELECT 'ABC' ~ '^[A-Z]{3}$';
Error:
Error: Parse error: Unexpected "~ '^ABC$';" at line 1 column 14
Square brackets for PostgreSQL exact length arrays
Another failing pattern:
CREATE TABLE x(
y integer[5]
)
Error: Parse error: Unexpected "[5]
)" at line 2 column 18
$$
delimiter
At first, from the error messages, I thought it was the lack of support for casts or tilde operators or square brackets in sql-formatter
, but they are all supported with prettier-plugin-sql
when used directly with .sql
files.
My versions and config, maybe I've configured something incorrectly? ๐ค
package.json
"prettier": "3.1.0",
"prettier-plugin-embed": "0.2.5",
"prettier-plugin-sql": "0.16.0",
prettier.config.mjs
/** @type {import('prettier').Config} */
const prettierConfig = {
plugins: [
'prettier-plugin-tailwindcss',
'prettier-plugin-embed',
'prettier-plugin-sql',
],
// Avoid excessive diffs on changes to MDX files
proseWrap: 'never',
singleQuote: true,
trailingComma: 'all',
};
/** @type {import('prettier-plugin-embed').PrettierPluginEmbedOptions} */
const prettierPluginEmbedConfig = {
embeddedSqlIdentifiers: ['sql'],
};
/** @type {import('prettier-plugin-sql').SqlBaseOptions} */
const prettierPluginSqlConfig = {
language: 'postgresql',
keywordCase: 'upper',
// - Wrap all parenthesized expressions to new lines (eg. `INSERT` columns)
// - Do not wrap foreign keys (eg. `REFERENCES table_name (id)`)
// - Do not wrap column type expressions (eg. `VARCHAR(255)`)
expressionWidth: 8,
};
const config = {
...prettierConfig,
...prettierPluginEmbedConfig,
...prettierPluginSqlConfig,
};
export default config;
cc @JounQin