Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Nov 24, 2023
1 parent 0277316 commit 60f91e9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 164 deletions.
85 changes: 5 additions & 80 deletions packages/example/src/Pokemon.generated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 17 additions & 33 deletions packages/graphqlsp/src/ast/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import ts from 'typescript/lib/tsserverlibrary';
import {
isCallExpression,
isImportDeclaration,
isNoSubstitutionTemplateLiteral,
isObjectLiteralExpression,
isPropertyAssignment,
isStringLiteral,
isTaggedTemplateExpression,
isTemplateExpression,
isTemplateSpan,
isToken,
isVariableStatement,
} from 'typescript';
import fs from 'fs';
import { FragmentDefinitionNode, parse } from 'graphql';
import { Logger } from '..';

export function isFileDirty(fileName: string, source: ts.SourceFile) {
const contents = fs.readFileSync(fileName, 'utf-8');
Expand Down Expand Up @@ -53,8 +39,8 @@ export function findAllTaggedTemplateNodes(
> = [];
function find(node: ts.Node) {
if (
isTaggedTemplateExpression(node) ||
isNoSubstitutionTemplateLiteral(node)
ts.isTaggedTemplateExpression(node) ||
ts.isNoSubstitutionTemplateLiteral(node)
) {
result.push(node);
return;
Expand All @@ -74,19 +60,17 @@ export function findAllCallExpressions(
nodes: Array<ts.NoSubstitutionTemplateLiteral>;
fragments: Array<FragmentDefinitionNode>;
} {
const logger: Logger = (msg: string) =>
info.project.projectService.logger.info(`[GraphQLSP] ${msg}`);
const result: Array<ts.NoSubstitutionTemplateLiteral> = [];
let fragments: Array<FragmentDefinitionNode> = [];
let hasTriedToFindFragments = false;
function find(node: ts.Node) {
if (isCallExpression(node) && node.expression.getText() === template) {
if (ts.isCallExpression(node) && node.expression.getText() === template) {
if (!hasTriedToFindFragments) {
hasTriedToFindFragments = true;
fragments = getAllFragments(sourceFile.fileName, node, info);
}
const [arg] = node.arguments;
if (arg && isNoSubstitutionTemplateLiteral(arg)) {
if (arg && ts.isNoSubstitutionTemplateLiteral(arg)) {
result.push(arg);
}
return;
Expand Down Expand Up @@ -117,19 +101,19 @@ export function getAllFragments(

ts.forEachChild(src, node => {
if (
isVariableStatement(node) &&
ts.isVariableStatement(node) &&
node.declarationList &&
node.declarationList.declarations[0].name.getText() === 'documents'
) {
const [declaration] = node.declarationList.declarations;
if (
declaration.initializer &&
isObjectLiteralExpression(declaration.initializer)
ts.isObjectLiteralExpression(declaration.initializer)
) {
declaration.initializer.properties.forEach(property => {
if (
isPropertyAssignment(property) &&
isStringLiteral(property.name)
ts.isPropertyAssignment(property) &&
ts.isStringLiteral(property.name)
) {
try {
const possibleFragment = JSON.parse(
Expand Down Expand Up @@ -162,15 +146,15 @@ export function getAllFragments(
export function findAllImports(
sourceFile: ts.SourceFile
): Array<ts.ImportDeclaration> {
return sourceFile.statements.filter(isImportDeclaration);
return sourceFile.statements.filter(ts.isImportDeclaration);
}

export function bubbleUpTemplate(node: ts.Node): ts.Node {
while (
isNoSubstitutionTemplateLiteral(node) ||
isToken(node) ||
isTemplateExpression(node) ||
isTemplateSpan(node)
ts.isNoSubstitutionTemplateLiteral(node) ||
ts.isToken(node) ||
ts.isTemplateExpression(node) ||
ts.isTemplateSpan(node)
) {
node = node.parent;
}
Expand All @@ -180,10 +164,10 @@ export function bubbleUpTemplate(node: ts.Node): ts.Node {

export function bubbleUpCallExpression(node: ts.Node): ts.Node {
while (
isNoSubstitutionTemplateLiteral(node) ||
isToken(node) ||
isTemplateExpression(node) ||
isTemplateSpan(node)
ts.isNoSubstitutionTemplateLiteral(node) ||
ts.isToken(node) ||
ts.isTemplateExpression(node) ||
ts.isTemplateSpan(node)
) {
node = node.parent;
}
Expand Down
31 changes: 12 additions & 19 deletions packages/graphqlsp/src/ast/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import {
isAsExpression,
isIdentifier,
isNoSubstitutionTemplateLiteral,
isObjectLiteralExpression,
isTaggedTemplateExpression,
NoSubstitutionTemplateLiteral,
TaggedTemplateExpression,
} from 'typescript';
import { print } from 'graphql';
import ts from 'typescript/lib/tsserverlibrary';
import { findNode } from '.';
Expand All @@ -23,17 +14,17 @@ type TemplateResult = {
};

export function resolveTemplate(
node: TaggedTemplateExpression | NoSubstitutionTemplateLiteral,
node: ts.TaggedTemplateExpression | ts.NoSubstitutionTemplateLiteral,
filename: string,
info: ts.server.PluginCreateInfo
): TemplateResult {
if (isNoSubstitutionTemplateLiteral(node)) {
if (ts.isNoSubstitutionTemplateLiteral(node)) {
return { combinedText: node.getText().slice(1, -1), resolvedSpans: [] };
}

let templateText = node.template.getText().slice(1, -1);
if (
isNoSubstitutionTemplateLiteral(node.template) ||
ts.isNoSubstitutionTemplateLiteral(node.template) ||
node.template.templateSpans.length === 0
) {
return { combinedText: templateText, resolvedSpans: [] };
Expand All @@ -42,7 +33,7 @@ export function resolveTemplate(
let addedCharacters = 0;
const resolvedSpans = node.template.templateSpans
.map(span => {
if (isIdentifier(span.expression)) {
if (ts.isIdentifier(span.expression)) {
const definitions = info.languageService.getDefinitionAtPosition(
filename,
span.expression.getStart()
Expand All @@ -68,7 +59,7 @@ export function resolveTemplate(
};
if (
parent.initializer &&
isTaggedTemplateExpression(parent.initializer)
ts.isTaggedTemplateExpression(parent.initializer)
) {
const text = resolveTemplate(
parent.initializer,
Expand All @@ -93,8 +84,8 @@ export function resolveTemplate(
return alteredSpan;
} else if (
parent.initializer &&
isAsExpression(parent.initializer) &&
isTaggedTemplateExpression(parent.initializer.expression)
ts.isAsExpression(parent.initializer) &&
ts.isTaggedTemplateExpression(parent.initializer.expression)
) {
const text = resolveTemplate(
parent.initializer.expression,
Expand All @@ -118,9 +109,11 @@ export function resolveTemplate(
return alteredSpan;
} else if (
parent.initializer &&
isAsExpression(parent.initializer) &&
isAsExpression(parent.initializer.expression) &&
isObjectLiteralExpression(parent.initializer.expression.expression)
ts.isAsExpression(parent.initializer) &&
ts.isAsExpression(parent.initializer.expression) &&
ts.isObjectLiteralExpression(
parent.initializer.expression.expression
)
) {
const astObject = JSON.parse(
parent.initializer.expression.expression.getText()
Expand Down
1 change: 0 additions & 1 deletion packages/graphqlsp/src/ast/token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ts from 'typescript/lib/tsserverlibrary';
import { onlineParser, State, CharacterStream } from 'graphql-language-service';
import { isTemplateLiteral } from 'typescript';

export interface Token {
start: number;
Expand Down
45 changes: 17 additions & 28 deletions packages/graphqlsp/src/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import ts from 'typescript/lib/tsserverlibrary';
import {
ImportTypeNode,
isAsExpression,
isExpressionStatement,
isImportTypeNode,
isNamedImportBindings,
isNamespaceImport,
isNoSubstitutionTemplateLiteral,
isTaggedTemplateExpression,
isTemplateExpression,
} from 'typescript';
import { Diagnostic, getDiagnostics } from 'graphql-language-service';
import {
FragmentDefinitionNode,
Expand Down Expand Up @@ -78,10 +67,11 @@ export function getGraphQLDiagnostics(

const texts = nodes.map(node => {
if (
(isNoSubstitutionTemplateLiteral(node) || isTemplateExpression(node)) &&
(ts.isNoSubstitutionTemplateLiteral(node) ||
ts.isTemplateExpression(node)) &&
!isCallExpression
) {
if (isTaggedTemplateExpression(node.parent)) {
if (ts.isTaggedTemplateExpression(node.parent)) {
node = node.parent;
} else {
return undefined;
Expand All @@ -101,9 +91,10 @@ export function getGraphQLDiagnostics(
let node = originalNode;
if (
!isCallExpression &&
(isNoSubstitutionTemplateLiteral(node) || isTemplateExpression(node))
(ts.isNoSubstitutionTemplateLiteral(node) ||
ts.isTemplateExpression(node))
) {
if (isTaggedTemplateExpression(node.parent)) {
if (ts.isTaggedTemplateExpression(node.parent)) {
node = node.parent;
} else {
return undefined;
Expand All @@ -118,14 +109,12 @@ export function getGraphQLDiagnostics(
const lines = text.split('\n');

let isExpression = false;
if (isAsExpression(node.parent)) {
if (isExpressionStatement(node.parent.parent)) {
isExpression = true;
}
} else {
if (isExpressionStatement(node.parent)) {
if (ts.isAsExpression(node.parent)) {
if (ts.isExpressionStatement(node.parent.parent)) {
isExpression = true;
}
} else if (ts.isExpressionStatement(node.parent)) {
isExpression = true;
}
// When we are dealing with a plain gql statement we have to add two these can be recognised
// by the fact that the parent is an expressionStatement
Expand Down Expand Up @@ -269,13 +258,13 @@ export function getGraphQLDiagnostics(

if (
imp.importClause.namedBindings &&
isNamespaceImport(imp.importClause.namedBindings)
ts.isNamespaceImport(imp.importClause.namedBindings)
) {
// TODO: we might need to warn here when the fragment is unused as a namespace import
return;
} else if (
imp.importClause.namedBindings &&
isNamedImportBindings(imp.importClause.namedBindings)
ts.isNamedImportBindings(imp.importClause.namedBindings)
) {
imp.importClause.namedBindings.elements.forEach(el => {
importedNames.push(el.name.text);
Expand Down Expand Up @@ -307,10 +296,10 @@ export function getGraphQLDiagnostics(
if (template) {
let node = template;
if (
isNoSubstitutionTemplateLiteral(node) ||
isTemplateExpression(node)
ts.isNoSubstitutionTemplateLiteral(node) ||
ts.isTemplateExpression(node)
) {
if (isTaggedTemplateExpression(node.parent)) {
if (ts.isTaggedTemplateExpression(node.parent)) {
node = node.parent;
} else {
return;
Expand Down Expand Up @@ -427,8 +416,8 @@ export function getGraphQLDiagnostics(
// This checks whether one of the children is an import-type
// which is a short-circuit if there is no as
const typeImport = parentChildren.find(x =>
isImportTypeNode(x)
) as ImportTypeNode;
ts.isImportTypeNode(x)
) as ts.ImportTypeNode;

if (typeImport && typeImport.getText().includes(exportName))
return sourceText;
Expand Down
Loading

0 comments on commit 60f91e9

Please sign in to comment.