Files
languageservices/actions-languageservice/src/validate.ts
T

146 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-11-21 17:55:24 -08:00
import {Evaluator, Lexer, Parser} from "@github/actions-expressions";
import {Expr} from "@github/actions-expressions/ast";
import {
convertWorkflowTemplate,
isBasicExpression,
parseWorkflow,
ParseWorkflowResult
} from "@github/actions-workflow-parser";
2022-11-22 10:36:17 -08:00
import {splitAllowedContext} from "@github/actions-workflow-parser/templates/allowed-context";
2022-11-21 17:55:24 -08:00
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
2022-11-15 14:08:12 -05:00
import {TokenRange} from "@github/actions-workflow-parser/templates/tokens/token-range";
import {File} from "@github/actions-workflow-parser/workflows/file";
import {TextDocument} from "vscode-languageserver-textdocument";
2022-11-21 17:55:24 -08:00
import {Diagnostic, DiagnosticSeverity, Range} from "vscode-languageserver-types";
import {ContextProviderConfig} from "./context-providers/config";
import {getContext} from "./context-providers/default";
import {AccessError, wrapDictionary} from "./expression-validation/error-dictionary";
2022-11-15 14:08:12 -05:00
import {nullTrace} from "./nulltrace";
2022-11-21 17:55:24 -08:00
import {ValueProviderConfig} from "./value-providers/config";
2022-11-08 17:00:59 -08:00
/**
* Validates a workflow file
*
* @param textDocument Document to validate
* @returns Array of diagnostics
*/
export async function validate(
2022-11-21 17:55:24 -08:00
textDocument: TextDocument,
2022-11-08 17:00:59 -08:00
// TODO: Support multiple files, context for API calls
2022-11-21 17:55:24 -08:00
valueProviderConfig?: ValueProviderConfig,
contextProviderConfig?: ContextProviderConfig
2022-11-08 17:00:59 -08:00
): Promise<Diagnostic[]> {
const file: File = {
name: textDocument.uri,
2022-11-15 14:08:12 -05:00
content: textDocument.getText()
2022-11-08 17:00:59 -08:00
};
2022-11-21 17:55:24 -08:00
const diagnostics: Diagnostic[] = [];
2022-11-08 17:00:59 -08:00
try {
2022-11-15 14:08:12 -05:00
const result: ParseWorkflowResult = parseWorkflow(file.name, [file], nullTrace);
2022-11-08 17:00:59 -08:00
if (result.value) {
// Errors will be updated in the context
convertWorkflowTemplate(result.context, result.value);
}
2022-11-21 17:55:24 -08:00
// Validate expressions
validateExpressions(diagnostics, result, contextProviderConfig);
2022-11-08 17:00:59 -08:00
2022-11-21 17:55:24 -08:00
// For now map parser errors directly to diagnostics
for (const error of result.context.errors.getErrors()) {
let range = mapRange(error.range);
diagnostics.push({
2022-11-08 17:00:59 -08:00
message: error.rawMessage,
2022-11-15 14:08:12 -05:00
range
2022-11-21 17:55:24 -08:00
});
}
2022-11-08 17:00:59 -08:00
} catch (e) {
// TODO: Handle error here
}
2022-11-21 17:55:24 -08:00
return diagnostics;
2022-11-08 17:00:59 -08:00
}
2022-11-21 17:55:24 -08:00
function mapRange(range: TokenRange | undefined): Range {
2022-11-08 17:00:59 -08:00
if (!range) {
2022-11-21 17:55:24 -08:00
return {
start: {
line: 1,
character: 1
},
end: {
line: 1,
character: 1
}
};
2022-11-08 17:00:59 -08:00
}
return {
start: {
line: range.start[0] - 1,
2022-11-15 14:08:12 -05:00
character: range.start[1] - 1
2022-11-08 17:00:59 -08:00
},
end: {
line: range.end[0] - 1,
2022-11-15 14:08:12 -05:00
character: range.end[1] - 1
}
2022-11-08 17:00:59 -08:00
};
}
2022-11-21 17:55:24 -08:00
function validateExpressions(
diagnotics: Diagnostic[],
result: ParseWorkflowResult,
contextProviderConfig: ContextProviderConfig | undefined
) {
if (!result.value) {
return;
}
// Iterate over the parsed workflow
for (const token of TemplateToken.traverse(result.value)) {
if (isBasicExpression(token)) {
// Validate the expression
for (const expression of token.originalExpressions || [token]) {
const allowedContexts = token.definition?.readerContext || [];
const {namedContexts, functions} = splitAllowedContext(allowedContexts);
let expr: Expr | undefined;
try {
const l = new Lexer(expression.expression);
const lr = l.lex();
const p = new Parser(lr.tokens, namedContexts, functions);
expr = p.parse();
} catch {
// Ignore any error here, we should've caught this earlier in the parsing process
continue;
}
try {
const context = getContext(namedContexts, contextProviderConfig);
const e = new Evaluator(expr, wrapDictionary(context));
e.evaluate();
// Any invalid context access would've thrown an error via the `ErrorDictionary`, for now we don't have to check the actual
// result of the evaluation.
} catch (e) {
if (e instanceof AccessError) {
diagnotics.push({
message: `Context access might be invalid: ${e.keyName}`,
severity: DiagnosticSeverity.Warning,
range: mapRange(expression.range)
});
} else {
// Ignore error
}
}
}
}
}
}