fix: support typographic quotes and zero-width spaces - #1
Draft
cnbei wants to merge 2 commits into
Draft
Conversation
Tokenize U+2018/U+2019 as string delimiters and treat U+200B as whitespace so copy-pasted Transact-SQL no longer fails to parse. Co-authored-by: cnbei <baymy@foxmail.com>
Co-authored-by: cnbei <baymy@foxmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes sql-formatter-org#942
This PR lives on the fork because the cloud-agent GitHub App token cannot open a pull request on
sql-formatter-org/sql-formatter(Resource not accessible by integration).Open against upstream (intended):
https://github.com/sql-formatter-org/sql-formatter/compare/master...cnbei:sql-formatter:cursor/unicode-quotes-transactsql-cf53?expand=1
Copy-pasted Transact-SQL from Word/Outlook/web UIs often wraps literals in typographic quotes and inserts zero-width spaces. The playground then throws:
Cause
The failing token is this 7-character sequence (confirmed from the issue input):
Two tokenizer gaps combine to produce the parse error:
WHITESPACE_REGEXis/\s+/uy. JavaScript\sincludes NBSP and BOM, but not U+200B, so the lexer does not skip the leading zero-width space.'. U+2018/U+2019 are not quote delimiters, so tokenization stops withUnexpected "'@p0' an".Fix
/\s+/uskip.‘…’) as a string token, using the samequotePatternsmechanism as''-qq,[],$$, etc. They are not interchangeable with ASCII', so a curly apostrophe inside'don't'still stays inside the string.The typographic-quote pattern is appended for every dialect in the shared STRING rule: this is a copy-paste artifact, not T-SQL syntax.
Tests
test/transactsql.test.ts: Issue Problem with unicode quotes in Transact-SQL sql-formatter-org/sql-formatter#942 sequence plus the original playground query.test/behavesLikeSqlFormatter.ts: ZWSP-as-whitespace, typographic strings, and the ASCII-string-with-curly-apostrophe guard (all dialects).test/unit/Tokenizer.test.ts: token-level coverage for both characters.pnpm exec jest --coverage=false: 27 suites, 5907 passed, 1 skipped.pnpm run ts:check, eslint, and prettier on the changed files are clean.