Use const for default value providers

This commit is contained in:
Christopher Schleiden
2022-11-23 14:34:44 -08:00
parent a6cc41d6ff
commit 94fffed2a7
3 changed files with 28 additions and 48 deletions
+7 -17
View File
@@ -1,15 +1,8 @@
import {complete as completeExpression} from "@github/actions-expressions";
import {
convertWorkflowTemplate,
isMapping,
isSequence,
isString,
parseWorkflow,
} from "@github/actions-workflow-parser";
import {convertWorkflowTemplate, isMapping, isSequence, isString, parseWorkflow} from "@github/actions-workflow-parser";
import {CLOSE_EXPRESSION, OPEN_EXPRESSION} from "@github/actions-workflow-parser/templates/template-constants";
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/index";
import {MappingToken} from "@github/actions-workflow-parser/templates/tokens/mapping-token";
import {SequenceToken} from "@github/actions-workflow-parser/templates/tokens/sequence-token";
import {TokenType} from "@github/actions-workflow-parser/templates/tokens/types";
import {File} from "@github/actions-workflow-parser/workflows/file";
import {Position, TextDocument} from "vscode-languageserver-textdocument";
@@ -109,21 +102,18 @@ async function getValues(
const existingValues = getExistingValues(token, parent, parentKey);
let customValues: Value[] | undefined = undefined;
if (token?.definition?.key) {
customValues = await valueProviderConfig?.getCustomValues(token.definition.key, workflowContext);
}
const customValues = await valueProviderConfig?.getCustomValues(token.definition.key, workflowContext);
if (customValues !== undefined) {
return filterAndSortCompletionOptions(customValues, existingValues);
if (customValues) {
return filterAndSortCompletionOptions(customValues, existingValues);
}
}
const valueProviders = defaultValueProviders(workflowContext);
// Use the value provider from the parent if we don't have a value provider for the current key
const valueProvider =
(token?.definition?.key && valueProviders[token.definition.key]) ||
(parent.definition?.key && valueProviders[parent.definition.key]);
(token?.definition?.key && defaultValueProviders[token.definition.key]) ||
(parent.definition?.key && defaultValueProviders[parent.definition.key]);
if (valueProvider) {
const values = valueProvider();
@@ -1,25 +1,23 @@
import {Value, ValueProvider, WorkflowContext} from "./config";
import {Value, WorkflowContext} from "./config";
import {getJobNames} from "./needs";
export function defaultValueProviders(workflowContext: WorkflowContext): {[key: string]: ValueProvider} {
return {
needs: () => getJobNames(workflowContext.template),
"runs-on": () =>
stringsToValues([
"ubuntu-latest",
"ubuntu-18.04",
"ubuntu-16.04",
"windows-latest",
"windows-2019",
"windows-2016",
"macos-latest",
"macos-10.15",
"macos-10.14",
"macos-10.13",
"self-hosted"
])
};
}
const defaultValueProviders: {[key: string]: (workflowContext: WorkflowContext) => Value[]} = {
needs: (workflowContext: WorkflowContext) => getJobNames(workflowContext.template),
"runs-on": () =>
stringsToValues([
"ubuntu-latest",
"ubuntu-18.04",
"ubuntu-16.04",
"windows-latest",
"windows-2019",
"windows-2016",
"macos-latest",
"macos-10.15",
"macos-10.14",
"macos-10.13",
"self-hosted"
])
};
export function stringsToValues(labels: string[]): Value[] {
return labels.map(x => ({label: x}));
@@ -1,19 +1,11 @@
import {Value} from "./config";
import {WorkflowTemplate} from "@github/actions-workflow-parser/model/workflow-template";
import {Value} from "./config";
export function getJobNames(template: WorkflowTemplate | undefined): Value[] {
if (!template) {
return [];
}
const jobNames = new Set<string>();
const jobList = template.jobs;
for (const job of jobList) {
const name = job.id;
if (name && !jobNames.has(name)) {
jobNames.add(name);
}
}
return Array.from(jobNames).map(label => ({label}));
const uniquejobIDs = new Set(template.jobs.map(j => j.id)).values();
return Array.from(uniquejobIDs).map(x => ({label: x}));
}