Skip to content

Commit

Permalink
Replace getLineCol and add end ranges for GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Apr 15, 2024
1 parent df30c36 commit a5271cd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
2 changes: 2 additions & 0 deletions packages/cli-utils/src/commands/check/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export function diagnosticMessageGithub(message: DiagnosticMessage): void {
file: message.file,
line: message.line,
col: message.col,
endLine: message.endLine,
endColumn: message.endColumn,
});
}

Expand Down
37 changes: 11 additions & 26 deletions packages/cli-utils/src/commands/check/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,14 @@ import * as path from 'node:path';
import { Project, ts } from 'ts-morph';

import type { GraphQLSPConfig } from '@gql.tada/internal';
import { load, resolveTypeScriptRootDir } from '@gql.tada/internal';
import { load } from '@gql.tada/internal';
import { init, getGraphQLDiagnostics } from '@0no-co/graphqlsp/api';

import { createPluginInfo } from '../../ts/project';
import { createPluginInfo, getFilePosition } from '../../ts';
import { expose } from '../../threads';

import type { Severity, DiagnosticMessage, DiagnosticSignal } from './types';

const getLineCol = (text: string, start: number | undefined): [number, number] => {
if (text && start) {
let counter = 0;
const parts = text.split('\n');
for (let i = 0; i <= parts.length; i++) {
const line = parts[i];
if (counter + line.length > start) {
return [i + 1, start + 1 - counter];
} else {
counter = counter + (line.length + 1);
continue;
}
}
}
return [0, 0];
};

const loadSchema = async (rootPath: string, config: GraphQLSPConfig) => {
const loader = load({ origin: config.schema, rootPath });
const result = await loader.load();
Expand Down Expand Up @@ -61,33 +44,35 @@ async function* _runDiagnostics(
fileCount: sourceFiles.length,
};

for (const sourceFile of sourceFiles) {
const filePath = sourceFile.getFilePath();
for (const { compilerNode: sourceFile } of sourceFiles) {
const filePath = sourceFile.fileName;
const diagnostics = getGraphQLDiagnostics(filePath, schemaRef, pluginInfo);
const messages: DiagnosticMessage[] = [];

if (diagnostics && diagnostics.length) {
const sourceText = sourceFile.getText();
for (const diagnostic of diagnostics) {
if (
!('messageText' in diagnostic) ||
typeof diagnostic.messageText !== 'string' ||
!diagnostic.file
)
) {
continue;
}
let severity: Severity = 'info';
if (diagnostic.category === ts.DiagnosticCategory.Error) {
severity = 'error';
} else if (diagnostic.category === ts.DiagnosticCategory.Warning) {
severity = 'warn';
}
const [line, col] = getLineCol(sourceText, diagnostic.start);
const position = getFilePosition(sourceFile, diagnostic.start, diagnostic.length);
messages.push({
severity,
message: diagnostic.messageText,
file: diagnostic.file.fileName,
line,
col,
line: position.line,
col: position.col,
endLine: position.endLine,
endColumn: position.endColumn,
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-utils/src/commands/check/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface DiagnosticMessage {
file: string;
line: number;
col: number;
endLine: number | undefined;
endColumn: number | undefined;
}

export interface FileDiagnosticsSignal {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-utils/src/ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './project';
export * from './utils';
27 changes: 27 additions & 0 deletions packages/cli-utils/src/ts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { SourceFile } from 'typescript';

export interface Position {
line: number;
col: number;
endLine: number | undefined;
endColumn: number | undefined;
}

export const getFilePosition = (
file: SourceFile,
start?: number | undefined,
length?: number | undefined
): Position => {
const output: Position = { line: 1, col: 1, endLine: undefined, endColumn: undefined };
if (start) {
let lineAndChar = file.getLineAndCharacterOfPosition(start);
output.line = lineAndChar.line + 1;
output.col = lineAndChar.character + 1;
if (length) {
lineAndChar = file.getLineAndCharacterOfPosition(start + length - 1);
output.endLine = lineAndChar.line + 1;
output.endColumn = lineAndChar.character + 1;
}
}
return output;
};

0 comments on commit a5271cd

Please sign in to comment.