Skip to content

Commit be38e8c

Browse files
committed
Makes it compile
1 parent 52aec45 commit be38e8c

File tree

6 files changed

+55
-47
lines changed

6 files changed

+55
-47
lines changed

packages/language-server/src/linting.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
findEndPosition,
3-
parserWrapper,
4-
validateSyntax,
5-
} from '@neo4j-cypher/language-support';
1+
import { validateSyntax } from '@neo4j-cypher/language-support';
62
import debounce from 'lodash.debounce';
73
import { join } from 'path';
84
import { Diagnostic, TextDocumentChangeEvent } from 'vscode-languageserver';
@@ -42,9 +38,7 @@ async function rawLintDocument(
4238
lastSemanticJob = proxyWorker.validateSemantics(query);
4339
const result = await lastSemanticJob;
4440

45-
sendDiagnostics(
46-
result.map((el) => findEndPosition(el, parserWrapper.parsingResult)),
47-
);
41+
sendDiagnostics(result);
4842
} catch (err) {
4943
if (!(err instanceof workerpool.Promise.CancellationError)) {
5044
console.error(err);

packages/language-support/src/highlighting/syntaxValidation/syntaxValidation.ts

+33-26
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function warnOnUndeclaredLabels(
8282
return warnings;
8383
}
8484

85-
export function findEndPosition(
85+
function findEndPosition(
8686
e: SemanticAnalysisElement,
8787
parsingResult: StatementParsing,
8888
): SyntaxDiagnostic {
@@ -147,49 +147,56 @@ export function sortByPosition(a: SyntaxDiagnostic, b: SyntaxDiagnostic) {
147147

148148
// TODO Does this need to be exported
149149
export function lintCypherQuery(
150-
wholeFileText: string,
150+
query: string,
151151
dbSchema: DbSchema,
152152
): SyntaxDiagnostic[] {
153-
const cachedParse = parserWrapper.parse(wholeFileText);
154-
const statements = cachedParse.statementsParsing;
155-
156-
const result = statements.flatMap((statementParsing) => {
157-
const syntaxErrors = validateSyntax(statementParsing, dbSchema);
158-
159-
if (syntaxErrors.length > 0) {
160-
return syntaxErrors;
161-
}
162-
163-
// TODO
164-
return validateSemantics(statementParsing.statement)
165-
.map((el) => findEndPosition(el, statementParsing))
166-
.sort(sortByPosition);
167-
});
153+
const syntaxErrors = validateSyntax(query, dbSchema);
154+
if (syntaxErrors.length > 0) {
155+
return syntaxErrors;
156+
}
168157

169-
return result;
158+
const semanticErrors = validateSemantics(query);
159+
return semanticErrors;
170160
}
171161

172162
// TODO Does this need to be exported
173163
export function validateSyntax(
174-
statementParsing: StatementParsing,
164+
query: string,
175165
dbSchema: DbSchema,
176166
): SyntaxDiagnostic[] {
177-
if (statementParsing.statement.length === 0) {
167+
if (query.length === 0) {
178168
return [];
179169
}
180-
const diagnostics = statementParsing.diagnostics;
181-
const labelWarnings = warnOnUndeclaredLabels(statementParsing, dbSchema);
182-
return diagnostics.concat(labelWarnings).sort(sortByPosition);
170+
const statements = parserWrapper.parse(query);
171+
const result = statements.statementsParsing.flatMap((statement) => {
172+
const diagnostics = statement.diagnostics;
173+
const labelWarnings = warnOnUndeclaredLabels(statement, dbSchema);
174+
return diagnostics.concat(labelWarnings).sort(sortByPosition);
175+
});
176+
177+
return result;
183178
}
184179

185180
/**
186181
* Assumes the provided query has no parse errors
187182
*/
188-
export function validateSemantics(query: string): SemanticAnalysisElement[] {
183+
export function validateSemantics(query: string): SyntaxDiagnostic[] {
189184
if (query.length > 0) {
190-
const { notifications, errors } = wrappedSemanticAnalysis(query);
185+
const cachedParse = parserWrapper.parse(query);
186+
const statements = cachedParse.statementsParsing;
187+
const semanticErrors = statements.flatMap((current) => {
188+
const { notifications, errors } = wrappedSemanticAnalysis(
189+
current.statement,
190+
);
191+
192+
const elements = notifications.concat(errors);
193+
const result = elements
194+
.map((el) => findEndPosition(el, current))
195+
.sort(sortByPosition);
196+
return result;
197+
});
191198

192-
return notifications.concat(errors);
199+
return semanticErrors;
193200
}
194201

195202
return [];

packages/language-support/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export {
99
} from './highlighting/syntaxColouring/syntaxColouring';
1010
export type { ParsedCypherToken } from './highlighting/syntaxColouring/syntaxColouringHelpers';
1111
export {
12-
findEndPosition,
1312
lintCypherQuery,
1413
validateSemantics,
1514
validateSyntax,

packages/react-codemirror-playground/src/tree-util.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ interface SimpleTree {
66
import {
77
antlrUtils,
88
CypherParser,
9-
parse,
109
ParserRuleContext,
10+
parserWrapper,
1111
} from '@neo4j-cypher/language-support';
1212

1313
export function getDebugTree(cypher: string): SimpleTree {
14-
const statements = parse(cypher);
14+
const statements = parserWrapper.parse(cypher).statementsParsing;
15+
1516
function walk(node: ParserRuleContext): SimpleTree {
1617
const name = antlrUtils.tree.Trees.getNodeText(
1718
node,
@@ -25,5 +26,11 @@ export function getDebugTree(cypher: string): SimpleTree {
2526
};
2627
}
2728

28-
return walk(statements);
29+
const children = statements.map((statement) => walk(statement.ctx));
30+
31+
return {
32+
// TODO Is this correct?
33+
name: 'root',
34+
children: children,
35+
};
2936
}

packages/react-codemirror/src/e2e_tests/syntax-validation.spec.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test('Prop lint set to false disables syntax validation', async ({
1616
});
1717
});
1818

19+
// TODO Should this be unskipped
1920
test.skip('Can turn linting back on', async ({ page, mount }) => {
2021
const editorPage = new CypherEditorPage(page);
2122
const query = 'METCH (n) RETURN n';

packages/react-codemirror/src/lang-cypher/syntax-validation.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { Diagnostic, linter } from '@codemirror/lint';
22
import { Extension } from '@codemirror/state';
3-
import {
4-
findEndPosition,
5-
parserWrapper,
6-
validateSyntax,
7-
} from '@neo4j-cypher/language-support';
3+
import { parserWrapper, validateSyntax } from '@neo4j-cypher/language-support';
84
import { DiagnosticSeverity } from 'vscode-languageserver-types';
95
import workerpool from 'workerpool';
106
import type { CypherConfig } from './lang-cypher';
@@ -59,7 +55,13 @@ export const semanticAnalysisLinter: (config: CypherConfig) => Extension = (
5955

6056
// we want to avoid the ANTLR4 reparse in the worker thread, this should hit our main thread cache
6157
const parse = parserWrapper.parse(query);
62-
if (parse.diagnostics.length !== 0) {
58+
const statements = parse.statementsParsing;
59+
60+
const anySyntacticError =
61+
statements.filter((statement) => statement.diagnostics.length !== 0)
62+
.length > 0;
63+
64+
if (anySyntacticError) {
6365
return [];
6466
}
6567

@@ -72,9 +74,7 @@ export const semanticAnalysisLinter: (config: CypherConfig) => Extension = (
7274
lastSemanticJob = proxyWorker.validateSemantics(query);
7375
const result = await lastSemanticJob;
7476

75-
return result.map((el) => {
76-
const diagnostic = findEndPosition(el, parse);
77-
77+
return result.map((diagnostic) => {
7878
return {
7979
from: diagnostic.offsets.start,
8080
to: diagnostic.offsets.end,

0 commit comments

Comments
 (0)