Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Feb 25, 2023
1 parent 18450ec commit 6f4e6d2
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 62 deletions.
28 changes: 2 additions & 26 deletions packages/codemirror-graphql/src/results/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@

import CodeMirror from 'codemirror';

import {
list,
t,
onlineParser,
p,
State,
Token,
} from 'graphql-language-service';
import { list, t, onlineParser, p, Token } from 'graphql-language-service';
import indent from '../utils/mode-indent';

/**
* This mode defines JSON, but provides a data-laden parser state to enable
Expand Down Expand Up @@ -44,24 +38,6 @@ CodeMirror.defineMode('graphql-results', config => {
};
});

function indent(
this: CodeMirror.Mode<any> & {
electricInput?: RegExp;
config?: CodeMirror.EditorConfiguration;
},
state: State,
textAfter: string,
) {
const { levels, indentLevel } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
!levels || levels.length === 0
? indentLevel
: levels.at(-1) - (this.electricInput?.test(textAfter) ? 1 : 0);
return (level || 0) * (this.config?.indentUnit || 0);
}

/**
* The lexer rules. These are exactly as described by the spec.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/src/utils/mode-indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export default function indent(
const level =
!levels || levels.length === 0
? indentLevel
: levels.at(-1) - (this.electricInput?.test(textAfter) ? 1 : 0);
: levels.at(-1)! - (this.electricInput?.test(textAfter) ? 1 : 0);
return (level || 0) * (this.config?.indentUnit || 0);
}
19 changes: 1 addition & 18 deletions packages/codemirror-graphql/src/variables/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
State,
Token,
} from 'graphql-language-service';
import indent from '../utils/mode-indent';

/**
* This mode defines JSON, but provides a data-laden parser state to enable
Expand Down Expand Up @@ -45,24 +46,6 @@ CodeMirror.defineMode('graphql-variables', config => {
};
});

function indent(
this: CodeMirror.Mode<any> & {
electricInput?: RegExp;
config?: CodeMirror.EditorConfiguration;
},
state: State,
textAfter: string,
) {
const { levels, indentLevel } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
!levels || levels.length === 0
? indentLevel
: levels.at(-1) - (this.electricInput?.test(textAfter) ? 1 : 0);
return (level || 0) * (this.config?.indentUnit || 0);
}

/**
* The lexer rules. These are exactly as described by the spec.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function DocExplorer() {
caller: DocExplorer,
});

const navItem = explorerNavStack.at(-1);
const navItem = explorerNavStack.at(-1)!;

let content: ReactNode = null;
if (fetchError) {
Expand Down Expand Up @@ -55,7 +55,7 @@ export function DocExplorer() {

let prevName;
if (explorerNavStack.length > 1) {
prevName = explorerNavStack.at(-2).name;
prevName = explorerNavStack.at(-2)!.name;
}

return (
Expand Down
4 changes: 2 additions & 2 deletions packages/graphiql-react/src/explorer/components/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function Search() {
return () => window.removeEventListener('keydown', handleKeyDown);
}, []);

const navItem = explorerNavStack.at(-1);
const navItem = explorerNavStack.at(-1)!;

const shouldSearchBoxAppear =
explorerNavStack.length === 1 ||
Expand Down Expand Up @@ -198,7 +198,7 @@ export function useSearchResults(caller?: Function) {
caller: caller || useSearchResults,
});

const navItem = explorerNavStack.at(-1);
const navItem = explorerNavStack.at(-1)!;

return useCallback(
(searchValue: string) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql-react/src/explorer/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function ExplorerContextProvider(props: ExplorerContextProviderProps) {

const push = useCallback((item: ExplorerNavStackItem) => {
setNavStack(currentState => {
const lastItem = currentState.at(-1);
const lastItem = currentState.at(-1)!;
return lastItem.def === item.def
? // Avoid pushing duplicate items
currentState
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-language-service-cli/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function main(
case 'autocomplete':
const lines = text.split('\n');
const row = parseInt(argv.row, 10) || lines.length - 1;
const column = parseInt(argv.column, 10) || lines.at(-1).length;
const column = parseInt(argv.column, 10) || lines.at(-1)!.length;
const point = new Position(row, column);
exitCode = _getAutocompleteSuggestions(text, point, schemaPath);
break;
Expand Down
12 changes: 3 additions & 9 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,22 +419,16 @@ export class MessageProcessor {
// with version information up-to-date, so that the textDocument contents
// may be used during performing language service features,
// e.g. auto-completions.
if (
!params ||
!params.textDocument ||
!params.contentChanges ||
!params.textDocument.uri
) {
if (!params?.textDocument?.uri || !params.contentChanges) {
throw new Error(
'`textDocument`, `textDocument.uri`, and `contentChanges` arguments are required.',
);
}
const { textDocument } = params;
const { textDocument, contentChanges } = params;
const { uri } = textDocument;
const project = this._graphQLCache.getProjectForFile(uri);
try {
const { contentChanges } = params;
const contentChange = contentChanges.at(-1);
const contentChange = contentChanges.at(-1)!;

// As `contentChanges` is an array, and we just want the
// latest update to the text, grab the last entry from the array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function parseDocument(
const lines = query.split('\n');
const range = new Range(
new Position(0, 0),
new Position(lines.length - 1, lines.at(-1).length - 1),
new Position(lines.length - 1, lines.at(-1)!.length - 1),
);
return [{ query, range }];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function getToken(
if (
state.indentLevel &&
levels.length > 0 &&
levels.at(-1) < state.indentLevel
levels.at(-1)! < state.indentLevel
) {
state.indentLevel = levels.at(-1);
}
Expand Down

0 comments on commit 6f4e6d2

Please sign in to comment.