Skip to content

Commit 7183c63

Browse files
authored
Fixes formatter capitalizing preparser keywords in procedure/function namespaces (#577)
1 parent 9b62ba8 commit 7183c63

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

.changeset/thick-signs-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@neo4j-cypher/language-support': patch
3+
---
4+
5+
Fixes a bug in formatting when using preparser keywords in function/procedure namespaces

packages/language-support/src/formatting/formattingHelpers.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { CharStreams, CommonTokenStream, TerminalNode, Token } from 'antlr4';
22
import { default as CypherCmdLexer } from '../generated-parser/CypherCmdLexer';
33
import CypherCmdParser, {
44
EscapedSymbolicNameStringContext,
5-
UnescapedSymbolicNameString_Context,
5+
UnescapedSymbolicNameStringContext,
66
} from '../generated-parser/CypherCmdParser';
77
import { lexerKeywords } from '../lexerSymbols';
8+
import { findParent } from '../helpers';
89

910
export const INTERNAL_FORMAT_ERROR_MESSAGE = `
1011
Internal formatting error: An unexpected issue occurred while formatting.
@@ -103,12 +104,20 @@ export function isComment(token: Token) {
103104
export const isInlineComment = (chunk: Chunk) =>
104105
chunk.comment && chunk.comment.startsWith('/*');
105106

106-
// Variables or property names that have the same name as a keyword should not be
107+
// Variables, functions/procedures namespaces or property names that have the same name as a keyword should not be
107108
// treated as keywords
108109
function isSymbolicName(node: TerminalNode): boolean {
109-
return (
110-
node.parentCtx instanceof UnescapedSymbolicNameString_Context ||
111-
node.parentCtx instanceof EscapedSymbolicNameStringContext
110+
const unescapedSymbolicNameStringParent = findParent(
111+
node,
112+
(x) => x instanceof UnescapedSymbolicNameStringContext,
113+
);
114+
const escapedSymbolicNameStringParent = findParent(
115+
node,
116+
(x) => x instanceof EscapedSymbolicNameStringContext,
117+
);
118+
return !(
119+
unescapedSymbolicNameStringParent == null &&
120+
escapedSymbolicNameStringParent == null
112121
);
113122
}
114123

packages/language-support/src/tests/formatting/edgecases.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,4 +1158,11 @@ RETURN
11581158
}[1..10]`;
11591159
verifyFormatting(query, expected);
11601160
});
1161+
1162+
test('Preparser keywords do not break in procedures', () => {
1163+
const query = `cypher CALL apoc.cypher.run("RETURN 5", {})`;
1164+
const expected = `CYPHER
1165+
CALL apoc.cypher.run("RETURN 5", {})`;
1166+
verifyFormatting(query, expected);
1167+
});
11611168
});

0 commit comments

Comments
 (0)