Repository navigation
fix(cli): end raw literals and line comments where BigQuery does #2501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,23 @@ | ||
| /** | ||
| * Whether the string or bytes literal opening quote (`'` or `"`) at `quoteIndex` is preceded by a | ||
| * GoogleSQL raw literal prefix (`r`, `rb` or `br`, case-insensitive). | ||
| */ | ||
| function hasRawLiteralPrefix(statement: string, quoteIndex: number): boolean { | ||
| const prefix = statement.slice(Math.max(0, quoteIndex - 3), quoteIndex); | ||
| return /(?:^|[^a-zA-Z0-9_])(?:r|rb|br)$/i.test(prefix); | ||
| } | ||
|
|
||
| /** | ||
| * Returns `statement` with comments (`--`, `#` and `/* *\/`) and the contents of string/bytes | ||
| * literals (single, double and triple quoted, including raw literals) and quoted identifiers | ||
| * replaced with spaces of the same length. Preserving lengths keeps character indices aligned | ||
| * with `statement`, while leaving only unquoted SQL tokens and empty quotes. | ||
| * with `statement`, while leaving only unquoted SQL tokens and empty quotes. An unterminated | ||
| * literal runs to the end of the statement, and its last character is replaced with its quote | ||
| * character rather than a space, so that it still ends in code. | ||
| * | ||
| * Exported only for unit testing; not intended for use outside this file. | ||
| */ | ||
| function stripCommentsAndQuotedContent(statement: string): string { | ||
| export function stripCommentsAndQuotedContent(statement: string): string { | ||
| let result = ""; | ||
| let i = 0; | ||
| while (i < statement.length) { | ||
| const char = statement[i]; | ||
| const next = statement[i + 1]; | ||
| if ((char === "-" && next === "-") || char === "#") { | ||
| const newline = statement.indexOf("\n", i); | ||
| const end = newline === -1 ? statement.length : newline; | ||
| // BigQuery also ends a line comment at a lone \r. | ||
| const newline = statement.slice(i).search(/[\r\n]/); | ||
| const end = newline === -1 ? statement.length : i + newline; | ||
| result += " ".repeat(end - i); | ||
| i = end; | ||
| } else if (char === "/" && next === "*") { | ||
|
|
@@ -30,20 +26,26 @@ function stripCommentsAndQuotedContent(statement: string): string { | |
| result += " ".repeat(end - i); | ||
| i = end; | ||
| } else if (char === "'" || char === '"' || char === "`") { | ||
| const isRaw = char !== "`" && hasRawLiteralPrefix(statement, i); | ||
| const tripleQuote = char.repeat(3); | ||
| const closing = char !== "`" && statement.startsWith(tripleQuote, i) ? tripleQuote : char; | ||
| const start = i; | ||
| i += closing.length; | ||
| while (i < statement.length && !statement.startsWith(closing, i)) { | ||
| // Skip escaped characters, e.g. \' inside a non-raw single-quoted string. | ||
| i += !isRaw && statement[i] === "\\" ? 2 : 1; | ||
| // A backslash keeps the next character from closing the literal, e.g. \' in 'it\'s'. This | ||
| // also applies to raw literals, where the backslash is kept: r'a\'' is the string a\'. | ||
| i += statement[i] === "\\" ? 2 : 1; | ||
| } | ||
| if (i < statement.length) { | ||
| i += closing.length; | ||
| result += closing + " ".repeat(i - start - 2 * closing.length) + closing; | ||
| } else { | ||
| // An unterminated literal runs to the end of the statement. It's replaced up to there, and | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (optional): the function's doc comment still says literal contents are replaced with spaces. With this branch an unterminated literal ends with a quote character instead. Now that the function is exported, it would help to say that in the doc comment as well, not only here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in ece27fe. The doc comment now says that an unterminated literal runs to the end of the statement, and that its last character is replaced with its quote character rather than a space, so it still ends in code. |
||
| // ends with a quote rather than a space, so that it still counts as code (e.g. a statement | ||
| // terminator goes after it, not inside it). | ||
| const bodyLength = statement.length - start - closing.length; | ||
| i = statement.length; | ||
| result += closing + (bodyLength > 0 ? " ".repeat(bodyLength - 1) + char : ""); | ||
| } | ||
| i = Math.min(i + closing.length, statement.length); | ||
| result += | ||
| closing + | ||
| " ".repeat(Math.max(0, i - start - 2 * closing.length)) + | ||
| (i - start >= 2 * closing.length ? closing : ""); | ||
| } else { | ||
| result += char; | ||
| i++; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit (optional): since this is exported only so it can be unit-tested, a short note in the doc comment saying so would discourage other callers from depending on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in ece27fe. I added a note to the doc comment that it's exported only for unit testing.