nene / prettier-plugin-sql-cst

Prettier SQL plugin that uses sql-parser-cst
https://nene.github.io/prettier-sql-playground/
GNU General Public License v3.0
127 stars 7 forks source link

Feature Request: Ignore and warn unrecognized syntax and continue formatting the text #37

Open Silver-Fang opened 1 day ago

Silver-Fang commented 1 day ago

Describe the Feature

Provides an extension option to ignore unrecognized syntax instead of reporting an error.

Why do you want this feature?

I know that there are many common PROCEDURE syntax that the extension will not support in a short time. But can we ignore and warn these unsupported syntax for a while, instead of just reporting an error and giving up formatting the whole text?

In any case, you are a code formatter, not a syntax checker. As far as I know, the general practice in the field of code formatters is that you should not require the code to be completely well-formed, but to format those well-formed parts as much as possible.

I think you can just skip and warn when you encounter syntax that you don't recognize and start over from the next semicolon. Would this logic be difficult to implement?

nene commented 1 day ago

Unfortunately this would be pretty difficult to implement. It's not so much a problem with the prettier plugin, but rather with the parser. Also, while this might not be common behavior of many formatters, it's how the core Prettier itself works. When you give it syntactically incorrect JavaScript, it will fail to format it.

Silver-Fang commented 1 day ago

But the right to determine whether the syntax is correct is in your hands, isn't it?

Error: Syntax Error: Unexpected "PROCEDURE"
Was expecting to see: "EXTERNAL", "FOREIGN", "FULLTEXT", "GLOBAL", "INDEX", "LOCAL", "MATERIALIZED", "RECURSIVE", "SEARCH", "SNAPSHOT", "SPATIAL", "TABLE", "TEMP", "TEMPORARY", "TRIGGER", "UNIQUE", "UNLOGGED", "VECTOR", "VIEW", "VIRTUAL", or whitespace
--> c:\Users\vhtmf\Documents\MATLAB\MATLAB-Extension\+MATLAB\+Database\安装存储过程\_MATLAB_反序列化.sql:2:12
  |
2 | OR REPLACE PROCEDURE `_MATLAB_反序列化` (

From the log I can see that you listed some acceptable keywords, which did not include the actual keyword encountered so the error. I wonder if there should be an inner logical structure like CASE-WHEN? Then you can just add an ELSE branch to handle all the cases that are not in the list: no processing, output as-is. Don't let Prettier know there may be syntax errors. An alternative workaround is prettier-ignore. Prettier appears to have provided a way of using special comments to mark pieces of code that should not be processed. However, your plugin doesn't seem to support it yet.

nene commented 1 day ago

But the right to determine whether the syntax is correct is in your hands, isn't it?

Correct. I also maintain the parser.

Regarding the specific error. It would be helpful if you provided the actual code that's causing you problems. Also, please state which SQL dialect you're using - it's hard for me to guess. The CREATE PROCEDURE syntax is supported for PostgreSQL and BigQuery.

An alternative workaround is prettier-ignore. Prettier appears to have provided a way of using special comments to mark pieces of code that should not be processed. However, your plugin doesn't seem to support it yet.

While it's true that this plugin doesn't yet support these ignore comments, I should point out that even in official Prettier, these comments only work when the parser is able to fully parse the code.

Silver-Fang commented 1 day ago

I want to CREATE PROCEDURE for MariaDB. But as I understand it, from what you're saying, plugins of Prettier family are either fully applicable or not applicable at all, impossible to be in a state of partially applicable for a while… Long code with just one unsupported keyword disables the entire formatter… extremely unrobust…

nene commented 21 hours ago

Yes, that's the downside of this architecture. That's why the support of MariaDB is also labeled "Experimental! expect crashes".

There are other SQL formatting tools out there which don't have this limitation.