Skip to content

Commit b8d5397

Browse files
authored
refactor: friendlier to read diagnostic codes (#1497)
1 parent e07da1d commit b8d5397

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

packages/graphql-language-service-interface/src/GraphQLLanguageService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
import { Kind, parse, print } from 'graphql';
3838
import { getAutocompleteSuggestions } from './getAutocompleteSuggestions';
3939
import { getHoverInformation } from './getHoverInformation';
40-
import { validateQuery, getRange, SEVERITY } from './getDiagnostics';
40+
import { validateQuery, getRange, DIAGNOSTIC_SEVERITY } from './getDiagnostics';
4141
import {
4242
getDefinitionQueryResultForFragmentSpread,
4343
getDefinitionQueryResultForDefinitionNode,
@@ -155,7 +155,7 @@ export class GraphQLLanguageService {
155155
const range = getRange(error.locations[0], query);
156156
return [
157157
{
158-
severity: SEVERITY.ERROR,
158+
severity: DIAGNOSTIC_SEVERITY.Error,
159159
message: error.message,
160160
source: 'GraphQL: Syntax',
161161
range,

packages/graphql-language-service-interface/src/__tests__/getDiagnostics-test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import fs from 'fs';
1212
import { buildSchema, parse, GraphQLSchema } from 'graphql';
1313
import path from 'path';
1414

15-
import { getDiagnostics, validateQuery, SEVERITY } from '../getDiagnostics';
15+
import {
16+
getDiagnostics,
17+
validateQuery,
18+
DIAGNOSTIC_SEVERITY,
19+
} from '../getDiagnostics';
1620

1721
describe('getDiagnostics', () => {
1822
let schema: GraphQLSchema;
@@ -30,7 +34,7 @@ describe('getDiagnostics', () => {
3034
expect(error.message).toEqual(
3135
'Cannot query field "title" on type "Query".',
3236
);
33-
expect(error.severity).toEqual(SEVERITY.ERROR);
37+
expect(error.severity).toEqual(DIAGNOSTIC_SEVERITY.Error);
3438
expect(error.source).toEqual('GraphQL: Validation');
3539
});
3640

@@ -43,7 +47,7 @@ describe('getDiagnostics', () => {
4347
// eslint-disable-next-line no-useless-escape
4448
'The field "Query.deprecatedField" is deprecated. Use test instead.',
4549
);
46-
expect(error.severity).toEqual(SEVERITY.WARNING);
50+
expect(error.severity).toEqual(DIAGNOSTIC_SEVERITY.Warning);
4751
expect(error.source).toEqual('GraphQL: Deprecation');
4852
});
4953

@@ -76,7 +80,7 @@ describe('getDiagnostics', () => {
7680
// eslint-disable-next-line no-useless-escape
7781
'Syntax Error: Expected ":", found Name "id".',
7882
);
79-
expect(error.severity).toEqual(SEVERITY.ERROR);
83+
expect(error.severity).toEqual(DIAGNOSTIC_SEVERITY.Error);
8084
expect(error.source).toEqual('GraphQL: Syntax');
8185
});
8286

packages/graphql-language-service-interface/src/getDiagnostics.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,24 @@ import {
3030

3131
import { DiagnosticSeverity, Diagnostic } from 'vscode-languageserver-types';
3232

33+
// this doesn't work without the 'as', kinda goofy
34+
3335
export const SEVERITY = {
34-
ERROR: 1 as DiagnosticSeverity,
35-
WARNING: 2 as DiagnosticSeverity,
36-
INFORMATION: 3 as DiagnosticSeverity,
37-
HINT: 4 as DiagnosticSeverity,
36+
Error: 'Error' as 'Error',
37+
Warning: 'Warning' as 'Warning',
38+
Information: 'Information' as 'Information',
39+
Hint: 'Hint' as 'Hint',
40+
};
41+
42+
export type Severity = typeof SEVERITY;
43+
44+
export type SeverityEnum = keyof Severity;
45+
46+
export const DIAGNOSTIC_SEVERITY = {
47+
[SEVERITY.Error]: 1 as DiagnosticSeverity,
48+
[SEVERITY.Warning]: 2 as DiagnosticSeverity,
49+
[SEVERITY.Information]: 3 as DiagnosticSeverity,
50+
[SEVERITY.Hint]: 4 as DiagnosticSeverity,
3851
};
3952

4053
export function getDiagnostics(
@@ -50,7 +63,7 @@ export function getDiagnostics(
5063
const range = getRange(error.locations[0], query);
5164
return [
5265
{
53-
severity: SEVERITY.ERROR as DiagnosticSeverity,
66+
severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity,
5467
message: error.message,
5568
source: 'GraphQL: Syntax',
5669
range,
@@ -74,15 +87,15 @@ export function validateQuery(
7487

7588
const validationErrorAnnotations = mapCat(
7689
validateWithCustomRules(schema, ast, customRules, isRelayCompatMode),
77-
error => annotations(error, SEVERITY.ERROR, 'Validation'),
90+
error => annotations(error, DIAGNOSTIC_SEVERITY.Error, 'Validation'),
7891
);
7992

8093
// Note: findDeprecatedUsages was added in [email protected], but we want to
8194
// support older versions of graphql-js.
8295
const deprecationWarningAnnotations = !findDeprecatedUsages
8396
? []
8497
: mapCat(findDeprecatedUsages(schema, ast), error =>
85-
annotations(error, SEVERITY.WARNING, 'Deprecation'),
98+
annotations(error, DIAGNOSTIC_SEVERITY.Warning, 'Deprecation'),
8699
);
87100

88101
return validationErrorAnnotations.concat(deprecationWarningAnnotations);

packages/graphql-language-service-interface/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export {
2828
getDefinitionQueryResultForDefinitionNode,
2929
} from './getDefinition';
3030

31-
export { getDiagnostics, validateQuery } from './getDiagnostics';
31+
export * from './getDiagnostics';
3232
export { getOutline } from './getOutline';
3333
export { getHoverInformation } from './getHoverInformation';
3434

packages/graphql-language-service-server/src/Logger.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import * as fs from 'fs';
1313
import * as os from 'os';
1414
import { join } from 'path';
1515

16-
const SEVERITY: { [key: string]: string } = {
17-
ERROR: 'ERROR',
18-
WARNING: 'WARNING',
19-
INFO: 'INFO',
20-
DEBUG: 'DEBUG',
21-
};
22-
16+
import {
17+
DIAGNOSTIC_SEVERITY,
18+
SeverityEnum,
19+
SEVERITY,
20+
} from 'graphql-language-service-interface';
2321
export class Logger implements VSCodeLogger {
2422
_logFilePath: string;
2523
_stream: fs.WriteStream | null;
@@ -46,30 +44,30 @@ export class Logger implements VSCodeLogger {
4644
}
4745

4846
error(message: string): void {
49-
this._log(message, 'ERROR');
47+
this._log(message, SEVERITY.Error);
5048
}
5149

5250
warn(message: string): void {
53-
this._log(message, 'WARNING');
51+
this._log(message, SEVERITY.Warning);
5452
}
5553

5654
info(message: string): void {
57-
this._log(message, 'INFO');
55+
this._log(message, SEVERITY.Information);
5856
}
5957

6058
log(message: string): void {
61-
this._log(message, 'DEBUG');
59+
this._log(message, SEVERITY.Hint);
6260
}
6361

64-
_log(message: string, severityKey: string = 'DEBUG'): void {
62+
_log(message: string, severityKey: SeverityEnum): void {
6563
const timestamp = new Date().toLocaleString(undefined);
66-
const severity = SEVERITY[severityKey];
64+
const severity = DIAGNOSTIC_SEVERITY[severityKey];
6765
const pid = process.pid;
6866

6967
const logMessage = `${timestamp} [${severity}] (pid: ${pid}) graphql-language-service-usage-logs: ${message}\n\n`;
7068
// write to the file in tmpdir
7169
fs.appendFile(this._logFilePath, logMessage, _error => {});
72-
// const processSt = (severity === SEVERITY.ERROR) ? process.stderr : process.stdout
70+
// const processSt = (severity === DIAGNOSTIC_SEVERITY.Error) ? process.stderr : process.stdout
7371
process.stderr.write(logMessage, _err => {
7472
// console.error(err);
7573
});

0 commit comments

Comments
 (0)