Merge pull request #28 from github/cschleiden/validate-if-expressions
Validate if expressions
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import {complete as completeExpression} from "@github/actions-expressions";
|
||||
import {convertWorkflowTemplate, isSequence, isString, parseWorkflow} from "@github/actions-workflow-parser";
|
||||
import {ErrorPolicy} from "@github/actions-workflow-parser/model/convert";
|
||||
import {DefinitionType} from "@github/actions-workflow-parser/templates/schema/definition-type";
|
||||
import {StringDefinition} from "@github/actions-workflow-parser/templates/schema/string-definition";
|
||||
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";
|
||||
@@ -70,11 +72,10 @@ export async function complete(
|
||||
// If we are inside an expression, take a different code-path. The workflow parser does not correctly create
|
||||
// expression nodes for invalid expressions and during editing expressions are invalid most of the time.
|
||||
if (token) {
|
||||
// We don't have any way of specifying that a token in the workflow schema is alwyas an expression. For now these
|
||||
// are only the job and step level `if` nodes, so check for those here.
|
||||
const isIfKey = keyToken && isString(keyToken) && keyToken.value === "if";
|
||||
const isExpression =
|
||||
token.definition?.definitionType === DefinitionType.String && (token.definition as StringDefinition).isExpression;
|
||||
const containsExpression = isString(token) && token.value.indexOf(OPEN_EXPRESSION) >= 0;
|
||||
if (isString(token) && (isIfKey || containsExpression)) {
|
||||
if (isString(token) && (isExpression || containsExpression)) {
|
||||
const currentInput = token.value;
|
||||
|
||||
// Transform the overall position into a node relative position
|
||||
|
||||
@@ -7,7 +7,13 @@ describe("expression validation", () => {
|
||||
const result = await validate(
|
||||
createDocument(
|
||||
"wf.yaml",
|
||||
"on: push\nrun-name: name-${{ github.does-not-exist }}\njobs:\n build:\n runs-on: ubuntu-latest\n steps:\n - run: echo"
|
||||
`on: push
|
||||
run-name: name-\${{ github.does-not-exist }}
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- run: echo`
|
||||
)
|
||||
);
|
||||
|
||||
@@ -73,4 +79,38 @@ jobs:
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
describe("expressions without markers", () => {
|
||||
it("access invalid context field", async () => {
|
||||
const result = await validate(
|
||||
createDocument(
|
||||
"wf.yaml",
|
||||
`on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- run: echo
|
||||
if: github.does-not-exist`
|
||||
)
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
message: "Context access might be invalid: does-not-exist",
|
||||
range: {
|
||||
start: {
|
||||
character: 12,
|
||||
line: 6
|
||||
},
|
||||
end: {
|
||||
character: 33,
|
||||
line: 6
|
||||
}
|
||||
},
|
||||
severity: DiagnosticSeverity.Warning
|
||||
}
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Generated
+6
-6
@@ -665,9 +665,9 @@
|
||||
"link": true
|
||||
},
|
||||
"node_modules/@github/actions-workflow-parser": {
|
||||
"version": "0.0.24",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.24/7bee081e2d0caa2152a4f2264905f4cf96500226",
|
||||
"integrity": "sha512-GnYIs8wnJCV0a5RBL1KYvdtSkUJbLW8Vic+dIiKyRCbIt2e0WuDlmVxh6Mllj+d7bAFiNVWDzUux23co1JTUzQ==",
|
||||
"version": "0.0.25",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.25/4ee85278c517247654ab7d5d91dce58176648bf8",
|
||||
"integrity": "sha512-fq3kL5cUofMyN3djR4wCTfzNzLumYfn0anDvPcbat2iu7LyWxJmB12xNdwoR+t63tVEWbOcP9YP2DB0lWnnp5w==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@github/actions-expressions": "*",
|
||||
@@ -10927,9 +10927,9 @@
|
||||
}
|
||||
},
|
||||
"@github/actions-workflow-parser": {
|
||||
"version": "0.0.24",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.24/7bee081e2d0caa2152a4f2264905f4cf96500226",
|
||||
"integrity": "sha512-GnYIs8wnJCV0a5RBL1KYvdtSkUJbLW8Vic+dIiKyRCbIt2e0WuDlmVxh6Mllj+d7bAFiNVWDzUux23co1JTUzQ==",
|
||||
"version": "0.0.25",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.25/4ee85278c517247654ab7d5d91dce58176648bf8",
|
||||
"integrity": "sha512-fq3kL5cUofMyN3djR4wCTfzNzLumYfn0anDvPcbat2iu7LyWxJmB12xNdwoR+t63tVEWbOcP9YP2DB0lWnnp5w==",
|
||||
"requires": {
|
||||
"@github/actions-expressions": "*",
|
||||
"yaml": "^2.0.0-8"
|
||||
|
||||
Reference in New Issue
Block a user