Skip to content

Commit

Permalink
feat: convert LSP Server to Typescript, remove watchman (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed Jan 18, 2020
1 parent adb73f5 commit 8e33dbb
Show file tree
Hide file tree
Showing 46 changed files with 733 additions and 889 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ packages/graphiql/storybook
packages/graphiql/lsp
**/renderExample.js
**/*.min.js
/coverage/
**/coverage/


# codemirror's build artefacts are exported from the package root
Expand Down
13 changes: 5 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module.exports = {
root: true,
parser: 'babel-eslint',
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 7,
sourceType: 'module',
Expand Down Expand Up @@ -273,7 +273,7 @@ module.exports = {
'prefer-object-spread/prefer-object-spread': 1,
},

plugins: ['babel', 'import', 'flowtype', 'prefer-object-spread'],
plugins: ['import', 'prefer-object-spread'],

overrides: [
// Cypress plugin, global, etc only for cypress directory
Expand Down Expand Up @@ -302,18 +302,15 @@ module.exports = {
// Rules for TypeScript only
{
files: ['*.ts', '*.tsx'],
parser: '@typescript-eslint/parser',
rules: {
'no-unused-vars': 'off',
},
},
// Rules for Flow only
{
files: [
'packages/codemirror-graphql/src/**/*.js',
'packages/codemirror-graphql/src/**/*.jsx',
],
plugins: ['flowtype'],
files: ['packages/codemirror-graphql/src/**/*.js'],
parser: 'babel-eslint',
plugins: ['flowtype', 'babel'],
rules: {
// flowtype (https://github.com/gajus/eslint-plugin-flowtype)
'flowtype/boolean-style': 1,
Expand Down
3 changes: 3 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[ignore]
.*/__mocks__/.*
.*/__tests__/.*
!*.flow.js
.*/coverage/.*
!packages/codemirror-graphql/.*
!packages/codemirror-graphql/.*
.*/dist/.*
.*/resources/.*
.*/node_modules/.*
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ module.exports = {
'!**/resources/**',
'!**/examples/**',
'!**/codemirror-graphql/**',
'!**/graphql-language-service-types/**',
],
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"scripts": {
"build": "yarn run build-clean && yarn build-ts && yarn build-js",
"build-js": "lerna run build --scope codemirror-graphql --scope graphql-language-service-server --scope graphql-language-service",
"build-js": "lerna run build --scope codemirror-graphql",
"build-ts": "yarn run tsc",
"build-clean": "yarn run tsc --clean && rimraf '{packages,examples}/**/{dist,esm,bundle,cdn,webpack,storybook}' && lerna run build-clean --parallel",
"build-validate": "lerna run build-validate",
Expand Down Expand Up @@ -63,6 +63,7 @@
"@commitlint/config-conventional": "^8.1.0",
"@commitlint/config-lerna-scopes": "^8.1.0",
"@strictsoftware/typedoc-plugin-monorepo": "^0.2.1",
"@types/fetch-mock": "^7.3.2",
"@types/jest": "^24.0.18",
"@typescript-eslint/parser": "^2.3.0",
"babel-eslint": "^10.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"build-clean": "rimraf {mode,hint,info,jump,lint}.{js,esm.js,js.flow} && rimraf esm results utils variables coverage __tests__",
"build-flow": "node ../../resources/buildFlow.js",
"watch": "babel --optional runtime resources/watch.js | node",
"test": "nyc --require @babel/polyfill --reporter lcov mocha $npm_package_options_mocha"
"test": "nyc --require @babel/polyfill --reporter lcov --reporter text mocha $npm_package_options_mocha"
},
"peerDependencies": {
"codemirror": "^5.26.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import List, { ListRow } from '../List/List';
import Tabs from './Tabs';
import React, { useState } from 'react';
import { layout } from '../../../.storybook/decorators';
import { layout } from '../themes/decorators';

export default { title: 'Tabbar', decorators: [layout] };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Tabs from './Tabs';
import React from 'react';
import Toolbar from './Toolbar';
import Content from './Content';
import { layout } from '../../../.storybook/decorators';
import { layout } from '../themes/decorators';

export default { title: 'Toolbar', decorators: [layout] };

Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql/src/new-components/Type.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { jsx } from 'theme-ui';
import List, { ListRow } from './List/List';
import { SectionHeader, Explainer } from './Type';
import { layout } from '../../.storybook/decorators';
import { layout } from './themes/decorators';

export default { title: 'Type', decorators: [layout] };

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/graphiql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"composite": false,
"composite": true,
"jsx": "react",
"allowJs": true,
"baseUrl": ".",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import {
GraphQLProjectConfig,
Uri,
Position,
CustomValidationRule,
} from 'graphql-language-service-types';

// import { Position } from 'graphql-language-service-utils';
import { Hover } from 'vscode-languageserver-types';
import { Hover, DiagnosticSeverity } from 'vscode-languageserver-types';

import { Kind, parse, print } from 'graphql';
import { getAutocompleteSuggestions } from './getAutocompleteSuggestions';
Expand Down Expand Up @@ -122,7 +123,7 @@ export class GraphQLLanguageService {
const range = getRange(error.locations[0], query);
return [
{
severity: SEVERITY.ERROR,
severity: SEVERITY.ERROR as DiagnosticSeverity,
message: error.message,
source: 'GraphQL: Syntax',
range,
Expand Down Expand Up @@ -166,7 +167,9 @@ export class GraphQLLanguageService {
/* eslint-disable no-implicit-coercion */
const rulesPath = resolveFile(customRulesModulePath);
if (rulesPath) {
const customValidationRules = await requireFile(rulesPath);
const customValidationRules: (
config: GraphQLConfig,
) => CustomValidationRule[] = await requireFile(rulesPath);
if (customValidationRules) {
customRules = customValidationRules(this._graphQLConfig);
}
Expand Down Expand Up @@ -220,7 +223,7 @@ export class GraphQLLanguageService {
query: string,
position: Position,
filePath: Uri,
): Promise<DefinitionQueryResult | null | undefined> {
): Promise<DefinitionQueryResult | null> {
const projectConfig = this.getConfigForURI(filePath);

let ast;
Expand Down Expand Up @@ -269,7 +272,7 @@ export class GraphQLLanguageService {
node: NamedTypeNode,
filePath: Uri,
projectConfig: GraphQLProjectConfig,
): Promise<DefinitionQueryResult | null | undefined> {
): Promise<DefinitionQueryResult | null> {
const objectTypeDefinitions = await this._graphQLCache.getObjectTypeDefinitions(
projectConfig,
);
Expand Down Expand Up @@ -313,7 +316,7 @@ export class GraphQLLanguageService {
node: FragmentSpreadNode,
filePath: Uri,
projectConfig: GraphQLProjectConfig,
): Promise<DefinitionQueryResult | null | undefined> {
): Promise<DefinitionQueryResult | null> {
const fragmentDefinitions = await this._graphQLCache.getFragmentDefinitions(
projectConfig,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export function getDefinitionState(
let definitionState;

// TODO - couldn't figure this one out
// @ts-ignore
forEachState(tokenState, (state: State): AllTypeInfo | null | undefined => {
forEachState(tokenState, (state: State): void => {
switch (state.kind) {
case 'Query':
case 'ShortQuery':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function getSuggestionsForFieldNames(
label: field.name,
detail: String(field.type),
documentation: field.description,
deprecated: field.isDeprecated,
isDeprecated: field.isDeprecated,
deprecationReason: field.deprecationReason,
})),
Expand All @@ -202,19 +203,22 @@ function getSuggestionsForFieldNames(
function getSuggestionsForInputValues(
token: ContextToken,
typeInfo: AllTypeInfo,
): Array<CompletionItem> {
): CompletionItem[] {
const namedInputType = getNamedType(typeInfo.inputType as GraphQLType);
if (namedInputType instanceof GraphQLEnumType) {
const values = namedInputType.getValues();
const values: GraphQLEnumValues[] = namedInputType.getValues();
return hintList(
token,
values.map(value => ({
label: value.name,
detail: String(namedInputType),
documentation: value.description,
isDeprecated: value.isDeprecated,
deprecationReason: value.deprecationReason,
})),
values.map(
(value: GraphQLEnumValue): CompletionItem => ({
label: value.name,
detail: String(namedInputType),
documentation: value.description,
deprecated: value.isDeprecated,
isDeprecated: value.isDeprecated,
deprecationReason: value.deprecationReason,
}),
),
);
} else if (namedInputType === GraphQLBoolean) {
return hintList(token, [
Expand Down
16 changes: 9 additions & 7 deletions packages/graphql-language-service-interface/src/getDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ import {
} from 'graphql-language-service-types';

import { locToRange, offsetToPosition } from 'graphql-language-service-utils';
import invariant from 'assert';
import assert from 'assert';

export const LANGUAGE = 'GraphQL';

function getRange(text: string, node: ASTNode): Range {
const location = node.loc as Location;
invariant(location, 'Expected ASTNode to have a location.');
assert(location, 'Expected ASTNode to have a location.');
return locToRange(text, location);
}

function getPosition(text: string, node: ASTNode): Position {
const location = node.loc as Location;
invariant(location, 'Expected ASTNode to have a location.');
assert(location, 'Expected ASTNode to have a location.');
return offsetToPosition(text, location.start);
}

Expand Down Expand Up @@ -111,14 +111,16 @@ function getDefinitionForFragmentDefinition(
definition: FragmentDefinitionNode | OperationDefinitionNode,
): Definition {
const name = definition.name;
invariant(name, 'Expected ASTNode to have a Name.');
if (!name) {
throw Error('Expected ASTNode to have a Name.');
}

return {
path,
position: getPosition(text, definition),
range: getRange(text, definition),
// @ts-ignore
// TODO: doesnt seem to pick up the inference
// from invariant() exception logic
// from assert() exception logic
name: name.value || '',
language: LANGUAGE,
// This is a file inside the project root, good enough for now
Expand All @@ -132,7 +134,7 @@ function getDefinitionForNodeDefinition(
definition: TypeDefinitionNode,
): Definition {
const name = definition.name;
invariant(name, 'Expected ASTNode to have a Name.');
assert(name, 'Expected ASTNode to have a Name.');
return {
path,
position: getPosition(text, definition),
Expand Down
19 changes: 9 additions & 10 deletions packages/graphql-language-service-interface/src/getDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import {
SourceLocation,
} from 'graphql';

import {
Diagnostic,
CustomValidationRule,
} from 'graphql-language-service-types';
import { CustomValidationRule } from 'graphql-language-service-types';

import invariant from 'assert';
import { findDeprecatedUsages, parse } from 'graphql';
Expand All @@ -32,11 +29,13 @@ import {
Position,
} from 'graphql-language-service-utils';

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

export const SEVERITY = {
ERROR: 1,
WARNING: 2,
INFORMATION: 3,
HINT: 4,
ERROR: 1 as DiagnosticSeverity,
WARNING: 2 as DiagnosticSeverity,
INFORMATION: 3 as DiagnosticSeverity,
HINT: 4 as DiagnosticSeverity,
};

export function getDiagnostics(
Expand All @@ -52,7 +51,7 @@ export function getDiagnostics(
const range = getRange(error.locations[0], query);
return [
{
severity: SEVERITY.ERROR,
severity: SEVERITY.ERROR as DiagnosticSeverity,
message: error.message,
source: 'GraphQL: Syntax',
range,
Expand Down Expand Up @@ -100,7 +99,7 @@ function mapCat<T>(

function annotations(
error: GraphQLError,
severity: number,
severity: DiagnosticSeverity,
type: string,
): Array<Diagnostic> {
if (!error.nodes) {
Expand Down
7 changes: 3 additions & 4 deletions packages/graphql-language-service-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
"typings": "esm/index.d.ts",
"scripts": {
"test": "node ../../resources/runTests.js",
"build": "yarn build-js && yarn build-esm && yarn build-flow",
"build-js": "node ../../resources/buildJs.js",
"build-esm": "cross-env ESM=true babel src --root-mode upward --ignore **/__tests__/**,**/__mocks__/** --out-dir esm",
"build": "yarn build-js && yarn build-flow",
"build-js": "rimraf dist && tsc",
"build-esm": "rimraf esm && tsc -p tsconfig.esm",
"build-flow": "node ../../resources/buildFlow.js"
},
"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
},
"dependencies": {
"@babel/parser": "^7.4.5",
"fb-watchman": "^2.0.0",
"glob": "^7.1.2",
"graphql-config": "2.2.1",
"graphql-language-service-interface": "^2.3.3",
Expand Down
Loading

0 comments on commit 8e33dbb

Please sign in to comment.