Combine methods

This commit is contained in:
Jacob Wallraff
2023-03-06 15:16:26 -08:00
parent 0001dd045c
commit b272ab29be
16 changed files with 42 additions and 70 deletions
+3 -10
View File
@@ -28,12 +28,7 @@ import {fetchActionMetadata} from "./utils/action-metadata";
import {TTLCache} from "./utils/cache";
import {timeOperation} from "./utils/timer";
import {valueProviders} from "./value-providers";
import {
clearParsedCache,
clearParsedCacheEntry,
clearWorkflowTemplateCache,
clearWorkflowTemplateCacheEntry
} from "@github/actions-languageservice/utils/workflow-cache";
import {clearCacheEntry, clearCache} from "@github/actions-languageservice/utils/workflow-cache";
export function initConnection(connection: Connection) {
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
@@ -100,8 +95,7 @@ export function initConnection(connection: Connection) {
connection.onInitialized(() => {
if (hasWorkspaceFolderCapability) {
connection.workspace.onDidChangeWorkspaceFolders(_event => {
clearParsedCache();
clearWorkflowTemplateCache();
clearCache();
});
}
});
@@ -109,8 +103,7 @@ export function initConnection(connection: Connection) {
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(change => {
clearParsedCacheEntry(change.document.uri);
clearWorkflowTemplateCacheEntry(change.document.uri);
clearCacheEntry(change.document.uri);
return timeOperation("validation", async () => await validateTextDocument(change.document));
});
@@ -6,7 +6,7 @@ import {registerLogger} from "./log";
import {getPositionFromCursor} from "./test-utils/cursor-position";
import {TestLogger} from "./test-utils/logger";
import {testFileProvider} from "./test-utils/test-file-provider";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
const contextProviderConfig: ContextProviderConfig = {
getContext: async (context: string) => {
@@ -26,8 +26,7 @@ const contextProviderConfig: ContextProviderConfig = {
registerLogger(new TestLogger());
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("expressions", () => {
@@ -2,7 +2,7 @@ import {CompletionItem, MarkupContent} from "vscode-languageserver-types";
import {complete} from "./complete";
import {getPositionFromCursor} from "./test-utils/cursor-position";
import {testFileProvider} from "./test-utils/test-file-provider";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
function mapResult(result: CompletionItem[]) {
return result.map(x => {
@@ -11,8 +11,7 @@ function mapResult(result: CompletionItem[]) {
}
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("completion with reusable workflows", () => {
+2 -3
View File
@@ -5,13 +5,12 @@ import {registerLogger} from "./log";
import {getPositionFromCursor} from "./test-utils/cursor-position";
import {TestLogger} from "./test-utils/logger";
import {ValueProviderConfig, ValueProviderKind} from "./value-providers/config";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
registerLogger(new TestLogger());
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("completion", () => {
+2 -3
View File
@@ -1,10 +1,9 @@
import {documentLinks} from "./document-links";
import {createDocument} from "./test-utils/document";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("documentLinks", () => {
@@ -5,7 +5,7 @@ import {hover} from "./hover";
import {registerLogger} from "./log";
import {getPositionFromCursor} from "./test-utils/cursor-position";
import {TestLogger} from "./test-utils/logger";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
const contextProviderConfig: ContextProviderConfig = {
getContext: async (context: string) => {
@@ -36,8 +36,7 @@ const contextProviderConfig: ContextProviderConfig = {
registerLogger(new TestLogger());
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("hover.expressions", () => {
@@ -1,11 +1,10 @@
import {hover} from "./hover";
import {testHoverConfig} from "./hover.test";
import {getPositionFromCursor} from "./test-utils/cursor-position";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("hover.reusable-workflow", () => {
+2 -3
View File
@@ -3,7 +3,7 @@ import {StringToken} from "@github/actions-workflow-parser/templates/tokens/stri
import {DescriptionProvider, hover, HoverConfig} from "./hover";
import {getPositionFromCursor} from "./test-utils/cursor-position";
import {testFileProvider} from "./test-utils/test-file-provider";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
export function testHoverConfig(tokenValue: string, tokenKey: string, description?: string) {
return {
@@ -24,8 +24,7 @@ export function testHoverConfig(tokenValue: string, tokenKey: string, descriptio
}
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("hover", () => {
@@ -1,9 +1,8 @@
import {clearParsedCache, clearWorkflowTemplateCache} from "../utils/workflow-cache";
import {clearCache} from "../utils/workflow-cache";
import {getPositionFromCursor} from "./cursor-position";
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("getPositionFromCursor", () => {
+3 -9
View File
@@ -9,19 +9,13 @@ import {CompletionConfig} from "../complete";
const parsedWorkflowCache = new Map<string, ParseWorkflowResult>();
const workflowTemplateCache = new Map<string, WorkflowTemplate>();
export function clearParsedCacheEntry(uri: string) {
export function clearCacheEntry(uri: string) {
parsedWorkflowCache.delete(uri);
}
export function clearParsedCache() {
parsedWorkflowCache.clear();
}
export function clearWorkflowTemplateCacheEntry(uri: string) {
workflowTemplateCache.delete(uri);
}
export function clearWorkflowTemplateCache() {
export function clearCache() {
parsedWorkflowCache.clear();
workflowTemplateCache.clear();
}
+2 -3
View File
@@ -5,13 +5,12 @@ import {createDocument} from "./test-utils/document";
import {TestLogger} from "./test-utils/logger";
import {validate, ValidationConfig} from "./validate";
import {ValueProviderKind} from "./value-providers/config";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
registerLogger(new TestLogger());
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
const validationConfig: ValidationConfig = {
@@ -5,13 +5,12 @@ import {registerLogger} from "./log";
import {createDocument} from "./test-utils/document";
import {TestLogger} from "./test-utils/logger";
import {validate, ValidationConfig} from "./validate";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
registerLogger(new TestLogger());
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("expression validation", () => {
+2 -3
View File
@@ -2,11 +2,10 @@ import {Diagnostic, DiagnosticSeverity} from "vscode-languageserver-types";
import {createDocument} from "./test-utils/document";
import {validate} from "./validate";
import {defaultValueProviders} from "./value-providers/default";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("validation", () => {
+7 -7
View File
@@ -1,6 +1,6 @@
import {Evaluator, ExpressionEvaluationError, Lexer, Parser} from "@github/actions-expressions";
import {Expr} from "@github/actions-expressions/ast";
import {isBasicExpression, isString, WorkflowTemplate} from "@github/actions-workflow-parser";
import {isBasicExpression, isString, ParseWorkflowResult, WorkflowTemplate} from "@github/actions-workflow-parser";
import {ErrorPolicy} from "@github/actions-workflow-parser/model/convert";
import {splitAllowedContext} from "@github/actions-workflow-parser/templates/allowed-context";
import {BasicExpressionToken} from "@github/actions-workflow-parser/templates/tokens/basic-expression-token";
@@ -46,24 +46,24 @@ export async function validate(textDocument: TextDocument, config?: ValidationCo
const diagnostics: Diagnostic[] = [];
try {
const parsedWorkflow = fetchOrParseWorkflow(file, textDocument.uri);
if (!parsedWorkflow) {
const result: ParseWorkflowResult | undefined = fetchOrParseWorkflow(file, textDocument.uri);
if (!result) {
return [];
}
if (parsedWorkflow.value) {
if (result.value) {
// Errors will be updated in the context. Attempt to do the conversion anyway in order to give the user more information
const template = await fetchOrConvertWorkflowTemplate(parsedWorkflow, textDocument.uri, config, {
const template = await fetchOrConvertWorkflowTemplate(result, textDocument.uri, config, {
fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0,
errorPolicy: ErrorPolicy.TryConversion
});
// Validate expressions and value providers
await additionalValidations(diagnostics, textDocument.uri, template, parsedWorkflow.value, config);
await additionalValidations(diagnostics, textDocument.uri, template, result.value, config);
}
// For now map parser errors directly to diagnostics
for (const error of parsedWorkflow.context.errors.getErrors()) {
for (const error of result.context.errors.getErrors()) {
let range = mapRange(error.range);
diagnostics.push({
@@ -1,11 +1,10 @@
import {createDocument} from "./test-utils/document";
import {testFileProvider} from "./test-utils/test-file-provider";
import {clearParsedCache, clearWorkflowTemplateCache} from "./utils/workflow-cache";
import {clearCache} from "./utils/workflow-cache";
import {validate} from "./validate";
beforeEach(() => {
clearWorkflowTemplateCache();
clearParsedCache();
clearCache();
});
describe("workflow references validation", () => {
@@ -6,7 +6,6 @@ import {File} from "./file";
import {WORKFLOW_ROOT} from "./workflow-constants";
import {getWorkflowSchema} from "./workflow-schema";
import {YamlObjectReader} from "./yaml-object-reader";
export interface ParseWorkflowResult {
context: TemplateContext;
value: TemplateToken | undefined;
@@ -27,17 +26,15 @@ export function parseWorkflow(entryFile: File, contextOrTrace: TraceWriter | Tem
for (const err of reader.errors) {
context.error(fileId, err.message, err.range);
}
const result = {
return {
context,
value: undefined
};
return result;
}
const templateToken = templateReader.readTemplate(context, WORKFLOW_ROOT, reader, fileId);
const result = templateReader.readTemplate(context, WORKFLOW_ROOT, reader, fileId);
const result = {
return <ParseWorkflowResult>{
context,
value: templateToken
} satisfies ParseWorkflowResult;
return result;
value: result
};
}