Ensure context is inherited in validation

This commit is contained in:
Josh Gross
2022-12-08 13:55:46 -05:00
parent 7e5c8d20ae
commit f2c04b7b89
3 changed files with 10 additions and 7 deletions
+1 -1
View File
@@ -84,7 +84,7 @@ export async function complete(
const expressionInput = (getExpressionInput(currentInput, relCharPos) || "").trim();
const allowedContext = getAllowedContext(token, parent!);
const allowedContext = getAllowedContext(token, parent);
const context = await getContext(allowedContext, contextProviderConfig, workflowContext);
return completeExpression(expressionInput, context, []);
@@ -1,11 +1,11 @@
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
export function getAllowedContext(token: TemplateToken, parent: TemplateToken): string[] {
export function getAllowedContext(token: TemplateToken, parent: TemplateToken | null | undefined): string[] {
// Workaround for https://github.com/github/c2c-actions-experience/issues/6876
// Context is inherited from the parent
const allowedContext = new Set<string>();
for (const t of [token, parent]) {
if (t.definition?.readerContext) {
if (t?.definition?.readerContext) {
for (const context of t.definition.readerContext) {
allowedContext.add(context);
}
+7 -4
View File
@@ -23,6 +23,7 @@ import {getContext} from "./context-providers/default";
import {getWorkflowContext, WorkflowContext} from "./context/workflow-context";
import {AccessError, wrapDictionary} from "./expression-validation/error-dictionary";
import {nullTrace} from "./nulltrace";
import {getAllowedContext} from "./utils/allowed-context";
import {findToken} from "./utils/find-token";
import {mapRange} from "./utils/range";
import {ValueProviderConfig, ValueProviderKind} from "./value-providers/config";
@@ -75,6 +76,7 @@ export async function validate(
}
} catch (e) {
// TODO: Handle error here
console.error(e);
}
return diagnostics;
@@ -94,12 +96,14 @@ async function additionalValidations(
const validationToken = key || parent || token;
const validationDefinition = validationToken.definition;
const allowedContext = getAllowedContext(validationToken, parent);
// If this is an expression, validate it
if (isBasicExpression(token)) {
await validateExpression(
diagnostics,
token,
validationDefinition,
allowedContext,
contextProviderConfig,
getProviderContext(documentUri, template, root, token)
);
@@ -170,14 +174,13 @@ function getProviderContext(
async function validateExpression(
diagnostics: Diagnostic[],
token: BasicExpressionToken,
definition: Definition | undefined,
allowedContext: string[],
contextProviderConfig: ContextProviderConfig | undefined,
workflowContext: WorkflowContext
) {
// Validate the expression
for (const expression of token.originalExpressions || [token]) {
const allowedContexts = definition?.readerContext || [];
const {namedContexts, functions} = splitAllowedContext(allowedContexts);
const {namedContexts, functions} = splitAllowedContext(allowedContext);
let expr: Expr | undefined;